Search in sources :

Example 16 with ImportCustomizer

use of org.codehaus.groovy.control.customizers.ImportCustomizer 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;
}
Also used : Binding(groovy.lang.Binding) HashMap(java.util.HashMap) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 17 with ImportCustomizer

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

the class GroovyTypeCheckingExtensionSupport method setup.

@Override
public void setup() {
    CompilerConfiguration config = new CompilerConfiguration();
    config.setScriptBaseClass("org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport.TypeCheckingDSL");
    ImportCustomizer ic = new ImportCustomizer();
    ic.addStarImports("org.codehaus.groovy.ast.expr");
    ic.addStaticStars("org.codehaus.groovy.ast.ClassHelper");
    ic.addStaticStars("org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport");
    config.addCompilationCustomizers(ic);
    final GroovyClassLoader transformLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : typeCheckingVisitor.getSourceUnit().getClassLoader();
    // since Groovy 2.2, it is possible to use FQCN for type checking extension scripts
    TypeCheckingDSL script = null;
    try {
        Class<?> clazz = transformLoader.loadClass(scriptPath, false, true);
        if (TypeCheckingDSL.class.isAssignableFrom(clazz)) {
            script = (TypeCheckingDSL) clazz.newInstance();
        } else if (TypeCheckingExtension.class.isAssignableFrom(clazz)) {
            // since 2.4, we can also register precompiled type checking extensions which are not scripts
            try {
                Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(StaticTypeCheckingVisitor.class);
                TypeCheckingExtension extension = (TypeCheckingExtension) declaredConstructor.newInstance(typeCheckingVisitor);
                typeCheckingVisitor.addTypeCheckingExtension(extension);
                extension.setup();
                return;
            } catch (InstantiationException e) {
                addLoadingError(config);
            } catch (IllegalAccessException e) {
                addLoadingError(config);
            } catch (NoSuchMethodException e) {
                context.getErrorCollector().addFatalError(new SimpleMessage("Static type checking extension '" + scriptPath + "' could not be loaded because it doesn't have a constructor accepting StaticTypeCheckingVisitor.", config.getDebug(), typeCheckingVisitor.getSourceUnit()));
            } catch (InvocationTargetException e) {
                addLoadingError(config);
            }
        }
    } catch (ClassNotFoundException e) {
    // silent
    } catch (InstantiationException e) {
        addLoadingError(config);
    } catch (IllegalAccessException e) {
        addLoadingError(config);
    }
    if (script == null) {
        ClassLoader cl = typeCheckingVisitor.getSourceUnit().getClassLoader();
        // cast to prevent incorrect @since 1.7 warning
        InputStream is = ((ClassLoader) transformLoader).getResourceAsStream(scriptPath);
        if (is == null) {
            // fallback to the source unit classloader
            is = cl.getResourceAsStream(scriptPath);
        }
        if (is == null) {
            // fallback to the compiler classloader
            cl = GroovyTypeCheckingExtensionSupport.class.getClassLoader();
            is = cl.getResourceAsStream(scriptPath);
        }
        if (is == null) {
            // if the input stream is still null, we've not found the extension
            context.getErrorCollector().addFatalError(new SimpleMessage("Static type checking extension '" + scriptPath + "' was not found on the classpath.", config.getDebug(), typeCheckingVisitor.getSourceUnit()));
        }
        try {
            GroovyShell shell = new GroovyShell(transformLoader, new Binding(), config);
            script = (TypeCheckingDSL) shell.parse(new InputStreamReader(is, typeCheckingVisitor.getSourceUnit().getConfiguration().getSourceEncoding()));
        } catch (CompilationFailedException e) {
            throw new GroovyBugError("An unexpected error was thrown during custom type checking", e);
        } catch (UnsupportedEncodingException e) {
            throw new GroovyBugError("Unsupported encoding found in compiler configuration", e);
        }
    }
    if (script != null) {
        script.extension = this;
        script.run();
        List<Closure> list = eventHandlers.get("setup");
        if (list != null) {
            for (Closure closure : list) {
                safeCall(closure);
            }
        }
    }
}
Also used : Closure(groovy.lang.Closure) GroovyBugError(org.codehaus.groovy.GroovyBugError) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) GroovyClassLoader(groovy.lang.GroovyClassLoader) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyClassLoader(groovy.lang.GroovyClassLoader) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) Binding(groovy.lang.Binding) InputStreamReader(java.io.InputStreamReader) Constructor(java.lang.reflect.Constructor) InputStream(java.io.InputStream) SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GroovyShell(groovy.lang.GroovyShell)

Example 18 with ImportCustomizer

use of org.codehaus.groovy.control.customizers.ImportCustomizer in project wildfly-camel by wildfly-extras.

the class CustomGroovyShellFactory method createGroovyShell.

public GroovyShell createGroovyShell(Exchange exchange) {
    ImportCustomizer importCustomizer = new ImportCustomizer();
    importCustomizer.addStaticStars("org.wildfly.camel.test.script.subA.CustomUtils");
    CompilerConfiguration configuration = new CompilerConfiguration();
    configuration.addCompilationCustomizers(importCustomizer);
    ClassLoader classLoader = exchange.getContext().getApplicationContextClassLoader();
    return new GroovyShell(classLoader, configuration);
}
Also used : CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) GroovyShell(groovy.lang.GroovyShell)

Example 19 with ImportCustomizer

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

the class ImportCustomizerFactory method newInstance.

public Object newInstance(final FactoryBuilderSupport builder, final Object name, final Object value, final Map attributes) throws InstantiationException, IllegalAccessException {
    ImportCustomizer customizer = new ImportCustomizer();
    addImport(customizer, value);
    return customizer;
}
Also used : ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer)

Example 20 with ImportCustomizer

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

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);
        }
    }
}
Also used : Binding(groovy.lang.Binding) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

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