@Test public void testClassWithInstantiableBaseClass() throws Exception { Class<?>[] hierarchy = ProxyTypeInspector.getCompatibleClassHierarchy(getClass().getClassLoader(), MockExtendsImplementsExternal.class); Assert.assertEquals(MockBaseClassExternal.class, hierarchy[0]); Assert.assertEquals(MockInterface.class, hierarchy[1]); Assert.assertEquals(MockNestedInterface.class, hierarchy[2]); }
public static Class<?>[] getCompatibleClassHierarchy(ClassLoader loader, Class<?> origin) { Set<Class<?>> hierarchy = new LinkedHashSet<Class<?>>(); Class<?> baseClass = origin; while (baseClass != null && Modifier.isFinal(baseClass.getModifiers())) { baseClass = baseClass.getSuperclass(); } while (baseClass != null && !baseClass.isInterface() && baseClass.getSuperclass() != null && !baseClass.getSuperclass().equals(Object.class) && !Proxies.isInstantiable(baseClass)) { baseClass = baseClass.getSuperclass(); } if (baseClass != null && ClassLoaders.containsClass(loader, baseClass.getName()) && !Object.class.equals(baseClass) && (Proxies.isInstantiable(baseClass) || baseClass.isInterface())) { hierarchy.add(ClassLoaders.loadClass(loader, baseClass)); } baseClass = origin; while (baseClass != null) { for (Class<?> type : baseClass.getInterfaces()) { if (ClassLoaders.containsClass(loader, type.getName())) hierarchy.add(ClassLoaders.loadClass(loader, type)); else hierarchy.addAll(java.util.Arrays.asList(getCompatibleClassHierarchy(loader, type))); } baseClass = baseClass.getSuperclass(); } return hierarchy.toArray(new Class<?>[hierarchy.size()]); }
ProxyTypeInspector { public static Class<?>[] getCompatibleClassHierarchy(ClassLoader loader, Class<?> origin) { Set<Class<?>> hierarchy = new LinkedHashSet<Class<?>>(); Class<?> baseClass = origin; while (baseClass != null && Modifier.isFinal(baseClass.getModifiers())) { baseClass = baseClass.getSuperclass(); } while (baseClass != null && !baseClass.isInterface() && baseClass.getSuperclass() != null && !baseClass.getSuperclass().equals(Object.class) && !Proxies.isInstantiable(baseClass)) { baseClass = baseClass.getSuperclass(); } if (baseClass != null && ClassLoaders.containsClass(loader, baseClass.getName()) && !Object.class.equals(baseClass) && (Proxies.isInstantiable(baseClass) || baseClass.isInterface())) { hierarchy.add(ClassLoaders.loadClass(loader, baseClass)); } baseClass = origin; while (baseClass != null) { for (Class<?> type : baseClass.getInterfaces()) { if (ClassLoaders.containsClass(loader, type.getName())) hierarchy.add(ClassLoaders.loadClass(loader, type)); else hierarchy.addAll(java.util.Arrays.asList(getCompatibleClassHierarchy(loader, type))); } baseClass = baseClass.getSuperclass(); } return hierarchy.toArray(new Class<?>[hierarchy.size()]); } }
ProxyTypeInspector { public static Class<?>[] getCompatibleClassHierarchy(ClassLoader loader, Class<?> origin) { Set<Class<?>> hierarchy = new LinkedHashSet<Class<?>>(); Class<?> baseClass = origin; while (baseClass != null && Modifier.isFinal(baseClass.getModifiers())) { baseClass = baseClass.getSuperclass(); } while (baseClass != null && !baseClass.isInterface() && baseClass.getSuperclass() != null && !baseClass.getSuperclass().equals(Object.class) && !Proxies.isInstantiable(baseClass)) { baseClass = baseClass.getSuperclass(); } if (baseClass != null && ClassLoaders.containsClass(loader, baseClass.getName()) && !Object.class.equals(baseClass) && (Proxies.isInstantiable(baseClass) || baseClass.isInterface())) { hierarchy.add(ClassLoaders.loadClass(loader, baseClass)); } baseClass = origin; while (baseClass != null) { for (Class<?> type : baseClass.getInterfaces()) { if (ClassLoaders.containsClass(loader, type.getName())) hierarchy.add(ClassLoaders.loadClass(loader, type)); else hierarchy.addAll(java.util.Arrays.asList(getCompatibleClassHierarchy(loader, type))); } baseClass = baseClass.getSuperclass(); } return hierarchy.toArray(new Class<?>[hierarchy.size()]); }  }
ProxyTypeInspector { public static Class<?>[] getCompatibleClassHierarchy(ClassLoader loader, Class<?> origin) { Set<Class<?>> hierarchy = new LinkedHashSet<Class<?>>(); Class<?> baseClass = origin; while (baseClass != null && Modifier.isFinal(baseClass.getModifiers())) { baseClass = baseClass.getSuperclass(); } while (baseClass != null && !baseClass.isInterface() && baseClass.getSuperclass() != null && !baseClass.getSuperclass().equals(Object.class) && !Proxies.isInstantiable(baseClass)) { baseClass = baseClass.getSuperclass(); } if (baseClass != null && ClassLoaders.containsClass(loader, baseClass.getName()) && !Object.class.equals(baseClass) && (Proxies.isInstantiable(baseClass) || baseClass.isInterface())) { hierarchy.add(ClassLoaders.loadClass(loader, baseClass)); } baseClass = origin; while (baseClass != null) { for (Class<?> type : baseClass.getInterfaces()) { if (ClassLoaders.containsClass(loader, type.getName())) hierarchy.add(ClassLoaders.loadClass(loader, type)); else hierarchy.addAll(java.util.Arrays.asList(getCompatibleClassHierarchy(loader, type))); } baseClass = baseClass.getSuperclass(); } return hierarchy.toArray(new Class<?>[hierarchy.size()]); }  static Class<?>[] getCompatibleClassHierarchy(ClassLoader loader, Class<?> origin); static boolean superclassHierarchyContains(Class<?> haystack, Class<?> needle); }
ProxyTypeInspector { public static Class<?>[] getCompatibleClassHierarchy(ClassLoader loader, Class<?> origin) { Set<Class<?>> hierarchy = new LinkedHashSet<Class<?>>(); Class<?> baseClass = origin; while (baseClass != null && Modifier.isFinal(baseClass.getModifiers())) { baseClass = baseClass.getSuperclass(); } while (baseClass != null && !baseClass.isInterface() && baseClass.getSuperclass() != null && !baseClass.getSuperclass().equals(Object.class) && !Proxies.isInstantiable(baseClass)) { baseClass = baseClass.getSuperclass(); } if (baseClass != null && ClassLoaders.containsClass(loader, baseClass.getName()) && !Object.class.equals(baseClass) && (Proxies.isInstantiable(baseClass) || baseClass.isInterface())) { hierarchy.add(ClassLoaders.loadClass(loader, baseClass)); } baseClass = origin; while (baseClass != null) { for (Class<?> type : baseClass.getInterfaces()) { if (ClassLoaders.containsClass(loader, type.getName())) hierarchy.add(ClassLoaders.loadClass(loader, type)); else hierarchy.addAll(java.util.Arrays.asList(getCompatibleClassHierarchy(loader, type))); } baseClass = baseClass.getSuperclass(); } return hierarchy.toArray(new Class<?>[hierarchy.size()]); }  static Class<?>[] getCompatibleClassHierarchy(ClassLoader loader, Class<?> origin); static boolean superclassHierarchyContains(Class<?> haystack, Class<?> needle);  }