use of org.codehaus.groovy.control.CompilerConfiguration in project groovy by apache.
the class Groovy method configureCompiler.
private void configureCompiler() {
if (scriptBaseClass != null) {
configuration.setScriptBaseClass(scriptBaseClass);
}
if (indy) {
configuration.getOptimizationOptions().put("indy", Boolean.TRUE);
configuration.getOptimizationOptions().put("int", Boolean.FALSE);
}
if (configscript != null) {
Binding binding = new Binding();
binding.setVariable("configuration", configuration);
CompilerConfiguration configuratorConfig = new CompilerConfiguration();
ImportCustomizer customizer = new ImportCustomizer();
customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
configuratorConfig.addCompilationCustomizers(customizer);
GroovyShell shell = new GroovyShell(binding, configuratorConfig);
File confSrc = new File(configscript);
try {
shell.evaluate(confSrc);
} catch (IOException e) {
throw new BuildException("Unable to configure compiler using configuration file: " + confSrc, e);
}
}
}
use of org.codehaus.groovy.control.CompilerConfiguration in project uPortal by Jasig.
the class PortalShell method main.
public static void main(String[] args) throws Exception {
final Options options = getOptions();
final CommandLineParser parser = new PosixParser();
final CommandLine commandLine;
try {
commandLine = parser.parse(options, args);
} catch (ParseException e) {
System.err.println(e.getMessage());
printHelp(options);
return;
}
final ApplicationContext applicationContext = PortalApplicationContextLocator.getApplicationContext();
try {
final Binding binding = new SpringBinding(applicationContext);
binding.setVariable("logger", LOGGER);
final CompilerConfiguration conf = new CompilerConfiguration(System.getProperties());
final GroovyShell shell = new GroovyShell(binding, conf);
if (commandLine.hasOption("script")) {
final String scriptName = commandLine.getOptionValue("script");
final File scriptFile = getAbsoluteFile(scriptName);
shell.run(scriptFile, args);
}
} finally {
if (applicationContext instanceof DisposableBean) {
((DisposableBean) applicationContext).destroy();
}
}
}
use of org.codehaus.groovy.control.CompilerConfiguration in project groovy by apache.
the class ProxyGeneratorAdapter method adjustSuperClass.
private Class adjustSuperClass(Class superClass, final Class[] interfaces) {
boolean isSuperClassAnInterface = superClass.isInterface();
if (!isSuperClassAnInterface) {
return superClass;
}
Class result = Object.class;
Set<ClassNode> traits = new LinkedHashSet<ClassNode>();
// check if it's a trait
collectTraits(superClass, traits);
if (interfaces != null) {
for (Class anInterface : interfaces) {
collectTraits(anInterface, traits);
}
}
if (!traits.isEmpty()) {
String name = superClass.getName() + "$TraitAdapter";
ClassNode cn = new ClassNode(name, ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.OBJECT_TYPE, traits.toArray(new ClassNode[traits.size()]), null);
CompilationUnit cu = new CompilationUnit(loader);
CompilerConfiguration config = new CompilerConfiguration();
SourceUnit su = new SourceUnit(name + "wrapper", "", config, loader, new ErrorCollector(config));
cu.addSource(su);
cu.compile(Phases.CONVERSION);
su.getAST().addClass(cn);
cu.compile(Phases.CLASS_GENERATION);
@SuppressWarnings("unchecked") List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses();
for (GroovyClass groovyClass : classes) {
if (groovyClass.getName().equals(name)) {
return loader.defineClass(name, groovyClass.getBytes());
}
}
}
return result;
}
use of org.codehaus.groovy.control.CompilerConfiguration in project groovy by apache.
the class FileSystemCompiler method commandLineCompile.
/**
* Same as main(args) except that exceptions are thrown out instead of causing
* the VM to exit and the lookup for .groovy files can be controlled
*/
public static void commandLineCompile(String[] args, boolean lookupUnnamedFiles) throws Exception {
Options options = createCompilationOptions();
CommandLineParser cliParser = new DefaultParser();
CommandLine cli;
cli = cliParser.parse(options, args);
if (cli.hasOption('h')) {
displayHelp(options);
return;
}
if (cli.hasOption('v')) {
displayVersion();
return;
}
displayStackTraceOnError = cli.hasOption('e');
CompilerConfiguration configuration = generateCompilerConfigurationFromOptions(cli);
// Load the file name list
String[] filenames = generateFileNamesFromOptions(cli);
boolean fileNameErrors = filenames == null;
if (!fileNameErrors && (filenames.length == 0)) {
displayHelp(options);
return;
}
fileNameErrors = fileNameErrors && !validateFiles(filenames);
if (!fileNameErrors) {
doCompilation(configuration, null, filenames, lookupUnnamedFiles);
}
}
use of org.codehaus.groovy.control.CompilerConfiguration in project groovy by apache.
the class FileSystemCompiler method generateCompilerConfigurationFromOptions.
public static CompilerConfiguration generateCompilerConfigurationFromOptions(CommandLine cli) throws IOException {
// Setup the configuration data
CompilerConfiguration configuration = new CompilerConfiguration();
if (cli.hasOption("classpath")) {
configuration.setClasspath(cli.getOptionValue("classpath"));
}
if (cli.hasOption('d')) {
configuration.setTargetDirectory(cli.getOptionValue('d'));
}
configuration.setParameters(cli.hasOption("pa"));
if (cli.hasOption("encoding")) {
configuration.setSourceEncoding(cli.getOptionValue("encoding"));
}
if (cli.hasOption("basescript")) {
configuration.setScriptBaseClass(cli.getOptionValue("basescript"));
}
// joint compilation parameters
if (cli.hasOption('j')) {
Map<String, Object> compilerOptions = new HashMap<String, Object>();
String[] namedValues = cli.getOptionValues("J");
compilerOptions.put("namedValues", namedValues);
String[] flags = cli.getOptionValues("F");
if (flags != null && cli.hasOption("pa")) {
flags = Arrays.copyOf(flags, flags.length + 1);
flags[flags.length - 1] = "parameters";
}
compilerOptions.put("flags", flags);
configuration.setJointCompilationOptions(compilerOptions);
}
if (cli.hasOption("indy")) {
configuration.getOptimizationOptions().put("int", false);
configuration.getOptimizationOptions().put("indy", true);
}
if (cli.hasOption("configscript")) {
String path = cli.getOptionValue("configscript");
File groovyConfigurator = new File(path);
Binding binding = new Binding();
binding.setVariable("configuration", configuration);
CompilerConfiguration configuratorConfig = new CompilerConfiguration();
ImportCustomizer customizer = new ImportCustomizer();
customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
configuratorConfig.addCompilationCustomizers(customizer);
GroovyShell shell = new GroovyShell(binding, configuratorConfig);
shell.evaluate(groovyConfigurator);
}
return configuration;
}
Aggregations