Search in sources :

Example 61 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testDeepNestedStaticPathWithTypedParameterWithImports.

@Test
public void testDeepNestedStaticPathWithTypedParameterWithImports() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "-i", "./src/test/resources/spoon/test/imports/testclasses2/StaticWithNested.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.StaticWithNested");
    assertTrue("new StaticWithNested.StaticNested.StaticNested2<K>();", mm.toString().indexOf("new StaticWithNested.StaticNested.StaticNested2<K>();") >= 0);
}
Also used : Launcher(spoon.Launcher) SpoonException(spoon.SpoonException) IOException(java.io.IOException) Test(org.junit.Test)

Example 62 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testSpoonWithImports.

@Test
public void testSpoonWithImports() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/imports/testclasses", "--output-type", "nooutput", "--with-imports" });
    final CtClass<ImportTest> aClass = launcher.getFactory().Class().get(ChildClass.class);
    final CtClass<ImportTest> anotherClass = launcher.getFactory().Class().get(ClientClass.class);
    final CtClass<ImportTest> classWithInvocation = launcher.getFactory().Class().get(ClassWithInvocation.class);
    ImportScanner importScanner = new ImportScannerImpl();
    importScanner.computeImports(aClass);
    assertEquals(2, importScanner.getAllImports().size());
    importScanner = new ImportScannerImpl();
    importScanner.computeImports(anotherClass);
    // ClientClass needs 2 imports: ChildClass, PublicInterface2
    assertEquals(2, importScanner.getAllImports().size());
    // check that printer did not used the package protected class like "SuperClass.InnerClassProtected"
    assertTrue(anotherClass.toString().indexOf("InnerClass extends ChildClass.InnerClassProtected") > 0);
    importScanner = new ImportScannerImpl();
    importScanner.computeImports(classWithInvocation);
    // java.lang imports are also computed
    assertEquals("Spoon ignores the arguments of CtInvocations", 3, importScanner.getAllImports().size());
}
Also used : ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) Launcher(spoon.Launcher) ImportScanner(spoon.reflect.visitor.ImportScanner) Test(org.junit.Test)

Example 63 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testStaticImportForInvocationInNoClasspath.

@Test
public void testStaticImportForInvocationInNoClasspath() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/resources/import-static", "--output-type", "nooutput", "--noclasspath" });
    final List<CtInvocation<?>> elements = new SortedList(new CtLineElementComparator());
    elements.addAll(Query.getElements(launcher.getFactory(), new TypeFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            return !element.getExecutable().isConstructor() && super.matches(element);
        }
    }));
    // Invocation for a static method with the declaring class specified.
    assertCorrectInvocation(new Expected().name("staticMethod").target("A").declaringType("A").typeIsNull(true), elements.get(0));
    // Invocation for a static method without the declaring class specified.
    assertCorrectInvocation(new Expected().name("staticMethod").target("pack1.A").declaringType("A").typeIsNull(true), elements.get(1));
    // Invocation for a static method with the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("staticMethod").target("A").declaringType("A").typeIsNull(false), elements.get(2));
    // Invocation for a static method without the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("staticMethod").target("pack1.A").declaringType("A").typeIsNull(false), elements.get(3));
    // Invocation for a static method in an inner class with the declaring class specified.
    assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(false), elements.get(4));
    // Invocation for a static method in an inner class without the declaring class specified.
    assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(true), elements.get(5));
    // Invocation for a static method in an inner class with the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(false), elements.get(6));
    // Invocation for a static method in an innser class without the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(false), elements.get(7));
    // Invocation for a static method in an inner class with the declaring class specified.
    assertCorrectInvocation(new Expected().name("staticD").target("C.D").declaringType("D").typeIsNull(true), elements.get(8));
    // Invocation for a static method in an inner class without the declaring class specified.
    assertCorrectInvocation(new Expected().name("staticD").target("pack2.C.D").declaringType("D").typeIsNull(true), elements.get(9));
    // Invocation for a static method in an inner class with the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("staticD").target("C.D").declaringType("D").typeIsNull(false), elements.get(10));
    // Invocation for a static method in an inner class without the declaring class specified and a return type.
    assertCorrectInvocation(new Expected().name("staticD").target("pack2.C.D").declaringType("D").typeIsNull(false), elements.get(11));
    // Invocation for a static method with the declaring class specified and an import *.
    assertCorrectInvocation(new Expected().name("staticE").target("pack3.E.E").declaringType("E").typeIsNull(true), elements.get(12));
    // Invocation for a static method without the declaring class specified and an import *.
    assertCorrectInvocationWithLimit(new Expected().name("staticE").typeIsNull(true), elements.get(13));
    // Invocation for a static method with the declaring class specified, a return type and an import *.
    assertCorrectInvocation(new Expected().name("staticE").target("pack3.E.E").declaringType("E").typeIsNull(false), elements.get(14));
    // Invocation for a static method without the declaring class specified, a return type and an import *.
    assertCorrectInvocationWithLimit(new Expected().name("staticE").typeIsNull(false), elements.get(15));
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) SortedList(spoon.support.util.SortedList) Launcher(spoon.Launcher) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 64 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testImportWithGenerics.

@Test
public void testImportWithGenerics() throws IOException {
    // contract: in noclasspath autoimport, we should be able to use generic type
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/import-with-generics/TestWithGenerics.java");
    launcher.getEnvironment().setAutoImports(true);
    launcher.getEnvironment().setShouldCompile(true);
    launcher.getEnvironment().setNoClasspath(true);
    launcher.setSourceOutputDirectory("./target/import-with-generics");
    launcher.run();
    PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
    CtType element = launcher.getFactory().Class().get("spoon.test.imports.testclasses.TestWithGenerics");
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String output = prettyPrinter.getResult();
    assertTrue(output.contains("import spoon.test.imports.testclasses.withgenerics.Target;"));
}
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 65 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testMissingImport.

@Test
public void testMissingImport() throws Exception {
    Launcher spoon = new Launcher();
    spoon.setArgs(new String[] { "--output-type", "nooutput" });
    Factory factory = spoon.createFactory();
    factory.getEnvironment().setNoClasspath(true);
    factory.getEnvironment().setLevel("OFF");
    SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/import-resources/fr/inria/MissingImport.java"));
    compiler.build();
    CtTypeReference<?> type = factory.Class().getAll().get(0).getFields().get(0).getType();
    assertEquals("Abcd", type.getSimpleName());
    assertEquals("fr.inria.internal", type.getPackage().getSimpleName());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) 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