Search in sources :

Example 1 with GroovyCodeSource

use of groovy.lang.GroovyCodeSource in project groovy by apache.

the class InvokerHelperTest method testCreateScriptWithScriptClass.

public void testCreateScriptWithScriptClass() {
    GroovyClassLoader classLoader = new GroovyClassLoader();
    String controlProperty = "text";
    String controlValue = "I am a script";
    String code = controlProperty + " = '" + controlValue + "'";
    GroovyCodeSource codeSource = new GroovyCodeSource(code, "testscript", "/groovy/shell");
    Class scriptClass = classLoader.parseClass(codeSource, false);
    Script script = InvokerHelper.createScript(scriptClass, new Binding(bindingVariables));
    assertEquals(bindingVariables, script.getBinding().getVariables());
    script.run();
    assertEquals(controlValue, script.getProperty(controlProperty));
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) Binding(groovy.lang.Binding) Script(groovy.lang.Script) GroovyCodeSource(groovy.lang.GroovyCodeSource)

Example 2 with GroovyCodeSource

use of groovy.lang.GroovyCodeSource in project groovy by apache.

the class Demo method compile.

protected GroovyObject compile(String fileName) throws Exception {
    Class groovyClass = loader.parseClass(new GroovyCodeSource(new File(fileName)));
    GroovyObject object = (GroovyObject) groovyClass.newInstance();
    assertTrue(object != null);
    return object;
}
Also used : GroovyCodeSource(groovy.lang.GroovyCodeSource) File(java.io.File) GroovyObject(groovy.lang.GroovyObject)

Example 3 with GroovyCodeSource

use of groovy.lang.GroovyCodeSource in project gradle by gradle.

the class DefaultScriptCompilationHandler method compileScript.

private void compileScript(final ScriptSource source, ClassLoader classLoader, CompilerConfiguration configuration, File metadataDir, final CompileOperation<?> extractingTransformer, final Action<? super ClassNode> customVerifier) {
    final Transformer transformer = extractingTransformer != null ? extractingTransformer.getTransformer() : null;
    logger.info("Compiling {} using {}.", source.getDisplayName(), transformer != null ? transformer.getClass().getSimpleName() : "no transformer");
    final EmptyScriptDetector emptyScriptDetector = new EmptyScriptDetector();
    final PackageStatementDetector packageDetector = new PackageStatementDetector();
    GroovyClassLoader groovyClassLoader = new GroovyClassLoader(classLoader, configuration, false) {

        @Override
        protected CompilationUnit createCompilationUnit(CompilerConfiguration compilerConfiguration, CodeSource codeSource) {
            CompilationUnit compilationUnit = new CustomCompilationUnit(compilerConfiguration, codeSource, customVerifier, this);
            if (transformer != null) {
                transformer.register(compilationUnit);
            }
            compilationUnit.addPhaseOperation(packageDetector, Phases.CANONICALIZATION);
            compilationUnit.addPhaseOperation(emptyScriptDetector, Phases.CANONICALIZATION);
            return compilationUnit;
        }
    };
    groovyClassLoader.setResourceLoader(NO_OP_GROOVY_RESOURCE_LOADER);
    String scriptText = source.getResource().getText();
    String scriptName = source.getClassName();
    GroovyCodeSource codeSource = new GroovyCodeSource(scriptText == null ? "" : scriptText, scriptName, "/groovy/script");
    try {
        try {
            groovyClassLoader.parseClass(codeSource, false);
        } catch (MultipleCompilationErrorsException e) {
            wrapCompilationFailure(source, e);
        } catch (CompilationFailedException e) {
            throw new GradleException(String.format("Could not compile %s.", source.getDisplayName()), e);
        }
        if (packageDetector.hasPackageStatement) {
            throw new UnsupportedOperationException(String.format("%s should not contain a package statement.", StringUtils.capitalize(source.getDisplayName())));
        }
        serializeMetadata(source, extractingTransformer, metadataDir, emptyScriptDetector.isEmptyScript(), emptyScriptDetector.getHasMethods());
    } finally {
        ClassLoaderUtils.tryClose(groovyClassLoader);
    }
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) Transformer(org.gradle.groovy.scripts.Transformer) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) GroovyCodeSource(groovy.lang.GroovyCodeSource) CodeSource(java.security.CodeSource) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyCodeSource(groovy.lang.GroovyCodeSource) GradleException(org.gradle.api.GradleException) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException)

Example 4 with GroovyCodeSource

use of groovy.lang.GroovyCodeSource in project groovy-core by groovy.

the class Demo method compile.

protected GroovyObject compile(String fileName) throws Exception {
    Class groovyClass = loader.parseClass(new GroovyCodeSource(new File(fileName)));
    GroovyObject object = (GroovyObject) groovyClass.newInstance();
    assertTrue(object != null);
    return object;
}
Also used : GroovyCodeSource(groovy.lang.GroovyCodeSource) File(java.io.File) GroovyObject(groovy.lang.GroovyObject)

Example 5 with GroovyCodeSource

use of groovy.lang.GroovyCodeSource in project groovy-core by groovy.

the class SecurityTestSupport method assertExecute.

/*
      * Execute the groovy script contained in file.  If missingPermission
      * is non-null, then this invocation expects an AccessControlException with missingPermission
      * as the reason.  If missingPermission is null, the script is expected to execute successfully.
      */
protected void assertExecute(File file, Permission missingPermission) {
    if (!isSecurityAvailable()) {
        return;
    }
    GroovyCodeSource gcs = null;
    try {
        gcs = new GroovyCodeSource(file);
    } catch (IOException fnfe) {
        fail(fnfe.toString());
    }
    parseAndExecute(gcs, missingPermission);
}
Also used : GroovyCodeSource(groovy.lang.GroovyCodeSource) IOException(java.io.IOException)

Aggregations

GroovyCodeSource (groovy.lang.GroovyCodeSource)7 GroovyClassLoader (groovy.lang.GroovyClassLoader)3 File (java.io.File)3 Binding (groovy.lang.Binding)2 GroovyObject (groovy.lang.GroovyObject)2 Script (groovy.lang.Script)2 Initializer (hudson.init.Initializer)1 IOException (java.io.IOException)1 URL (java.net.URL)1 CodeSource (java.security.CodeSource)1 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)1 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)1 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)1 MultipleCompilationErrorsException (org.codehaus.groovy.control.MultipleCompilationErrorsException)1 GradleException (org.gradle.api.GradleException)1 Transformer (org.gradle.groovy.scripts.Transformer)1