Search in sources :

Example 21 with CompiledExpression

use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.

the class GenericsTypeInferenceTest method testInferLastTypeParametersFromPropertyMethod.

public final void testInferLastTypeParametersFromPropertyMethod() {
    ParserContext context = new ParserContext();
    context.setStrongTyping(true);
    context.addInput("a", A.class);
    final CompiledExpression compiledExpression = new ExpressionCompiler("a.getFooMap()[\"key\"].someMethod()").compile(context);
    final Object val = MVEL.executeExpression(compiledExpression, new AWrapper());
    assertEquals("Expression did not evaluate correctly: ", "bar", val);
    assertNotNull("No type parameters detected", context.getLastTypeParameters());
    assertEquals("Wrong parametric type inferred", String.class, context.getLastTypeParameters()[0]);
}
Also used : ParserContext(org.mvel2.ParserContext)

Example 22 with CompiledExpression

use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.

the class GenericsTypeInferenceTest method testInferLastTypeParametersFromProperty.

public final void testInferLastTypeParametersFromProperty() {
    ParserContext context = new ParserContext();
    context.setStrongTyping(true);
    context.addInput("a", A.class);
    final CompiledExpression compiledExpression = new ExpressionCompiler("a.strings").compile(context);
    final Object val = MVEL.executeExpression(compiledExpression, new AWrapper());
    assertTrue("Expression did not evaluate correctly: " + val, STRINGS.equals(val));
    assertTrue("No type parameters detected", null != context.getLastTypeParameters());
    assertTrue("Wrong parametric type inferred", String.class.equals(context.getLastTypeParameters()[0]));
}
Also used : ParserContext(org.mvel2.ParserContext)

Example 23 with CompiledExpression

use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.

the class CoreConfidenceTests method testTestIntToLong.

public void testTestIntToLong() {
    String s = "1+(long)a";
    ParserContext pc = new ParserContext();
    pc.addInput("a", Integer.class);
    ExpressionCompiler compiler = new ExpressionCompiler(s, pc);
    CompiledExpression expr = compiler.compile();
    Map vars = new HashMap();
    vars.put("a", 1);
    Object r = ((ExecutableStatement) expr).getValue(null, new MapVariableResolverFactory(vars));
    assertEquals(new Long(2), r);
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 24 with CompiledExpression

use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.

the class ArithmeticTests method testJIRA161.

public void testJIRA161() {
    Serializable s = MVEL.compileExpression("1==-(-1)", ParserContext.create().stronglyTyped());
    assertEquals(1 == -(-1), MVEL.executeExpression(s));
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    CompiledExpression compiledExpression = new ExpressionCompiler("1==-(-1)").compile(ctx);
    assertEquals(1 == -(-1), MVEL.executeExpression(compiledExpression));
}
Also used : Serializable(java.io.Serializable) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 25 with CompiledExpression

use of org.mvel2.compiler.CompiledExpression in project mvel by mikebrock.

the class ArithmeticTests method testStrongTypingModeComparison.

public void testStrongTypingModeComparison() {
    ParserContext parserContext = new ParserContext();
    parserContext.setStrongTyping(true);
    parserContext.addInput("a", Long.class);
    CompiledExpression compiledExpression = new ExpressionCompiler("a==0").compile(parserContext);
    HashMap<String, Object> variables = new HashMap<String, Object>();
    variables.put("a", 0l);
    MVEL.executeExpression(compiledExpression, variables);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Aggregations

CompiledExpression (org.mvel2.compiler.CompiledExpression)38 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)34 ParserContext (org.mvel2.ParserContext)21 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)10 Foo (org.mvel2.tests.core.res.Foo)10 Debugger (org.mvel2.debug.Debugger)8 Frame (org.mvel2.debug.Frame)8 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)5 ASTNode (org.mvel2.ast.ASTNode)5 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)4 Serializable (java.io.Serializable)3 Interceptor (org.mvel2.integration.Interceptor)3 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)3 Type (java.lang.reflect.Type)2 Macro (org.mvel2.Macro)2 WithNode (org.mvel2.ast.WithNode)2 Base (org.mvel2.tests.core.res.Base)2 ASTIterator (org.mvel2.util.ASTIterator)2 ASTLinkedList (org.mvel2.util.ASTLinkedList)2