Java Code Examples for org.codehaus.groovy.control.CompilerConfiguration#DEFAULT

The following examples show how to use org.codehaus.groovy.control.CompilerConfiguration#DEFAULT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AstStringCompiler.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Performs the String source to {@link java.util.List} of {@link ASTNode}.
 *
 * @param script
 *      a Groovy script in String form
 * @param compilePhase
 *      the int based CompilePhase to compile it to.
 * @param statementsOnly
 * @return {@link java.util.List} of {@link ASTNode}
 */
public List<ASTNode> compile(String script, CompilePhase compilePhase, boolean statementsOnly) {
    final String scriptClassName = makeScriptClassName();
    GroovyCodeSource codeSource = new GroovyCodeSource(script, scriptClassName + ".groovy", "/groovy/script");
    CompilationUnit cu = new CompilationUnit(CompilerConfiguration.DEFAULT, codeSource.getCodeSource(),
            AccessController.doPrivileged((PrivilegedAction<GroovyClassLoader>) GroovyClassLoader::new));
    cu.addSource(codeSource.getName(), script);
    cu.compile(compilePhase.getPhaseNumber());

    // collect all the ASTNodes into the result, possibly ignoring the script body if desired
    List<ASTNode> result = cu.getAST().getModules().stream().reduce(new LinkedList<>(), (acc, node) -> {
        BlockStatement statementBlock = node.getStatementBlock();
        if (null != statementBlock) {
            acc.add(statementBlock);
        }
        acc.addAll(
                node.getClasses().stream()
                    .filter(c -> !(statementsOnly && scriptClassName.equals(c.getName())))
                    .collect(Collectors.toList())
        );

        return acc;
    }, (o1, o2) -> o1);

    return result;
}
 
Example 2
Source File: Groovy2Compiler.java    From Nicobar with Apache License 2.0 6 votes vote down vote up
@Override
public Set<Class<?>> compile(ScriptArchive archive, JBossModuleClassLoader moduleClassLoader, Path compilationRootDir)
    throws ScriptCompilationException, IOException {
    
    List<CompilationCustomizer> customizers = new LinkedList<CompilationCustomizer>();

    for (String klassName: this.customizerClassNames) {
        CompilationCustomizer instance = this.getCustomizerInstanceFromString(klassName, moduleClassLoader);
        if (instance != null ) {
            customizers.add(instance);
        }
    }

    CompilerConfiguration config = new CompilerConfiguration(CompilerConfiguration.DEFAULT);
    config.addCompilationCustomizers(customizers.toArray(new CompilationCustomizer[0]));

     new Groovy2CompilerHelper(compilationRootDir)
        .addScriptArchive(archive)
        .withParentClassloader(moduleClassLoader) // TODO: replace JBossModuleClassLoader with generic class loader
        .withConfiguration(config)
        .compile();
    return Collections.emptySet();
}
 
Example 3
Source File: GroovyClassLoader.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * creates a GroovyClassLoader.
 *
 * @param parent                    the parent class loader
 * @param config                    the compiler configuration
 * @param useConfigurationClasspath determines if the configurations classpath should be added
 */
public GroovyClassLoader(ClassLoader parent, CompilerConfiguration config, boolean useConfigurationClasspath) {
    super(EMPTY_URL_ARRAY, parent);
    if (config == null) config = CompilerConfiguration.DEFAULT;
    this.config = config;
    if (useConfigurationClasspath) {
        for (String path : config.getClasspath()) {
            this.addClasspath(path);
        }
    }

    initSourceEncoding(config);
}
 
Example 4
Source File: GremlinGroovyScriptEngine.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private synchronized void createClassLoader() {
    final CompilerConfiguration conf = new CompilerConfiguration(CompilerConfiguration.DEFAULT);
    conf.addCompilationCustomizers(this.importGroovyCustomizer.create());

    // ConfigurationCustomizerProvider is treated separately
    groovyCustomizers.stream().filter(cp -> !(cp instanceof ConfigurationGroovyCustomizer))
            .forEach(p -> conf.addCompilationCustomizers(p.create()));

    groovyCustomizers.stream().filter(cp -> cp instanceof ConfigurationGroovyCustomizer).findFirst()
            .ifPresent(cp -> ((ConfigurationGroovyCustomizer) cp).applyCustomization(conf));

    this.loader = new GremlinGroovyClassLoader(getParentLoader(), conf);
}
 
Example 5
Source File: AbstractReaderSource.java    From groovy with Apache License 2.0 4 votes vote down vote up
public AbstractReaderSource(final CompilerConfiguration configuration) {
    this.configuration = configuration != null ? configuration : CompilerConfiguration.DEFAULT;
}
 
Example 6
Source File: GroovyShell.java    From groovy with Apache License 2.0 4 votes vote down vote up
public GroovyShell(ClassLoader parent, Binding binding) {
    this(parent, binding, CompilerConfiguration.DEFAULT);
}
 
Example 7
Source File: GroovyShell.java    From groovy with Apache License 2.0 4 votes vote down vote up
public GroovyShell(ClassLoader parent) {
    this(parent, new Binding(), CompilerConfiguration.DEFAULT);
}