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;
}
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;
}
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));
}
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);
}
}
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);
}
Aggregations