List of usage examples for java.lang Class isAssignableFrom
@HotSpotIntrinsicCandidate public native boolean isAssignableFrom(Class<?> cls);
From source file:com.manisha.allmybooksarepacked.utility.JSONUtils.java
@SuppressWarnings("unchecked") public static <T> T JSONToObject(String json, Class<T> clazz) throws IOException { if (clazz == null || clazz.isAssignableFrom(String.class)) return (T) json; if (StringUtils.isBlank(json)) return null; return mapper.readValue(StringEscapeUtils.unescapeJson(json), clazz); }
From source file:Main.java
private static Object getRandomValue(Class<?> type) { if (type.isAssignableFrom(Double.class)) { return new Random().nextDouble(); }//from w w w . j ava 2 s. c o m if (type.isAssignableFrom(Short.class)) { return (short) new Random().nextInt(); } if (type.isAssignableFrom(Long.class)) { return new Random().nextLong(); } if (type.isAssignableFrom(Integer.class)) { return new Random().nextInt(); } if (type.isAssignableFrom(String.class)) { return "" + new Random().nextInt(); } if (type.isAssignableFrom(Boolean.class)) { return new Random().nextBoolean(); } if (type.isAssignableFrom(Date.class)) { return new Date(new Random().nextLong()); } if (type.isAssignableFrom(List.class)) { Class<?> listType = (Class<?>) ((ParameterizedType) type.getClass().getGenericSuperclass()) .getActualTypeArguments()[0]; return getRandomCollection(listType); } return null; }
From source file:Main.java
public static Object get(Class c, Map map) { if (map.keySet().contains(c)) { return map.get(c); }/* w ww . j av a 2s .co m*/ for (Iterator i = map.keySet().iterator(); i.hasNext();) { Class candidateClass = (Class) i.next(); if (candidateClass.isAssignableFrom(c)) { return map.get(candidateClass); } } return null; }
From source file:Main.java
/** * @param stringConvertibleTypes A collection of interfaces. * @param clazz An interface to test./*from ww w .j ava 2 s .c om*/ * @return <code>true</code> if the given interface is an Enum, or if the collection contains a super-interface. */ public static boolean isStringConvertibleType(Set<Class> stringConvertibleTypes, Class clazz) { if (clazz.isEnum()) return true; for (Class toStringOutputType : stringConvertibleTypes) { if (toStringOutputType.isAssignableFrom(clazz)) { return true; } } return false; }
From source file:Main.java
@Nullable public static Class<?> getInterfaceOfClassExtendingGivenInterface(final Class<?> possibleExtendingClass, final Class<?> givenInterface) { if (!givenInterface.isAssignableFrom(possibleExtendingClass)) { // not possible return null; }/*from w ww . ja va 2 s . c om*/ // assignable, find the interface Class<?> viewClass = possibleExtendingClass; while (viewClass != null) { final Class<?>[] interfaces = viewClass.getInterfaces(); for (final Class<?> clazz : interfaces) { if (givenInterface.isAssignableFrom(clazz)) { return clazz; } } // check super viewClass = viewClass.getSuperclass(); } // should never happen return null; }
From source file:Main.java
static boolean canAssign(Class<?> baseClass, Class<?> classToCheck) { if (baseClass.isAssignableFrom(classToCheck)) { return true; } else {//from www.ja v a2 s . c o m if (baseClass.isPrimitive()) { Class primitiveClass = PRIMITIVE_WRAPPERS.get(classToCheck); if (primitiveClass != null) { return baseClass.isAssignableFrom(primitiveClass); } } else if (classToCheck.isPrimitive()) { Class primitiveClass = PRIMITIVE_WRAPPERS.get(baseClass); if (primitiveClass != null) { return primitiveClass.isAssignableFrom(classToCheck); } } } return false; }
From source file:Main.java
public static void reduceChildrenList(Element elem, Class clazz) { for (int i = 0; i < elem.getChildNodes().getLength();) { if (clazz.isAssignableFrom(elem.getChildNodes().item(i).getClass())) { ++i;/*from w ww.ja v a2 s . co m*/ } else { elem.removeChild(elem.getChildNodes().item(i)); } } }
From source file:Main.java
@SuppressWarnings("unchecked") private static <T extends Component> void findChildComponentsRec(Component parent, Class<T> type, List<T> list, int maxCount) { if (type.isAssignableFrom(parent.getClass())) { list.add((T) parent);/*from www .ja v a 2s . c o m*/ if (list.size() >= maxCount) { return; } } // if (parent instanceof Container) { for (Component c : ((Container) parent).getComponents()) { findChildComponentsRec(c, type, list, maxCount); if (list.size() >= maxCount) { return; } } } }
From source file:Main.java
/** * Retrieves the first contained component of a certain type. * * @param component/* w w w .jav a 2 s . c o m*/ * the component to start from. * @param childComponentType * the type of the component to look for. * @return the first contained component of the looked for type or null if * none. */ public static Component getFirstChildComponentOfType(Component component, Class<? extends JComponent> childComponentType) { if (childComponentType.isAssignableFrom(component.getClass())) { return component; } if (component instanceof Container) { Component[] children = ((Container) component).getComponents(); for (Component child : children) { Component childResult = getFirstChildComponentOfType(child, childComponentType); if (childResult != null) { return childResult; } } } return null; }
From source file:Main.java
/** * The internal, recursive implementation of getChildComponents(Container, Class...). * * @param parent Container which's children are being gathered. * @param filter The class filter applied, may be null. * @param resultList The list of gathered child components. *///from www . jav a2 s. co m protected static void getChildComponents(final Container parent, final Class[] filter, final Collection resultList) { final Component[] components = parent.getComponents(); for (Component c : components) { if (filter.length == 0) resultList.add(c); else { for (Class f : filter) { if (f.isAssignableFrom(c.getClass())) { resultList.add(c); break; } } } if (c instanceof Container) getChildComponents((Container) c, filter, resultList); } }