/** * Collects all implemented interfaces for a given class * @param clazz the IClass object to analyze * @return a set of IClass objects representing the interfaces */ public static Set<IClass> collectAllInterfaces(IClass clazz) { // do not check array classes if (clazz.isArrayClass()) return new HashSet<IClass>(); Set<IClass> interfaces = new HashSet<IClass>(clazz.getDirectInterfaces()); for (IClass c : clazz.getDirectInterfaces()) interfaces.addAll(collectAllInterfaces(c)); return interfaces; }
public static boolean isAppClass(IClass clazz) { // Normalization: // filter empty dummy classes // possibly related too: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4295934 boolean isEmptyInnerClass = WalaUtils.isInnerClass(clazz) && isAnonymousInnerClass(clazz) && (clazz.getDeclaredMethods().isEmpty() || (clazz.getDeclaredMethods().size() == 1 && clazz.getDeclaredMethods().iterator().next().isClinit()) && clazz.getDeclaredInstanceFields().isEmpty() && clazz.getDeclaredStaticFields().isEmpty() && clazz.getDirectInterfaces().isEmpty()); return clazz.getClassHierarchy().getScope().isApplicationLoader(clazz.getClassLoader()) && !isAndroidResourceClass(clazz) && !isEmptyInnerClass && !clazz.isSynthetic(); }
/** * @return Collection of IClasses, representing the interfaces this class implements. */ protected Collection<IClass> computeAllInterfacesAsCollection() { Collection<? extends IClass> c = getDirectInterfaces(); Set<IClass> result = HashSetFactory.make(); for (IClass klass : c) { if (klass.isInterface()) { result.add(klass); } else { Warnings.add(ClassHierarchyWarning.create("expected an interface " + klass)); } } // at this point result holds all interfaces the class directly extends. // now expand to a fixed point. Set<IClass> last = null; do { last = HashSetFactory.make(result); for (IClass i : last) { result.addAll(i.getDirectInterfaces()); } } while (last.size() < result.size()); // now add any interfaces implemented by the super class IClass sup = null; sup = getSuperclass(); if (sup != null) { result.addAll(sup.getAllImplementedInterfaces()); } return result; }
/** * @return Collection of IClasses, representing the interfaces this class implements. */ protected Collection<IClass> computeAllInterfacesAsCollection() { Collection<? extends IClass> c = getDirectInterfaces(); Set<IClass> result = HashSetFactory.make(); for (IClass klass : c) { if (klass.isInterface()) { result.add(klass); } else { Warnings.add(ClassHierarchyWarning.create("expected an interface " + klass)); } } // at this point result holds all interfaces the class directly extends. // now expand to a fixed point. Set<IClass> last = null; do { last = HashSetFactory.make(result); for (IClass i : last) { result.addAll(i.getDirectInterfaces()); } } while (last.size() < result.size()); // now add any interfaces implemented by the super class IClass sup = null; sup = getSuperclass(); if (sup != null) { result.addAll(sup.getAllImplementedInterfaces()); } return result; }
@Test public void testInterfaceTest1() throws IllegalArgumentException, CancelException, IOException { runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), Arrays.asList( cg -> { final String typeStr = "IFoo"; final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy()); final IClass iClass = cg.getClassHierarchy().lookupClass(type); Assert.assertNotNull("Could not find class " + typeStr, iClass); Assert.assertTrue("Expected IFoo to be an interface.", iClass.isInterface()); }, cg -> { final String typeStr = "FooIT1"; final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy()); final IClass iClass = cg.getClassHierarchy().lookupClass(type); Assert.assertNotNull("Could not find class " + typeStr, iClass); final Collection<? extends IClass> interfaces = iClass.getDirectInterfaces(); Assert.assertEquals("Expected one single interface.", interfaces.size(), 1); Assert.assertTrue("Expected Foo to implement IFoo", interfaces.contains(cg.getClassHierarchy().lookupClass( findOrCreateTypeReference("Source", "IFoo", cg.getClassHierarchy())))); }), true, null); }