Search in sources :

Example 6 with ImportCustomizer

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();
}
Also used : Binding(groovy.lang.Binding) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) GroovyClassLoaderFactory.addImportPathToImportCustomizer(com.twosigma.beakerx.groovy.evaluator.GroovyClassLoaderFactory.addImportPathToImportCustomizer)

Example 7 with ImportCustomizer

use of org.codehaus.groovy.control.customizers.ImportCustomizer in project groovy-core by groovy.

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.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();
}
Also used : Binding(groovy.lang.Binding) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) List(java.util.List) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) ParseException(org.apache.commons.cli.ParseException) GroovyShell(groovy.lang.GroovyShell)

Example 8 with ImportCustomizer

use of org.codehaus.groovy.control.customizers.ImportCustomizer in project groovy-core by groovy.

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'));
    }
    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[] opts = cli.getOptionValues("J");
        compilerOptions.put("namedValues", opts);
        opts = cli.getOptionValues("F");
        compilerOptions.put("flags", opts);
        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;
}
Also used : Binding(groovy.lang.Binding) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) GroovyShell(groovy.lang.GroovyShell)

Example 9 with ImportCustomizer

use of org.codehaus.groovy.control.customizers.ImportCustomizer in project groovy-core by groovy.

the class ImportCustomizerFactory method onNodeChildren.

@Override
public boolean onNodeChildren(final FactoryBuilderSupport builder, final Object node, final Closure childContent) {
    if (node instanceof ImportCustomizer) {
        Closure clone = (Closure) childContent.clone();
        clone.setDelegate(new ImportHelper((ImportCustomizer) node));
        clone.call();
    }
    return false;
}
Also used : Closure(groovy.lang.Closure) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer)

Example 10 with ImportCustomizer

use of org.codehaus.groovy.control.customizers.ImportCustomizer in project spring-framework by spring-projects.

the class GroovyScriptEvaluatorTests method testGroovyScriptWithImportCustomizer.

@Test
public void testGroovyScriptWithImportCustomizer() {
    GroovyScriptEvaluator evaluator = new GroovyScriptEvaluator();
    ImportCustomizer importCustomizer = new ImportCustomizer();
    importCustomizer.addStarImports("org.springframework.util");
    evaluator.setCompilationCustomizers(importCustomizer);
    Object result = evaluator.evaluate(new StaticScriptSource("return ResourceUtils.CLASSPATH_URL_PREFIX"));
    assertThat(result).isEqualTo("classpath:");
}
Also used : StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) Test(org.junit.jupiter.api.Test)

Aggregations

ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)22 GroovyShell (groovy.lang.GroovyShell)13 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)13 Binding (groovy.lang.Binding)11 Closure (groovy.lang.Closure)4 File (java.io.File)3 GroovyClassLoader (groovy.lang.GroovyClassLoader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 List (java.util.List)2 ParseException (org.apache.commons.cli.ParseException)2 BuildException (org.apache.tools.ant.BuildException)2 GroovyBugError (org.codehaus.groovy.GroovyBugError)2 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)2 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)2 GroovityASTTransformation (com.disney.groovity.compile.GroovityASTTransformation)1