use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class FilterTest method testFunctionQueryStep.
@Test
public void testFunctionQueryStep() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
class Context {
int count = 0;
}
Context context = new Context();
CtQuery query = launcher.getFactory().Package().getRootPackage().filterChildren((CtClass<?> c) -> {
return true;
}).name("filter CtClass only").map((CtClass<?> c) -> c.getSuperInterfaces()).name("super interfaces").map((CtTypeReference<?> iface) -> iface.getTypeDeclaration()).map((CtType<?> iface) -> iface.getAllMethods()).name("allMethods if interface").map((CtMethod<?> method) -> method.getSimpleName().equals("make")).map((CtMethod<?> m) -> m.getType()).map((CtTypeReference<?> t) -> t.getTypeDeclaration());
((CtQueryImpl) query).logging(true);
query.forEach((CtInterface<?> c) -> {
assertEquals("ITostada", c.getSimpleName());
context.count++;
});
assertTrue(context.count > 0);
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class FilterTest method testSubInheritanceHierarchyResolver.
@Test
public void testSubInheritanceHierarchyResolver() throws Exception {
// contract; SubInheritanceHierarchyResolver supports finding subtypes in an incremental manner
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.buildModel();
SubInheritanceHierarchyResolver resolver = new SubInheritanceHierarchyResolver(launcher.getModel().getRootPackage());
// contract: by default, nothing is sent to the consumer
resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {
@Override
public void accept(CtType<?> ctType) {
fail();
}
});
// we add a type
resolver.addSuperType(launcher.getFactory().Type().createReference(AbstractTostada.class));
class Counter {
int counter = 0;
}
Counter c = new Counter();
resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {
@Override
public void accept(CtType<?> ctType) {
c.counter++;
}
});
// there are 5 subtypes of AbstractTostada
assertEquals(5, c.counter);
// we add a type already visited
resolver.addSuperType(launcher.getFactory().Type().createReference(Tostada.class));
// nothing is sent to the consumer
resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {
@Override
public void accept(CtType<?> ctType) {
fail();
}
});
// we add a new type
resolver.addSuperType(launcher.getFactory().Type().createReference(ITostada.class));
Counter c2 = new Counter();
resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {
@Override
public void accept(CtType<?> ctType) {
c2.counter++;
assertEquals("spoon.test.filters.testclasses.Tacos", ctType.getQualifiedName());
}
});
// only one subtype remains unvisited
assertEquals(1, c2.counter);
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class ImportTest method testStaticMethodWithDifferentClassSameNameCollision.
@Test
public void testStaticMethodWithDifferentClassSameNameCollision() {
// contract: when using static method, if there is a collision between class name AND between method names,
// we can only use the fully qualified name of the class to call the static method
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
String outputDir = "./target/spooned-apache";
launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/staticcollision/");
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.staticcollision.AllLangTestSuite");
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 ", !output.contains("import static spoon.test.imports.testclasses2.apachetestsuite.enum2.EnumTestSuite.suite;"));
assertTrue("The call to the last EnumTestSuite should be in FQN", output.contains("suite.addTest(spoon.test.imports.testclasses2.apachetestsuite.enum2.EnumTestSuite.suite());"));
canBeBuilt(outputDir, 3);
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class ImportTest method testImportWithGenerics.
@Test
public void testImportWithGenerics() throws IOException {
// contract: in noclasspath autoimport, we should be able to use generic type
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/import-with-generics/TestWithGenerics.java");
launcher.getEnvironment().setAutoImports(true);
launcher.getEnvironment().setShouldCompile(true);
launcher.getEnvironment().setNoClasspath(true);
launcher.setSourceOutputDirectory("./target/import-with-generics");
launcher.run();
PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
CtType element = launcher.getFactory().Class().get("spoon.test.imports.testclasses.TestWithGenerics");
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
String output = prettyPrinter.getResult();
assertTrue(output.contains("import spoon.test.imports.testclasses.withgenerics.Target;"));
}
use of spoon.reflect.declaration.CtType 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);
}
Aggregations