@Test public void testFindPublicFieldsOrdereBySuper() throws Exception { List<Field> fields = Reflections.findPublicFieldsOrderedBySuper(MySubClass.class); assertContainsInOrder(fields, ImmutableList.of( MyInterface.class.getField("MY_FIELD"), MySuperClass.class.getField("MY_FIELD"), MySubClass.class.getField("MY_FIELD"))); assertNoDuplicates(fields); }
public static List<Field> findPublicFieldsOrderedBySuper(Class<?> clazz) { checkNotNull(clazz, "clazz"); MutableList.Builder<Field> result = MutableList.<Field>builder(); Stack<Class<?>> tovisit = new Stack<Class<?>>(); Set<Class<?>> visited = Sets.newLinkedHashSet(); tovisit.push(clazz); while (!tovisit.isEmpty()) { Class<?> nextclazz = tovisit.pop(); if (!visited.add(nextclazz)) { continue; } if (nextclazz.getSuperclass() != null) tovisit.add(nextclazz.getSuperclass()); tovisit.addAll(Arrays.asList(nextclazz.getInterfaces())); result.addAll(Iterables.filter(Arrays.asList(nextclazz.getDeclaredFields()), new Predicate<Field>() { @Override public boolean apply(Field input) { return Modifier.isPublic(input.getModifiers()); }})); } List<Field> resultList = result.build(); Collections.sort(resultList, new Comparator<Field>() { @Override public int compare(Field f1, Field f2) { Field fsubbest = inferSubbestField(f1, f2); return (fsubbest == null) ? 0 : (fsubbest == f1 ? 1 : -1); }}); return resultList; }
Reflections { public static List<Field> findPublicFieldsOrderedBySuper(Class<?> clazz) { checkNotNull(clazz, "clazz"); MutableList.Builder<Field> result = MutableList.<Field>builder(); Stack<Class<?>> tovisit = new Stack<Class<?>>(); Set<Class<?>> visited = Sets.newLinkedHashSet(); tovisit.push(clazz); while (!tovisit.isEmpty()) { Class<?> nextclazz = tovisit.pop(); if (!visited.add(nextclazz)) { continue; } if (nextclazz.getSuperclass() != null) tovisit.add(nextclazz.getSuperclass()); tovisit.addAll(Arrays.asList(nextclazz.getInterfaces())); result.addAll(Iterables.filter(Arrays.asList(nextclazz.getDeclaredFields()), new Predicate<Field>() { @Override public boolean apply(Field input) { return Modifier.isPublic(input.getModifiers()); }})); } List<Field> resultList = result.build(); Collections.sort(resultList, new Comparator<Field>() { @Override public int compare(Field f1, Field f2) { Field fsubbest = inferSubbestField(f1, f2); return (fsubbest == null) ? 0 : (fsubbest == f1 ? 1 : -1); }}); return resultList; } }
Reflections { public static List<Field> findPublicFieldsOrderedBySuper(Class<?> clazz) { checkNotNull(clazz, "clazz"); MutableList.Builder<Field> result = MutableList.<Field>builder(); Stack<Class<?>> tovisit = new Stack<Class<?>>(); Set<Class<?>> visited = Sets.newLinkedHashSet(); tovisit.push(clazz); while (!tovisit.isEmpty()) { Class<?> nextclazz = tovisit.pop(); if (!visited.add(nextclazz)) { continue; } if (nextclazz.getSuperclass() != null) tovisit.add(nextclazz.getSuperclass()); tovisit.addAll(Arrays.asList(nextclazz.getInterfaces())); result.addAll(Iterables.filter(Arrays.asList(nextclazz.getDeclaredFields()), new Predicate<Field>() { @Override public boolean apply(Field input) { return Modifier.isPublic(input.getModifiers()); }})); } List<Field> resultList = result.build(); Collections.sort(resultList, new Comparator<Field>() { @Override public int compare(Field f1, Field f2) { Field fsubbest = inferSubbestField(f1, f2); return (fsubbest == null) ? 0 : (fsubbest == f1 ? 1 : -1); }}); return resultList; } Reflections(ClassLoader classLoader); }
Reflections { public static List<Field> findPublicFieldsOrderedBySuper(Class<?> clazz) { checkNotNull(clazz, "clazz"); MutableList.Builder<Field> result = MutableList.<Field>builder(); Stack<Class<?>> tovisit = new Stack<Class<?>>(); Set<Class<?>> visited = Sets.newLinkedHashSet(); tovisit.push(clazz); while (!tovisit.isEmpty()) { Class<?> nextclazz = tovisit.pop(); if (!visited.add(nextclazz)) { continue; } if (nextclazz.getSuperclass() != null) tovisit.add(nextclazz.getSuperclass()); tovisit.addAll(Arrays.asList(nextclazz.getInterfaces())); result.addAll(Iterables.filter(Arrays.asList(nextclazz.getDeclaredFields()), new Predicate<Field>() { @Override public boolean apply(Field input) { return Modifier.isPublic(input.getModifiers()); }})); } List<Field> resultList = result.build(); Collections.sort(resultList, new Comparator<Field>() { @Override public int compare(Field f1, Field f2) { Field fsubbest = inferSubbestField(f1, f2); return (fsubbest == null) ? 0 : (fsubbest == f1 ? 1 : -1); }}); return resultList; } Reflections(ClassLoader classLoader); Reflections applyClassRenames(Map<String,String> newClassRenames); Object loadInstance(String classname, Object...argValues); Object loadInstance(String classname, Class<?>[] argTypes, Object[] argValues); Object loadInstance(String classname); Class<?> loadClass(String classname); @SuppressWarnings("unchecked") Class<? extends T> loadClass(String classname, Class<T> superType); static Class<?> loadInnerClassPossiblyInheritted(Class<?> clazz, String nestedPart); Class<?> loadInnerClassNotInheritted(String outerClassname, String innerClassname); Class<?> loadInnerClassNotInheritted(Class<?> outerClazz, String innerClassname); Constructor<?> loadConstructor(Class<?> clazz, Class<?>[] argTypes); static Optional<T> invokeConstructorWithArgs(ClassLoader classLoader, String className, Object...argsArray); static Optional<T> invokeConstructorWithArgs(ClassLoader classLoader, Class<T> clazz, Object[] argsArray, boolean setAccessible); static Optional<T> invokeConstructorWithArgs(Class<T> clazz, Object...argsArray); static Optional<T> invokeConstructorWithArgs(Class<T> clazz, Object[] argsArray, boolean setAccessible); @SuppressWarnings("unchecked") static Optional<T> invokeConstructorWithArgs(Reflections reflections, Class<T> clazz, Object[] argsArray, boolean setAccessible); Constructor<?> loadSingleConstructor(Class<?> clazz); T loadInstance(Constructor<T> constructor, Object...argValues); Method loadMethod(Class<?> clazz, String methodName, Class<?>[] argTypes); Method loadMethod(Class<?> clazz, String methodName); Object invokeMethod(Method method, Object obj, Object... argValues); Object invokeStaticMethod(Method method, Object... argValues); Object loadStaticField(Class<?> clazz, String fieldname); Object[] loadStaticFields(Class<?> clazz, String[] fieldnamesArray, Object[] defaults); static void copyFields(T source, T target); Class<?> loadClassFromCanonicalName(String canonicalName); @Nullable URL getResource(String r); final Object updateFromNewClassLoader(Object data); ClassLoader getClassLoader(); @SuppressWarnings("unchecked") static Class<? super T> findSuperType(T impl, String typeName); static Set<Class<?>> getInterfacesIncludingClassAncestors(Class<?> clazz); static Method findMethod(Class<?> clazz, String name, Class<?>... parameterTypes); static Field findField(Class<?> clazz, String name); static List<Field> findPublicFieldsOrderedBySuper(Class<?> clazz); static List<Method> findPublicMethodsOrderedBySuper(Class<?> clazz); static Field inferSubbestField(Field f1, Field f2); static Method inferSubbestMethod(Method m1, Method m2); static Class<?> inferSubbest(Class<?> c1, Class<?> c2); @SuppressWarnings("unchecked") static T cast(Object candidate, Class<? extends T> type); static Optional<Object> invokeMethodWithArgs(Object clazzOrInstance, String method, List<Object> args); static Optional<Object> invokeMethodWithArgs(Object clazzOrInstance, String method, List<Object> args, boolean setAccessible); static boolean typesMatch(Object[] argsArray, Class<?>[] parameterTypes); static boolean typesMatchUpTo(Object[] argsArray, Class<?>[] parameterTypes, int lengthRequired); static List<Class<?>> getAllInterfaces(@Nullable Class<?> type); static boolean hasNoArgConstructor(Class<?> clazz); static boolean hasNoNonObjectFields(Class<? extends Object> clazz); static Optional<String> tryFindMappedName(Map<String, String> renames, String name); static String findMappedNameAndLog(Map<String, String> renames, String name); }
Reflections { public static List<Field> findPublicFieldsOrderedBySuper(Class<?> clazz) { checkNotNull(clazz, "clazz"); MutableList.Builder<Field> result = MutableList.<Field>builder(); Stack<Class<?>> tovisit = new Stack<Class<?>>(); Set<Class<?>> visited = Sets.newLinkedHashSet(); tovisit.push(clazz); while (!tovisit.isEmpty()) { Class<?> nextclazz = tovisit.pop(); if (!visited.add(nextclazz)) { continue; } if (nextclazz.getSuperclass() != null) tovisit.add(nextclazz.getSuperclass()); tovisit.addAll(Arrays.asList(nextclazz.getInterfaces())); result.addAll(Iterables.filter(Arrays.asList(nextclazz.getDeclaredFields()), new Predicate<Field>() { @Override public boolean apply(Field input) { return Modifier.isPublic(input.getModifiers()); }})); } List<Field> resultList = result.build(); Collections.sort(resultList, new Comparator<Field>() { @Override public int compare(Field f1, Field f2) { Field fsubbest = inferSubbestField(f1, f2); return (fsubbest == null) ? 0 : (fsubbest == f1 ? 1 : -1); }}); return resultList; } Reflections(ClassLoader classLoader); Reflections applyClassRenames(Map<String,String> newClassRenames); Object loadInstance(String classname, Object...argValues); Object loadInstance(String classname, Class<?>[] argTypes, Object[] argValues); Object loadInstance(String classname); Class<?> loadClass(String classname); @SuppressWarnings("unchecked") Class<? extends T> loadClass(String classname, Class<T> superType); static Class<?> loadInnerClassPossiblyInheritted(Class<?> clazz, String nestedPart); Class<?> loadInnerClassNotInheritted(String outerClassname, String innerClassname); Class<?> loadInnerClassNotInheritted(Class<?> outerClazz, String innerClassname); Constructor<?> loadConstructor(Class<?> clazz, Class<?>[] argTypes); static Optional<T> invokeConstructorWithArgs(ClassLoader classLoader, String className, Object...argsArray); static Optional<T> invokeConstructorWithArgs(ClassLoader classLoader, Class<T> clazz, Object[] argsArray, boolean setAccessible); static Optional<T> invokeConstructorWithArgs(Class<T> clazz, Object...argsArray); static Optional<T> invokeConstructorWithArgs(Class<T> clazz, Object[] argsArray, boolean setAccessible); @SuppressWarnings("unchecked") static Optional<T> invokeConstructorWithArgs(Reflections reflections, Class<T> clazz, Object[] argsArray, boolean setAccessible); Constructor<?> loadSingleConstructor(Class<?> clazz); T loadInstance(Constructor<T> constructor, Object...argValues); Method loadMethod(Class<?> clazz, String methodName, Class<?>[] argTypes); Method loadMethod(Class<?> clazz, String methodName); Object invokeMethod(Method method, Object obj, Object... argValues); Object invokeStaticMethod(Method method, Object... argValues); Object loadStaticField(Class<?> clazz, String fieldname); Object[] loadStaticFields(Class<?> clazz, String[] fieldnamesArray, Object[] defaults); static void copyFields(T source, T target); Class<?> loadClassFromCanonicalName(String canonicalName); @Nullable URL getResource(String r); final Object updateFromNewClassLoader(Object data); ClassLoader getClassLoader(); @SuppressWarnings("unchecked") static Class<? super T> findSuperType(T impl, String typeName); static Set<Class<?>> getInterfacesIncludingClassAncestors(Class<?> clazz); static Method findMethod(Class<?> clazz, String name, Class<?>... parameterTypes); static Field findField(Class<?> clazz, String name); static List<Field> findPublicFieldsOrderedBySuper(Class<?> clazz); static List<Method> findPublicMethodsOrderedBySuper(Class<?> clazz); static Field inferSubbestField(Field f1, Field f2); static Method inferSubbestMethod(Method m1, Method m2); static Class<?> inferSubbest(Class<?> c1, Class<?> c2); @SuppressWarnings("unchecked") static T cast(Object candidate, Class<? extends T> type); static Optional<Object> invokeMethodWithArgs(Object clazzOrInstance, String method, List<Object> args); static Optional<Object> invokeMethodWithArgs(Object clazzOrInstance, String method, List<Object> args, boolean setAccessible); static boolean typesMatch(Object[] argsArray, Class<?>[] parameterTypes); static boolean typesMatchUpTo(Object[] argsArray, Class<?>[] parameterTypes, int lengthRequired); static List<Class<?>> getAllInterfaces(@Nullable Class<?> type); static boolean hasNoArgConstructor(Class<?> clazz); static boolean hasNoNonObjectFields(Class<? extends Object> clazz); static Optional<String> tryFindMappedName(Map<String, String> renames, String name); static String findMappedNameAndLog(Map<String, String> renames, String name);  }