use of spoon.Launcher in project spoon by INRIA.
the class CompilationTest method testPrecompile.
@Test
public void testPrecompile() {
// without precompile
Launcher l = new Launcher();
l.setArgs(new String[] { "--noclasspath", "-i", "src/test/resources/compilation/" });
l.buildModel();
CtClass klass = l.getFactory().Class().get("compilation.Bar");
// without precompile, actualClass does not exist (an exception is thrown)
try {
klass.getSuperInterfaces().toArray(new CtTypeReference[0])[0].getActualClass();
fail();
} catch (SpoonClassNotFoundException ignore) {
}
// with precompile
Launcher l2 = new Launcher();
l2.setArgs(new String[] { "--precompile", "--noclasspath", "-i", "src/test/resources/compilation/" });
l2.buildModel();
CtClass klass2 = l2.getFactory().Class().get("compilation.Bar");
// with precompile, actualClass is not null
Class actualClass = klass2.getSuperInterfaces().toArray(new CtTypeReference[0])[0].getActualClass();
assertNotNull(actualClass);
assertEquals("IBar", actualClass.getSimpleName());
// precompile can be used to compile processors on the fly
Launcher l3 = new Launcher();
l3.setArgs(new String[] { "--precompile", "--noclasspath", "-i", "src/test/resources/compilation/", "-p", "compilation.SimpleProcessor" });
l3.run();
}
use of spoon.Launcher in project spoon by INRIA.
the class CompilationTest method testFilterResourcesFile.
@Test
public void testFilterResourcesFile() throws Exception {
// shows how to filter input java files, for https://github.com/INRIA/spoon/issues/877
Launcher launcher = new Launcher() {
@Override
public SpoonModelBuilder createCompiler() {
return new JDTBasedSpoonCompiler(getFactory()) {
@Override
protected JDTBatchCompiler createBatchCompiler() {
return new JDTBatchCompiler(this) {
@Override
public CompilationUnit[] getCompilationUnits() {
List<CompilationUnit> units = new ArrayList<>();
for (CompilationUnit u : super.getCompilationUnits()) {
if (new String(u.getMainTypeName()).contains("Foo")) {
units.add(u);
}
}
return units.toArray(new CompilationUnit[0]);
}
};
}
};
}
};
launcher.addInputResource("./src/test/java/spoon/test/imports");
launcher.buildModel();
int n = 0;
// we indeed only have types declared in a file called *Foo*
for (CtType<?> t : launcher.getFactory().getModel().getAllTypes()) {
n++;
assertTrue(t.getPosition().getFile().getAbsolutePath().contains("Foo"));
}
assertTrue(n >= 2);
}
use of spoon.Launcher in project spoon by INRIA.
the class CompilationTest method testNewInstance.
@Test
public void testNewInstance() throws Exception {
// contract: a ctclass can be instantiated, and each modification results in a new valid object
Factory factory = new Launcher().getFactory();
CtClass<Ifoo> c = factory.Code().createCodeSnippetStatement("class X implements spoon.test.compilation.Ifoo { public int foo() {int i=0; return i;} }").compile();
// required otherwise java.lang.IllegalAccessException at runtime when instantiating
c.addModifier(ModifierKind.PUBLIC);
CtBlock body = c.getElements(new TypeFilter<>(CtBlock.class)).get(1);
Ifoo o = c.newInstance();
assertEquals(0, o.foo());
for (int i = 1; i <= 10; i++) {
body.getStatement(0).replace(factory.Code().createCodeSnippetStatement("int i = " + i + ";"));
o = c.newInstance();
// each time this is a new class
// each time the behavior has changed!
assertEquals(i, o.foo());
}
}
use of spoon.Launcher in project spoon by INRIA.
the class CompilationTest method testSingleClassLoader.
@Test
public void testSingleClassLoader() throws Exception {
/*
* contract: the environment exposes a classloader configured by the spoonclass path,
* there is one class loader, so the loaded classes are compatible
*/
Launcher launcher = new Launcher();
launcher.addInputResource(new FileSystemFolder("./src/test/resources/classloader-test"));
File outputBinDirectory = new File("./target/classloader-test");
if (!outputBinDirectory.exists()) {
outputBinDirectory.mkdirs();
}
launcher.setBinaryOutputDirectory(outputBinDirectory);
launcher.getModelBuilder().build();
CtTypeReference<?> mIFoo = launcher.getFactory().Type().createReference("spoontest.IFoo");
CtTypeReference<?> mFoo = launcher.getFactory().Type().createReference("spoontest.Foo");
assertTrue("Foo subtype of IFoo", mFoo.isSubtypeOf(mIFoo));
launcher.getModelBuilder().compile(SpoonModelBuilder.InputType.FILES);
// Create new launcher which uses classes compiled by previous launcher.
// It simulates the classes without sources, which has to be accessed using reflection
launcher = new Launcher();
// not in the classpath
try {
Class.forName("spoontest.IFoo");
fail();
} catch (ClassNotFoundException expected) {
}
// not in the spoon classpath before setting it
try {
launcher.getEnvironment().getInputClassLoader().loadClass("spoontest.IFoo");
fail();
} catch (ClassNotFoundException expected) {
}
launcher.getEnvironment().setSourceClasspath(new String[] { outputBinDirectory.getAbsolutePath() });
mIFoo = launcher.getFactory().Type().createReference("spoontest.IFoo");
mFoo = launcher.getFactory().Type().createReference("spoontest.Foo");
// if it fails then it is because each class is loaded by different class loader
assertTrue("Foo subtype of IFoo", mFoo.isSubtypeOf(mIFoo));
// not in the spoon classpath before setting it
Class<?> ifoo = launcher.getEnvironment().getInputClassLoader().loadClass("spoontest.IFoo");
Class<?> foo = launcher.getEnvironment().getInputClassLoader().loadClass("spoontest.Foo");
assertTrue(ifoo.isAssignableFrom(foo));
assertTrue(ifoo.getClassLoader() == foo.getClassLoader());
}
use of spoon.Launcher in project spoon by INRIA.
the class GetBinaryFilesTest method testAnonymousClasses.
@Test
public void testAnonymousClasses() throws IOException {
final String input = "./src/test/java/spoon/test/secondaryclasses/testclasses/AnonymousClass.java";
final Launcher launcher = new Launcher();
launcher.addInputResource(input);
launcher.setBinaryOutputDirectory(tmpFolder.getRoot());
launcher.buildModel();
launcher.getModelBuilder().compile(SpoonModelBuilder.InputType.FILES);
final Map<String, CompilationUnit> cus = launcher.getFactory().CompilationUnit().getMap();
assertEquals(1, cus.size());
final List<File> binaries = cus.get(new File(input).getCanonicalFile().getAbsolutePath()).getBinaryFiles();
assertEquals(4, binaries.size());
assertEquals("AnonymousClass.class", binaries.get(0).getName());
assertEquals("AnonymousClass$I.class", binaries.get(1).getName());
assertEquals("AnonymousClass$1.class", binaries.get(2).getName());
assertEquals("AnonymousClass$2.class", binaries.get(3).getName());
assertTrue(binaries.get(0).isFile());
assertTrue(binaries.get(1).isFile());
assertTrue(binaries.get(2).isFile());
assertTrue(binaries.get(3).isFile());
}
Aggregations