/** * typecheck and then compile this rule unless either action has been tried before * * @return true if the rule successfully type checks and then compiles under this call or a * previous call or false if either operation has previously failed or fails under this call. */ private synchronized boolean ensureTypeCheckedCompiled() { if (checkFailed) { return false; } if (!checked) { // ensure we don't trigger any code inside the type check or compile // n.b. we may still allow recursive triggering while executing boolean triggerEnabled = false; String detail = ""; try { typeCheck(); compile(); checked = true; installed(); } catch (TypeWarningException te) { checkFailed = true; if (Transformer.isVerbose()) { StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); writer.println( "Rule.ensureTypeCheckedCompiled : warning type checking rule " + getName()); te.printStackTrace(writer); detail = stringWriter.toString(); System.out.println(detail); } } catch (TypeException te) { checkFailed = true; StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); writer.println("Rule.ensureTypeCheckedCompiled : error type checking rule " + getName()); te.printStackTrace(writer); detail = stringWriter.toString(); System.out.println(detail); } catch (CompileException ce) { checkFailed = true; StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); writer.println("Rule.ensureTypeCheckedCompiled : error compiling rule " + getName()); ce.printStackTrace(writer); detail = stringWriter.toString(); System.out.println(detail); } ruleScript.recordCompile(triggerClass, loader, !checkFailed, detail); return !checkFailed; } return true; }
public void testStrictTypingCompilationWithVarInsideConstructor() { ParserContext ctx = new ParserContext(); ctx.addInput("$likes", String.class); ctx.addInput("results", List.class); ctx.addImport(Cheese.class); ctx.setStrongTyping(true); Serializable expr = null; try { expr = MVEL.compileExpression("Cheese c = new Cheese( $likes, 15 );\nresults.add( c ); ", ctx); } catch (CompileException e) { e.printStackTrace(); fail("This should not fail:\n" + e.getMessage()); } List results = new ArrayList(); Map vars = new HashMap(); vars.put("$likes", "stilton"); vars.put("results", results); executeExpression(expr, vars); assertEquals(new Cheese("stilton", 15), results.get(0)); }