Search in sources :

Example 1 with VirtualFile

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"));
}
Also used : VirtualFile(spoon.support.compiler.VirtualFile) SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 2 with VirtualFile

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());
}
Also used : VirtualFile(spoon.support.compiler.VirtualFile) Launcher(spoon.Launcher) SpoonResource(spoon.compiler.SpoonResource) Test(org.junit.Test)

Example 3 with VirtualFile

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.");
    }
}
Also used : VirtualFile(spoon.support.compiler.VirtualFile) CtClass(spoon.reflect.declaration.CtClass) CtType(spoon.reflect.declaration.CtType)

Aggregations

VirtualFile (spoon.support.compiler.VirtualFile)3 Test (org.junit.Test)2 Launcher (spoon.Launcher)2 SpoonModelBuilder (spoon.SpoonModelBuilder)1 SpoonResource (spoon.compiler.SpoonResource)1 CtClass (spoon.reflect.declaration.CtClass)1 CtType (spoon.reflect.declaration.CtType)1