use of spoon.Launcher in project spoon by INRIA.
the class ImportTest method testDeepNestedStaticPathWithTypedParameterWithImports.
@Test
public void testDeepNestedStaticPathWithTypedParameterWithImports() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "-i", "./src/test/resources/spoon/test/imports/testclasses2/StaticWithNested.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.StaticWithNested");
assertTrue("new StaticWithNested.StaticNested.StaticNested2<K>();", mm.toString().indexOf("new StaticWithNested.StaticNested.StaticNested2<K>();") >= 0);
}
use of spoon.Launcher in project spoon by INRIA.
the class ImportTest method testSpoonWithImports.
@Test
public void testSpoonWithImports() throws Exception {
final Launcher launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/imports/testclasses", "--output-type", "nooutput", "--with-imports" });
final CtClass<ImportTest> aClass = launcher.getFactory().Class().get(ChildClass.class);
final CtClass<ImportTest> anotherClass = launcher.getFactory().Class().get(ClientClass.class);
final CtClass<ImportTest> classWithInvocation = launcher.getFactory().Class().get(ClassWithInvocation.class);
ImportScanner importScanner = new ImportScannerImpl();
importScanner.computeImports(aClass);
assertEquals(2, importScanner.getAllImports().size());
importScanner = new ImportScannerImpl();
importScanner.computeImports(anotherClass);
// ClientClass needs 2 imports: ChildClass, PublicInterface2
assertEquals(2, importScanner.getAllImports().size());
// check that printer did not used the package protected class like "SuperClass.InnerClassProtected"
assertTrue(anotherClass.toString().indexOf("InnerClass extends ChildClass.InnerClassProtected") > 0);
importScanner = new ImportScannerImpl();
importScanner.computeImports(classWithInvocation);
// java.lang imports are also computed
assertEquals("Spoon ignores the arguments of CtInvocations", 3, importScanner.getAllImports().size());
}
use of spoon.Launcher in project spoon by INRIA.
the class ImportTest method testStaticImportForInvocationInNoClasspath.
@Test
public void testStaticImportForInvocationInNoClasspath() throws Exception {
final Launcher launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/resources/import-static", "--output-type", "nooutput", "--noclasspath" });
final List<CtInvocation<?>> elements = new SortedList(new CtLineElementComparator());
elements.addAll(Query.getElements(launcher.getFactory(), new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return !element.getExecutable().isConstructor() && super.matches(element);
}
}));
// Invocation for a static method with the declaring class specified.
assertCorrectInvocation(new Expected().name("staticMethod").target("A").declaringType("A").typeIsNull(true), elements.get(0));
// Invocation for a static method without the declaring class specified.
assertCorrectInvocation(new Expected().name("staticMethod").target("pack1.A").declaringType("A").typeIsNull(true), elements.get(1));
// Invocation for a static method with the declaring class specified and a return type.
assertCorrectInvocation(new Expected().name("staticMethod").target("A").declaringType("A").typeIsNull(false), elements.get(2));
// Invocation for a static method without the declaring class specified and a return type.
assertCorrectInvocation(new Expected().name("staticMethod").target("pack1.A").declaringType("A").typeIsNull(false), elements.get(3));
// Invocation for a static method in an inner class with the declaring class specified.
assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(false), elements.get(4));
// Invocation for a static method in an inner class without the declaring class specified.
assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(true), elements.get(5));
// Invocation for a static method in an inner class with the declaring class specified and a return type.
assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(false), elements.get(6));
// Invocation for a static method in an innser class without the declaring class specified and a return type.
assertCorrectInvocation(new Expected().name("makeBurritos").target("Tacos.Burritos").declaringType("Burritos").typeIsNull(false), elements.get(7));
// Invocation for a static method in an inner class with the declaring class specified.
assertCorrectInvocation(new Expected().name("staticD").target("C.D").declaringType("D").typeIsNull(true), elements.get(8));
// Invocation for a static method in an inner class without the declaring class specified.
assertCorrectInvocation(new Expected().name("staticD").target("pack2.C.D").declaringType("D").typeIsNull(true), elements.get(9));
// Invocation for a static method in an inner class with the declaring class specified and a return type.
assertCorrectInvocation(new Expected().name("staticD").target("C.D").declaringType("D").typeIsNull(false), elements.get(10));
// Invocation for a static method in an inner class without the declaring class specified and a return type.
assertCorrectInvocation(new Expected().name("staticD").target("pack2.C.D").declaringType("D").typeIsNull(false), elements.get(11));
// Invocation for a static method with the declaring class specified and an import *.
assertCorrectInvocation(new Expected().name("staticE").target("pack3.E.E").declaringType("E").typeIsNull(true), elements.get(12));
// Invocation for a static method without the declaring class specified and an import *.
assertCorrectInvocationWithLimit(new Expected().name("staticE").typeIsNull(true), elements.get(13));
// Invocation for a static method with the declaring class specified, a return type and an import *.
assertCorrectInvocation(new Expected().name("staticE").target("pack3.E.E").declaringType("E").typeIsNull(false), elements.get(14));
// Invocation for a static method without the declaring class specified, a return type and an import *.
assertCorrectInvocationWithLimit(new Expected().name("staticE").typeIsNull(false), elements.get(15));
}
use of spoon.Launcher 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.Launcher in project spoon by INRIA.
the class ImportTest method testMissingImport.
@Test
public void testMissingImport() throws Exception {
Launcher spoon = new Launcher();
spoon.setArgs(new String[] { "--output-type", "nooutput" });
Factory factory = spoon.createFactory();
factory.getEnvironment().setNoClasspath(true);
factory.getEnvironment().setLevel("OFF");
SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/import-resources/fr/inria/MissingImport.java"));
compiler.build();
CtTypeReference<?> type = factory.Class().getAll().get(0).getFields().get(0).getType();
assertEquals("Abcd", type.getSimpleName());
assertEquals("fr.inria.internal", type.getPackage().getSimpleName());
}
Aggregations