use of spoon.support.compiler.VirtualFile 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.support.compiler.VirtualFile in project spoon by INRIA.
the class SnippetTest method testIssue981.
@Test
public void testIssue981() throws Exception {
// contract: one can get the package of a string
Launcher spoon = new Launcher();
spoon.getEnvironment().setNoClasspath(true);
SpoonResource input = new VirtualFile("package foo.bar; class X {}");
spoon.addInputResource(input);
spoon.buildModel();
assertEquals("foo.bar", spoon.getFactory().Type().get("foo.bar.X").getPackage().getQualifiedName());
}
use of spoon.support.compiler.VirtualFile in project spoon by INRIA.
the class Launcher method parseClass.
/**
* returns the AST of an inline class
*/
public static CtClass<?> parseClass(String code) {
Launcher launcher = new Launcher();
launcher.addInputResource(new VirtualFile(code));
launcher.getEnvironment().setNoClasspath(true);
launcher.getEnvironment().setAutoImports(true);
Collection<CtType<?>> allTypes = launcher.buildModel().getAllTypes();
if (allTypes.size() != 1) {
throw new SpoonException("parseClass only considers one class. Please consider using a Launcher object for more advanced usage.");
}
try {
return (CtClass<?>) allTypes.stream().findFirst().get();
} catch (ClassCastException e) {
throw new SpoonException("parseClass only considers classes (and not interfaces and enums). Please consider using a Launcher object for more advanced usage.");
}
}
Aggregations