Apache Lucene open-source search software
LUCENE-6451: Expressions now support bindings keys that look like zero arg functions
Ryan Ernst458f5042027e
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 87ca4349952..214f6f8ea21 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -82,6 +82,9 @@ New FeaturesWhen used, you should typically disable RPT's pruneLeafyBranches option.(Nick Knize, David Smiley)+* LUCENE-6451: Expressions now support bindings keys that look like+ zero arg functions (Jack Conradson via Ryan Ernst)+Optimizations* LUCENE-6379: IndexWriter.deleteDocuments(Query...) now detects ifdiff --git a/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java b/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.javaindex ef1639cdf17..9e475f489a4 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@@ -235,31 +235,33 @@ public class JavascriptCompiler {int arguments = current.getChildCount() - 1;Method method = functions.get(call);- if (method == null) {- throw new IllegalArgumentException("Unrecognized method call (" + call + ").");- }-- int arity = method.getParameterTypes().length;- if (arguments != arity) {- throw new IllegalArgumentException("Expected (" + arity + ") arguments for method call (" +- call + "), but found (" + arguments + ").");- }-- for (int argument = 1; argument <= arguments; ++argument) {- recursiveCompile(current.getChild(argument), Type.DOUBLE_TYPE);+ if (method == null && (arguments > 0 || !call.contains("."))) {+ throw new IllegalArgumentException("Unrecognized function call (" + call + ").");+ } else if (method != null) {+ int arity = method.getParameterTypes().length;+ if (arguments != arity) {+ throw new IllegalArgumentException("Expected (" + arity + ") arguments for function call (" ++ call + "), but found (" + arguments + ").");+ }++ for (int argument = 1; argument <= arguments; ++argument) {+ recursiveCompile(current.getChild(argument), Type.DOUBLE_TYPE);+ }++ gen.invokeStatic(Type.getType(method.getDeclaringClass()),+ org.objectweb.asm.commons.Method.getMethod(method));++ gen.cast(Type.DOUBLE_TYPE, expected);+ break;+ } else {+ text = call + "()";+ // intentionally fall through to the variable case to allow this non-static+ // method to be forwarded to the bindings for processing}-- gen.invokeStatic(Type.getType(method.getDeclaringClass()),- org.objectweb.asm.commons.Method.getMethod(method));-- gen.cast(Type.DOUBLE_TYPE, expected);- break;case JavascriptParser.VARIABLE:int index;- // normalize quotestext = normalizeQuotes(text);-if (externalsMap.containsKey(text)) {index = externalsMap.get(text);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.javaindex 2288a7cc0a0..ff6c2b9d523 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@@ -20,6 +20,7 @@ package org.apache.lucene.expressions.js;import java.io.PrintWriter;import java.io.StringWriter;import java.lang.reflect.Method;+import java.text.ParseException;import java.util.Collections;import java.util.HashMap;import java.util.Map;@@ -42,7 +43,7 @@ public class TestCustomFunctions extends LuceneTestCase {JavascriptCompiler.compile("sqrt(20)", functions, getClass().getClassLoader());fail();} catch (IllegalArgumentException e) {- assertTrue(e.getMessage().contains("Unrecognized method"));+ assertTrue(e.getMessage().contains("Unrecognized function"));}}@@ -62,7 +63,7 @@ public class TestCustomFunctions extends LuceneTestCase {Expression expr = JavascriptCompiler.compile("foo()", functions, getClass().getClassLoader());assertEquals(5, expr.evaluate(0, null), DELTA);}-+public static double oneArgMethod(double arg1) { return 3 + arg1; }/** tests a method with one arguments */@@ -91,7 +92,28 @@ public class TestCustomFunctions extends LuceneTestCase {Expression expr = JavascriptCompiler.compile("foo() + bar(3)", functions, getClass().getClassLoader());assertEquals(11, expr.evaluate(0, null), DELTA);}-++ /** tests invalid methods that are not allowed to become variables to be mapped */+ public void testInvalidVariableMethods() {+ try {+ JavascriptCompiler.compile("method()");+ fail();+ } catch (ParseException exception) {+ fail();+ } catch (IllegalArgumentException exception) {+ //expected+ }++ try {+ JavascriptCompiler.compile("method.method(1)");+ fail();+ } catch (ParseException exception) {+ fail();+ } catch (IllegalArgumentException exception) {+ //expected+ }+ }+public static String bogusReturnType() { return "bogus!"; }/** wrong return type: must be double */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.javaindex f52a97e0685..4c304d8055a 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@@ -50,6 +50,15 @@ public class TestJavascriptCompiler extends LuceneTestCase {doTestValidVariable("mixed[23]['key'].sub.sub[1]");doTestValidVariable("mixed[23]['key'].sub.sub[1].sub");doTestValidVariable("mixed[23]['key'].sub.sub[1].sub['abc']");+ doTestValidVariable("method.method()");+ doTestValidVariable("method.getMethod()");+ doTestValidVariable("method.METHOD()");+ doTestValidVariable("method['key'].method()");+ doTestValidVariable("method['key'].getMethod()");+ doTestValidVariable("method['key'].METHOD()");+ doTestValidVariable("method[23][\"key\"].method()", "method[23]['key'].method()");+ doTestValidVariable("method[23][\"key\"].getMethod()", "method[23]['key'].getMethod()");+ doTestValidVariable("method[23][\"key\"].METHOD()", "method[23]['key'].METHOD()");}void doTestValidVariable(String variable) throws Exception {@@ -164,14 +173,14 @@ public class TestJavascriptCompiler extends LuceneTestCase {JavascriptCompiler.compile("tan()");fail();} catch (IllegalArgumentException expected) {- assertTrue(expected.getMessage().contains("arguments for method call"));+ assertTrue(expected.getMessage().contains("arguments for function call"));}try {JavascriptCompiler.compile("tan(1, 1)");fail();} catch (IllegalArgumentException expected) {- assertTrue(expected.getMessage().contains("arguments for method call"));+ assertTrue(expected.getMessage().contains("arguments for function call"));}}