Search in sources :

Example 66 with CtClass

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

the class ImportBuilderTest method testWithSimpleImportNoAutoimport.

@Test
public void testWithSimpleImportNoAutoimport() {
    // contract: when the source has one import, nothing is imported when not in autoimport
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses/ClassWithInvocation.java");
    spoon.getEnvironment().setAutoImports(false);
    spoon.buildModel();
    CtClass classA = spoon.getFactory().Class().get(ClassWithInvocation.class);
    CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
    assertTrue(unitA.getImports().isEmpty());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 67 with CtClass

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

the class ImportBuilderTest method testWithStaticInheritedImport.

@Test
public void testWithStaticInheritedImport() {
    // contract: When using starred static import of a type, it imports a starred type
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/StaticImportWithInheritance.java");
    spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/staticimport");
    spoon.getEnvironment().setAutoImports(true);
    spoon.getEnvironment().setShouldCompile(true);
    spoon.setSourceOutputDirectory("./target/spoon-jdtimport-inheritedstatic");
    spoon.run();
    CtClass classStatic = spoon.getFactory().Class().get(StaticImportWithInheritance.class);
    CompilationUnit unitStatic = spoon.getFactory().CompilationUnit().getMap().get(classStatic.getPosition().getFile().getPath());
    Collection<CtImport> imports = unitStatic.getImports();
    assertEquals(1, imports.size());
    CtImport ctImport = imports.iterator().next();
    assertEquals(CtImportKind.ALL_STATIC_MEMBERS, ctImport.getImportKind());
    assertEquals("import static spoon.test.jdtimportbuilder.testclasses.staticimport.DependencySubClass.*;\n", ctImport.toString());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) CtImport(spoon.reflect.declaration.CtImport) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 68 with CtClass

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

the class ImportBuilderTest method testWithSimpleImport.

@Test
public void testWithSimpleImport() {
    // contract: when the source has one import, the same import is created as a reference in auto-import mode
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses/ClassWithInvocation.java");
    spoon.getEnvironment().setAutoImports(true);
    spoon.buildModel();
    CtClass classA = spoon.getFactory().Class().get(ClassWithInvocation.class);
    CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
    Collection<CtImport> imports = unitA.getImports();
    assertEquals(1, imports.size());
    CtImport ref = imports.iterator().next();
    assertEquals("import spoon.test.annotation.testclasses.GlobalAnnotation;\n", ref.toString());
    assertTrue(ref.getReference() instanceof CtTypeReference);
    CtTypeReference refType = (CtTypeReference) ref.getReference();
    assertEquals("spoon.test.annotation.testclasses.GlobalAnnotation", refType.getQualifiedName());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) CtImport(spoon.reflect.declaration.CtImport) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 69 with CtClass

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

the class CtRenameLocalVariableRefactoringTest method testRenameAllLocalVariablesOfRenameTestSubject.

/**
 * The {@link CtRenameLocalVariableRefactoringTestSubject} class is loaded as spoon model. Then:
 * - It looks for each CtVariable and it's CtAnnotation and tries to rename that variable to the name defined by annotation.
 * - If the annotation name is prefixed with "-", then that refactoring should fail.
 * - If the annotation name is not prefixed, then that refactoring should pass.
 * If it behaves different then expected, then this test fails
 */
@Test
public void testRenameAllLocalVariablesOfRenameTestSubject() throws Exception {
    final Launcher launcher = new Launcher();
    final SpoonModelBuilder comp = launcher.createCompiler();
    comp.addInputSources(SpoonResourceHelper.resources("./src/test/java/" + CtRenameLocalVariableRefactoringTestSubject.class.getName().replace('.', '/') + ".java"));
    comp.build();
    final Factory factory = comp.getFactory();
    CtClass<?> varRenameClass = (CtClass<?>) factory.Type().get(CtRenameLocalVariableRefactoringTestSubject.class);
    CtTypeReference<TestTryRename> tryRename = varRenameClass.getFactory().createCtTypeReference(TestTryRename.class);
    varRenameClass.getMethods().forEach(method -> {
        // debugging support
        if (DEBUG.length == 3 && DEBUG[0].equals(method.getSimpleName()) == false)
            return;
        method.filterChildren((CtVariable var) -> true).map((CtVariable var) -> var.getAnnotation(tryRename)).forEach((CtAnnotation<TestTryRename> annotation) -> {
            String[] newNames = annotation.getActualAnnotation().value();
            CtVariable<?> targetVariable = (CtVariable<?>) annotation.getAnnotatedElement();
            for (String newName : newNames) {
                boolean renameShouldPass = newName.startsWith("-") == false;
                if (!renameShouldPass) {
                    newName = newName.substring(1);
                }
                if (targetVariable instanceof CtLocalVariable<?>) {
                    // debugging support
                    if (DEBUG.length == 3 && DEBUG[1].equals(targetVariable.getSimpleName()) && DEBUG[2].equals(newName)) {
                        // put breakpoint here and continue debugging of the buggy case
                        this.getClass();
                    }
                    checkLocalVariableRename(launcher, (CtLocalVariable<?>) targetVariable, newName, renameShouldPass);
                } else {
                // TODO test rename of other variables, e.g. parameters and catch... later
                }
            }
        });
    });
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Factory(spoon.reflect.factory.Factory) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtClass(spoon.reflect.declaration.CtClass) TestTryRename(spoon.test.refactoring.testclasses.TestTryRename) CtVariable(spoon.reflect.declaration.CtVariable) Launcher(spoon.Launcher) CtRenameLocalVariableRefactoringTestSubject(spoon.test.refactoring.testclasses.CtRenameLocalVariableRefactoringTestSubject) Test(org.junit.Test)

Example 70 with CtClass

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

the class RefactoringTest method testThisInConstructorAfterATransformation.

@Test
public void testThisInConstructorAfterATransformation() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "-i", "src/test/java/spoon/test/refactoring/testclasses", "-o", "target/spooned/refactoring", "-p", ThisTransformationProcessor.class.getName() });
    launcher.run();
    final CtClass<?> aClassX = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.refactoring.testclasses.AClassX");
    final CtInvocation<?> thisInvocation = aClassX.getElements(new AbstractFilter<CtInvocation<?>>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation<?> element) {
            return element.getExecutable().isConstructor();
        }
    }).get(0);
    assertEquals("this(\"\")", thisInvocation.toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

CtClass (spoon.reflect.declaration.CtClass)168 Test (org.junit.Test)151 Launcher (spoon.Launcher)102 Factory (spoon.reflect.factory.Factory)84 CtMethod (spoon.reflect.declaration.CtMethod)42 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)22 CtAnnotation (spoon.reflect.declaration.CtAnnotation)19 SpoonModelBuilder (spoon.SpoonModelBuilder)17 CtInvocation (spoon.reflect.code.CtInvocation)16 File (java.io.File)15 CtTypeReference (spoon.reflect.reference.CtTypeReference)15 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)15 Annotation (java.lang.annotation.Annotation)14 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)14 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)14 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)14 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)14 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)14 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)14 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)14