Search in sources :

Example 11 with SpoonModelBuilder

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();
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher)

Example 12 with SpoonModelBuilder

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) Launcher(spoon.Launcher) INameEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment) Test(org.junit.Test)

Example 13 with SpoonModelBuilder

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

Example 14 with SpoonModelBuilder

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 15 with SpoonModelBuilder

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);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) File(java.io.File) Before(org.junit.Before)

Aggregations

SpoonModelBuilder (spoon.SpoonModelBuilder)45 Launcher (spoon.Launcher)43 Test (org.junit.Test)35 Factory (spoon.reflect.factory.Factory)29 File (java.io.File)17 CtClass (spoon.reflect.declaration.CtClass)16 CtMethod (spoon.reflect.declaration.CtMethod)6 SpoonResource (spoon.compiler.SpoonResource)5 CtInvocation (spoon.reflect.code.CtInvocation)5 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 DefaultCoreFactory (spoon.support.DefaultCoreFactory)4 JDTSnippetCompiler (spoon.support.compiler.jdt.JDTSnippetCompiler)4 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)4 Before (org.junit.Before)3 FileNotFoundException (java.io.FileNotFoundException)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 CtParameterRemoveRefactoring (spoon.refactoring.CtParameterRemoveRefactoring)2 CtNewClass (spoon.reflect.code.CtNewClass)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 FactoryImpl (spoon.reflect.factory.FactoryImpl)2