use of org.codehaus.groovy.control.customizers.ImportCustomizer in project groovy by apache.
the class GroovyMain method process.
/**
* Process the users request.
*
* @param line the parsed command line.
* @throws ParseException if invalid options are chosen
*/
private static boolean process(CommandLine line) throws ParseException, IOException {
List args = line.getArgList();
if (line.hasOption('D')) {
String[] values = line.getOptionValues('D');
for (int i = 0; i < values.length; i++) {
setSystemPropertyFrom(values[i]);
}
}
GroovyMain main = new GroovyMain();
// add the ability to parse scripts with a specified encoding
main.conf.setSourceEncoding(line.getOptionValue('c', main.conf.getSourceEncoding()));
main.isScriptFile = !line.hasOption('e');
main.debug = line.hasOption('d');
main.conf.setDebug(main.debug);
main.conf.setParameters(line.hasOption("pa"));
main.processFiles = line.hasOption('p') || line.hasOption('n');
main.autoOutput = line.hasOption('p');
main.editFiles = line.hasOption('i');
if (main.editFiles) {
main.backupExtension = line.getOptionValue('i');
}
main.autoSplit = line.hasOption('a');
String sp = line.getOptionValue('a');
if (sp != null)
main.splitPattern = sp;
if (main.isScriptFile) {
if (args.isEmpty())
throw new ParseException("neither -e or filename provided");
main.script = (String) args.remove(0);
if (main.script.endsWith(".java"))
throw new ParseException("error: cannot compile file with .java extension: " + main.script);
} else {
main.script = line.getOptionValue('e');
}
main.processSockets = line.hasOption('l');
if (main.processSockets) {
// default port to listen to
String p = line.getOptionValue('l', "1960");
main.port = Integer.parseInt(p);
}
// we use "," as default, because then split will create
// an empty array if no option is set
String disabled = line.getOptionValue("disableopt", ",");
String[] deopts = disabled.split(",");
for (String deopt_i : deopts) {
main.conf.getOptimizationOptions().put(deopt_i, false);
}
if (line.hasOption("indy")) {
CompilerConfiguration.DEFAULT.getOptimizationOptions().put("indy", true);
main.conf.getOptimizationOptions().put("indy", true);
}
if (line.hasOption("basescript")) {
main.conf.setScriptBaseClass(line.getOptionValue("basescript"));
}
if (line.hasOption("configscript")) {
String path = line.getOptionValue("configscript");
File groovyConfigurator = new File(path);
Binding binding = new Binding();
binding.setVariable("configuration", main.conf);
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);
}
main.args = args;
return main.run();
}
use of org.codehaus.groovy.control.customizers.ImportCustomizer in project workflow-cps-plugin by jenkinsci.
the class CpsGroovyShellFactory method makeImportCustomizer.
private ImportCustomizer makeImportCustomizer() {
ImportCustomizer ic = new ImportCustomizer();
ic.addStarImports(NonCPS.class.getPackage().getName());
ic.addStarImports("hudson.model", "jenkins.model");
for (GroovyShellDecorator d : decorators) {
d.customizeImports(execution, ic);
}
return ic;
}
use of org.codehaus.groovy.control.customizers.ImportCustomizer in project groovity by disney.
the class Groovity method createCompilerConfiguration.
protected CompilerConfiguration createCompilerConfiguration(Map<Integer, Integer> sourceLineNumbers, Collection<String> initDependencies) {
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
if (scriptBaseClass != null) {
compilerConfiguration.setScriptBaseClass(scriptBaseClass);
compilerConfiguration.addCompilationCustomizers(new ImportCustomizer().addImports(scriptBaseClass));
}
compilerConfiguration.addCompilationCustomizers(new ImportCustomizer().addStarImports("java.util.concurrent", "groovy.transform", "java.util.function", "com.disney.groovity.model").addImports(ClosureWritable.class.getName(), AsyncChannel.class.getName(), GroovityStatistics.class.getName(), Taggable.class.getName(), Tag.class.getName(), Attr.class.getName(), Function.class.getName(), Arg.class.getName(), SkipStatistics.class.getName()));
compilerConfiguration.addCompilationCustomizers(new ASTTransformationCustomizer(new GroovityASTTransformation(this, sourceLineNumbers, initDependencies)));
compilerConfiguration.addCompilationCustomizers(new ASTTransformationCustomizer(new StatsASTTransformation()));
Map<String, Boolean> options = compilerConfiguration.getOptimizationOptions();
options.put(CompilerConfiguration.JDK8, true);
compilerConfigurationDecorators.forEach(decorator -> {
decorator.decorate(compilerConfiguration);
});
return compilerConfiguration;
}
use of org.codehaus.groovy.control.customizers.ImportCustomizer in project sponge by softelnet.
the class GroovyKnowledgeBaseInterpreter method prepareInterpreter.
@Override
protected void prepareInterpreter() {
ImportCustomizer importCustomizer = new ImportCustomizer();
PROCESSOR_CLASSES.forEach((interfaceClass, scriptClass) -> addImport(importCustomizer, scriptClass, interfaceClass.getSimpleName()));
addImport(importCustomizer, GroovyPlugin.class, Plugin.class.getSimpleName());
getStandardImportClasses().forEach(cls -> addImport(importCustomizer, cls));
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.addCompilationCustomizers(importCustomizer);
binding = createBinding();
shell = new GroovyShell(binding, configuration);
scripts = Collections.synchronizedList(new ArrayList<>());
setVariable(KnowledgeBaseConstants.VAR_ENGINE_OPERATIONS, getEngineOperations());
setClasspath(getEngineOperations() != null ? getEngineOperations().getEngine() : null);
}
use of org.codehaus.groovy.control.customizers.ImportCustomizer in project beakerx by twosigma.
the class GroovyEvaluator method reloadClassloader.
private void reloadClassloader() {
this.beakerxUrlClassLoader = newParentClassLoader(getClasspath());
this.icz = new ImportCustomizer();
this.groovyClassLoader = newEvaluator(getImports(), getClasspath(), getOutDir(), icz, beakerxUrlClassLoader);
this.scriptBinding = new Binding();
}
Aggregations