MVEL not evaluating and throwing invalid number literal

198 views Asked by At

MVEL is not evaluating the expression properly which contains the string with number like below

<div><br>@{11412Test}</div>

Caused by: [Error: invalid number literal: 11412Test] [Near : {... v>
@{11412Test} ....}]

[Line: 1, Column: 57] at org.mvel2.util.ParseTools.isNumber(ParseTools.java:1903) at org.mvel2.ast.ASTNode.setName(ASTNode.java:257) at org.mvel2.ast.ASTNode.(ASTNode.java:422) at org.mvel2.compiler.AbstractParser.createPropertyToken(AbstractParser.java:1428) at org.mvel2.compiler.AbstractParser.nextToken(AbstractParser.java:896) at org.mvel2.compiler.ExpressionCompiler._compile(ExpressionCompiler.java:127) at org.mvel2.MVEL.compileExpression(MVEL.java:832) at org.mvel2.templates.res.CompiledExpressionNode.(CompiledExpressionNode.java:41) at org.mvel2.templates.TemplateCompiler.compileFrom(TemplateCompiler.java:211) ... 131 more

and i am using mvel2-2.4.14-Final version. Please let me know how to resolve this issue. Note: If it is only string'Test' or only number '11412' then it is working

1

There are 1 answers

2
Olivier On

The claim that "it is working with number '11412'" is not true:

import java.util.HashMap;
import org.mvel2.templates.TemplateRuntime;

public class Test
{
    public static void main(String[] args)
    {
        String template = "Hello, @{11412}";
        HashMap<String,Object> vars = new HashMap<String,Object>();
        vars.put("11412", "world");
        System.out.println(TemplateRuntime.eval(template, vars));
    }
}

Output:

Hello, 11412

@{11412} was not replaced by world. 11412 was just interpreted as a literal integer (not as a variable name).

Like in most (if not all) languages, names are not allowed to start with a digit. A token that starts with a digit is interpreted as a number. You may, however, start with an underscore:

import java.util.HashMap;
import org.mvel2.templates.TemplateRuntime;

public class Test
{
    public static void main(String[] args)
    {
        String template = "Hello, @{_11412}";
        HashMap<String,Object> vars = new HashMap<String,Object>();
        vars.put("_11412", "world");
        System.out.println(TemplateRuntime.eval(template, vars));
    }
}

Output:

Hello, world

_11412Test will work as well.