Java Code Examples for org.codehaus.groovy.control.CompilerConfiguration#setSourceEncoding()
The following examples show how to use
org.codehaus.groovy.control.CompilerConfiguration#setSourceEncoding() .
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: GroovyScriptEngineFactory.java From nexus-public with Eclipse Public License 1.0 | 6 votes |
private GroovyScriptEngine create() { // custom the configuration of the compiler CompilerConfiguration cc = new CompilerConfiguration(); cc.setTargetDirectory(new File(applicationDirectories.getTemporaryDirectory(), "groovy-classes")); cc.setSourceEncoding("UTF-8"); cc.setScriptBaseClass(ScriptWithCleanup.class.getName()); cc.addCompilationCustomizers(secureASTCustomizer()); GroovyClassLoader gcl = new GroovyClassLoader(classLoader, cc); engine = new GroovyScriptEngine(gcl); // HACK: For testing log.info("Created engine: {}", engine); return engine; }
Example 2
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 3
Source File: GroovyScriptController.java From groovy-script-example with Apache License 2.0 | 5 votes |
@PostConstruct public void init(){ GroovyClassLoader groovyClassLoader = new GroovyClassLoader(this.getClass().getClassLoader()); CompilerConfiguration compilerConfiguration = new CompilerConfiguration(); compilerConfiguration.setSourceEncoding("utf-8"); compilerConfiguration.setScriptBaseClass(TestScript.class.getName()); groovyShell = new GroovyShell(groovyClassLoader, groovyBinding, compilerConfiguration); }
Example 4
Source File: FileSystemCompiler.java From groovy with Apache License 2.0 | 4 votes |
public CompilerConfiguration toCompilerConfiguration() throws IOException { // Setup the configuration data CompilerConfiguration configuration = new CompilerConfiguration(); if (classpath != null) { configuration.setClasspath(classpath); } if (targetDir != null && targetDir.getName().length() > 0) { configuration.setTargetDirectory(targetDir); } configuration.setParameters(parameterMetadata); configuration.setPreviewFeatures(previewFeatures); configuration.setSourceEncoding(encoding); configuration.setScriptBaseClass(scriptBaseClass); // joint compilation parameters if (jointCompilation) { Map<String, Object> compilerOptions = new HashMap<>(); compilerOptions.put("flags", javacFlags()); compilerOptions.put("namedValues", javacNamedValues()); configuration.setJointCompilationOptions(compilerOptions); } final List<String> transformations = new ArrayList<>(); if (compileStatic) { transformations.add("ast(groovy.transform.CompileStatic)"); } if (typeChecked) { transformations.add("ast(groovy.transform.TypeChecked)"); } if (!transformations.isEmpty()) { processConfigScriptText(buildConfigScriptText(transformations), configuration); } String configScripts = System.getProperty("groovy.starter.configscripts", null); if (configScript != null || (configScripts != null && !configScripts.isEmpty())) { List<String> scripts = new ArrayList<>(); if (configScript != null) { scripts.add(configScript); } if (configScripts != null) { scripts.addAll(StringGroovyMethods.tokenize(configScripts, ',')); } processConfigScripts(scripts, configuration); } return configuration; }