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());
}
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());
}
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());
}
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
}
}
});
});
}
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());
}
Aggregations