Search in sources :

Example 66 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testNestedStaticPathWithTypedParameterWithImports.

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

Example 67 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testStaticMethodWithDifferentClassSameNameJava3NoCollision.

@Test
public void testStaticMethodWithDifferentClassSameNameJava3NoCollision() {
    // contract: when there is a collision between class names when using static method, we could not create a static import
    // as it is not compliant with java < 1.5, so we should use fully qualified name of the class
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(true);
    String outputDir = "./target/spooned-staticjava3";
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/staticjava3/");
    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.staticjava3.AllLangTestJava3");
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String output = prettyPrinter.getResult();
    assertFalse("The file should not contain a static import ", output.contains("import static"));
    assertTrue("The call to the last EnumTestSuite should be in FQN", output.contains("suite.addTest(spoon.test.imports.testclasses2.apachetestsuite.enums.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 68 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testGetImportKindReturnRightValue.

@Test
public void testGetImportKindReturnRightValue() {
    // contract: the importKind is computed based on the reference class type and the boolean isImportAllStaticTypeMembers
    final Launcher spoon = new Launcher();
    CtType aType = spoon.getFactory().Type().get(Reflection.class);
    CtImport ctImport = spoon.getFactory().createImport(aType.getReference());
    assertEquals(CtImportKind.TYPE, ctImport.getImportKind());
    ctImport = spoon.getFactory().createImport(spoon.getFactory().Type().createWildcardStaticTypeMemberReference(aType.getReference()));
    assertEquals(CtImportKind.ALL_STATIC_MEMBERS, ctImport.getImportKind());
    ctImport = spoon.getFactory().createImport(((CtMethod) aType.getAllMethods().iterator().next()).getReference());
    assertEquals(CtImportKind.METHOD, ctImport.getImportKind());
    ctImport = spoon.getFactory().createImport(((CtField) aType.getFields().get(0)).getReference());
    assertEquals(CtImportKind.FIELD, ctImport.getImportKind());
    ctImport = spoon.getFactory().createImport(aType.getPackage().getReference());
    assertEquals(CtImportKind.ALL_TYPES, ctImport.getImportKind());
}
Also used : CtImport(spoon.reflect.declaration.CtImport) CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 69 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testMultipleCU.

@Test
public void testMultipleCU() throws IOException {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(true);
    String outputDir = "./target/spooned-multiplecu";
    launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/multiplecu/");
    launcher.setSourceOutputDirectory(outputDir);
    launcher.run();
    canBeBuilt(outputDir, 7);
    String pathA = "spoon/test/imports/testclasses/multiplecu/A.java";
    String pathB = "spoon/test/imports/testclasses/multiplecu/B.java";
    File outputA = new File(outputDir + "/" + pathA);
    String codeA = IOUtils.toString(new FileReader(outputA));
    assertThat(codeA, containsString("import java.util.List;"));
    File outputB = new File(outputDir + "/" + pathB);
    String codeB = IOUtils.toString(new FileReader(outputB));
    assertThat(codeB, containsString("import java.awt.List;"));
}
Also used : Launcher(spoon.Launcher) FileReader(java.io.FileReader) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 70 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class ImportTest method testWithInnerEnumDoesNotImportStaticInnerMethods.

@Test
public void testWithInnerEnumDoesNotImportStaticInnerMethods() {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(true);
    String outputDir = "./target/spooned-innerenum";
    launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/StaticImportsFromEnum.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 to the inner enum method values", !output.contains("import static spoon.test.imports.testclasses.StaticImportsFromEnum$DataElement.values;"));
    assertTrue("The file should not contain a static import to the inner enum method values of a distinct interface", !output.contains("import static spoon.test.imports.testclasses.ItfWithEnum$Bar.values;"));
    assertTrue("The file should not contain a static import to the inner enum value", !output.contains("import static spoon.test.imports.testclasses.ItfWithEnum$Bar.Lip;"));
    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)

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