ReflectTools implements Serializable { public static Optional<ClassLoader> findClosestCommonClassLoaderAncestor( ClassLoader classLoaderA, ClassLoader classLoaderB) { if (classLoaderA == null || classLoaderB == null) { return Optional.empty(); } HashSet<ClassLoader> parents = new HashSet<>(); ClassLoader parentA = classLoaderA; ClassLoader parentB = classLoaderB; while (parentA != null || parentB != null) { if (parentA != null) { if (parents.contains(parentA)) { return Optional.of(parentA); } parents.add(parentA); parentA = parentA.getParent(); } if (parentB != null) { if (parents.contains(parentB)) { return Optional.of(parentB); } parents.add(parentB); parentB = parentB.getParent(); } } return Optional.empty(); }  static Method findMethod(Class<?> cls, String methodName,
            Class<?>... parameterTypes); static Object getJavaFieldValue(Object object,
            java.lang.reflect.Field field); static Object getJavaFieldValue(Object object,
            java.lang.reflect.Field field, Class<?> propertyType); static void setJavaFieldValue(Object object,
            java.lang.reflect.Field field, Object value); static Class<?> convertPrimitiveType(Class<?> type); static Serializable getPrimitiveDefaultValue(
            Class<?> primitiveType); static boolean isSetter(Method method); static boolean isSetterName(String methodName); static boolean isGetterName(String methodName, boolean isBoolean); static boolean isGetter(Method method); static Stream<Method> getGetterMethods(Class<?> type); static Stream<Method> getSetterMethods(Class<?> type); static boolean isNotObjectMethod(Method method); static String getPropertyName(Method method); static Type getPropertyType(Method method); static T createInstance(Class<T> cls); static T createProxyInstance(Class<T> proxyClass,
            Class<?> originalClass); static void checkClassAccessibility(Class<?> clazz); static Type createParameterizedType(Class<?> rawType, Type subType); static Class<?> getGenericInterfaceType(Class<?> clazz,
            Class<?> interfaceType); static Optional<Method> getGetter(Class<? extends Object> beanClass,
            String propertyName); static boolean isCheckedException(Class<?> exceptionClass); static Method getFunctionalMethod(Class<?> functionalClass); static List<Integer> getConstantIntValues(Class<?> clazz); static Class<?> findCommonBaseType(Class<?> a, Class<?> b); static Optional<ClassLoader> findClosestCommonClassLoaderAncestor(
            ClassLoader classLoaderA, ClassLoader classLoaderB); static boolean hasAnnotation(AnnotatedElement element,
            String annotationFqn); static Object getAnnotationMethodValue(Annotation annotation,
            String methodName); static Optional<Annotation> getAnnotation(AnnotatedElement element,
            String annotationFqn);  }
@Test public void findClosestCommonClassLoaderAncestor_findAncestor_whenBothArgumentsAreTheSame() { CustomClassLoader loader = new CustomClassLoader(); ClassLoader ret = ReflectTools .findClosestCommonClassLoaderAncestor(loader, loader).get(); Assert.assertEquals(loader, ret); }
@Test public void findClosestCommonClassLoaderAncestor_findsAncestor_whenOneIsParentOfTheOther() { CustomClassLoader parent = new CustomClassLoader(); CustomClassLoader child = new CustomClassLoader(parent); ClassLoader ret = ReflectTools .findClosestCommonClassLoaderAncestor(parent, child).get(); Assert.assertEquals(parent, ret); }
@Test public void findClosestCommonClassLoaderAncestor_findsAncestor_whenLoadersShareParent() { CustomClassLoader parent = new CustomClassLoader(); CustomClassLoader childA = new CustomClassLoader(parent); CustomClassLoader childB = new CustomClassLoader(parent); ClassLoader ret = ReflectTools .findClosestCommonClassLoaderAncestor(childA, childB).get(); Assert.assertEquals(parent, ret); }
@Test public void findClosestCommonClassLoaderAncestor_findsAncestor_whenAncestorsAreOnDifferentLevels() { CustomClassLoader grandParent = new CustomClassLoader(); CustomClassLoader parent = new CustomClassLoader(grandParent); CustomClassLoader childA = new CustomClassLoader(parent); CustomClassLoader childB = new CustomClassLoader(grandParent); ClassLoader ret = ReflectTools .findClosestCommonClassLoaderAncestor(childA, childB).get(); Assert.assertEquals(grandParent, ret); }
@Test public void findClosestCommonClassLoaderAncestor_empty_whenEitherOrBothNull() { CustomClassLoader loader = new CustomClassLoader(); Optional<ClassLoader> ret; ret = ReflectTools.findClosestCommonClassLoaderAncestor(loader, null); Assert.assertFalse(ret.isPresent()); ret = ReflectTools.findClosestCommonClassLoaderAncestor(null, loader); Assert.assertFalse(ret.isPresent()); ret = ReflectTools.findClosestCommonClassLoaderAncestor(null, null); Assert.assertFalse(ret.isPresent()); }