ClassBodyEvaluator cbe = new ClassBodyEvaluator(); if ( clsloader == null ) { cbe.setParentClassLoader( Thread.currentThread().getContextClassLoader() ); } else { cbe.setParentClassLoader( clsloader ); cbe.setClassName( def.getClassName() ); cbe.setExtendedType( TransformClassBase.class ); sr = new StringReader( def.getTransformedSource() ); } else { 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;
data.expressionEvaluator = new ExpressionEvaluator(); data.expressionEvaluator.setParameters( parameterNames.toArray( new String[parameterNames.size()] ), parameterTypes .toArray( new Class<?>[parameterTypes.size()] ) ); data.expressionEvaluator.setReturnType( Object.class ); data.expressionEvaluator.setThrownExceptions( new Class<?>[] { Exception.class } ); data.expressionEvaluator.cook( realCondition ); Object formulaResult = data.expressionEvaluator.evaluate( data.argumentData );
public Condition build(String script) throws IllegalAccessException, CompileException, InstantiationException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setImplementedInterfaces(new Class[]{Condition.class}); cbe.setExtendedClass(PropertyWrapperForScripts.class); cbe.setParentClassLoader(ClassBodyEvaluator.class.getClassLoader()); cbe.cook(SCRIPT_PREFIX + script + SCRIPT_SUFFIX); Class<?> clazz = cbe.getClazz(); Condition instance = (Condition) clazz.newInstance(); Method setMapMethod = clazz.getMethod("setPropertyContainers", PropertyContainer.class, PropertyContainer.class); setMapMethod.invoke(instance, localPropContainer, context); return instance; }
private ClassFile[] doCompile(final String sourceCode) throws CompileException, IOException, ClassNotFoundException { StringReader reader = new StringReader(sourceCode); Scanner scanner = new Scanner((String) null, reader); Java.CompilationUnit compilationUnit = new Parser(scanner).parseCompilationUnit(); return new UnitCompiler(compilationUnit, compilationClassLoader) .compileUnit(this.debug, this.debug, this.debug); }
private ExpressionEvaluator cookBooleanExpression(String expression) throws CompileException { ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setParameters(new String[] { "ride", "watermark" }, new Class[] { TaxiRide.class, long.class }); ee.setExpressionType(boolean.class); ee.cook(expression); return ee; }
@SuppressWarnings("unchecked") protected Collection<String> getMappings(ServletConfig config) throws CompileException, InvocationTargetException { ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator( "config.getServletContext().getServletRegistration(config.getServletName()).getMappings()", Collection.class, new String[]{"config"}, new Class[]{ServletConfig.class}); return (Collection<String>) expressionEvaluator.evaluate(new Object[]{config}); }
@Override public void start() { try { assert context != null; scriptEvaluator = new ScriptEvaluator(getDecoratedExpression(), EXPRESSION_TYPE, getParameterNames(), getParameterTypes(), THROWN_EXCEPTIONS); super.start(); } catch (Exception e) { addError( "Could not start evaluator with expression [" + expression + "]", e); } }
public boolean evaluate(E event) throws EvaluationException { if (!isStarted()) { throw new IllegalStateException("Evaluator [" + name + "] was called in stopped state"); } try { Boolean result = (Boolean) scriptEvaluator.evaluate(getParameterValues(event)); return result.booleanValue(); } catch (Exception ex) { errorCount++; if (errorCount >= ERROR_THRESHOLD) { stop(); } throw new EvaluationException("Evaluator [" + name + "] caused an exception", ex); } }
private boolean evaluateBooleanExpression(ExpressionEvaluator ee, TaxiRide ride, long watermark) throws InvocationTargetException { boolean result= false; if (ee != null) { result = (boolean) ee.evaluate(new Object[] { ride, watermark }); } return result; } }
protected Java.Rvalue generateGetInvocation(Class type, int index) throws CompileException { return new Java.MethodInvocation(loc, new Java.AmbiguousName(loc, new String[] {"unsafe"}), "get" + accessorForType(type), new Java.Rvalue[] { new Java.BinaryOperation(loc, new Java.AmbiguousName(loc, new String[] {"address"}), "+", new Java.IntegerLiteral(loc, Integer.toString(layout[index]))) } ); }
@Override protected Java.Rvalue generateSetInvocation(Class type, int index, String value) throws CompileException { return new Java.Assignment(loc, new Java.FieldAccessExpression(loc, new Java.ThisReference(loc), fieldNames[index]), "=", new Java.Cast(loc, classToRefType(fieldTypes[index]), new Java.AmbiguousName(loc, new String[] {value}))); } }
protected Java.SwitchStatement.SwitchBlockStatementGroup generateDefaultCase() { return new Java.SwitchStatement.SwitchBlockStatementGroup( loc, Lists.<Java.Rvalue>newArrayList(), true, Lists.<Java.BlockStatement>newArrayList( new Java.ThrowStatement( loc, new Java.NewClassInstance( loc, null, new Java.ReferenceType(loc, new String[] {"IllegalArgumentException"}, null), new Java.Rvalue[0])) )); }
@Override public Java.BlockStatement copyReturnStatement(Java.ReturnStatement subject) { return new Java.BreakStatement(subject.getLocation(), returnLabel); } };
@Override protected Java.Rvalue generateGetInvocation(Class type, int index) throws CompileException { return new Java.FieldAccessExpression(loc, new Java.ThisReference(loc), fieldNames[index]); }
private Java.BlockStatement maybeGenerateReturn(Location loc, Java.Rvalue statement) throws Exception { if (returnType.equals(Void.TYPE)) { return new Java.ExpressionStatement(statement); } else { return new Java.ReturnStatement(loc, statement); } }
/** * Unparse the given {@link Java.CompilationUnit} to the given {@link Writer}. */ public static void unparse(Java.CompilationUnit cu, Writer w) { UnparseVisitor.unparse(cu, w); }
/** * Testing of parsing/unparsing. * <p> * Reads compilation units from the files named on the command line * and unparses them to {@link System#out}. */ public static void main(String[] args) throws Exception { UnparseVisitor.main(args); }
data.expressionEvaluators[m] = new ExpressionEvaluator(); data.expressionEvaluators[m].setParameters( parameterNames.toArray( new String[parameterNames.size()] ), parameterTypes .toArray( new Class<?>[parameterTypes.size()] ) ); data.expressionEvaluators[m].setReturnType( Object.class ); data.expressionEvaluators[m].setThrownExceptions( new Class<?>[] { Exception.class } ); data.expressionEvaluators[m].cook( fn.getFormula() ); } else { throw new KettleException( "Unable to find field name for formula [" Object formulaResult = data.expressionEvaluators[i].evaluate( argumentData );
private ExpressionEvaluator cookBooleanExpression(String expression) throws CompileException { ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setParameters(new String[] { "ride", "watermark" }, new Class[] { TaxiRide.class, long.class }); ee.setExpressionType(boolean.class); ee.cook(expression); return ee; }
protected Java.Rvalue generateSetInvocation(Class type, int index, String value) throws CompileException { return new Java.MethodInvocation(loc, new Java.AmbiguousName(loc, new String[] {"unsafe"}), "put" + accessorForType(type), new Java.Rvalue[]{ new Java.BinaryOperation(loc, new Java.AmbiguousName(loc, new String[]{"address"}), "+", new Java.IntegerLiteral(loc, Integer.toString(layout[index]))), new Java.Cast(loc, classToRefType(type), new Java.AmbiguousName(loc, new String[] {value})) } ); }