Refine search
public static Object eval(String str, Map<String, Object> vars) { ParserConfiguration pconf = new ParserConfiguration(); pconf.addPackageImport("org.kie.internal.task.api.model"); pconf.addPackageImport("org.jbpm.services.task"); pconf.addPackageImport("org.jbpm.services.task.impl.model"); pconf.addPackageImport("org.jbpm.services.task.query"); pconf.addPackageImport("org.jbpm.services.task.internals.lifecycle"); pconf.addImport(Status.class); pconf.addImport(Allowed.class); pconf.addPackageImport("java.util"); ParserContext context = new ParserContext(pconf); Serializable s = MVEL.compileExpression(str.trim(), context); if (vars != null) { return MVELSafeHelper.getEvaluator().executeExpression(s, vars); } else { return MVELSafeHelper.getEvaluator().executeExpression(s); } }
@Override protected ParserConfiguration getParserConfiguration(InternalWorkingMemory workingMemory) { ParserConfiguration parserConfiguration = new ParserConfiguration(); parserConfiguration.addImport(Cheese.class); return parserConfiguration; }
public void testAssignmentOnTwoDimensionalArrayUsingIndexedInput() { ParserConfiguration conf = new ParserConfiguration(); ParserContext pctx = new ParserContext( conf ); pctx.setIndexAllocation( true ); pctx.setStrictTypeEnforcement(true); pctx.setStrongTyping(true); pctx.addInput("array", Double[][].class); pctx.addIndexedInput("array"); Map vars = new HashMap() {{ put("array", new Double[2][2]); }}; assertEquals(42.0, MVEL.executeExpression(MVEL.compileExpression("array[1][1] = 42.0;\narray[1][1]", pctx), vars)); } }
public FactFieldValueVerifier(Map<String, Object> populatedData, String factName, Object factObject, final TypeResolver resolver, final ClassLoader classLoader) { this.populatedData = populatedData; this.factName = factName; this.factObject = factObject; this.resolver = resolver; this.pconf = new ParserConfiguration(); pconf.setClassLoader(classLoader); this.pctx = new ParserContext(pconf); pctx.setStrongTyping(true); }
public void testStaticMethodsInInputsBug() { String text = " getList( java.util.Formatter )"; ParserConfiguration pconf = new ParserConfiguration(); for (Method m : CoreConfidenceTests.StaticMethods.class.getMethods()) { if (Modifier.isStatic(m.getModifiers())) { pconf.addImport(m.getName(), m); } } ParserContext pctx = new ParserContext(pconf); pctx.setStrictTypeEnforcement(false); pctx.setStrongTyping(false); Map<String, Object> vars = new HashMap<String, Object>(); Serializable expr = MVEL.compileExpression(text, pctx); List list = (List) MVEL.executeExpression(expr, null, vars); assertEquals(Formatter.class, list.get(0)); assertEquals(0, pctx.getInputs().size()); }
public void testNestedEnumFromJar() throws ClassNotFoundException, SecurityException, NoSuchFieldException { String expr = "EventRequest.Status.ACTIVE"; // creating a classloader for the jar URL resource = getClass().getResource("/eventing-example.jar"); assertNotNull(resource); URLClassLoader loader = new URLClassLoader(new URL[]{resource}, getClass().getClassLoader()); // loading the class to prove it works Class<?> er = loader.loadClass("org.drools.examples.eventing.EventRequest"); assertNotNull(er); assertEquals("org.drools.examples.eventing.EventRequest", er.getCanonicalName()); // getting the value of the enum to prove it works: Class<?> st = er.getDeclaredClasses()[0]; assertNotNull(st); Field active = st.getField("ACTIVE"); assertNotNull(active); // now, trying with MVEL ParserConfiguration pconf = new ParserConfiguration(); pconf.setClassLoader(loader); pconf.addImport(er); ParserContext pctx = new ParserContext(pconf); pctx.setStrongTyping(true); Serializable compiled = MVEL.compileExpression(expr, pctx); Object result = MVEL.executeExpression(compiled); assertNotNull(result); }
public void testGetCorrectInputs() { String str = "total = total + $cheese.price"; ParserConfiguration pconf = new ParserConfiguration(); ParserContext pctx = new ParserContext(pconf); pctx.setStrongTyping(true); pctx.addInput("total", int.class); pctx.addInput("$cheese", Cheese.class); ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx); assertTrue("Should not contain" + pctx.getVariables(), pctx.getVariables().isEmpty()); }
public void testEmptyConstructorWithSpace() throws Exception { ParserConfiguration pconf = new ParserConfiguration(); pconf.addImport("getString", StaticClassWithStaticMethod.class.getMethod("getString", null)); String text = "getString( )"; ParserContext pctx = new ParserContext(pconf); pctx.setStrongTyping(true); pctx.setStrictTypeEnforcement(true); MVEL.compileExpression(text, pctx); }
ParserConfiguration conf = data.getParserConfiguration(); conf.setClassLoader( context.getPackageBuilder().getRootClassLoader() ); final ParserContext parserContext1 = new ParserContext( conf ); if ( localTypes != null ) { for ( Entry entry : localTypes.entrySet() ) { parserContext1.addInput( (String) entry.getKey(), (Class) entry.getValue() ); parserContext1.addInput( "this", availableIdentifiers.getThisClass() ); returnType = MVEL.analyze( expr, parserContext1 ); } catch ( Exception e ) { returnType = MVEL.analyze( expr, parserContext2 ); typesafe = true;
ParserConfiguration conf = data.getParserConfiguration(); conf.setClassLoader( context.getPackageBuilder().getRootClassLoader() ); final ParserContext pctx = new ParserContext( conf ); pctx.setStrictTypeEnforcement(false); pctx.setStrongTyping( false ); pctx.addInput( "this", thisClass ); MVEL.analysisCompile( expr, pctx ); } catch (Exception e) {
public void testMethodReturningPrimitiveTypeAnalysis() { String str = "value"; ParserConfiguration pconf = new ParserConfiguration(); ParserContext pctx = new ParserContext(pconf); pctx.addInput("this", MyObj.class); pctx.setStrongTyping(true); Class<?> returnType = MVEL.analyze(str, pctx); assertEquals(long.class, returnType); }
public void testUnwantedImport() { ParserConfiguration conf = new ParserConfiguration(); conf.addPackageImport("java.util"); conf.addPackageImport("org.mvel2.tests.core.res"); ParserContext pctx = new ParserContext( conf ); MVEL.analysisCompile( "ScenarioType.Set.ADD", pctx ); assertNull(conf.getImports().get("Set")); }
private ParserConfiguration getParserConfiguration() { ParserConfiguration parserConfig = new ParserConfiguration(); parserConfig.addPackageImport("java.util"); parserConfig.addPackageImport("org.apache.commons.lang3"); parserConfig.addPackageImport("org.joda.time"); parserConfig.addImport(Math.class); if (imports != null) { for (String imported : imports) parserConfig.addPackageImport(imported); } return parserConfig; }
/** * Adds a static import for the specified {@link MethodStub} with an alias. * * @param name The alias to use * @param method The instance of <tt>Method</tt> which represents the static import. * @see #addImport(String, org.mvel2.util.MethodStub) */ public void addImport(String name, MethodStub method) { parserConfiguration.addImport(name, method); }
this.parserConfiguration = new ParserConfiguration(); this.parserConfiguration.setImports( this.imports ); this.parserConfiguration.setPackageImports( this.packageImports ); this.parserConfiguration.setClassLoader( classLoader );
/** * Adds a package import to a parse session. * * @param packageName A fully qualified package (eg. <tt>java.util.concurrent</tt>). */ public void addPackageImport(String packageName) { parserConfiguration.addPackageImport(packageName); }
public ClassImportResolverFactory(ParserConfiguration pCfg, VariableResolverFactory nextFactory, boolean compiled) { if ( pCfg != null ) { if (!compiled) { packageImports = pCfg.getPackageImports(); } classLoader = pCfg.getClassLoader(); imports = Collections.unmodifiableMap(pCfg.getImports()); } else { classLoader = Thread.currentThread().getContextClassLoader(); } this.nextFactory = nextFactory; }
/** * This is such a horrible hack, but it's more performant than any other horrible hack I can think of * right now. * * @param expr * @param cursor * @param pCtx */ public static void checkForPossibleUnresolvedViolations(char[] expr, int cursor, ParserContext pCtx) { if (isUnresolvedWaiting()) { LinkedHashMap<String, Object> imports = (LinkedHashMap<String, Object>) pCtx.getParserConfiguration().getImports(); Object o = imports.values().toArray()[imports.size() - 1]; if (o instanceof Proto) { Proto proto = (Proto) o; int last = proto.getCursorEnd(); cursor--; /** * We walk backwards to ensure that the last valid statement was a proto declaration. */ while (cursor > last && ParseTools.isWhitespace(expr[cursor])) cursor--; while (cursor > last && ParseTools.isIdentifierPart(expr[cursor])) cursor--; while (cursor > last && (ParseTools.isWhitespace(expr[cursor]) || expr[cursor] == ';')) cursor--; if (cursor != last) { throw new CompileException("unresolved reference (possible illegal forward-reference?): " + ProtoParser.getNextUnresolvedWaiting(), expr, proto.getCursorStart()); } } } }
initVariableVisibility(); pushVariableScope(); Set<String> scope = getVariableScope(); scope.addAll( parserConfiguration.getImports().keySet() );
ParserContext pCtx = (ParserContext)node.getLiteralValue(); Class<?> thisClass = pCtx.getInputs().get("this"); try { return new EvaluatedExpression(new MethodInvocation(thisClass.getMethod(variableName))); if (node.getEgressType() == Class.class) { Class<?> classLiteral = pCtx.getParserConfiguration().getImport(variableName); if (classLiteral != null) { return new FixedExpression(Class.class, classLiteral);