Search in sources :

Example 1 with ClientClass

use of spoon.test.imports.testclasses.ClientClass in project spoon by INRIA.

the class ImportTest method testAccessType.

@Test
public void testAccessType() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "-i", "./src/test/java/spoon/test/imports/testclasses", "--with-imports" });
    launcher.buildModel();
    final CtClass<ImportTest> aInnerClass = launcher.getFactory().Class().get(ClientClass.class.getName() + "$InnerClass");
    final CtClass<ImportTest> aSuperClass = launcher.getFactory().Class().get("spoon.test.imports.testclasses.internal.SuperClass");
    assertEquals(ClientClass.class.getName() + "$InnerClass", aInnerClass.getQualifiedName());
    // Check that access type of ClientClass$InnerClass in package protected class is still ClientClass
    assertEquals(ClientClass.class.getName(), aInnerClass.getReference().getAccessType().getQualifiedName());
    final CtTypeReference<?> innerClassProtectedByGetSuperClass = aInnerClass.getSuperclass();
    final CtTypeReference<?> innerClassProtectedByQualifiedName = launcher.getFactory().Class().get("spoon.test.imports.testclasses.internal.SuperClass$InnerClassProtected").getReference();
    assertEquals("spoon.test.imports.testclasses.internal.SuperClass$InnerClassProtected", innerClassProtectedByGetSuperClass.getQualifiedName());
    assertEquals("spoon.test.imports.testclasses.internal.SuperClass$InnerClassProtected", innerClassProtectedByQualifiedName.getQualifiedName());
    assertEquals("spoon.test.imports.testclasses.internal.ChildClass", innerClassProtectedByGetSuperClass.getAccessType().getQualifiedName());
    assertEquals("spoon.test.imports.testclasses.internal.SuperClass", innerClassProtectedByQualifiedName.getAccessType().getQualifiedName());
    assertEquals("spoon.test.imports.testclasses.internal.ChildClass.InnerClassProtected", innerClassProtectedByGetSuperClass.toString());
    assertEquals("spoon.test.imports.testclasses.internal.SuperClass.InnerClassProtected", innerClassProtectedByQualifiedName.toString());
}
Also used : Launcher(spoon.Launcher) ClientClass(spoon.test.imports.testclasses.ClientClass) Test(org.junit.Test)

Example 2 with ClientClass

use of spoon.test.imports.testclasses.ClientClass in project spoon by INRIA.

the class ImportTest method testSuperInheritanceHierarchyFunctionListener.

@Test
public void testSuperInheritanceHierarchyFunctionListener() throws Exception {
    CtType<?> clientClass = (CtClass<?>) ModelUtils.buildClass(ClientClass.class);
    CtTypeReference<?> childClass = clientClass.getSuperclass();
    CtTypeReference<?> superClass = childClass.getSuperclass();
    // contract: the enter and exit are always called with CtTypeReference instance
    List<String> result = clientClass.map(new SuperInheritanceHierarchyFunction().includingSelf(true).setListener(new CtScannerListener() {

        @Override
        public ScanningMode enter(CtElement element) {
            assertTrue(element instanceof CtTypeReference);
            return ScanningMode.NORMAL;
        }

        @Override
        public void exit(CtElement element) {
            assertTrue(element instanceof CtTypeReference);
        }
    })).map(e -> {
        assertTrue(e instanceof CtType);
        return ((CtType) e).getQualifiedName();
    }).list();
    assertTrue(result.contains(clientClass.getQualifiedName()));
    assertTrue(result.contains(childClass.getQualifiedName()));
    assertTrue(result.contains(superClass.getQualifiedName()));
    assertTrue(result.contains(Object.class.getName()));
    // contract: if listener skips ALL, then skipped element and all super classes are not returned
    result = clientClass.map(new SuperInheritanceHierarchyFunction().includingSelf(true).setListener(new CtScannerListener() {

        @Override
        public ScanningMode enter(CtElement element) {
            assertTrue(element instanceof CtTypeReference);
            if (superClass.getQualifiedName().equals(((CtTypeReference<?>) element).getQualifiedName())) {
                return ScanningMode.SKIP_ALL;
            }
            return ScanningMode.NORMAL;
        }

        @Override
        public void exit(CtElement element) {
            assertTrue(element instanceof CtTypeReference);
        }
    })).map(e -> {
        assertTrue(e instanceof CtType);
        return ((CtType) e).getQualifiedName();
    }).list();
    assertTrue(result.contains(clientClass.getQualifiedName()));
    assertTrue(result.contains(childClass.getQualifiedName()));
    assertFalse(result.contains(superClass.getQualifiedName()));
    assertFalse(result.contains(Object.class.getName()));
    // contract: if listener skips CHIDLREN, then skipped element is returned but all super classes are not returned
    result = clientClass.map(new SuperInheritanceHierarchyFunction().includingSelf(true).setListener(new CtScannerListener() {

        @Override
        public ScanningMode enter(CtElement element) {
            assertTrue(element instanceof CtTypeReference);
            if (superClass.getQualifiedName().equals(((CtTypeReference<?>) element).getQualifiedName())) {
                return ScanningMode.SKIP_CHILDREN;
            }
            return ScanningMode.NORMAL;
        }

        @Override
        public void exit(CtElement element) {
            assertTrue(element instanceof CtTypeReference);
        }
    })).map(e -> {
        assertTrue(e instanceof CtType);
        return ((CtType) e).getQualifiedName();
    }).list();
    assertTrue(result.contains(clientClass.getQualifiedName()));
    assertTrue(result.contains(childClass.getQualifiedName()));
    assertTrue(result.contains(superClass.getQualifiedName()));
    assertFalse(result.contains(Object.class.getName()));
}
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) SuperInheritanceHierarchyFunction(spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction) CtElement(spoon.reflect.declaration.CtElement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ClientClass(spoon.test.imports.testclasses.ClientClass) ScanningMode(spoon.reflect.visitor.chain.ScanningMode) CtClass(spoon.reflect.declaration.CtClass) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtScannerListener(spoon.reflect.visitor.chain.CtScannerListener) Test(org.junit.Test)

Example 3 with ClientClass

use of spoon.test.imports.testclasses.ClientClass 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)

Example 4 with ClientClass

use of spoon.test.imports.testclasses.ClientClass in project spoon by INRIA.

the class ChildClass method m.

public void m() {
    File f;
    ClientClass c;
}
Also used : ClientClass(spoon.test.imports.testclasses.ClientClass) File(java.io.File)

Aggregations

ClientClass (spoon.test.imports.testclasses.ClientClass)4 File (java.io.File)3 Test (org.junit.Test)3 Launcher (spoon.Launcher)3 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 StringTokenizer (java.util.StringTokenizer)2 Collectors (java.util.stream.Collectors)2 IOUtils (org.apache.commons.io.IOUtils)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertFalse (org.junit.Assert.assertFalse)2 Assert.assertNotEquals (org.junit.Assert.assertNotEquals)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2