List of usage examples for java.lang Class getSuperclass
@HotSpotIntrinsicCandidate public native Class<? super T> getSuperclass();
From source file:ReflectUtil.java
/** * Fetches all fields of all access types from the supplied class and super * classes. Fieldss that have been overridden in the inheritance hierarchy are * only returned once, using the instance lowest down the hierarchy. * * @param clazz the class to inspect//from w ww .j a v a 2 s . c o m * @return a collection of fields */ public static Collection<Field> getFields(Class<?> clazz) { Map<String, Field> fields = new HashMap<String, Field>(); while (clazz != null) { for (Field field : clazz.getDeclaredFields()) { if (!fields.containsKey(field.getName())) { fields.put(field.getName(), field); } } clazz = clazz.getSuperclass(); } return fields.values(); }
From source file:Main.java
/** * Sorts objects in the given collection into different types by Class. * @param <E> the element type/* ww w.j a v a 2s. com*/ * @param objects a Collection of objects * @param inherit if true, objects are put into all their class's superclasses as well, except * Object - the original Collection can be used in that case * @return a Map from Class to List of objects in that class */ public static <E> Map<Class<?>, List<E>> groupByClass(Collection<E> objects, boolean inherit) { Map<Class<?>, List<E>> retval = new HashMap<Class<?>, List<E>>(); for (E o : objects) { Class<?> c = o.getClass(); if (inherit) { Set<Class<?>> done = new HashSet<Class<?>>(); done.add(Object.class); Stack<Class<?>> todo = new Stack<Class<?>>(); todo.push(c); while (!todo.empty()) { c = todo.pop(); if ((c != null) && !done.contains(c)) { done.add(c); List<E> l = retval.get(c); if (l == null) { l = new ArrayList<E>(); retval.put(c, l); } l.add(o); todo.push(c.getSuperclass()); Class<?>[] classes = c.getInterfaces(); for (int i = 0; i < classes.length; i++) { todo.push(classes[i]); } } } } else { List<E> l = retval.get(c); if (l == null) { l = new ArrayList<E>(); retval.put(c, l); } l.add(o); } } return retval; }
From source file:com.google.code.guice.repository.spi.TypeUtil.java
public static void getGenericSuperclassActualTypes(Collection<Type> types, Class aClass) { if (aClass != null && types != null) { Type superclass = aClass.getGenericSuperclass(); if (superclass instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) superclass; Type[] interfaces = parameterizedType.getActualTypeArguments(); types.addAll(Arrays.asList(interfaces)); } else if (superclass instanceof Class) { Class sClass = (Class) superclass; getGenericInterfacesActualTypes(types, sClass); getGenericSuperclassActualTypes(types, aClass.getSuperclass()); }//from w w w. j a va2 s. c o m } }
From source file:com.ocs.dynamo.utils.ClassUtils.java
/** * Returns a field with a certain name from a class * //from w w w. ja va2s. com * @param clazz * the class * @param fieldName * the name of the filed * @return */ public static Field getField(Class<?> clazz, String fieldName) { Field field = null; if (clazz != null) { try { field = clazz.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { LOG.debug(e.getMessage(), e); if (clazz.getSuperclass() != null) { return getField(clazz.getSuperclass(), fieldName); } } } return field; }
From source file:com.ms.commons.test.common.ReflectUtil.java
public static Field getDeclaredField(Class<?> clazz, String field) throws SecurityException, NoSuchFieldException { if (clazz == Object.class) { throw new NoSuchFieldException( "Field `" + field + "` in class `" + clazz.getName() + "` cannot be found."); }/*from w w w .ja v a 2s. c o m*/ try { return clazz.getDeclaredField(field); } catch (SecurityException e) { throw e; } catch (NoSuchFieldException e) { return getDeclaredField(clazz.getSuperclass(), field); } }
From source file:cc.sion.core.utils.Reflections.java
/** * ??//from ww w .j a v a 2 s . c o m * * @param clazz * @param index * @param <T> * @return */ public static <T> Class<T> findParameterizedType(Class<?> clazz, int index) { //getSuperclass() //getGenericSuperclass() //Type Java ??????? Type type = clazz.getGenericSuperclass(); //CGLUB subclass target object() if (!(type instanceof ParameterizedType)) { type = clazz.getSuperclass().getGenericSuperclass(); } if (!(type instanceof ParameterizedType)) { return null; } //ParameterizedType?? ParameterizedType p = (ParameterizedType) type; //getActualTypeArguments??? Type[] actualTypeArguments = p.getActualTypeArguments(); if (actualTypeArguments == null || actualTypeArguments.length == 0) { return null; } return (Class<T>) actualTypeArguments[0]; }
From source file:com.iisigroup.cap.utils.CapBeanUtil.java
/** * ?Clazz?(?super class)// w w w . j a v a 2 s .c o m * * @param clazz * class * @param containSuperClazz * ??Class? * @return Field[] */ @SuppressWarnings({ "rawtypes" }) public static Field[] getField(Class clazz, boolean containSuperClazz) { Set<Field> cols = new LinkedHashSet<Field>(); Class searchClazz = clazz; while (!Object.class.equals(searchClazz) && searchClazz != null) { Field[] fields = searchClazz.getDeclaredFields(); for (Field f : fields) { if ("serialVersionUID".equals(f.getName())) continue; cols.add(f); } searchClazz = containSuperClazz ? searchClazz.getSuperclass() : null; } return cols.toArray(new Field[] {}); }
From source file:de.micromata.genome.util.runtime.AssertUtils.java
/** * Gets the stack above./*from ww w.jav a2s . c o m*/ * * @param above the above * @return the stack above */ public static StackTraceElement getStackAbove(final Class<?> above) { final StackTraceElement[] stel = Thread.currentThread().getStackTrace(); boolean foundFirst = false; final String clsName = above.getCanonicalName(); for (int i = 0; i < stel.length; ++i) { final StackTraceElement s = stel[i]; final String scn = s.getClassName(); if (clsName.equals(scn) == false) { if (foundFirst == true) { return s; } } else { foundFirst = true; } } if (above.getSuperclass() != null) { return getStackAbove(above.getSuperclass()); } return null; }
From source file:Main.java
public static Set<Class<?>> getMinimalImplementedInterfaceNames(Class<?> klass) { Set<Class<?>> interfaces = new HashSet<Class<?>>(); while (klass != null) { Class<?>[] localInterfaces = klass.getInterfaces(); for (Class<?> intf : localInterfaces) { boolean subsumed = false; for (Class<?> i : new ArrayList<Class<?>>(interfaces)) { if (intf.isAssignableFrom(i)) { subsumed = true;//from w w w. j ava2 s .c o m break; } else if (i.isAssignableFrom(intf)) { interfaces.remove(i); } } if (subsumed) { continue; } interfaces.add(intf); } klass = klass.getSuperclass(); } return interfaces; }
From source file:com.iisigroup.cap.utils.CapBeanUtil.java
/** * ?Clazz?(?super class)//from w w w.j a v a 2s . c om * * @param clazz * class * @param containSuperClazz * ??Class? * @return String[] */ @SuppressWarnings({ "rawtypes" }) public static String[] getFieldName(Class clazz, boolean containSuperClazz) { Set<String> cols = new LinkedHashSet<String>(); Class searchClazz = clazz; while (!Object.class.equals(searchClazz) && searchClazz != null) { Field[] fields = searchClazz.getDeclaredFields(); for (Field f : fields) { if ("serialVersionUID".equals(f.getName())) continue; cols.add(f.getName()); } searchClazz = containSuperClazz ? searchClazz.getSuperclass() : null; } return cols.toArray(new String[cols.size()]); }