use of spoon.SpoonAPI in project spoon by INRIA.
the class FactoryTest method testIncrementalModel.
@Test
public void testIncrementalModel() throws Exception {
// Feed some inputResources to a spoon compiler
SpoonAPI spoon = new Launcher();
spoon.addInputResource("src/test/java/spoon/test/factory/testclasses");
// Build model
spoon.buildModel();
assertEquals(1, spoon.getModel().getAllTypes().size());
// Do something with that model..
CtModel model = spoon.getModel();
model.processWith(new AbstractProcessor<CtMethod>() {
@Override
public void process(CtMethod element) {
element.setDefaultMethod(false);
}
});
// Feed some new inputResources
SpoonAPI spoon2 = new Launcher();
spoon2.addInputResource("src/test/java/spoon/test/factory/testclasses2");
// Build models of newly added classes/packages
spoon2.buildModel();
assertEquals(1, spoon2.getModel().getAllTypes().size());
// attach them to the existing model.
model.getRootPackage().addPackage(spoon2.getModel().getRootPackage());
// checking the results
assertEquals(6, model.getAllPackages().size());
assertEquals(2, model.getAllTypes().size());
assertEquals(1, model.getElements(new AbstractFilter<CtPackage>() {
@Override
public boolean matches(CtPackage element) {
return "spoon.test.factory.testclasses2".equals(element.getQualifiedName());
}
}).size());
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class FactoryTest method testCtModel.
@Test
public void testCtModel() throws Exception {
SpoonAPI spoon = new Launcher();
spoon.addInputResource("src/test/java/spoon/test/factory/testclasses");
spoon.buildModel();
CtModel model = spoon.getModel();
// contains Foo and Foo.@Bar
assertEquals(1, model.getAllTypes().size());
// [, spoon, spoon.test, spoon.test.factory, spoon.test.factory.testclasses]
assertEquals(5, model.getAllPackages().size());
// add to itself is fine
model.getRootPackage().addPackage(model.getRootPackage());
assertEquals(1, model.getAllTypes().size());
assertEquals(5, model.getAllPackages().size());
model.getRootPackage().getPackage("spoon").addPackage(model.getRootPackage().getPackage("spoon"));
assertEquals(1, model.getAllTypes().size());
assertEquals(5, model.getAllPackages().size());
model.getRootPackage().addPackage(model.getRootPackage().getPackage("spoon"));
assertEquals(1, model.getAllTypes().size());
assertEquals(5, model.getAllPackages().size());
CtPackage p = model.getElements(new NamedElementFilter<>(CtPackage.class, "spoon")).get(0).clone();
// if we change the implem, merge is impossible
CtField f = spoon.getFactory().Core().createField();
f.setSimpleName("foo");
f.setType(spoon.getFactory().Type().BYTE);
p.getElements(new NamedElementFilter<>(CtPackage.class, "testclasses")).get(0).getType("Foo").addField(f);
try {
model.getRootPackage().addPackage(p);
fail("no exception thrown");
} catch (IllegalStateException success) {
}
model.processWith(new AbstractProcessor<CtType>() {
@Override
public void process(CtType element) {
element.delete();
}
});
assertEquals(0, model.getAllTypes().size());
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class APITest method testPrintNotAllSourcesWithFilter.
@Test
public void testPrintNotAllSourcesWithFilter() throws Exception {
// contract: setOutputFilter can take an arbitrary filter
final File target = new File("./target/print-not-all/default");
final SpoonAPI launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("./src/main/java/spoon/template/");
launcher.setSourceOutputDirectory(target);
launcher.setOutputFilter(new AbstractFilter<CtType<?>>(CtType.class) {
@Override
public boolean matches(CtType<?> element) {
return "spoon.template.Parameter".equals(element.getQualifiedName()) || "spoon.template.AbstractTemplate".equals(element.getQualifiedName());
}
});
launcher.run();
List<File> list = new ArrayList<>(FileUtils.listFiles(target, new String[] { "java" }, true));
final List<String> filesName = list.stream().map(File::getName).sorted().collect(Collectors.<String>toList());
assertEquals(2, filesName.size());
assertEquals("AbstractTemplate.java", filesName.get(0));
assertEquals("Parameter.java", filesName.get(1));
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class APITest method testOutputOfSpoon.
@Test
public void testOutputOfSpoon() throws Exception {
final File sourceOutput = new File("./target/spoon/test/output/");
final SpoonAPI launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/api/testclasses");
launcher.setSourceOutputDirectory(sourceOutput);
launcher.run();
assertTrue(sourceOutput.exists());
}
use of spoon.SpoonAPI in project spoon by INRIA.
the class APITest method testPrintNotAllSourcesInCommandLine.
@Test
public void testPrintNotAllSourcesInCommandLine() throws Exception {
final File target = new File("./target/print-not-all/command");
final SpoonAPI launcher = new Launcher();
launcher.run(new String[] { //
"-i", //
"./src/main/java", //
"-o", //
"./target/print-not-all/command", //
"-f", //
"spoon.Launcher:spoon.template.AbstractTemplate", "--noclasspath" });
List<File> list = new ArrayList<>(FileUtils.listFiles(target, new String[] { "java" }, true));
final List<String> filesName = list.stream().map(File::getName).sorted().collect(Collectors.<String>toList());
assertEquals(2, filesName.size());
assertEquals("AbstractTemplate.java", filesName.get(0));
assertEquals("Launcher.java", filesName.get(1));
}
Aggregations