Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

Expression's JavascriptCompiler now throw ParseException with bad function names or bad arity instead of IllegalArgumentException

Tomas Eduardo Fernandez Lobbecc546f4e3931
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index a1e7e3d41d9..25260c80147 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -226,6 +226,10 @@ Other
AnalysisSPILoader: Don't wrap exceptions occuring in factory's
ctor inside InvocationTargetException. (Uwe Schindler)
+* LUCENE-6965: Expression's JavascriptCompiler now throw ParseException
+ with bad function names or bad arity instead of IllegalArgumentException
+ (Tomás Fernández Löbbe)
+
======================= Lucene 5.4.0 =======================
New Features
diff --git a/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java b/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
index 187972429b6..00a2c7a51af 100644
--- a/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
+++ b/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
@@ -228,7 +228,7 @@ public final class JavascriptCompiler {
/**
* Sends the bytecode of class file to {@link ClassWriter}.
*/
- private void generateClass(final ParseTree parseTree, final ClassWriter classWriter, final Map<String, Integer> externalsMap) {
+ private void generateClass(final ParseTree parseTree, final ClassWriter classWriter, final Map<String, Integer> externalsMap) throws ParseException {
classWriter.visit(CLASSFILE_VERSION,
Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC,
COMPILED_EXPRESSION_INTERNAL,
@@ -247,9 +247,9 @@ public final class JavascriptCompiler {
final GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC,
EVALUATE_METHOD, null, null, classWriter);
-
+
// to completely hide the ANTLR visitor we use an anonymous impl:
- new JavascriptBaseVisitor<Void>() {
+ JavascriptBaseVisitor<Void> visitor = new JavascriptBaseVisitor<Void>() {
private final Deque<Type> typeStack = new ArrayDeque<>();
@Override
@@ -295,8 +295,9 @@ public final class JavascriptCompiler {
int arity = method.getParameterTypes().length;
if (arguments != arity) {
- throw new IllegalArgumentException(
- "Expected (" + arity + ") arguments for function call (" + text + "), but found (" + arguments + ").");
+ throw new ParseRuntimeException(
+ "Expected (" + arity + ") arguments for function call (" + text + "), but found (" + arguments + ").",
+ ctx.start.getStartIndex());
}
typeStack.push(Type.DOUBLE_TYPE);
@@ -330,7 +331,7 @@ public final class JavascriptCompiler {
gen.invokeVirtual(FUNCTION_VALUES_TYPE, DOUBLE_VAL_METHOD);
gen.cast(Type.DOUBLE_TYPE, typeStack.peek());
} else {
- throw new IllegalArgumentException("Unrecognized function call (" + text + ").");
+ throw new ParseRuntimeException("Unrecognized function call (" + text + ").", ctx.start.getStartIndex());
}
return null;
@@ -621,7 +622,15 @@ public final class JavascriptCompiler {
throw new IllegalStateException("Invalid expected type: " + typeStack.peek());
}
}
- }.visit(parseTree);
+ };
+
+ try {
+ visitor.visit(parseTree);
+ } catch (final ParseRuntimeException e) {
+ ParseException exception = new ParseException("Invalid expression '" + sourceText + "': " + e.getMessage(), e.position);
+ exception.initCause(e);
+ throw exception;
+ }
gen.returnValue();
gen.endMethod();
@@ -703,6 +712,16 @@ public final class JavascriptCompiler {
}
DEFAULT_FUNCTIONS = Collections.unmodifiableMap(map);
}
+
+ @SuppressWarnings("serial")
+ private final static class ParseRuntimeException extends RuntimeException {
+ final int position;
+ public ParseRuntimeException(String msg, int position) {
+ super(msg);
+ this.position = position;
+ }
+
+ }
/** Check Method signature for compatibility. */
private static void checkFunction(Method method) {
diff --git a/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java b/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java
index c460154175f..53938d03514 100644
--- a/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java
+++ b/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java
@@ -42,8 +42,9 @@ public class TestCustomFunctions extends LuceneTestCase {
try {
JavascriptCompiler.compile("sqrt(20)", functions, getClass().getClassLoader());
fail();
- } catch (IllegalArgumentException e) {
- assertTrue(e.getMessage().contains("Unrecognized function"));
+ } catch (ParseException expected) {
+ assertEquals("Invalid expression 'sqrt(20)': Unrecognized function call (sqrt).", expected.getMessage());
+ assertEquals(expected.getErrorOffset(), 0);
}
}
@@ -98,19 +99,35 @@ public class TestCustomFunctions extends LuceneTestCase {
try {
JavascriptCompiler.compile("method()");
fail();
- } catch (ParseException exception) {
- fail();
} catch (IllegalArgumentException exception) {
+ fail();
+ } catch (ParseException expected) {
//expected
+ assertEquals("Invalid expression 'method()': Unrecognized function call (method).", expected.getMessage());
+ assertEquals(0, expected.getErrorOffset());
+ assertTrue(expected.getCause() != null && expected.getCause() != expected);
}
try {
JavascriptCompiler.compile("method.method(1)");
fail();
- } catch (ParseException exception) {
+ } catch (IllegalArgumentException exception) {
+ fail();
+ } catch (ParseException expected) {
+ //expected
+ assertEquals("Invalid expression 'method.method(1)': Unrecognized function call (method.method).", expected.getMessage());
+ assertEquals(0, expected.getErrorOffset());
+ }
+
+ try {
+ JavascriptCompiler.compile("1 + method()");
fail();
} catch (IllegalArgumentException exception) {
+ fail();
+ } catch (ParseException expected) {
//expected
+ assertEquals("Invalid expression '1 + method()': Unrecognized function call (method).", expected.getMessage());
+ assertEquals(4, expected.getErrorOffset());
}
}
diff --git a/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestJavascriptCompiler.java b/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestJavascriptCompiler.java
index 3f0c0b0eeaa..c572c332a25 100644
--- a/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestJavascriptCompiler.java
+++ b/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestJavascriptCompiler.java
@@ -181,16 +181,33 @@ public class TestJavascriptCompiler extends LuceneTestCase {
try {
JavascriptCompiler.compile("tan()");
fail();
- } catch (IllegalArgumentException expected) {
- assertTrue(expected.getMessage().contains("arguments for function call"));
+ } catch (ParseException expected) {
+ assertEquals("Invalid expression 'tan()': Expected (1) arguments for function call (tan), but found (0).", expected.getMessage());
+ assertEquals(expected.getErrorOffset(), 0);
}
try {
JavascriptCompiler.compile("tan(1, 1)");
fail();
- } catch (IllegalArgumentException expected) {
+ } catch (ParseException expected) {
assertTrue(expected.getMessage().contains("arguments for function call"));
}
+
+ try {
+ JavascriptCompiler.compile(" tan()");
+ fail();
+ } catch (ParseException expected) {
+ assertEquals("Invalid expression ' tan()': Expected (1) arguments for function call (tan), but found (0).", expected.getMessage());
+ assertEquals(expected.getErrorOffset(), 1);
+ }
+
+ try {
+ JavascriptCompiler.compile("1 + tan()");
+ fail();
+ } catch (ParseException expected) {
+ assertEquals("Invalid expression '1 + tan()': Expected (1) arguments for function call (tan), but found (0).", expected.getMessage());
+ assertEquals(expected.getErrorOffset(), 4);
+ }
}
public void testVariableNormalization() throws Exception {