use of org.codehaus.janino.ClassBodyEvaluator in project pentaho-kettle by pentaho.
the class UserDefinedJavaClassMeta method cookClass.
private Class<?> cookClass(UserDefinedJavaClassDef def) throws CompileException, ParseException, ScanException, IOException, RuntimeException, KettleStepException {
if (Thread.currentThread().getContextClassLoader() == null) {
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
}
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.setParentClassLoader(Thread.currentThread().getContextClassLoader());
cbe.setClassName(def.getClassName());
StringReader sr;
if (def.isTransformClass()) {
cbe.setExtendedType(TransformClassBase.class);
sr = new StringReader(def.getTransformedSource());
} else {
sr = new StringReader(def.getSource());
}
cbe.setDefaultImports(new String[] { "org.pentaho.di.trans.steps.userdefinedjavaclass.*", "org.pentaho.di.trans.step.*", "org.pentaho.di.core.row.*", "org.pentaho.di.core.*", "org.pentaho.di.core.exception.*" });
cbe.cook(new Scanner(null, sr));
return cbe.getClazz();
}
use of org.codehaus.janino.ClassBodyEvaluator in project joons-renderer by joonhyublee.
the class Plugins method registerPlugin.
/**
* Define a new plugin type from java source code. The code string contains
* import declarations and a class body only. The implemented type is
* implicitly the one of the plugin list being registered against.If the
* plugin type name was previously associated with a different class, it
* will be overriden. This allows the behavior core classes to be modified
* at runtime.
*
* @param name plugin type name
* @param sourceCode Java source code definition for the plugin
* @return <code>true</code> if the code compiled and registered
* successfully, <code>false</code> otherwise
*/
@SuppressWarnings("unchecked")
public boolean registerPlugin(String name, String sourceCode) {
try {
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.setClassName(name);
if (baseClass.isInterface()) {
cbe.setImplementedTypes(new Class[] { baseClass });
} else {
cbe.setExtendedType(baseClass);
}
cbe.cook(sourceCode);
return registerPlugin(name, cbe.getClazz());
} catch (CompileException e) {
UI.printError(Module.API, "Plugin \"%s\" could not be declared - %s", name, e.getLocalizedMessage());
return false;
} catch (ParseException e) {
UI.printError(Module.API, "Plugin \"%s\" could not be declared - %s", name, e.getLocalizedMessage());
return false;
} catch (ScanException e) {
UI.printError(Module.API, "Plugin \"%s\" could not be declared - %s", name, e.getLocalizedMessage());
return false;
}
}
use of org.codehaus.janino.ClassBodyEvaluator in project pentaho-kettle by pentaho.
the class UserDefinedJavaClassMeta method cookClass.
@VisibleForTesting
Class<?> cookClass(UserDefinedJavaClassDef def, ClassLoader clsloader) throws CompileException, IOException, RuntimeException, KettleStepException {
String checksum = def.getChecksum();
Class<?> rtn = UserDefinedJavaClassMeta.classCache.getIfPresent(checksum);
if (rtn != null) {
return rtn;
}
if (Thread.currentThread().getContextClassLoader() == null) {
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
}
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
if (clsloader == null) {
cbe.setParentClassLoader(Thread.currentThread().getContextClassLoader());
} else {
cbe.setParentClassLoader(clsloader);
}
cbe.setClassName(def.getClassName());
StringReader sr;
if (def.isTransformClass()) {
cbe.setExtendedType(TransformClassBase.class);
sr = new StringReader(def.getTransformedSource());
} else {
sr = new StringReader(def.getSource());
}
cbe.setDefaultImports(new String[] { "org.pentaho.di.trans.steps.userdefinedjavaclass.*", "org.pentaho.di.trans.step.*", "org.pentaho.di.core.row.*", "org.pentaho.di.core.*", "org.pentaho.di.core.exception.*" });
cbe.cook(new Scanner(null, sr));
rtn = cbe.getClazz();
UserDefinedJavaClassMeta.classCache.put(checksum, rtn);
return rtn;
}
use of org.codehaus.janino.ClassBodyEvaluator in project calcite by apache.
the class RexExecutable method compile.
private static Function1<DataContext, Object[]> compile(String code, Object reason) {
try {
final ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.setClassName(GENERATED_CLASS_NAME);
cbe.setExtendedClass(Utilities.class);
cbe.setImplementedInterfaces(new Class[] { Function1.class, Serializable.class });
cbe.setParentClassLoader(RexExecutable.class.getClassLoader());
cbe.cook(new Scanner(null, new StringReader(code)));
Class c = cbe.getClazz();
// noinspection unchecked
final Constructor<Function1<DataContext, Object[]>> constructor = c.getConstructor();
return constructor.newInstance();
} catch (CompileException | IOException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("While compiling " + reason, e);
}
}
Aggregations