Skip to content

Commit

Permalink
fix for loop parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Nov 19, 2020
1 parent bac95e0 commit f33227e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/main/java/org/mvel2/ast/ForEachNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.mvel2.ast;

import java.lang.reflect.Array;

import org.mvel2.CompileException;
import org.mvel2.DataConversion;
import org.mvel2.MVEL;
Expand All @@ -27,9 +29,9 @@
import org.mvel2.integration.impl.ItemResolverFactory;
import org.mvel2.util.ParseTools;

import java.lang.reflect.Array;

import static org.mvel2.util.ParseTools.*;
import static org.mvel2.util.ParseTools.createStringTrimmed;
import static org.mvel2.util.ParseTools.getBaseComponentType;
import static org.mvel2.util.ParseTools.subCompileExpression;

/**
* @author Christopher Brock
Expand Down Expand Up @@ -178,11 +180,10 @@ private void handleCond(char[] condition, int start, int offset, int fields, Par

int x;
if ((x = (item = createStringTrimmed(condition, start, cursor - start)).indexOf(' ')) != -1) {
String tk = new String(condition, start, x).trim();
String tk = item.substring(0, x);
try {
itemType = ParseTools.findClass(null, tk, pCtx);
item = new String(condition, start + x, (cursor - start) - x).trim();

item = item.substring( item.lastIndexOf( ' ' )+1, item.length() );
}
catch (ClassNotFoundException e) {
throw new CompileException("cannot resolve identifier: " + tk, condition, start);
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/mvel2/tests/core/CoreConfidenceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -4899,4 +4899,23 @@ public void testGetBestCandidateForBigDecimalArg() {
assertEquals(double.class, method.getReturnType());
Assert.assertArrayEquals(new Class<?>[] {double.class, int.class}, method.getParameterTypes());
}

public void testForLoopWithSpaces() {
VariableResolverFactory factory = new MapVariableResolverFactory(new HashMap<String, Object>());
factory.createVariable("strings", Arrays.asList( "test" ));

ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrictTypeEnforcement(true);
pctx.setStrongTyping(true);
pctx.addInput("strings", List.class);

String expression =
"for ( String s : strings ) {\n" +
" return s;\n" +
"}";

Serializable compiledExpr = MVEL.compileExpression(expression, pctx);
assertEquals( "test", MVEL.executeExpression(compiledExpr, null, factory));
}
}

0 comments on commit f33227e

Please sign in to comment.