Search in sources :

Example 36 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project zuul by Netflix.

the class GroovyCompiler method compile.

/**
 * Compiles Groovy code and returns the Class of the compiles code.
 */
public Class<?> compile(String sCode, String sName) {
    GroovyClassLoader loader = getGroovyClassLoader();
    LOG.warn("Compiling filter: " + sName);
    Class<?> groovyClass = loader.parseClass(sCode, sName);
    return groovyClass;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader)

Example 37 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project atlas by alibaba.

the class ClosureFactory method buildClosure.

public static Closure<?> buildClosure(String... strings) throws IOException {
    Closure<?> closure = null;
    // Create a method returning a closure
    StringBuilder sb = new StringBuilder("def closure() { { script -> ");
    sb.append(StringUtils.join(strings, "\n"));
    sb.append(" } }");
    // Create an anonymous class for the method
    GroovyClassLoader loader = new GroovyClassLoader();
    Class<?> groovyClass = loader.parseClass(sb.toString());
    try {
        // Create an instance of the class
        GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
        // Invoke the object's method and thus obtain the closure
        closure = (Closure<?>) groovyObject.invokeMethod("closure", null);
    } catch (Throwable e) {
        throw new RuntimeException(e);
    } finally {
        loader.close();
    }
    return closure;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyObject(groovy.lang.GroovyObject)

Example 38 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project fastjson by alibaba.

the class GroovyTest method test_groovy.

public void test_groovy() throws Exception {
    ClassLoader parent = Thread.currentThread().getContextClassLoader();
    GroovyClassLoader loader = new GroovyClassLoader(parent);
    // A类
    Class AClass = loader.parseClass(// 
    "class A {\n" + // 
    "    int id\n" + "}");
    // A实例
    GroovyObject a = (GroovyObject) AClass.newInstance();
    a.setProperty("id", 33);
    String textA = JSON.toJSONString(a);
    GroovyObject aa = (GroovyObject) JSON.parseObject(textA, AClass);
    Assert.assertEquals(a.getProperty("id"), aa.getProperty("id"));
    System.out.println(a);
    // B类,继承于A
    Class BClass = loader.parseClass(// 
    "class B extends A {\n" + // 
    "    String name\n" + "}");
    // B实例
    GroovyObject b = (GroovyObject) BClass.newInstance();
    b.setProperty("name", "jobs");
    String textB = JSON.toJSONString(b);
    GroovyObject bb = (GroovyObject) JSON.parseObject(textB, BClass);
    Assert.assertEquals(b.getProperty("id"), bb.getProperty("id"));
    Assert.assertEquals(b.getProperty("name"), bb.getProperty("name"));
    // 序列化失败
    System.out.println(JSON.toJSONString(b, true));
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyObject(groovy.lang.GroovyObject)

Example 39 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project DataX by alibaba.

the class GroovyTransformer method initGroovyTransformer.

private void initGroovyTransformer(String code, List<String> extraPackage) {
    GroovyClassLoader loader = new GroovyClassLoader(GroovyTransformer.class.getClassLoader());
    String groovyRule = getGroovyRule(code, extraPackage);
    Class groovyClass;
    try {
        groovyClass = loader.parseClass(groovyRule);
    } catch (CompilationFailedException cfe) {
        throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, cfe);
    }
    try {
        Object t = groovyClass.newInstance();
        if (!(t instanceof Transformer)) {
            throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, "datax bug! contact askdatax");
        }
        this.groovyTransformer = (Transformer) t;
    } catch (Throwable ex) {
        throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, ex);
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) Transformer(com.alibaba.datax.transformer.Transformer) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException)

Example 40 with GroovyClassLoader

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

the class ClassNodeResolver method tryAsLoaderClassOrScript.

/**
 * This method is used to realize the lookup of a class using the compilation
 * unit class loader. Should no class be found we fall back to a script lookup.
 * If a class is found we check if there is also a script and maybe use that
 * one in case it is newer.<p/>
 *
 * Two class search strategies are possible: by ASM decompilation or by usual Java classloading.
 * The latter is slower but is unavoidable for scripts executed in dynamic environments where
 * the referenced classes might only be available in the classloader, not on disk.
 */
private LookupResult tryAsLoaderClassOrScript(final String name, final CompilationUnit compilationUnit) {
    GroovyClassLoader loader = compilationUnit.getClassLoader();
    Map<String, Boolean> options = compilationUnit.configuration.getOptimizationOptions();
    if (!Boolean.FALSE.equals(options.get("asmResolving"))) {
        LookupResult result = findDecompiled(name, compilationUnit, loader);
        if (result != null) {
            return result;
        }
    }
    if (!Boolean.FALSE.equals(options.get("classLoaderResolving"))) {
        return findByClassLoading(name, compilationUnit, loader);
    }
    return tryAsScript(name, compilationUnit, null);
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)136 File (java.io.File)28 GroovyObject (groovy.lang.GroovyObject)19 Test (org.junit.jupiter.api.Test)18 IOException (java.io.IOException)15 HashMap (java.util.HashMap)15 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)14 Map (java.util.Map)12 Binding (groovy.lang.Binding)10 URL (java.net.URL)10 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)10 BuildException (org.apache.tools.ant.BuildException)9 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)9 ClassNode (org.codehaus.groovy.ast.ClassNode)9 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)9 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)8 GrailsApplication (grails.core.GrailsApplication)8 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)8 ArtefactHandler (grails.core.ArtefactHandler)7 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)7