org.codehaus.groovy.control.customizers.CompilationCustomizer Java Examples

The following examples show how to use org.codehaus.groovy.control.customizers.CompilationCustomizer. 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: Groovy2CompilerTest.java    From Nicobar with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompile() throws Exception {
    Groovy2Compiler compiler;
    List<CompilationCustomizer> customizers;
    Map<String, Object> compilerParams;

    Path scriptRootPath = GroovyTestResourceUtil.findRootPathForScript(TestScript.HELLO_WORLD);
    PathScriptArchive scriptArchive = new PathScriptArchive.Builder(scriptRootPath)
        .setRecurseRoot(false)
        .addFile(TestScript.HELLO_WORLD.getScriptPath())
        .build();

    compilerParams = new HashMap<String, Object>();
    compilerParams.put(Groovy2Compiler.GROOVY2_COMPILER_PARAMS_CUSTOMIZERS, Arrays.asList(new Object[] {"testmodule.customizers.TestCompilationCustomizer"}));

    compiler = new Groovy2Compiler(compilerParams);
    compiler.compile(scriptArchive, null, scriptRootPath);
}
 
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: GroovyDriverTestCase.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
protected void configure(Binder binder) {
   binder
      .bind(new Key<Set<GroovyDriverPlugin>>() {})
      .toInstance(getPlugins());

   binder.bind(InMemoryPlatformMessageBus.class).in(Singleton.class);
   binder.bind(InMemoryProtocolMessageBus.class).in(Singleton.class);
   binder.bind(PlatformMessageBus.class).to(InMemoryPlatformMessageBus.class);
   binder.bind(ProtocolMessageBus.class).to(InMemoryProtocolMessageBus.class);
   
   Multibinder.newSetBinder(binder, CompilationCustomizer.class)
      .addBinding()
      .to(DriverCompilationCustomizer.class);

   binder
      .bind(Scheduler.class)
      .toInstance(new ExecutorScheduler(Executors.newScheduledThreadPool(1)))
      ;
}
 
Example #4
Source File: GroovyDriverModule.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
protected void configure() {
   bind(GroovyDriverFactory.class);
   bindSetOf(CompilationCustomizer.class)
      .addBinding()
      .toInstance(new ImportCustomizer()
         .addImports("groovy.transform.Field")
         .addStaticStars("java.util.concurrent.TimeUnit")
      );
   bindSetOf(CompilationCustomizer.class)
      .addBinding()
      .to(DriverCompilationCustomizer.class);
   bindSetOf(GroovyDriverPlugin.class)
      .addBinding()
      .to(SchedulerPlugin.class);
   bindSetOf(GroovyDriverPlugin.class)
      .addBinding()
      .to(PinManagementPlugin.class);
}
 
Example #5
Source File: ImportGroovyCustomizer.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public CompilationCustomizer create() {
    final org.codehaus.groovy.control.customizers.ImportCustomizer ic = new org.codehaus.groovy.control.customizers.ImportCustomizer();

    // there's something weird in groovy about doing specific imports instead of wildcard imports. with wildcard
    // imports groovy seems to allow methods to be overloaded with enums such that things like Column.values and
    // __.values() can be resolved by the compiler. if they are both directly in the imports then one or the other
    // can't be found. the temporary fix is to hardcode a wildcard import __ and then filter out the core imports
    // from the incoming customizer. ultimately, the fix should be to resolve the naming conflicts to ensure a
    // unique space somehow.
    ic.addStaticStars(__.class.getCanonicalName());
    for (ImportCustomizer customizer : customizers) {
        customizer.getClassImports().forEach(i -> ic.addImports(i.getCanonicalName()));
        customizer.getMethodImports().stream()
                .filter(m -> !m.getDeclaringClass().equals(__.class))
                .forEach(m -> ic.addStaticImport(m.getDeclaringClass().getCanonicalName(), m.getName()));
        customizer.getEnumImports().forEach(m -> ic.addStaticImport(m.getDeclaringClass().getCanonicalName(), m.name()));
        customizer.getFieldImports().forEach(f -> ic.addStaticImport(f.getDeclaringClass().getCanonicalName(), f.getName()));
    }

    return ic;
}
 
Example #6
Source File: MarkupTemplateEngine.java    From groovy with Apache License 2.0 6 votes vote down vote up
public MarkupTemplateEngine(final ClassLoader parentLoader, final TemplateConfiguration tplConfig, final TemplateResolver resolver) {
    compilerConfiguration = new CompilerConfiguration();
    templateConfiguration = tplConfig;
    compilerConfiguration.addCompilationCustomizers(new TemplateASTTransformer(tplConfig));
    compilerConfiguration.addCompilationCustomizers(
            new ASTTransformationCustomizer(Collections.singletonMap("extensions", "groovy.text.markup.MarkupTemplateTypeCheckingExtension"), CompileStatic.class));
    if (templateConfiguration.isAutoNewLine()) {
        compilerConfiguration.addCompilationCustomizers(
                new CompilationCustomizer(CompilePhase.CONVERSION) {
                    @Override
                    public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) throws CompilationFailedException {
                        new AutoNewLineTransformer(source).visitClass(classNode);
                    }
                }
        );
    }
    groovyClassLoader = AccessController.doPrivileged((PrivilegedAction<TemplateGroovyClassLoader>) () -> new TemplateGroovyClassLoader(parentLoader, compilerConfiguration));
    if (DEBUG_BYTECODE) {
        compilerConfiguration.setBytecodePostprocessor(BytecodeDumper.STANDARD_ERR);
    }
    templateResolver = resolver == null ? new DefaultTemplateResolver() : resolver;
    templateResolver.configure(groovyClassLoader, templateConfiguration);
}
 
Example #7
Source File: InlinedASTCustomizerFactory.java    From groovy with Apache License 2.0 6 votes vote down vote up
public Object postCompleteNode(final FactoryBuilderSupport factory, final Object parent, final Object node) {
    if (node instanceof Map) {
        Map map = (Map) node;
        ProxyGeneratorAdapter adapter = new ProxyGeneratorAdapter(
                map,
                map.containsKey("superClass")?(Class)map.get("superClass"):CompilationCustomizer.class,
                map.containsKey("interfaces")?(Class[])map.get("interfaces"):null,
                this.getClass().getClassLoader(),
                false,
                null
        );
        Object phase = map.get("phase");
        if (!(phase instanceof CompilePhase)) {
            phase = CompilePhase.valueOf(phase.toString());
        }
        return adapter.proxy(map, phase);
    }
    return node;
}
 
Example #8
Source File: CompilationUnit.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void applyCompilationCustomizers() {
    for (CompilationCustomizer customizer : getConfiguration().getCompilationCustomizers()) {
        if (customizer instanceof CompilationUnitAware) {
            ((CompilationUnitAware) customizer).setCompilationUnit(this);
        }
        addPhaseOperation(customizer, customizer.getPhase().getPhaseNumber());
    }
}
 
Example #9
Source File: TimedInterruptGroovyCustomizer.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public CompilationCustomizer create() {
    final Map<String, Object> timedInterruptAnnotationParams = new HashMap<>();
    timedInterruptAnnotationParams.put("value", interruptionTimeout);
    timedInterruptAnnotationParams.put("unit", GeneralUtils.propX(GeneralUtils.classX(TimeUnit.class), TimeUnit.MILLISECONDS.toString()));
    timedInterruptAnnotationParams.put("checkOnMethodStart", false);
    timedInterruptAnnotationParams.put("thrown", GeneralUtils.classX(TimedInterruptTimeoutException.class));
    return new ASTTransformationCustomizer(timedInterruptAnnotationParams, TimedInterrupt.class);
}
 
Example #10
Source File: CompileStaticGroovyCustomizer.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public CompilationCustomizer create() {
    final Map<String, Object> annotationParams = new HashMap<>();
    if (extensions != null && !extensions.isEmpty()) {
        if (extensions.contains(","))
            annotationParams.put("extensions", Stream.of(extensions.split(",")).collect(Collectors.toList()));
        else
            annotationParams.put("extensions", Collections.singletonList(extensions));
    }

    return new ASTTransformationCustomizer(annotationParams, CompileStatic.class);
}
 
Example #11
Source File: TypeCheckedGroovyCustomizer.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public CompilationCustomizer create() {
    final Map<String, Object> annotationParams = new HashMap<>();
    if (extensions != null && !extensions.isEmpty()) {
        if (extensions.contains(","))
            annotationParams.put("extensions", Stream.of(extensions.split(",")).collect(Collectors.toList()));
        else
            annotationParams.put("extensions", Collections.singletonList(extensions));
    }
    return new ASTTransformationCustomizer(annotationParams, TypeChecked.class);
}
 
Example #12
Source File: SourceAwareCustomizerFactory.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Object newInstance(final FactoryBuilderSupport builder, final Object name, final Object value, final Map attributes) throws InstantiationException, IllegalAccessException {
    SourceOptions data = new SourceOptions();
    if (value instanceof CompilationCustomizer) {
        data.delegate = (CompilationCustomizer) value;
    } else {
        // GROOVY-9035 supply a "no-op" CompilationCustomizer if none found to make DSL friendly for empty case
        data.delegate = new CompilationCustomizer(CompilePhase.FINALIZATION) {
            @Override
            public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) {
            }
        };
    }
    return data;
}
 
Example #13
Source File: CustomizersFactory.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void setChild(final FactoryBuilderSupport builder, final Object parent, final Object child) {
    if (parent instanceof Collection && child instanceof CompilationCustomizer) {
        ((Collection) parent).add(child);
    }
}
 
Example #14
Source File: MockGroovyDriverModule.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {
   bindSetOf(GroovyDriverPlugin.class)
     .addBinding()
     .toInstance(mockGroovyDriverPlugin);
   bindSetOf(CompilationCustomizer.class)
      .addBinding()
      .toInstance(new ImportCustomizer()
         .addImports("groovy.transform.Field")
         .addStaticStars("java.util.concurrent.TimeUnit")
      );
   bindSetOf(CompilationCustomizer.class)
      .addBinding()
      .to(DriverCompilationCustomizer.class);
}
 
Example #15
Source File: GroovyDriverTestCase.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
public GroovyScriptEngine scriptEngine(Set<CompilationCustomizer> customizers) {
   GroovyScriptEngine engine = scriptEngine();
   for(CompilationCustomizer customizer: customizers) {
      engine.getConfig().addCompilationCustomizers(customizer);
   }
   engine.getConfig().setScriptExtensions(ImmutableSet.of("driver", "capability", "groovy"));
   return engine;
}
 
Example #16
Source File: CompilerConfiguration.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public CompilerConfiguration addCompilationCustomizers(final CompilationCustomizer... customizers) {
    throw new UnsupportedOperationException();
}
 
Example #17
Source File: CompilerConfiguration.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public List<CompilationCustomizer> getCompilationCustomizers() {
    return Collections.unmodifiableList(super.getCompilationCustomizers());
}
 
Example #18
Source File: SourceAwareCustomizerFactory.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void setChild(final FactoryBuilderSupport builder, final Object parent, final Object child) {
    if (child instanceof CompilationCustomizer && parent instanceof SourceOptions) {
        ((SourceOptions) parent).delegate = (CompilationCustomizer) child;
    }
}
 
Example #19
Source File: InterpreterModeGroovyCustomizer.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public CompilationCustomizer create() {
    return new ASTTransformationCustomizer(InterpreterMode.class);
}
 
Example #20
Source File: ThreadInterruptGroovyCustomizer.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public CompilationCustomizer create() {
    return new ASTTransformationCustomizer(ThreadInterrupt.class);
}
 
Example #21
Source File: ConfigurationGroovyCustomizer.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public CompilationCustomizer create() {
    throw new UnsupportedOperationException("This is a marker implementation that does not create a CompilationCustomizer instance");
}
 
Example #22
Source File: GroovyScriptFactory.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Create a new GroovyScriptFactory for the given script source,
 * specifying a strategy interface that can customize Groovy's compilation
 * process within the underlying GroovyClassLoader.
 * @param scriptSourceLocator a locator that points to the source of the script.
 * Interpreted by the post-processor that actually creates the script.
 * @param compilationCustomizers one or more customizers to be applied to the
 * GroovyClassLoader compiler configuration
 * @since 4.3.3
 * @see CompilerConfiguration#addCompilationCustomizers
 * @see org.codehaus.groovy.control.customizers.ImportCustomizer
 */
public GroovyScriptFactory(String scriptSourceLocator, CompilationCustomizer... compilationCustomizers) {
	this(scriptSourceLocator);
	if (!ObjectUtils.isEmpty(compilationCustomizers)) {
		this.compilerConfiguration = new CompilerConfiguration();
		this.compilerConfiguration.addCompilationCustomizers(compilationCustomizers);
	}
}
 
Example #23
Source File: GroovyScriptFactory.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a new GroovyScriptFactory for the given script source,
 * specifying a strategy interface that can customize Groovy's compilation
 * process within the underlying GroovyClassLoader.
 * @param scriptSourceLocator a locator that points to the source of the script.
 * Interpreted by the post-processor that actually creates the script.
 * @param compilationCustomizers one or more customizers to be applied to the
 * GroovyClassLoader compiler configuration
 * @since 4.3.3
 * @see CompilerConfiguration#addCompilationCustomizers
 * @see org.codehaus.groovy.control.customizers.ImportCustomizer
 */
public GroovyScriptFactory(String scriptSourceLocator, CompilationCustomizer... compilationCustomizers) {
	this(scriptSourceLocator);
	if (!ObjectUtils.isEmpty(compilationCustomizers)) {
		this.compilerConfiguration = new CompilerConfiguration();
		this.compilerConfiguration.addCompilationCustomizers(compilationCustomizers);
	}
}
 
Example #24
Source File: GroovyScriptFactory.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Create a new GroovyScriptFactory for the given script source,
 * specifying a strategy interface that can customize Groovy's compilation
 * process within the underlying GroovyClassLoader.
 * @param scriptSourceLocator a locator that points to the source of the script.
 * Interpreted by the post-processor that actually creates the script.
 * @param compilationCustomizers one or more customizers to be applied to the
 * GroovyClassLoader compiler configuration
 * @since 4.3.3
 * @see CompilerConfiguration#addCompilationCustomizers
 * @see org.codehaus.groovy.control.customizers.ImportCustomizer
 */
public GroovyScriptFactory(String scriptSourceLocator, CompilationCustomizer... compilationCustomizers) {
	this(scriptSourceLocator);
	if (!ObjectUtils.isEmpty(compilationCustomizers)) {
		this.compilerConfiguration = new CompilerConfiguration();
		this.compilerConfiguration.addCompilationCustomizers(compilationCustomizers);
	}
}
 
Example #25
Source File: GroovyScriptEvaluator.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Set one or more customizers to be applied to this evaluator's compiler configuration.
 * <p>Note that this modifies the shared compiler configuration held by this evaluator.
 * @since 4.3.3
 * @see #setCompilerConfiguration
 */
public void setCompilationCustomizers(CompilationCustomizer... compilationCustomizers) {
	this.compilerConfiguration.addCompilationCustomizers(compilationCustomizers);
}
 
Example #26
Source File: CompilerConfiguration.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Adds compilation customizers to the compilation process. A compilation customizer is a class node
 * operation which performs various operations going from adding imports to access control.
 * @param customizers the list of customizers to be added
 * @return this configuration instance
 */
public CompilerConfiguration addCompilationCustomizers(final CompilationCustomizer... customizers) {
    if (customizers == null) throw new IllegalArgumentException("provided customizers list must not be null");
    Collections.addAll(compilationCustomizers, customizers);
    return this;
}
 
Example #27
Source File: GroovyCustomizer.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new instance of a {@code CompilationCustomizer} to add to the {@link GremlinGroovyScriptEngine}.
 */
public CompilationCustomizer create();
 
Example #28
Source File: GroovyScriptEvaluator.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Set one or more customizers to be applied to this evaluator's compiler configuration.
 * <p>Note that this modifies the shared compiler configuration held by this evaluator.
 * @since 4.3.3
 * @see #setCompilerConfiguration
 */
public void setCompilationCustomizers(CompilationCustomizer... compilationCustomizers) {
	this.compilerConfiguration.addCompilationCustomizers(compilationCustomizers);
}
 
Example #29
Source File: GroovyScriptEvaluator.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Set one or more customizers to be applied to this evaluator's compiler configuration.
 * <p>Note that this modifies the shared compiler configuration held by this evaluator.
 * @since 4.3.3
 * @see #setCompilerConfiguration
 */
public void setCompilationCustomizers(CompilationCustomizer... compilationCustomizers) {
	this.compilerConfiguration.addCompilationCustomizers(compilationCustomizers);
}
 
Example #30
Source File: CompilerConfiguration.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the list of compilation customizers.
 * @return the customizers (always not null)
 */
public List<CompilationCustomizer> getCompilationCustomizers() {
    return compilationCustomizers;
}