org.codehaus.groovy.control.customizers.ImportCustomizer Java Examples
The following examples show how to use
org.codehaus.groovy.control.customizers.ImportCustomizer.
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: GroovyClassLoaderFactory.java From beakerx with Apache License 2.0 | 6 votes |
public static GroovyClassLoader newEvaluator(Imports imports, Classpath classpath, String outDir, ImportCustomizer icz, ClassLoader parent) { try { Class.forName("org.codehaus.groovy.control.customizers.ImportCustomizer"); } catch (ClassNotFoundException e1) { String gjp = System.getenv(GROOVY_JAR_PATH); String errorMsg = null; if (gjp != null && !gjp.isEmpty()) { errorMsg = "Groovy libary not found, GROOVY_JAR_PATH = " + gjp; } else { errorMsg = "Default groovy libary not found. No GROOVY_JAR_PATH variable set."; } throw new GroovyNotFoundException(errorMsg); } icz = addImportsCustomizer(icz, imports); CompilerConfiguration config = new CompilerConfiguration().addCompilationCustomizers(icz); String acloader_cp = String.join(File.pathSeparatorChar + "", classpath.getPathsAsStrings()); config.setClasspath(acloader_cp); return new GroovyClassLoader(parent, config); }
Example #2
Source File: ApiGroovyCompiler.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 6 votes |
private void applyConfigurationScript(File configScript, CompilerConfiguration configuration) { VersionNumber version = parseGroovyVersion(); if (version.compareTo(VersionNumber.parse("2.1")) < 0) { throw new GradleException("Using a Groovy compiler configuration script requires Groovy 2.1+ but found Groovy " + version + ""); } 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); try { shell.evaluate(configScript); } catch (Exception e) { throw new GradleException("Could not execute Groovy compiler configuration script: " + configScript.getAbsolutePath(), e); } }
Example #3
Source File: ImportCustomizerFactory.java From groovy with Apache License 2.0 | 6 votes |
private void addImport(final ImportCustomizer customizer, final Object value) { if (value==null) return; if (value instanceof Collection) { for (Object e : (Collection)value) { addImport(customizer, e); } } else if (value instanceof String) { customizer.addImports((String)value); } else if (value instanceof Class) { customizer.addImports(((Class) value).getName()); } else if (value instanceof GString) { customizer.addImports(value.toString()); } else { throw new RuntimeException("Unsupported import value type ["+value+"]"); } }
Example #4
Source File: Groovy.java From groovy with Apache License 2.0 | 6 votes |
private void configureCompiler() { if (scriptBaseClass != null) { configuration.setScriptBaseClass(scriptBaseClass); } 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); } } }
Example #5
Source File: ForbiddenApisPlugin.java From forbidden-apis with Apache License 2.0 | 6 votes |
private static Class<? extends DelegatingScript> loadScript() { final ImportCustomizer importCustomizer = new ImportCustomizer().addStarImports(ForbiddenApisPlugin.class.getPackage().getName()); final CompilerConfiguration configuration = new CompilerConfiguration().addCompilationCustomizers(importCustomizer); configuration.setScriptBaseClass(DelegatingScript.class.getName()); configuration.setSourceEncoding(StandardCharsets.UTF_8.name()); final URL scriptUrl = ForbiddenApisPlugin.class.getResource(PLUGIN_INIT_SCRIPT); if (scriptUrl == null) { throw new RuntimeException("Cannot find resource with script: " + PLUGIN_INIT_SCRIPT); } return AccessController.doPrivileged(new PrivilegedAction<Class<? extends DelegatingScript>>() { @Override public Class<? extends DelegatingScript> run() { try { // We don't close the classloader, as we may need it later when loading other classes from inside script: @SuppressWarnings("resource") final GroovyClassLoader loader = new GroovyClassLoader(ForbiddenApisPlugin.class.getClassLoader(), configuration); final GroovyCodeSource csrc = new GroovyCodeSource(scriptUrl); @SuppressWarnings("unchecked") final Class<? extends DelegatingScript> clazz = loader.parseClass(csrc, false).asSubclass(DelegatingScript.class); return clazz; } catch (Exception e) { throw new RuntimeException("Cannot compile Groovy script: " + PLUGIN_INIT_SCRIPT); } } }); }
Example #6
Source File: GroovySandbox.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
private CompilerConfiguration getSandboxConfiguration() throws IOException { ImportCustomizer imports = new ImportCustomizer(); SecureASTCustomizer secure = new SecureASTCustomizer(); secure.setClosuresAllowed(true); secure.setMethodDefinitionAllowed(true); String[] staticImportsWhitelist = new String[] {}; secure.setStaticImportsWhitelist(Arrays.asList(staticImportsWhitelist)); secure.setIndirectImportCheckEnabled(true); // add also Object.class secure.setImportsWhitelist(getStringClasses("", CLASSES_WHITELIST, addedClasses, new Class[] { Object.class })); List<String> staticImportMethods = getStaticImportMethods(CLASSES_WHITELIST, addedClasses); addPredefinedMethods(staticImportMethods); secure.setStaticImportsWhitelist(staticImportMethods); secure.setConstantTypesClassesWhiteList(getClasses(CONSTANT_TYTPE_CLASSES_WHITELIST, CLASSES_WHITELIST, addedClasses)); // add also Object.class secure.setReceiversClassesWhiteList(getClasses(CLASSES_WHITELIST, addedClasses, new Class[] { Object.class })); CompilerConfiguration res = new CompilerConfiguration(); res.addCompilationCustomizers(imports, secure); return res; }
Example #7
Source File: GroovyExecutor.java From hugegraph-loader with Apache License 2.0 | 6 votes |
public void execute(String groovyScript, HugeClient client) { CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass(DelegatingScript.class.getName()); ImportCustomizer importCustomizer = new ImportCustomizer(); importCustomizer.addImports(HugeClient.class.getName()); importCustomizer.addImports(SchemaManager.class.getName()); config.addCompilationCustomizers(importCustomizer); GroovyShell shell = new GroovyShell(getClass().getClassLoader(), this.binding, config); // Groovy invoke java through the delegating script. DelegatingScript script = (DelegatingScript) shell.parse(groovyScript); script.setDelegate(client); script.run(); }
Example #8
Source File: ApiGroovyCompiler.java From pushfish-android with BSD 2-Clause "Simplified" License | 6 votes |
private void applyConfigurationScript(File configScript, CompilerConfiguration configuration) { VersionNumber version = parseGroovyVersion(); if (version.compareTo(VersionNumber.parse("2.1")) < 0) { throw new GradleException("Using a Groovy compiler configuration script requires Groovy 2.1+ but found Groovy " + version + ""); } 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); try { shell.evaluate(configScript); } catch (Exception e) { throw new GradleException("Could not execute Groovy compiler configuration script: " + configScript.getAbsolutePath(), e); } }
Example #9
Source File: GroovyDriverModule.java From arcusplatform with Apache License 2.0 | 6 votes |
@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 #10
Source File: GroovyScriptEngine.java From chaosblade-exec-jvm with Apache License 2.0 | 6 votes |
@Override public Object compile(com.alibaba.chaosblade.exec.plugin.jvm.script.base.Script script, ClassLoader classLoader, Map<String, String> configs) { String className = "groovy_script_" + script.getId(); GroovyCodeSource codeSource = new GroovyCodeSource(script.getContent(), className, UNTRUSTED_CODEBASE); codeSource.setCachable(true); CompilerConfiguration compilerConfiguration = new CompilerConfiguration() .addCompilationCustomizers( new ImportCustomizer().addStaticStars("java.lang.Math")) .addCompilationCustomizers(new GroovyBigDecimalTransformer(CompilePhase.CONVERSION)); compilerConfiguration.getOptimizationOptions().put(GROOVY_INDY_SETTING_NAME, true); GroovyClassLoader groovyClassLoader = new GroovyClassLoader(classLoader, compilerConfiguration); try { return groovyClassLoader.parseClass(script.getContent()); } catch (Exception ex) { throw convertToScriptException("Compile script failed:" + className, script.getId(), ex); } }
Example #11
Source File: GroovyClassLoaderFactory.java From beakerx with Apache License 2.0 | 5 votes |
private static ImportCustomizer addImportsCustomizer(ImportCustomizer icz, Imports imports) { if (!imports.isEmpty()) { for (ImportPath importLine : imports.getImportPaths()) { addImportPathToImportCustomizer(icz, importLine); } } return icz; }
Example #12
Source File: CustomGroovyShellFactory.java From wildfly-camel with Apache License 2.0 | 5 votes |
public GroovyShell createGroovyShell(Exchange exchange) { ImportCustomizer importCustomizer = new ImportCustomizer(); importCustomizer.addStaticStars("org.wildfly.camel.test.groovy.subA.CustomUtils"); CompilerConfiguration configuration = new CompilerConfiguration(); configuration.addCompilationCustomizers(importCustomizer); ClassLoader classLoader = exchange.getContext().getApplicationContextClassLoader(); return new GroovyShell(classLoader, configuration); }
Example #13
Source File: EurekaServerCompilerAutoConfiguration.java From spring-cloud-cli with Apache License 2.0 | 5 votes |
@Override public void applyImports(ImportCustomizer imports) throws CompilationFailedException { imports.addImports("org.springframework.cloud.netflix.eureka.server.EnableEurekaServer", "org.springframework.cloud.client.discovery.EnableDiscoveryClient", "org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean", "org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean", "org.springframework.cloud.netflix.eureka.EurekaClientConfigBean", "com.netflix.discovery.DiscoveryClient", "com.netflix.appinfo.InstanceInfo"); }
Example #14
Source File: EurekaClientCompilerAutoConfiguration.java From spring-cloud-cli with Apache License 2.0 | 5 votes |
@Override public void applyImports(ImportCustomizer imports) throws CompilationFailedException { imports.addImports( "org.springframework.cloud.client.discovery.EnableDiscoveryClient", "org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean", "org.springframework.cloud.netflix.eureka.EurekaClientConfigBean", "org.springframework.cloud.client.discovery.DiscoveryClient", "org.springframework.cloud.client.ServiceInstance"); }
Example #15
Source File: GroovyClassLoaderFactory.java From beakerx with Apache License 2.0 | 5 votes |
public static void addImportPathToImportCustomizer(ImportCustomizer icz, ImportPath importLine) { if (importLine.asString().startsWith(STATIC_WORD_WITH_SPACE)) { String pureImport = importLine.asString() .replace(STATIC_WORD_WITH_SPACE, StringUtils.EMPTY) .replace(DOT_STAR_POSTFIX, StringUtils.EMPTY); if (importLine.asString().endsWith(DOT_STAR_POSTFIX)) { icz.addStaticStars(pureImport); } else { int index = pureImport.lastIndexOf('.'); if (index == -1) { return; } icz.addStaticImport(pureImport.substring(0, index), pureImport.substring(index + 1)); } } else { if (importLine.asString().endsWith(DOT_STAR_POSTFIX)) { icz.addStarImports(importLine.asString().replace(DOT_STAR_POSTFIX, StringUtils.EMPTY)); } else { icz.addImports(importLine.asString()); } } }
Example #16
Source File: BaseStreamCompilerAutoConfiguration.java From spring-cloud-cli with Apache License 2.0 | 5 votes |
@Override public void applyImports(ImportCustomizer imports) throws CompilationFailedException { this.integration.applyImports(imports); imports.addImports("org.springframework.boot.groovy.cloud.EnableBinding"); imports.addImport("IntegrationMessageSource", "org.springframework.integration.core.MessageSource"); imports.addStarImports("org.springframework.cloud.stream.annotation", "org.springframework.cloud.stream.messaging"); }
Example #17
Source File: GroovyScriptEvaluatorTests.java From spring-analysis-note with MIT License | 5 votes |
@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")); assertEquals("classpath:", result); }
Example #18
Source File: CompilerConfigurationTest.java From vertx-lang-groovy with Apache License 2.0 | 5 votes |
@Test public void testSystemPropertyGroovy() throws Exception { System.setProperty("vertx.groovy.compilerConfiguration", "configs/compilerConfiguration.groovy"); try { deployVerticle("groovy:io/vertx/lang/groovy/CompilerConfigVerticleScript.groovy"); assertEquals("groovy.lang.Script", config.getScriptBaseClass()); assertEquals("UTF-8", config.getSourceEncoding()); assertEquals(1, config.getCompilationCustomizers().size()); assertTrue(config.getCompilationCustomizers().get(0) instanceof ImportCustomizer); } finally { System.clearProperty("vertx.groovy.compilerConfiguration"); } }
Example #19
Source File: CompilerConfigurationTest.java From vertx-lang-groovy with Apache License 2.0 | 5 votes |
@Test public void testDefaultPropertiesGroovy() throws Exception { deployVerticle("groovy:io/vertx/lang/groovy/CompilerConfigVerticleScript.groovy", new AbstractMap.SimpleEntry<>("compilerConfiguration.groovy", "configs/compilerConfiguration.groovy")); assertEquals("groovy.lang.Script", config.getScriptBaseClass()); assertEquals("UTF-8", config.getSourceEncoding()); assertEquals(1, config.getCompilationCustomizers().size()); assertTrue(config.getCompilationCustomizers().get(0) instanceof ImportCustomizer); }
Example #20
Source File: ImportCustomizerFactory.java From groovy with Apache License 2.0 | 5 votes |
@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; }
Example #21
Source File: GroovyService.java From JuniperBot with GNU General Public License v3.0 | 5 votes |
@PostConstruct public void init() { ImportCustomizer importCustomizer = new ImportCustomizer(); importCustomizer.addStarImports("net.dv8tion.jda.api.entities"); configuration = new CompilerConfiguration(); configuration.addCompilationCustomizers(importCustomizer); sharedData = new Binding(); sharedData.setProperty("ctx", context); }
Example #22
Source File: AstUtilTest.java From powsybl-core with Mozilla Public License 2.0 | 5 votes |
@Test public void testPrint() { ASTTransformationCustomizer astCustomizer = new ASTTransformationCustomizer(new FakeTransformer()); ImportCustomizer imports = new ImportCustomizer(); CompilerConfiguration config = new CompilerConfiguration(); config.addCompilationCustomizers(astCustomizer, imports); Binding binding = new Binding(); assertNull(new GroovyShell(binding, config).evaluate("print('hello')")); }
Example #23
Source File: GroovyScriptEvaluatorTests.java From java-technology-stack with MIT License | 5 votes |
@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")); assertEquals("classpath:", result); }
Example #24
Source File: MockGroovyDriverModule.java From arcusplatform with Apache License 2.0 | 5 votes |
@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 #25
Source File: CompilerConfigurationTest.java From groovy with Apache License 2.0 | 4 votes |
@Test public void testCopyConstructor1() { CompilerConfiguration init = new CompilerConfiguration(); init.setWarningLevel(WarningMessage.POSSIBLE_ERRORS); init.setDebug(true); init.setParameters(true); init.setVerbose(false); init.setTolerance(720); init.setMinimumRecompilationInterval(234); init.setScriptBaseClass("blarg.foo.WhatSit"); init.setSourceEncoding("LEAD-123"); init.setTargetBytecode(CompilerConfiguration.JDK5); init.setRecompileGroovySource(true); init.setClasspath("File1" + File.pathSeparator + "Somewhere"); File targetDirectory = new File("A wandering path"); init.setTargetDirectory(targetDirectory); init.setDefaultScriptExtension(".jpp"); init.setJointCompilationOptions(Collections.singletonMap("somekey", "somevalue")); init.addCompilationCustomizers(new ImportCustomizer().addStarImports("groovy.transform")); ParserPluginFactory pluginFactory = ParserPluginFactory.antlr4(); init.setPluginFactory(pluginFactory); assertEquals(WarningMessage.POSSIBLE_ERRORS, init.getWarningLevel()); assertEquals(Boolean.TRUE, init.getDebug()); assertEquals(Boolean.TRUE, init.getParameters()); assertEquals(Boolean.FALSE, init.getVerbose()); assertEquals(720, init.getTolerance()); assertEquals(234, init.getMinimumRecompilationInterval()); assertEquals("blarg.foo.WhatSit", init.getScriptBaseClass()); assertEquals("LEAD-123", init.getSourceEncoding()); assertEquals(CompilerConfiguration.JDK5, init.getTargetBytecode()); assertEquals(Boolean.TRUE, init.getRecompileGroovySource()); assertEquals("File1", init.getClasspath().get(0)); assertEquals("Somewhere", init.getClasspath().get(1)); assertEquals(targetDirectory, init.getTargetDirectory()); assertEquals(".jpp", init.getDefaultScriptExtension()); assertEquals("somevalue", init.getJointCompilationOptions().get("somekey")); assertEquals(pluginFactory, init.getPluginFactory()); assertEquals(1, init.getCompilationCustomizers().size()); // CompilerConfiguration config = new CompilerConfiguration(init); assertEquals(WarningMessage.POSSIBLE_ERRORS, config.getWarningLevel()); assertEquals(Boolean.TRUE, config.getDebug()); assertEquals(Boolean.FALSE, config.getVerbose()); assertEquals(720, config.getTolerance()); assertEquals(234, config.getMinimumRecompilationInterval()); assertEquals("blarg.foo.WhatSit", config.getScriptBaseClass()); assertEquals("LEAD-123", config.getSourceEncoding()); assertEquals(CompilerConfiguration.JDK5, config.getTargetBytecode()); assertEquals(Boolean.TRUE, config.getRecompileGroovySource()); assertEquals("File1", config.getClasspath().get(0)); assertEquals("Somewhere", config.getClasspath().get(1)); assertEquals(targetDirectory, config.getTargetDirectory()); assertEquals(".jpp", config.getDefaultScriptExtension()); assertEquals("somevalue", config.getJointCompilationOptions().get("somekey")); assertEquals(pluginFactory, config.getPluginFactory()); // TODO GROOVY-9585: re-enable below assertion once prod code is fixed // assertEquals(1, config.getCompilationCustomizers().size()); }
Example #26
Source File: ImportCustomizerFactory.java From groovy with Apache License 2.0 | 4 votes |
private ImportHelper(final ImportCustomizer customizer) { this.customizer = customizer; }
Example #27
Source File: ImportCustomizerFactory.java From groovy with Apache License 2.0 | 4 votes |
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; }
Example #28
Source File: ReadCSVStep.java From pipeline-utility-steps-plugin with MIT License | 4 votes |
@Override public void customizeImports(CpsFlowExecution context, ImportCustomizer ic) { ic.addStarImports(ORG_APACHE_COMMONS_CSV); }
Example #29
Source File: SpringCloudCompilerAutoConfiguration.java From spring-cloud-cli with Apache License 2.0 | 4 votes |
@Override public void applyImports(ImportCustomizer imports) throws CompilationFailedException { imports.addImports("org.springframework.cloud.context.config.annotation.RefreshScope"); }
Example #30
Source File: ReadMavenPomStep.java From pipeline-utility-steps-plugin with MIT License | 4 votes |
@Override public void customizeImports(CpsFlowExecution context, ImportCustomizer ic) { ic.addStarImports(ORG_APACHE_MAVEN_MODEL); }