Search in sources :

Example 31 with CompilerConfiguration

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

Example 32 with CompilerConfiguration

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();
        }
    }
}
Also used : Binding(groovy.lang.Binding) Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) GroovyShell(groovy.lang.GroovyShell) CommandLine(org.apache.commons.cli.CommandLine) ApplicationContext(org.springframework.context.ApplicationContext) DisposableBean(org.springframework.beans.factory.DisposableBean) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Example 33 with CompilerConfiguration

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyClass(org.codehaus.groovy.tools.GroovyClass) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) SourceUnit(org.codehaus.groovy.control.SourceUnit) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyClass(org.codehaus.groovy.tools.GroovyClass) GroovyObject(groovy.lang.GroovyObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 34 with CompilerConfiguration

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);
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) CommandLineParser(org.apache.commons.cli.CommandLineParser) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 35 with CompilerConfiguration

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

Aggregations

CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)42 GroovyShell (groovy.lang.GroovyShell)14 Binding (groovy.lang.Binding)10 File (java.io.File)10 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)9 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)8 GroovyClassLoader (groovy.lang.GroovyClassLoader)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 HashMap (java.util.HashMap)4 GroovyBugError (org.codehaus.groovy.GroovyBugError)4 ClassNode (org.codehaus.groovy.ast.ClassNode)4 GroovyClass (org.codehaus.groovy.tools.GroovyClass)4 Map (java.util.Map)3 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)3 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)3 Closure (groovy.lang.Closure)2 GroovyObject (groovy.lang.GroovyObject)2 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2