use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class ModelUtils method build.
/**
* Utility method for testing: creates the model of the given `classesToBuild` from src/test/java and returns the factory
*/
public static Factory build(Class<?>... classesToBuild) throws Exception {
SpoonModelBuilder comp = new Launcher().createCompiler();
for (Class<?> classToBuild : classesToBuild) {
comp.addInputSources(SpoonResourceHelper.resources("./src/test/java/" + classToBuild.getName().replace('.', '/') + ".java"));
}
comp.build();
return comp.getFactory();
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class ExtendedStringLiteralTest method testExtendedStringLiteral.
@Test
public void testExtendedStringLiteral() throws Exception {
Launcher launcher = new Launcher() {
@Override
public SpoonModelBuilder createCompiler() {
return new JDTBasedSpoonCompiler(getFactory()) {
@Override
protected JDTBatchCompiler createBatchCompiler() {
return new JDTBatchCompiler(this) {
@Override
public CompilationUnitDeclaration[] getUnits() {
startTime = System.currentTimeMillis();
INameEnvironment environment = this.jdtCompiler.environment;
if (environment == null) {
environment = getLibraryAccess();
}
CompilerOptions compilerOptions = new CompilerOptions(this.options);
// set to true to force executing visit(ExtendedStringLiteral,BlockScope)
// which is not the case when set to false which is the default in JDTBatchCompiler
// the test not succeeds since this was not the case before the pull request
// and since visit(ExtendedStringLiteral,BlockScope) was throwing a RuntimeException
compilerOptions.parseLiteralExpressionsAsConstants = true;
TreeBuilderCompiler treeBuilderCompiler = new TreeBuilderCompiler(environment, getHandlingPolicy(), compilerOptions, this.jdtCompiler.requestor, getProblemFactory(), this.out, null);
CompilationUnitDeclaration[] units = treeBuilderCompiler.buildUnits(getCompilationUnits());
return units;
}
};
}
};
}
};
SpoonModelBuilder comp = launcher.createCompiler();
comp.addInputSources(SpoonResourceHelper.resources("./src/test/java/" + ExtendedStringLiteralTestClass.class.getCanonicalName().replace('.', '/') + ".java"));
comp.build();
CtClass<?> cl = comp.getFactory().Package().get("spoon.support.compiler.jdt").getType("ExtendedStringLiteralTestClass");
CtField<?> f = cl.getField("extendedStringLiteral");
CtExpression<?> de = f.getDefaultExpression();
assertEquals("\"hello world!\"", de.toString());
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class JarTest method testResource.
@Test
public void testResource() throws Exception {
Launcher launcher = new Launcher();
SpoonModelBuilder compiler = launcher.createCompiler(launcher.getFactory(), Arrays.asList(new VirtualFile("class Foo {}", "Foo.java")));
Assert.assertTrue(compiler.build());
Assert.assertNotNull(launcher.getFactory().Type().get("Foo"));
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class JarTest method testJar.
@Test
public void testJar() throws Exception {
Launcher spoon = new Launcher();
Factory factory = spoon.createFactory();
factory.getEnvironment().setNoClasspath(true);
SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/sourceJar/test.jar"));
Assert.assertTrue(compiler.build());
assertEquals(1, factory.getModel().getAllTypes().size());
assertEquals("spoon.test.strings.Main", factory.getModel().getAllTypes().iterator().next().getQualifiedName());
}
use of spoon.SpoonModelBuilder in project spoon by INRIA.
the class MethodReferenceTest method setUp.
@Before
public void setUp() throws Exception {
final Launcher launcher = new Launcher();
final Factory factory = launcher.getFactory();
launcher.getEnvironment().setComplianceLevel(8);
final SpoonModelBuilder compiler = launcher.createCompiler(factory);
launcher.setSourceOutputDirectory("./target/spooned/");
compiler.addInputSource(new File("./src/test/java/spoon/test/methodreference/testclasses/"));
compiler.build();
compiler.generateProcessedSourceFiles(OutputType.CLASSES);
foo = (CtClass<?>) factory.Type().get(Foo.class);
}
Aggregations