Search in sources :

Example 1 with JDTBasedSpoonCompiler

use of spoon.support.compiler.jdt.JDTBasedSpoonCompiler 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 2 with JDTBasedSpoonCompiler

use of spoon.support.compiler.jdt.JDTBasedSpoonCompiler in project spoon by INRIA.

the class CtClassImpl method newInstance.

@Override
public T newInstance() {
    try {
        JDTBasedSpoonCompiler spooner = new JDTBasedSpoonCompiler(getFactory());
        // compiling the types of the factory
        spooner.compile(InputType.CTTYPES);
        Class<?> klass = new NewInstanceClassloader(spooner.getBinaryOutputDirectory()).loadClass(getQualifiedName());
        return (T) klass.newInstance();
    } catch (Exception e) {
        throw new SpoonException(e);
    }
}
Also used : SpoonException(spoon.SpoonException) JDTBasedSpoonCompiler(spoon.support.compiler.jdt.JDTBasedSpoonCompiler) MalformedURLException(java.net.MalformedURLException) SpoonException(spoon.SpoonException)

Example 3 with JDTBasedSpoonCompiler

use of spoon.support.compiler.jdt.JDTBasedSpoonCompiler in project spoon by INRIA.

the class ComputeClasspathTest method setUp.

@Before
public void setUp() {
    Launcher launcher = new Launcher() {

        public SpoonModelBuilder createCompiler(Factory factory) {
            return new JDTBasedSpoonCompiler(factory);
        }
    };
    launcher.getEnvironment().setLevel("OFF");
    this.compiler = (JDTBasedSpoonCompiler) launcher.createCompiler();
    this.compilerClass = compiler.getClass();
    this.systemClasspath = TEST_CLASSPATH.split(File.pathSeparator);
}
Also used : Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) JDTBasedSpoonCompiler(spoon.support.compiler.jdt.JDTBasedSpoonCompiler) Before(org.junit.Before)

Example 4 with JDTBasedSpoonCompiler

use of spoon.support.compiler.jdt.JDTBasedSpoonCompiler in project spoon by INRIA.

the class CompilationTest method testFilterResourcesDir.

@Test
public void testFilterResourcesDir() throws Exception {
    // shows how to filter input java dir
    // only in package called "reference"
    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.getFileName()).replace('\\', '/').contains("/reference/")) {
                                    units.add(u);
                                }
                            }
                            return units.toArray(new CompilationUnit[0]);
                        }
                    };
                }
            };
        }
    };
    launcher.addInputResource("./src/test/java/spoon/test");
    launcher.buildModel();
    // we indeed only have types declared in a file in package reference
    int n = 0;
    for (CtType<?> t : launcher.getModel().getAllTypes()) {
        n++;
        assertTrue(t.getQualifiedName().contains("reference"));
    }
    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 5 with JDTBasedSpoonCompiler

use of spoon.support.compiler.jdt.JDTBasedSpoonCompiler in project spoon by INRIA.

the class ProcessorUtils method process.

public static void process(Factory factory, Collection<Processor<?>> processors) {
    final JDTBasedSpoonCompiler compiler = (JDTBasedSpoonCompiler) new Launcher().createCompiler(factory);
    compiler.process(processors);
}
Also used : Launcher(spoon.Launcher) JDTBasedSpoonCompiler(spoon.support.compiler.jdt.JDTBasedSpoonCompiler)

Aggregations

JDTBasedSpoonCompiler (spoon.support.compiler.jdt.JDTBasedSpoonCompiler)7 Launcher (spoon.Launcher)5 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 CompilationUnit (org.eclipse.jdt.internal.compiler.batch.CompilationUnit)2 SpoonException (spoon.SpoonException)2 JDTBatchCompiler (spoon.support.compiler.jdt.JDTBatchCompiler)2 MalformedURLException (java.net.MalformedURLException)1 Before (org.junit.Before)1 Environment (spoon.compiler.Environment)1 Factory (spoon.reflect.factory.Factory)1 StandardEnvironment (spoon.support.StandardEnvironment)1