Search in sources :

Example 1 with ClassBodyEvaluator

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();
}
Also used : Scanner(org.codehaus.janino.Scanner) StringReader(java.io.StringReader) ClassBodyEvaluator(org.codehaus.janino.ClassBodyEvaluator)

Example 2 with ClassBodyEvaluator

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;
    }
}
Also used : ScanException(org.codehaus.janino.Scanner.ScanException) ClassBodyEvaluator(org.codehaus.janino.ClassBodyEvaluator) CompileException(org.codehaus.janino.CompileException) ParseException(org.codehaus.janino.Parser.ParseException)

Example 3 with ClassBodyEvaluator

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;
}
Also used : Scanner(org.codehaus.janino.Scanner) StringReader(java.io.StringReader) ClassBodyEvaluator(org.codehaus.janino.ClassBodyEvaluator) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with ClassBodyEvaluator

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);
    }
}
Also used : Scanner(org.codehaus.janino.Scanner) Function1(org.apache.calcite.linq4j.function.Function1) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) StringReader(java.io.StringReader) ClassBodyEvaluator(org.codehaus.janino.ClassBodyEvaluator) CompileException(org.codehaus.commons.compiler.CompileException)

Aggregations

ClassBodyEvaluator (org.codehaus.janino.ClassBodyEvaluator)4 StringReader (java.io.StringReader)3 Scanner (org.codehaus.janino.Scanner)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Function1 (org.apache.calcite.linq4j.function.Function1)1 CompileException (org.codehaus.commons.compiler.CompileException)1 CompileException (org.codehaus.janino.CompileException)1 ParseException (org.codehaus.janino.Parser.ParseException)1 ScanException (org.codehaus.janino.Scanner.ScanException)1