Search in sources :

Example 6 with SuperInheritanceHierarchyFunction

use of spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction in project spoon by INRIA.

the class ImportTest method testSuperInheritanceHierarchyFunctionNoClasspath.

@Test
public void testSuperInheritanceHierarchyFunctionNoClasspath() {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("src/test/resources/noclasspath/superclass/UnknownSuperClass.java");
    launcher.buildModel();
    final CtModel model = launcher.getModel();
    CtClass<?> classUSC = launcher.getFactory().Class().get("UnknownSuperClass");
    // contract: super inheritance scanner returns only Types on class path including final Object
    List<CtType> types = classUSC.map(new SuperInheritanceHierarchyFunction().includingSelf(true)).list();
    assertEquals(2, types.size());
    assertEquals("UnknownSuperClass", types.get(0).getQualifiedName());
    assertEquals("java.lang.Object", types.get(1).getQualifiedName());
    // contract: super inheritance scanner in reference mode returns type references including these which are not on class path and including final Object
    List<CtTypeReference> typeRefs = classUSC.map(new SuperInheritanceHierarchyFunction().includingSelf(true).returnTypeReferences(true)).list();
    assertEquals(3, typeRefs.size());
    assertEquals("UnknownSuperClass", typeRefs.get(0).getQualifiedName());
    assertEquals("NotInClasspath", typeRefs.get(1).getQualifiedName());
    assertEquals("java.lang.Object", typeRefs.get(2).getQualifiedName());
    // contract: super inheritance scanner in reference mode, which starts on class which is not available in model returns no Object, because it does not know if type is class or interface
    typeRefs = classUSC.getSuperclass().map(new SuperInheritanceHierarchyFunction().includingSelf(true).returnTypeReferences(true)).list();
    assertEquals(1, typeRefs.size());
    assertEquals("NotInClasspath", typeRefs.get(0).getQualifiedName());
    // contract: super inheritance scanner in type mode, which starts on class which is not available in model returns nothing
    types = classUSC.getSuperclass().map(new SuperInheritanceHierarchyFunction().includingSelf(true)).list();
    assertEquals(0, types.size());
}
Also used : SuperInheritanceHierarchyFunction(spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) CtModel(spoon.reflect.CtModel) Test(org.junit.Test)

Example 7 with SuperInheritanceHierarchyFunction

use of spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction in project spoon by INRIA.

the class ImportTest method testSuperInheritanceHierarchyFunction.

@Test
public void testSuperInheritanceHierarchyFunction() throws Exception {
    CtType<?> clientClass = (CtClass<?>) ModelUtils.buildClass(ClientClass.class);
    CtTypeReference<?> childClass = clientClass.getSuperclass();
    CtTypeReference<?> superClass = childClass.getSuperclass();
    List<String> result = clientClass.map(new SuperInheritanceHierarchyFunction().includingSelf(true)).map(e -> {
        assertTrue(e instanceof CtType);
        return ((CtType) e).getQualifiedName();
    }).list();
    // contract: includingSelf(true) should return input type too
    assertTrue(result.contains(clientClass.getQualifiedName()));
    assertTrue(result.contains(childClass.getQualifiedName()));
    assertTrue(result.contains(superClass.getQualifiedName()));
    assertTrue(result.contains(Object.class.getName()));
    result = clientClass.map(new SuperInheritanceHierarchyFunction().includingSelf(false)).map(e -> {
        assertTrue(e instanceof CtType);
        return ((CtType) e).getQualifiedName();
    }).list();
    // contract: includingSelf(false) should return input type too
    assertFalse(result.contains(clientClass.getQualifiedName()));
    assertTrue(result.contains(childClass.getQualifiedName()));
    assertTrue(result.contains(superClass.getQualifiedName()));
    assertTrue(result.contains(Object.class.getName()));
    // contract: returnTypeReferences(true) returns CtTypeReferences
    result = clientClass.map(new SuperInheritanceHierarchyFunction().includingSelf(true).returnTypeReferences(true)).map(e -> {
        assertTrue(e instanceof CtTypeReference);
        return ((CtTypeReference) e).getQualifiedName();
    }).list();
    // contract: includingSelf(false) should return input type too
    assertTrue(result.contains(clientClass.getQualifiedName()));
    assertTrue(result.contains(childClass.getQualifiedName()));
    assertTrue(result.contains(superClass.getQualifiedName()));
    assertTrue(result.contains(Object.class.getName()));
    // contract: the mapping can be started on type reference too
    result = clientClass.getReference().map(new SuperInheritanceHierarchyFunction().includingSelf(true).returnTypeReferences(true)).map(e -> {
        assertTrue(e instanceof CtTypeReference);
        return ((CtTypeReference) e).getQualifiedName();
    }).list();
    // contract: includingSelf(false) should return input type too
    assertTrue(result.contains(clientClass.getQualifiedName()));
    assertTrue(result.contains(childClass.getQualifiedName()));
    assertTrue(result.contains(superClass.getQualifiedName()));
    assertTrue(result.contains(Object.class.getName()));
    // contract: super type of Object is nothing
    List<CtTypeReference<?>> typeResult = clientClass.getFactory().Type().OBJECT.map(new SuperInheritanceHierarchyFunction().includingSelf(false).returnTypeReferences(true)).list();
    assertEquals(0, typeResult.size());
    typeResult = clientClass.getFactory().Type().OBJECT.map(new SuperInheritanceHierarchyFunction().includingSelf(true).returnTypeReferences(true)).list();
    assertEquals(1, typeResult.size());
    assertEquals(clientClass.getFactory().Type().OBJECT, typeResult.get(0));
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) SortedList(spoon.support.util.SortedList) Assert.assertThat(org.junit.Assert.assertThat) CtType(spoon.reflect.declaration.CtType) CtElement(spoon.reflect.declaration.CtElement) Reflection(spoon.test.imports.testclasses.Reflection) CtTypeAccess(spoon.reflect.code.CtTypeAccess) Assert.fail(org.junit.Assert.fail) Query(spoon.reflect.visitor.Query) ClientClass(spoon.test.imports.testclasses.ClientClass) CtThisAccess(spoon.reflect.code.CtThisAccess) CtImport(spoon.reflect.declaration.CtImport) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtField(spoon.reflect.declaration.CtField) CtReference(spoon.reflect.reference.CtReference) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SpoonResourceHelper(spoon.compiler.SpoonResourceHelper) SpoonException(spoon.SpoonException) Collection(java.util.Collection) ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) Set(java.util.Set) ModelUtils(spoon.testing.utils.ModelUtils) Collectors(java.util.stream.Collectors) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) IOUtils(org.apache.commons.io.IOUtils) NotImportExecutableType(spoon.test.imports.testclasses.NotImportExecutableType) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) CtParameter(spoon.reflect.declaration.CtParameter) CtMethod(spoon.reflect.declaration.CtMethod) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CompilationUnit(spoon.reflect.cu.CompilationUnit) CtStatement(spoon.reflect.code.CtStatement) ArrayList(java.util.ArrayList) SpoonModelBuilder(spoon.SpoonModelBuilder) ClassWithInvocation(spoon.test.imports.testclasses.ClassWithInvocation) HashSet(java.util.HashSet) Assert.assertSame(org.junit.Assert.assertSame) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) ChildClass(spoon.test.imports.testclasses.internal.ChildClass) ModelUtils.canBeBuilt(spoon.testing.utils.ModelUtils.canBeBuilt) StringTokenizer(java.util.StringTokenizer) SpoonResource(spoon.compiler.SpoonResource) ScanningMode(spoon.reflect.visitor.chain.ScanningMode) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) CtScannerListener(spoon.reflect.visitor.chain.CtScannerListener) PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) A(spoon.test.imports.testclasses.A) ImportScanner(spoon.reflect.visitor.ImportScanner) CtInvocation(spoon.reflect.code.CtInvocation) CtImportKind(spoon.reflect.declaration.CtImportKind) Assert.assertNotNull(org.junit.Assert.assertNotNull) SuperInheritanceHierarchyFunction(spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Factory(spoon.reflect.factory.Factory) Mole(spoon.test.imports.testclasses.Mole) Tacos(spoon.test.imports.testclasses.Tacos) File(java.io.File) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Pozole(spoon.test.imports.testclasses.Pozole) CtTypeReference(spoon.reflect.reference.CtTypeReference) StaticNoOrdered(spoon.test.imports.testclasses.StaticNoOrdered) Assert.assertNull(org.junit.Assert.assertNull) SubClass(spoon.test.imports.testclasses.SubClass) CtConstructorCall(spoon.reflect.code.CtConstructorCall) CtModel(spoon.reflect.CtModel) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) FileReader(java.io.FileReader) Assert.assertEquals(org.junit.Assert.assertEquals) CtClass(spoon.reflect.declaration.CtClass) SuperInheritanceHierarchyFunction(spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ClientClass(spoon.test.imports.testclasses.ClientClass) Test(org.junit.Test)

Aggregations

CtTypeReference (spoon.reflect.reference.CtTypeReference)7 SuperInheritanceHierarchyFunction (spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction)7 CtType (spoon.reflect.declaration.CtType)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Test (org.junit.Test)4 Launcher (spoon.Launcher)4 CtElement (spoon.reflect.declaration.CtElement)4 Arrays (java.util.Arrays)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 SpoonException (spoon.SpoonException)3 CtModel (spoon.reflect.CtModel)3 CtClass (spoon.reflect.declaration.CtClass)3 CtField (spoon.reflect.declaration.CtField)3 CtMethod (spoon.reflect.declaration.CtMethod)3 ModifierKind (spoon.reflect.declaration.ModifierKind)3 Factory (spoon.reflect.factory.Factory)3