Search in sources :

Example 11 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class CompilationTest method testPrecompile.

@Test
public void testPrecompile() {
    // without precompile
    Launcher l = new Launcher();
    l.setArgs(new String[] { "--noclasspath", "-i", "src/test/resources/compilation/" });
    l.buildModel();
    CtClass klass = l.getFactory().Class().get("compilation.Bar");
    // without precompile, actualClass does not exist (an exception is thrown)
    try {
        klass.getSuperInterfaces().toArray(new CtTypeReference[0])[0].getActualClass();
        fail();
    } catch (SpoonClassNotFoundException ignore) {
    }
    // with precompile
    Launcher l2 = new Launcher();
    l2.setArgs(new String[] { "--precompile", "--noclasspath", "-i", "src/test/resources/compilation/" });
    l2.buildModel();
    CtClass klass2 = l2.getFactory().Class().get("compilation.Bar");
    // with precompile, actualClass is not null
    Class actualClass = klass2.getSuperInterfaces().toArray(new CtTypeReference[0])[0].getActualClass();
    assertNotNull(actualClass);
    assertEquals("IBar", actualClass.getSimpleName());
    // precompile can be used to compile processors on the fly
    Launcher l3 = new Launcher();
    l3.setArgs(new String[] { "--precompile", "--noclasspath", "-i", "src/test/resources/compilation/", "-p", "compilation.SimpleProcessor" });
    l3.run();
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) CtClass(spoon.reflect.declaration.CtClass) Test(org.junit.Test)

Example 12 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class CompilationTest method testFilterResourcesFile.

@Test
public void testFilterResourcesFile() throws Exception {
    // shows how to filter input java files, for https://github.com/INRIA/spoon/issues/877
    Launcher launcher = new Launcher() {

        @Override
        public SpoonModelBuilder createCompiler() {
            return new JDTBasedSpoonCompiler(getFactory()) {

                @Override
                protected JDTBatchCompiler createBatchCompiler() {
                    return new JDTBatchCompiler(this) {

                        @Override
                        public CompilationUnit[] getCompilationUnits() {
                            List<CompilationUnit> units = new ArrayList<>();
                            for (CompilationUnit u : super.getCompilationUnits()) {
                                if (new String(u.getMainTypeName()).contains("Foo")) {
                                    units.add(u);
                                }
                            }
                            return units.toArray(new CompilationUnit[0]);
                        }
                    };
                }
            };
        }
    };
    launcher.addInputResource("./src/test/java/spoon/test/imports");
    launcher.buildModel();
    int n = 0;
    // we indeed only have types declared in a file called *Foo*
    for (CtType<?> t : launcher.getFactory().getModel().getAllTypes()) {
        n++;
        assertTrue(t.getPosition().getFile().getAbsolutePath().contains("Foo"));
    }
    assertTrue(n >= 2);
}
Also used : CompilationUnit(org.eclipse.jdt.internal.compiler.batch.CompilationUnit) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) JDTBatchCompiler(spoon.support.compiler.jdt.JDTBatchCompiler) JDTBasedSpoonCompiler(spoon.support.compiler.jdt.JDTBasedSpoonCompiler) Test(org.junit.Test)

Example 13 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class CompilationTest method testNewInstance.

@Test
public void testNewInstance() throws Exception {
    // contract: a ctclass can be instantiated, and each modification results in a new valid object
    Factory factory = new Launcher().getFactory();
    CtClass<Ifoo> c = factory.Code().createCodeSnippetStatement("class X implements spoon.test.compilation.Ifoo { public int foo() {int i=0; return i;} }").compile();
    // required otherwise java.lang.IllegalAccessException at runtime when instantiating
    c.addModifier(ModifierKind.PUBLIC);
    CtBlock body = c.getElements(new TypeFilter<>(CtBlock.class)).get(1);
    Ifoo o = c.newInstance();
    assertEquals(0, o.foo());
    for (int i = 1; i <= 10; i++) {
        body.getStatement(0).replace(factory.Code().createCodeSnippetStatement("int i = " + i + ";"));
        o = c.newInstance();
        // each time this is a new class
        // each time the behavior has changed!
        assertEquals(i, o.foo());
    }
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CoreFactory(spoon.reflect.factory.CoreFactory) Factory(spoon.reflect.factory.Factory) CodeFactory(spoon.reflect.factory.CodeFactory) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 14 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class CompilationTest method testSingleClassLoader.

@Test
public void testSingleClassLoader() throws Exception {
    /*
		 *  contract: the environment exposes a classloader configured by the spoonclass path, 
		 *  there is one class loader, so the loaded classes are compatible
		 */
    Launcher launcher = new Launcher();
    launcher.addInputResource(new FileSystemFolder("./src/test/resources/classloader-test"));
    File outputBinDirectory = new File("./target/classloader-test");
    if (!outputBinDirectory.exists()) {
        outputBinDirectory.mkdirs();
    }
    launcher.setBinaryOutputDirectory(outputBinDirectory);
    launcher.getModelBuilder().build();
    CtTypeReference<?> mIFoo = launcher.getFactory().Type().createReference("spoontest.IFoo");
    CtTypeReference<?> mFoo = launcher.getFactory().Type().createReference("spoontest.Foo");
    assertTrue("Foo subtype of IFoo", mFoo.isSubtypeOf(mIFoo));
    launcher.getModelBuilder().compile(SpoonModelBuilder.InputType.FILES);
    // Create new launcher which uses classes compiled by previous launcher.
    // It simulates the classes without sources, which has to be accessed using reflection
    launcher = new Launcher();
    // not in the classpath
    try {
        Class.forName("spoontest.IFoo");
        fail();
    } catch (ClassNotFoundException expected) {
    }
    // not in the spoon classpath before setting it
    try {
        launcher.getEnvironment().getInputClassLoader().loadClass("spoontest.IFoo");
        fail();
    } catch (ClassNotFoundException expected) {
    }
    launcher.getEnvironment().setSourceClasspath(new String[] { outputBinDirectory.getAbsolutePath() });
    mIFoo = launcher.getFactory().Type().createReference("spoontest.IFoo");
    mFoo = launcher.getFactory().Type().createReference("spoontest.Foo");
    // if it fails then it is because each class is loaded by different class loader
    assertTrue("Foo subtype of IFoo", mFoo.isSubtypeOf(mIFoo));
    // not in the spoon classpath before setting it
    Class<?> ifoo = launcher.getEnvironment().getInputClassLoader().loadClass("spoontest.IFoo");
    Class<?> foo = launcher.getEnvironment().getInputClassLoader().loadClass("spoontest.Foo");
    assertTrue(ifoo.isAssignableFrom(foo));
    assertTrue(ifoo.getClassLoader() == foo.getClassLoader());
}
Also used : Launcher(spoon.Launcher) FileSystemFolder(spoon.support.compiler.FileSystemFolder) File(java.io.File) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) Test(org.junit.Test)

Example 15 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class GetBinaryFilesTest method testAnonymousClasses.

@Test
public void testAnonymousClasses() throws IOException {
    final String input = "./src/test/java/spoon/test/secondaryclasses/testclasses/AnonymousClass.java";
    final Launcher launcher = new Launcher();
    launcher.addInputResource(input);
    launcher.setBinaryOutputDirectory(tmpFolder.getRoot());
    launcher.buildModel();
    launcher.getModelBuilder().compile(SpoonModelBuilder.InputType.FILES);
    final Map<String, CompilationUnit> cus = launcher.getFactory().CompilationUnit().getMap();
    assertEquals(1, cus.size());
    final List<File> binaries = cus.get(new File(input).getCanonicalFile().getAbsolutePath()).getBinaryFiles();
    assertEquals(4, binaries.size());
    assertEquals("AnonymousClass.class", binaries.get(0).getName());
    assertEquals("AnonymousClass$I.class", binaries.get(1).getName());
    assertEquals("AnonymousClass$1.class", binaries.get(2).getName());
    assertEquals("AnonymousClass$2.class", binaries.get(3).getName());
    assertTrue(binaries.get(0).isFile());
    assertTrue(binaries.get(1).isFile());
    assertTrue(binaries.get(2).isFile());
    assertTrue(binaries.get(3).isFile());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Aggregations

Launcher (spoon.Launcher)524 Test (org.junit.Test)486 Factory (spoon.reflect.factory.Factory)163 CtClass (spoon.reflect.declaration.CtClass)111 CtMethod (spoon.reflect.declaration.CtMethod)79 File (java.io.File)75 CtType (spoon.reflect.declaration.CtType)66 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)60 CtTypeReference (spoon.reflect.reference.CtTypeReference)48 SpoonModelBuilder (spoon.SpoonModelBuilder)44 ArrayList (java.util.ArrayList)43 CtAnnotation (spoon.reflect.declaration.CtAnnotation)38 CtInvocation (spoon.reflect.code.CtInvocation)32 CtElement (spoon.reflect.declaration.CtElement)27 CtPackage (spoon.reflect.declaration.CtPackage)27 FileSystemFile (spoon.support.compiler.FileSystemFile)26 CtStatement (spoon.reflect.code.CtStatement)25 CtField (spoon.reflect.declaration.CtField)24 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)24 SpoonAPI (spoon.SpoonAPI)22