Search in sources :

Example 56 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testStaticMethodWithDifferentClassSameNameCollision.

@Test
public void testStaticMethodWithDifferentClassSameNameCollision() {
    // contract: when using static method, if there is a collision between class name AND between method names,
    // we can only use the fully qualified name of the class to call the static method
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(true);
    String outputDir = "./target/spooned-apache";
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/staticcollision/");
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/enums/");
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/enum2/");
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/LangTestSuite.java");
    launcher.setSourceOutputDirectory(outputDir);
    launcher.getEnvironment().setComplianceLevel(3);
    launcher.run();
    PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
    CtType element = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.apachetestsuite.staticcollision.AllLangTestSuite");
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String output = prettyPrinter.getResult();
    assertTrue("The file should not contain a static import ", !output.contains("import static spoon.test.imports.testclasses2.apachetestsuite.enum2.EnumTestSuite.suite;"));
    assertTrue("The call to the last EnumTestSuite should be in FQN", output.contains("suite.addTest(spoon.test.imports.testclasses2.apachetestsuite.enum2.EnumTestSuite.suite());"));
    canBeBuilt(outputDir, 3);
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 57 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testAccessToNestedClass.

@Test
public void testAccessToNestedClass() 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> aClass = launcher.getFactory().Class().get(ClientClass.class.getName() + "$InnerClass");
    assertEquals(ClientClass.class.getName() + "$InnerClass", aClass.getQualifiedName());
    final CtTypeReference<?> parentClass = aClass.getSuperclass();
    // comment next line and parentClass.getActualClass(); will fail anyway
    assertEquals("spoon.test.imports.testclasses.internal.SuperClass$InnerClassProtected", parentClass.getQualifiedName());
    Class<?> actualClass = parentClass.getActualClass();
    assertEquals("spoon.test.imports.testclasses.internal.SuperClass$InnerClassProtected", actualClass.getName());
}
Also used : Launcher(spoon.Launcher) Test(org.junit.Test)

Example 58 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testNewInnerClassDefinesInItsClassAndSuperClass.

@Test
public void testNewInnerClassDefinesInItsClassAndSuperClass() throws Exception {
    Launcher spoon = new Launcher();
    spoon.setArgs(new String[] { "--output-type", "nooutput" });
    Factory factory = spoon.createFactory();
    SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/imports/testclasses/SuperClass.java", "./src/test/java/spoon/test/imports/testclasses/SubClass.java"));
    compiler.build();
    final CtClass<?> subClass = (CtClass<?>) factory.Type().get(SubClass.class);
    final CtConstructorCall<?> ctConstructorCall = subClass.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class)).get(0);
    assertEquals("new spoon.test.imports.testclasses.SubClass.Item(\"\")", ctConstructorCall.toString());
    // here the buggy behavior with type members was encoded
    // so we fix it
    final String expected = "public class SubClass extends spoon.test.imports.testclasses.SuperClass {" + System.lineSeparator() + "    public void aMethod() {" + System.lineSeparator() + "        new spoon.test.imports.testclasses.SubClass.Item(\"\");" + System.lineSeparator() + "    }" + System.lineSeparator() + System.lineSeparator() + "    public static class Item extends spoon.test.imports.testclasses.SuperClass.Item {" + System.lineSeparator() + "        public Item(java.lang.String s) {" + System.lineSeparator() + "            super(1, s);" + System.lineSeparator() + "        }" + System.lineSeparator() + "    }" + System.lineSeparator() + "}";
    assertEquals(expected, subClass.toString());
}
Also used : SubClass(spoon.test.imports.testclasses.SubClass) SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 59 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testCanAccess.

@Test
public void testCanAccess() throws Exception {
    class Checker {

        final Launcher launcher;

        final CtTypeReference<?> aClientClass;

        final CtTypeReference<?> anotherClass;

        Checker() {
            launcher = new Launcher();
            launcher.setArgs(new String[] { "-i", "./src/test/java/spoon/test/imports/testclasses", "--with-imports" });
            launcher.buildModel();
            aClientClass = launcher.getFactory().Class().get(ClientClass.class).getReference();
            anotherClass = launcher.getFactory().Class().get(Tacos.class).getReference();
        }

        void checkCanAccess(String aClassName, boolean isInterface, boolean canAccessClientClass, boolean canAccessAnotherClass, String clientAccessType, String anotherAccessType) {
            CtTypeReference<?> target;
            if (isInterface) {
                target = launcher.getFactory().Interface().create(aClassName).getReference();
            } else {
                target = launcher.getFactory().Class().get(aClassName).getReference();
            }
            boolean isNested = target.getDeclaringType() != null;
            CtTypeReference<?> accessType;
            target.setParent(aClientClass.getTypeDeclaration());
            if (canAccessClientClass) {
                assertTrue("ClientClass should have access to " + aClassName + " but it has not", aClientClass.canAccess(target));
            } else {
                assertFalse("ClientClass should have NO access to " + aClassName + " but it has", aClientClass.canAccess(target));
            }
            if (isNested) {
                accessType = target.getAccessType();
                if (clientAccessType != null) {
                    assertEquals(clientAccessType, accessType.getQualifiedName());
                } else if (accessType != null) {
                    fail("ClientClass should have NO accessType to " + aClassName + " but it has " + accessType.getQualifiedName());
                }
            }
            target.setParent(anotherClass.getTypeDeclaration());
            if (canAccessAnotherClass) {
                assertTrue("Tacos class should have access to " + aClassName + " but it has not", anotherClass.canAccess(target));
            } else {
                assertFalse("Tacos class should have NO access to " + aClassName + " but it has", anotherClass.canAccess(target));
            }
            if (isNested) {
                if (anotherAccessType != null) {
                    accessType = target.getAccessType();
                    assertEquals(anotherAccessType, accessType.getQualifiedName());
                } else {
                    try {
                        accessType = target.getAccessType();
                    } catch (SpoonException e) {
                        if (e.getMessage().indexOf("Cannot compute access path to type: ") == -1) {
                            throw e;
                        }
                        // else OK, it should throw exception
                        accessType = null;
                    }
                    if (accessType != null) {
                        fail("Tacos class should have NO accessType to " + aClassName + " but it has " + accessType.getQualifiedName());
                    }
                }
            }
        }
    }
    Checker c = new Checker();
    c.checkCanAccess("spoon.test.imports.testclasses.ClientClass", false, true, true, null, null);
    c.checkCanAccess("spoon.test.imports.testclasses.ClientClass$InnerClass", false, true, false, "spoon.test.imports.testclasses.ClientClass", "spoon.test.imports.testclasses.ClientClass");
    c.checkCanAccess("spoon.test.imports.testclasses.internal.ChildClass", false, true, true, null, null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.PublicInterface2", true, true, true, null, null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.PublicInterface2$NestedInterface", true, true, true, "spoon.test.imports.testclasses.internal.PublicInterface2", "spoon.test.imports.testclasses.internal.PublicInterface2");
    c.checkCanAccess("spoon.test.imports.testclasses.internal.PublicInterface2$NestedClass", true, true, true, "spoon.test.imports.testclasses.internal.PublicInterface2", "spoon.test.imports.testclasses.internal.PublicInterface2");
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$PublicInterface", true, true, true, "spoon.test.imports.testclasses.internal.ChildClass", null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$PackageProtectedInterface", true, false, false, "spoon.test.imports.testclasses.internal.ChildClass", null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$ProtectedInterface", true, true, false, "spoon.test.imports.testclasses.internal.ChildClass", null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$ProtectedInterface$NestedOfProtectedInterface", true, true, true, /*canAccess, but has no access to accessType*/
    "spoon.test.imports.testclasses.internal.SuperClass$ProtectedInterface", null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$ProtectedInterface$NestedPublicInterface", true, true, true, /*canAccess, but has no access to accessType*/
    "spoon.test.imports.testclasses.internal.SuperClass$ProtectedInterface", null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$PublicInterface", true, true, true, /*canAccess, but has no access to accessType*/
    "spoon.test.imports.testclasses.internal.ChildClass", null);
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$PublicInterface$NestedOfPublicInterface", true, true, true, /*canAccess, has access to first accessType, but not to full accesspath*/
    "spoon.test.imports.testclasses.internal.SuperClass$PublicInterface", "spoon.test.imports.testclasses.internal.SuperClass$PublicInterface");
    c.checkCanAccess("spoon.test.imports.testclasses.internal.SuperClass$PublicInterface$NestedPublicInterface", true, true, true, /*canAccess, has access to first accessType, but not to full accesspath*/
    "spoon.test.imports.testclasses.internal.SuperClass$PublicInterface", "spoon.test.imports.testclasses.internal.SuperClass$PublicInterface");
}
Also used : SpoonException(spoon.SpoonException) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 60 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testNestedAccessPathWithTypedParameterWithImports.

@Test
public void testNestedAccessPathWithTypedParameterWithImports() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "-i", "./src/test/resources/spoon/test/imports/testclasses2/AbstractMapBasedMultimap.java", "--with-imports" });
    launcher.buildModel();
    launcher.prettyprint();
    try {
        launcher.getModelBuilder().compile();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    CtClass<?> mm = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.AbstractMapBasedMultimap");
    CtClass<?> mmwli = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.AbstractMapBasedMultimap$WrappedList$WrappedListIterator");
    assertEquals("private class WrappedListIterator extends AbstractMapBasedMultimap<K, V>.WrappedCollection.WrappedIterator {}", mmwli.toString());
    assertTrue(mm.toString().indexOf("AbstractMapBasedMultimap<K, V>.WrappedCollection.WrappedIterator") >= 0);
    CtClass<?> mmwliother = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.AbstractMapBasedMultimap$OtherWrappedList$WrappedListIterator");
    assertEquals("private class WrappedListIterator extends AbstractMapBasedMultimap<K, V>.OtherWrappedList.WrappedIterator {}", mmwliother.toString());
}
Also used : Launcher(spoon.Launcher) SpoonException(spoon.SpoonException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Launcher (spoon.Launcher)524 Test (org.junit.Test)486 Factory (spoon.reflect.factory.Factory)163 CtClass (spoon.reflect.declaration.CtClass)111 CtMethod (spoon.reflect.declaration.CtMethod)79 File (java.io.File)75 CtType (spoon.reflect.declaration.CtType)66 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)60 CtTypeReference (spoon.reflect.reference.CtTypeReference)48 SpoonModelBuilder (spoon.SpoonModelBuilder)44 ArrayList (java.util.ArrayList)43 CtAnnotation (spoon.reflect.declaration.CtAnnotation)38 CtInvocation (spoon.reflect.code.CtInvocation)32 CtElement (spoon.reflect.declaration.CtElement)27 CtPackage (spoon.reflect.declaration.CtPackage)27 FileSystemFile (spoon.support.compiler.FileSystemFile)26 CtStatement (spoon.reflect.code.CtStatement)25 CtField (spoon.reflect.declaration.CtField)24 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)24 SpoonAPI (spoon.SpoonAPI)22