List of usage examples for java.lang Class isAssignableFrom
@HotSpotIntrinsicCandidate public native boolean isAssignableFrom(Class<?> cls);
From source file:jcurl.sim.core.SlideStrategy.java
public static SlideStrategy newInstance(final Class clz) { final Class parent = SlideStrategy.class; if (!parent.isAssignableFrom(clz)) throw new IllegalArgumentException( "Class [" + clz.getName() + "] is no descendant of [" + parent.getName() + "]"); try {//from www . ja v a2 s. c o m return (SlideStrategy) clz.newInstance(); } catch (InstantiationException e) { final IllegalArgumentException ex = new IllegalArgumentException(); ex.initCause(e); throw ex; } catch (IllegalAccessException e) { final IllegalArgumentException ex = new IllegalArgumentException(); ex.initCause(e); throw ex; } catch (SecurityException e) { final IllegalArgumentException ex = new IllegalArgumentException(); ex.initCause(e); throw ex; } }
From source file:de.micromata.genome.gwiki.utils.ClassUtils.java
@SuppressWarnings("unchecked") public static <T> T invokeDefaultStaticMethod(Class<T> expectedClass, String className, String methodName) { Class<?> cls = classForName(className); try {// w ww . j a va 2s . co m Method m = cls.getMethod(methodName, new Class[] {}); T t = (T) m.invoke(null, new Object[] {}); if (t == null) { return t; } if (expectedClass.isAssignableFrom(t.getClass()) == false) { throw new RuntimeException("method: " + methodName + " from class: " + className + " does not return correct class type: " + expectedClass + " but: " + t.getClass()); } return t; } catch (NoSuchMethodException ex) { throw new RuntimeException("Cannot find method: " + methodName + " in class: " + className); } catch (IllegalArgumentException ex) { throw new RuntimeException(ex); } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } catch (InvocationTargetException ex) { Throwable nex = ex.getCause(); if (nex instanceof RuntimeException) { throw (RuntimeException) nex; } throw new RuntimeException(ex); } }
From source file:com.expressui.core.util.ReflectionUtil.java
/** * Converts object value to given type. Converts primitives to their wrappers. * Converts strings to numbers.//from w w w. ja v a 2s .com * * @param value value to convert * @param type type to convert to * @param <T> type * @return converted value * @throws InvocationTargetException * @throws IllegalAccessException * @throws InstantiationException * @throws NoSuchMethodException */ public static <T> T convertValue(Object value, Class<T> type) throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException { Class clazz; if (type.isPrimitive()) { clazz = ClassUtils.primitiveToWrapper(type); } else { clazz = type; } if (null == value || clazz.isAssignableFrom(value.getClass())) { return (T) value; } Constructor<T> constructor = clazz.getConstructor(new Class[] { String.class }); return constructor.newInstance(value.toString()); }
From source file:fr.xebia.management.statistics.ServiceStatistics.java
/** * Returns <code>true</code> if the given <code>throwable</code> or one of * its cause is an instance of one of the given <code>throwableTypes</code>. *//* ww w . j a v a 2 s . c o m*/ public static boolean containsThrowableOfType(Throwable throwable, Class<?>... throwableTypes) { List<Throwable> alreadyProcessedThrowables = new ArrayList<Throwable>(); while (true) { if (throwable == null) { // end of the list of causes return false; } else if (alreadyProcessedThrowables.contains(throwable)) { // infinite loop in causes return false; } else { for (Class<?> throwableType : throwableTypes) { if (throwableType.isAssignableFrom(throwable.getClass())) { return true; } } alreadyProcessedThrowables.add(throwable); throwable = throwable.getCause(); } } }
From source file:de.micromata.genome.tpsb.htmlunit.By.java
public static By elementType(final Class<?> clazz) { return new By() { @Override//from w ww .jav a 2s .c o m public List<Element> findElements(HtmlPage page) { Iterable<HtmlElement> ell = page.getAllHtmlChildElements(); List<Element> ret = new ArrayList<Element>(); for (HtmlElement el : ell) { if (clazz.isAssignableFrom(el.getClass()) == true) { ret.add(el); } } return ret; } }; }
From source file:eu.europa.ec.fisheries.uvms.exchange.search.SearchFieldMapper.java
public static <T> T buildValueFromClassType(SearchValue entry, Class<T> valueType) { StringBuilder builder = new StringBuilder(); try {// w w w . j a v a2s . c o m if (valueType.isAssignableFrom(String.class)) { if (entry.getValue().contains("*")) { String value = entry.getValue().replace("*", "%"); builder.append("'").append(value).append("'"); } else { builder.append(entry.getValue()); } return valueType.cast(builder.toString()); } else if (valueType.isAssignableFrom(Boolean.class)) { if ("TRUE".equalsIgnoreCase(entry.getValue()) || "T".equalsIgnoreCase(entry.getValue())) { return valueType.cast(Boolean.TRUE); } else { return valueType.cast(Boolean.FALSE); } } else if (valueType.isAssignableFrom(Date.class)) { return valueType.cast(DateUtils.parseToUTCDateTime(entry.getValue())); } else if (valueType.isAssignableFrom(Integer.class)) { return valueType.cast(Integer.valueOf(entry.getValue())); } else if (valueType.isAssignableFrom(TypeRefType.class)) { return valueType.cast(TypeRefType.valueOf(entry.getValue())); } else if (valueType.isAssignableFrom(ExchangeLogStatusTypeType.class)) { return valueType.cast(ExchangeLogStatusTypeType.valueOf(entry.getValue())); } return valueType.cast(entry.getValue()); } catch (ClassCastException cce) { LOG.error("Error casting parameter: " + entry.getField().getFieldName() + " having value: " + entry.getValue(), cce); return null; } }
From source file:org.jdal.dao.jpa.JpaUtils.java
/** * Test if attribute is type or in collections has element type * @param attribute attribute to test/*from w ww . j ava 2 s . c om*/ * @param clazz Class to test * @return true if clazz is asignable from type or element type */ public static boolean isTypeOrElementType(Attribute<?, ?> attribute, Class<?> clazz) { if (attribute.isCollection()) { return clazz.isAssignableFrom(((CollectionAttribute<?, ?>) attribute).getBindableJavaType()); } return clazz.isAssignableFrom(attribute.getJavaType()); }
From source file:com.project.framework.util.ReflectionUtils.java
/** * ??, Class??./*from w ww. ja v a 2 s.c o m*/ * , null Class Array. * * @param clazz * @param interfaze * @param index * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Class getInterfaceGenricType(final Class clazz, final Class interfaze, final int index) { if (!interfaze.isInterface()) throw new IllegalArgumentException("interfaze must be a interface."); if (!interfaze.isAssignableFrom(clazz)) throw new IllegalArgumentException("clazz must be implemnet form interfaze"); Type[] genTypes = clazz.getGenericInterfaces(); for (Type genType : genTypes) { if (genType instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) genType; if (interfaze.equals(pt.getRawType())) { Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) { logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: " + params.length); return Object.class; } if (!(params[index] instanceof Class)) { logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter"); return Object.class; } return (Class) params[index]; } } } return null; }
From source file:org.kuali.kfs.sys.context.SpringContext.java
private static boolean hasSingletonSuperType(Class<? extends Object> type, Set<Class<? extends Object>> knownSingletonTypes) { for (Class<? extends Object> singletonType : knownSingletonTypes) { if (singletonType.isAssignableFrom(type)) { return true; }//from w w w. ja va 2s .c om } return false; }
From source file:jef.tools.Assert.java
/** * classc// w w w .j av a 2 s . c om * @param obj * @param c */ public static void isType(Object obj, Class<?> c) { if (!(c.isAssignableFrom(obj.getClass()))) throw new ClassCastException(); }