Search in sources :

Example 6 with Environment

use of spoon.compiler.Environment in project spoon by INRIA.

the class LauncherTest method testInitEnvironmentDefault.

@Test
public void testInitEnvironmentDefault() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[0]);
    launcher.processArguments();
    final Environment environment = launcher.getEnvironment();
    // specify the default values
    Assert.assertFalse(environment.isAutoImports());
    Assert.assertFalse(environment.isUsingTabulations());
    Assert.assertFalse(environment.isPreserveLineNumbers());
    assertEquals(4, environment.getTabulationSize());
    Assert.assertTrue(environment.isCopyResources());
    JavaOutputProcessor processor = (JavaOutputProcessor) environment.getDefaultFileGenerator();
    Assert.assertTrue(processor.getPrinter() instanceof DefaultJavaPrettyPrinter);
    // now assertions on the model builder
    final SpoonModelBuilder builder = launcher.getModelBuilder();
    assertEquals(new File("spooned").getCanonicalFile(), builder.getSourceOutputDirectory());
    assertEquals(0, builder.getInputSources().size());
    assertEquals("UTF-8", environment.getEncoding().displayName());
}
Also used : Environment(spoon.compiler.Environment) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) File(java.io.File) JavaOutputProcessor(spoon.support.JavaOutputProcessor) Test(org.junit.Test)

Example 7 with Environment

use of spoon.compiler.Environment in project spoon by INRIA.

the class LauncherTest method testInitEnvironment.

@Test
public void testInitEnvironment() throws Exception {
    // Main class of Spoon who contain initEnvironment method.
    final Launcher launcher = new Launcher();
    launcher.setArgs("--tabs --tabsize 42 --compliance 5 --with-imports -r --lines -o spooned2 -i src/main/java --encoding UTF-16".split(" "));
    launcher.processArguments();
    final Environment environment = launcher.getEnvironment();
    // Verify if the environment is correct.
    Assert.assertTrue(environment.isAutoImports());
    Assert.assertTrue(environment.isUsingTabulations());
    Assert.assertTrue(environment.isPreserveLineNumbers());
    assertEquals(42, environment.getTabulationSize());
    assertEquals(5, environment.getComplianceLevel());
    Assert.assertFalse(environment.isCopyResources());
    final SpoonModelBuilder builder = launcher.getModelBuilder();
    assertEquals(new File("spooned2").getCanonicalFile(), builder.getSourceOutputDirectory());
    // the input directories
    List<File> inputSources = new ArrayList<>(builder.getInputSources());
    assertTrue(inputSources.get(0).getPath().replace('\\', '/').contains("src/main/java"));
    assertEquals("UTF-16", environment.getEncoding().displayName());
}
Also used : ArrayList(java.util.ArrayList) Environment(spoon.compiler.Environment) File(java.io.File) Test(org.junit.Test)

Example 8 with Environment

use of spoon.compiler.Environment in project spoon by INRIA.

the class Launcher method createCompiler.

/**
 * Creates a new Spoon Java compiler in order to process and compile Java
 * source code.
 *
 * @param factory
 * 		the factory this compiler works on
 */
public SpoonModelBuilder createCompiler(Factory factory) {
    SpoonModelBuilder comp = new JDTBasedSpoonCompiler(factory);
    Environment env = getEnvironment();
    // building
    comp.setBinaryOutputDirectory(jsapActualArgs.getFile("destination"));
    // we don't have to set the source classpath
    if (jsapActualArgs.contains("source-classpath")) {
        comp.setSourceClasspath(jsapActualArgs.getString("source-classpath").split(System.getProperty("path.separator")));
    }
    env.debugMessage("destination: " + comp.getBinaryOutputDirectory());
    env.debugMessage("source classpath: " + Arrays.toString(comp.getSourceClasspath()));
    env.debugMessage("template classpath: " + Arrays.toString(comp.getTemplateClasspath()));
    return comp;
}
Also used : StandardEnvironment(spoon.support.StandardEnvironment) Environment(spoon.compiler.Environment) JDTBasedSpoonCompiler(spoon.support.compiler.jdt.JDTBasedSpoonCompiler)

Example 9 with Environment

use of spoon.compiler.Environment in project spoon by INRIA.

the class Launcher method processArguments.

protected void processArguments() {
    jsapActualArgs = getArguments();
    Environment environment = factory.getEnvironment();
    // environment initialization
    environment.setComplianceLevel(jsapActualArgs.getInt("compliance"));
    environment.setLevel(jsapActualArgs.getString("level"));
    environment.setAutoImports(jsapActualArgs.getBoolean("imports"));
    environment.setNoClasspath(jsapActualArgs.getBoolean("noclasspath"));
    environment.setPreserveLineNumbers(jsapActualArgs.getBoolean("lines"));
    environment.setTabulationSize(jsapActualArgs.getInt("tabsize"));
    environment.useTabulations(jsapActualArgs.getBoolean("tabs"));
    environment.setCopyResources(!jsapActualArgs.getBoolean("no-copy-resources"));
    environment.setCommentEnabled(jsapActualArgs.getBoolean("enable-comments"));
    environment.setShouldCompile(jsapActualArgs.getBoolean("compile"));
    environment.setSelfChecks(jsapActualArgs.getBoolean("disable-model-self-checks"));
    String outputString = jsapActualArgs.getString("output-type");
    OutputType outputType = OutputType.fromString(outputString);
    if (outputType == null) {
        throw new SpoonException("Unknown output type: " + outputString);
    } else {
        environment.setOutputType(outputType);
    }
    try {
        Charset charset = Charset.forName(jsapActualArgs.getString("encoding"));
        environment.setEncoding(charset);
    } catch (Exception e) {
        throw new SpoonException(e);
    }
    if (getArguments().getString("generate-files") != null) {
        setOutputFilter(getArguments().getString("generate-files").split(":"));
    }
    // now we are ready to create a spoon compiler
    modelBuilder = createCompiler();
    if (getArguments().getString("input") != null) {
        for (String s : getArguments().getString("input").split("[" + File.pathSeparatorChar + "]")) {
            try {
                modelBuilder.addInputSource(SpoonResourceHelper.createResource(new File(s)));
            } catch (FileNotFoundException e) {
                throw new SpoonException(e);
            }
        }
    }
    if (jsapActualArgs.getBoolean("precompile")) {
        modelBuilder.compile(InputType.FILES);
        getEnvironment().setSourceClasspath(new String[] { getEnvironment().getBinaryOutputDirectory() });
    }
    if (getArguments().getFile("output") != null) {
        setSourceOutputDirectory(getArguments().getFile("output"));
    }
    // Adding template from command-line
    if (getArguments().getString("template") != null) {
        for (String s : getArguments().getString("template").split("[" + File.pathSeparatorChar + "]")) {
            try {
                modelBuilder.addTemplateSource(SpoonResourceHelper.createResource(new File(s)));
            } catch (FileNotFoundException e) {
                environment.report(null, Level.ERROR, "Unable to add template file: " + e.getMessage());
                LOGGER.error(e.getMessage(), e);
            }
        }
    }
    if (getArguments().getString("processors") != null) {
        for (String processorName : getArguments().getString("processors").split(File.pathSeparator)) {
            addProcessor(processorName);
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) StandardEnvironment(spoon.support.StandardEnvironment) Environment(spoon.compiler.Environment) Charset(java.nio.charset.Charset) VirtualFile(spoon.support.compiler.VirtualFile) File(java.io.File) FileSystemFile(spoon.support.compiler.FileSystemFile) JSAPException(com.martiansoftware.jsap.JSAPException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 10 with Environment

use of spoon.compiler.Environment in project spoon by INRIA.

the class PackageTest method testAnnotationInPackageInfoWhenTemplatesCompiled.

@Test
public void testAnnotationInPackageInfoWhenTemplatesCompiled() throws Exception {
    final Launcher launcher = new Launcher();
    Environment environment = launcher.getEnvironment();
    environment.setAutoImports(true);
    environment.setCommentEnabled(true);
    launcher.addInputResource("./src/test/java/spoon/test/pkg/package-info.java");
    launcher.setSourceOutputDirectory("./target/spooned/packageAndTemplate");
    // SpoonResourceHelper.resources("./src/test/java/spoon/test/pkg/test_templates").forEach(r->launcher.addTemplateResource(r));
    launcher.addTemplateResource(SpoonResourceHelper.createResource(new File("./src/test/java/spoon/test/pkg/test_templates/FakeTemplate.java")));
    launcher.buildModel();
    launcher.prettyprint();
    canBeBuilt("./target/spooned/packageAndTemplate/spoon/test/pkg/package-info.java", 8);
}
Also used : Launcher(spoon.Launcher) Environment(spoon.compiler.Environment) File(java.io.File) Test(org.junit.Test)

Aggregations

Environment (spoon.compiler.Environment)10 File (java.io.File)4 Test (org.junit.Test)4 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)4 StandardEnvironment (spoon.support.StandardEnvironment)3 Launcher (spoon.Launcher)2 CtType (spoon.reflect.declaration.CtType)2 JavaOutputProcessor (spoon.support.JavaOutputProcessor)2 JSAPException (com.martiansoftware.jsap.JSAPException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)1 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)1 INameEnvironment (org.eclipse.jdt.internal.compiler.env.INameEnvironment)1 FieldBinding (org.eclipse.jdt.internal.compiler.lookup.FieldBinding)1 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)1