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());
}
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());
}
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;
}
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);
}
}
}
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);
}
Aggregations