List of usage examples for java.lang Class isAssignableFrom
@HotSpotIntrinsicCandidate public native boolean isAssignableFrom(Class<?> cls);
From source file:GenericsUtil.java
public static boolean isTypeOf(final Type type, final Class<?> clazz) { if (Class.class.isInstance(type)) { final Class<?> typeAsClass = Class.class.cast(type); return clazz.isAssignableFrom(typeAsClass); }/*from ww w. java 2 s .co m*/ if (ParameterizedType.class.isInstance(type)) { final ParameterizedType parameterizedType = ParameterizedType.class.cast(type); final Type rawType = parameterizedType.getRawType(); return isTypeOf(rawType, clazz); } return false; }
From source file:Main.java
/** * Checks whether objects of the given class can be cast to the specified * type.// www .j a va 2s . co m * * @see #cast(Object, Class) */ public static boolean canCast(final Class<?> c, final Class<?> type) { return type.isAssignableFrom(c); }
From source file:Main.java
/** * Filters Views based on the given class type. * /*from ww w.j a v a 2 s .c o m*/ * @param classToFilterBy the class to filter * @param viewList the Iterable to filter from * @return an ArrayList with filtered views */ public static <T> ArrayList<T> filterViews(Class<T> classToFilterBy, Iterable<?> viewList) { ArrayList<T> filteredViews = new ArrayList<T>(); for (Object view : viewList) { if (view != null && classToFilterBy.isAssignableFrom(view.getClass())) { filteredViews.add(classToFilterBy.cast(view)); } } viewList = null; return filteredViews; }
From source file:Main.java
static void getSubviewsTree(ViewGroup parentView, Class<?> classOfSubviews, ArrayList<View> result) { for (int i = 0; i < parentView.getChildCount(); i++) { View child = parentView.getChildAt(i); if (classOfSubviews.isAssignableFrom(child.getClass())) result.add(child);//from w w w . jav a 2 s . c o m if (child instanceof ViewGroup) getSubviewsTree((ViewGroup) child, classOfSubviews, result); } }
From source file:com.myee.tarot.core.exception.ExceptionHelper.java
public static <G extends Throwable, J extends RuntimeException> RuntimeException refineException( Class<G> refineType, Class<J> wrapType, String message, Throwable e) { if (refineType.isAssignableFrom(e.getClass())) { return wrapException(e, wrapType, message); }/*from www . ja va 2 s . c om*/ if (e.getCause() != null) { return refineException(refineType, wrapType, message, e.getCause()); } if (e instanceof UndeclaredThrowableException) { return refineException(refineType, wrapType, message, ((UndeclaredThrowableException) e).getUndeclaredThrowable()); } if (e instanceof InvocationTargetException) { return refineException(refineType, wrapType, message, ((InvocationTargetException) e).getTargetException()); } return wrapException(e, wrapType, message); }
From source file:io.twipple.springframework.data.clusterpoint.convert.support.ConversionUtils.java
/** * Check if the left class is a subtype of the right. * * @param left the first class./*w w w . j a v a 2s. c o m*/ * @param right the second class. * @return true if it is a subtype, false otherwise. */ public static boolean isSubtype(@NotNull Class<?> left, @NotNull Class<?> right) { Assert.notNull(left); Assert.notNull(right); return left.isAssignableFrom(right) && !left.equals(right); }
From source file:com.ejisto.modules.web.util.FieldSerializationUtil.java
private static boolean isBlackListed(Class<?> clazz) { for (Class<?> blackListedClass : BLACKLISTED_CLASSES) { if (blackListedClass.isAssignableFrom(clazz)) { return true; }/*from w w w .j a va 2s . c o m*/ } return false; }
From source file:io.sqp.core.util.TypeUtil.java
/** * Checks a value to be not null and tries to map it to a given class. * The method first checks the value to be not-null and then if it's assignable to the desired class. * If it's not directly assignable (e.g. a int is not assignable to a Double), Jackson's object mapper * is used to convert the value, as it is able to map any classes if they are somehow compatible. * Therefore this method can also be used to map a {@literal List<Integer>} to an {@literal int[]}. * This method also does range checks, e.g. it fails if you try to convert 2^30 to a Short. * @param value The value to be checked and mapped. * @param clazz The class the value should be mapped to * @param what A brief description of the message that is used in a potential error message. E.g. "the identifier" * @param <T> The desired object type * @return The converted, non-null object. * @throws IllegalArgumentException If the value is null or cannot be converted to the desired type *///from w w w . j a va 2 s . com public static <T> T checkAndConvert(Object value, Class<T> clazz, String what) { if (value == null) { throw new IllegalArgumentException(what + " is null."); } if (clazz.isAssignableFrom(value.getClass())) { return clazz.cast(value); } try { return _objectMapper.convertValue(value, clazz); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( what + " could not be mapped to type " + clazz.getName() + ": " + e.getMessage(), e); } }
From source file:com.vanda.beivandalibnetwork.utils.CommonUtils.java
/** * ?//w ww .j a v a2s .c o m * @param clazz1 * @param clazz2 * @return */ public static boolean isAssignable(Class<?> clazz1, Class<?> clazz2) { if (clazz1 == null || clazz2 == null) return false; return clazz1.isAssignableFrom(clazz2) || clazz2.isAssignableFrom(clazz1); }
From source file:edu.umn.msi.tropix.common.test.BeanTest.java
public static void testBeanProperties(final Object testBean, final HashMultimap<Class<?>, Object> typeObjects) { try {/* w w w. ja va 2 s.c o m*/ @SuppressWarnings("unchecked") final Map<String, ?> propertyMap = PropertyUtils.describe(testBean); for (final String propertyName : propertyMap.keySet()) { if (propertyName.equals("class") || !PropertyUtils.isWriteable(testBean, propertyName)) { continue; } final Class<?> type = PropertyUtils.getPropertyType(testBean, propertyName); Collection<?> objects = null; for (final Class<?> typeQuery : typeObjects.keySet()) { if (typeQuery.isAssignableFrom(type)) { objects = typeObjects.get(typeQuery); } } boolean useEquals = true; if (objects == null) { useEquals = false; try { objects = Lists.<Object>newArrayList(EasyMock.createMock(type)); } catch (final Exception e) { // Cannot instantiate mock of this type continue; } } for (final Object expectedObject : objects) { PropertyUtils.setProperty(testBean, propertyName, expectedObject); final Object object = PropertyUtils.getProperty(testBean, propertyName); if (useEquals) { assert object.equals(expectedObject) : "Expected " + expectedObject + " obtained " + object; } else { assert object == expectedObject : "Expected " + expectedObject + " obtained " + object; } } } } catch (final Exception e) { throw new IllegalStateException(e); } }