Search in sources :

Example 71 with Launcher

use of spoon.Launcher 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 72 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testShouldNotCreateAutoreference.

@Test
public void testShouldNotCreateAutoreference() {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(false);
    String outputDir = "./target/spooned-autoref";
    launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/ShouldNotAutoreference.java");
    launcher.setSourceOutputDirectory(outputDir);
    launcher.run();
    PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
    CtType element = launcher.getFactory().Class().getAll().get(0);
    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 for NOFOLLOW_LINKS", !output.contains("import static java.nio.file.LinkOption.NOFOLLOW_LINKS;"));
    canBeBuilt(outputDir, 7);
}
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 73 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testmportInCu.

@Test
public void testmportInCu() throws Exception {
    // contract: auto-import works for compilation units with multiple classes
    String[] options = { "--output-type", "compilationunits", "--output", "target/testmportInCu", "--with-imports" };
    String path = "spoon/test/prettyprinter/testclasses/A.java";
    final Launcher launcher = new Launcher();
    launcher.setArgs(options);
    launcher.addInputResource("./src/test/java/" + path);
    launcher.run();
    File output = new File("target/testmportInCu/" + path);
    String code = IOUtils.toString(new FileReader(output));
    // the ArrayList is imported and used in short mode
    assertTrue(code.contains("import java.util.ArrayList"));
    // no fully qualified usage
    assertFalse(code.contains("new java.util.ArrayList"));
    // sanity check: the actual code
    assertTrue(code.contains("ArrayList<String> list = new ArrayList<>()"));
    // cleaning
    output.delete();
}
Also used : Launcher(spoon.Launcher) FileReader(java.io.FileReader) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 74 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FieldAccessTest method testGetReference.

@Test
public void testGetReference() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setShouldCompile(true);
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/fieldaccesses/testclasses/");
    launcher.getEnvironment().setAutoImports(true);
    launcher.run();
    final CtClass<B> aClass = launcher.getFactory().Class().get(B.class);
    // now static fields are used with the name of the parent class
    assertEquals("A.myField", aClass.getElements(new TypeFilter<>(CtFieldWrite.class)).get(0).toString());
    assertEquals("finalField", aClass.getElements(new TypeFilter<>(CtFieldWrite.class)).get(1).toString());
}
Also used : B(spoon.test.fieldaccesses.testclasses.B) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 75 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FieldAccessTest method testFieldAccessWithoutAnyImport.

@Test
public void testFieldAccessWithoutAnyImport() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/fieldaccesses/testclasses/Kuu.java");
    launcher.addInputResource("./src/test/java/spoon/test/fieldaccesses/testclasses/Mole.java");
    launcher.run();
    final CtType<Kuu> aType = launcher.getFactory().Type().get(Kuu.class);
    final DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(aType.getFactory().getEnvironment());
    assertEquals(0, printer.computeImports(aType).size());
    assertEquals("spoon.test.fieldaccesses.testclasses.Mole.Delicious delicious", aType.getMethodsByName("m").get(0).getParameters().get(0).toString());
}
Also used : Launcher(spoon.Launcher) Kuu(spoon.test.fieldaccesses.testclasses.Kuu) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) 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