/** * Search if class implements the specified interface. If so it returns the class representing * the interface. This is recursive method, so it searches also all the superclasses of the * given class and all the superinterfaces of directly implemented interfaces. * * @param clazz * Class to check * @param interfaceName * Interface to locate * @return Interface {@link Class} object or <code>null</code> if one can not be found on the * given class and it's superclasses. */ public static Class<?> searchInterface(Class<?> clazz, String interfaceName) { if (clazz == null) { return null; } List<Class<?>> searched = new ArrayList<Class<?>>(); return searchInterface(clazz, interfaceName, searched); }
classesToRetransform.add(clazz); String classLoaderName = ClassUtil.getClassLoaderName(clazz, "unknown classloader"); classLoaderMultiset.add(classLoaderName);
Class<?> found = searchInterface(interf, interfaceName, searched); if (null != found) { return found; return searchInterface(clazz.getSuperclass(), interfaceName, searched);
@Test public void classWithoutSomeInterface() { String interfaceName = SearchedInterface.class.getName(); Class<?> found = ClassUtil.searchInterface(ClassWithSomeInterface.class, interfaceName); assertThat(found, is(nullValue())); }
@Test public void classWithInterfaceOther() { String interfaceName = "other"; Class<?> found = ClassUtil.searchInterface(ClassWithSearchedInterface.class, interfaceName); assertThat(found, is(nullValue())); }
@Test public void superclassExtendsClassWithSearchedInterfaceOther() { String interfaceName = "other"; Class<?> found = ClassUtil.searchInterface(SuperclassExtendsClassWithSearchedInterface.class, interfaceName); assertThat(found, is(nullValue())); }
@Test public void superclassExtendsClassClassWithSubInterfaceOther() { String interfaceName = "other"; Class<?> found = ClassUtil.searchInterface(SuperclassExtendsClassClassWithSubInterface.class, interfaceName); assertThat(found, is(nullValue())); }
@Test public void classWithSubInterfaceOther() { String interfaceName = "other"; Class<?> found = ClassUtil.searchInterface(ClassWithSubInterface.class, interfaceName); assertThat(found, is(nullValue())); }
@Test public void classNull() { String interfaceName = SearchedInterface.class.getName(); Class<?> found = ClassUtil.searchInterface(null, interfaceName); assertThat(found, is(nullValue())); }
@Test public void classWithoutInterface() { String interfaceName = SearchedInterface.class.getName(); Class<?> found = ClassUtil.searchInterface(ClassWithoutInterface.class, interfaceName); assertThat(found, is(nullValue())); }
@Test public void superclassExtendsClassWithSearchedInterface() { String interfaceName = SearchedInterface.class.getName(); Class<?> found = ClassUtil.searchInterface(SuperclassExtendsClassWithSearchedInterface.class, interfaceName); assertThat(SearchedInterface.class.equals(found), is(true)); }
@Test public void classWithInterface() { String interfaceName = SearchedInterface.class.getName(); Class<?> found = ClassUtil.searchInterface(ClassWithSearchedInterface.class, interfaceName); assertThat(SearchedInterface.class.equals(found), is(true)); }
@Test public void classWithSubInterface() { String interfaceName = SearchedInterface.class.getName(); Class<?> found = ClassUtil.searchInterface(ClassWithSubInterface.class, interfaceName); assertThat(SearchedInterface.class.equals(found), is(true)); }
@Test public void superclassExtendsClassClassWithSubInterface() { String interfaceName = SearchedInterface.class.getName(); Class<?> found = ClassUtil.searchInterface(SuperclassExtendsClassClassWithSubInterface.class, interfaceName); assertThat(SearchedInterface.class.equals(found), is(true)); }
return false; Class<?> intf = ClassUtil.searchInterface(c, interfaceName); if (null != intf) { whiteList.addIfAbsent(c);
interfaceClass = ClassUtil.searchInterface(clazz, interfaceName);