Search in sources :

Example 16 with CtType

use of spoon.reflect.declaration.CtType 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 17 with CtType

use of spoon.reflect.declaration.CtType 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)

Example 18 with CtType

use of spoon.reflect.declaration.CtType 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 19 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class SignatureTest method testBugSignature.

@Test
public void testBugSignature() throws Exception {
    // contract: two methods with same name and return type yet different argument types
    // must have different signatures
    Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    SpoonModelBuilder comp = launcher.createCompiler();
    comp.addInputSources(SpoonResourceHelper.resources("./src/main/java/spoon/SpoonModelBuilder.java"));
    comp.build();
    CtType<?> ctClass = (CtType<?>) comp.getFactory().Type().get(SpoonModelBuilder.class);
    List<CtMethod> methods = ctClass.getElements(new NamedElementFilter<>(CtMethod.class, "addInputSource"));
    assertEquals(2, methods.size());
    CtMethod<?> method = methods.get(0);
    assertEquals("addInputSource(java.io.File)", method.getSignature());
    CtMethod<?> method2 = methods.get(1);
    assertEquals("addInputSource(spoon.compiler.SpoonResource)", method2.getSignature());
    assertNotEquals(method, method2);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 20 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class MetaModelTest method testGetRoleHandlersOfClass.

@Test
public void testGetRoleHandlersOfClass() {
    int countOfIfaces = 0;
    for (CtType spoonIface : Metamodel.getAllMetamodelInterfaces()) {
        countOfIfaces++;
        checkRoleHandlersOfType(spoonIface);
    }
    assertTrue(countOfIfaces > 10);
}
Also used : CtType(spoon.reflect.declaration.CtType) Test(org.junit.Test)

Aggregations

CtType (spoon.reflect.declaration.CtType)134 Test (org.junit.Test)67 Launcher (spoon.Launcher)60 ArrayList (java.util.ArrayList)42 CtMethod (spoon.reflect.declaration.CtMethod)38 CtTypeReference (spoon.reflect.reference.CtTypeReference)30 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)20 File (java.io.File)19 Factory (spoon.reflect.factory.Factory)19 PrettyPrinter (spoon.reflect.visitor.PrettyPrinter)19 List (java.util.List)18 Collectors (java.util.stream.Collectors)17 CtField (spoon.reflect.declaration.CtField)17 CtElement (spoon.reflect.declaration.CtElement)16 CtPackage (spoon.reflect.declaration.CtPackage)16 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)14 IOException (java.io.IOException)12 SpoonException (spoon.SpoonException)12 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)11 Set (java.util.Set)11