Skip to content

Apache Lucene open-source search software

LUCENE-5207: allow specifying classloader when using custom functions

Robert Muircdc36f41d2ed
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 bb3531eb7fe..9dd2f67d7bd 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
@@ -161,24 +161,24 @@ public class JavascriptCompiler {
* @throws ParseException on failure to compile
*/
public static Expression compile(String sourceText) throws ParseException {
- return new JavascriptCompiler(sourceText).compileExpression();
+ return new JavascriptCompiler(sourceText).compileExpression(JavascriptCompiler.class.getClassLoader());
}
/**
* Compiles the given expression with the supplied custom functions.
* <p>
- * Functions must return a double.
+ * Functions must return {@code double} and can take from zero to 256 {@code double} parameters.
*
* @param sourceText The expression to compile
* @param functions map of String names to functions
* @return A new compiled expression
* @throws ParseException on failure to compile
*/
- public static Expression compile(String sourceText, Map<String,Method> functions) throws ParseException {
+ public static Expression compile(String sourceText, Map<String,Method> functions, ClassLoader parent) throws ParseException {
for (Method m : functions.values()) {
checkFunction(m);
}
- return new JavascriptCompiler(sourceText, functions).compileExpression();
+ return new JavascriptCompiler(sourceText, functions).compileExpression(parent);
}
/**
@@ -213,12 +213,12 @@ public class JavascriptCompiler {
}
/**
- * Compiles the given expression.
+ * Compiles the given expression with the specified parent classloader
*
* @return A new compiled expression
* @throws ParseException on failure to compile
*/
- private Expression compileExpression() throws ParseException {
+ private Expression compileExpression(ClassLoader parent) throws ParseException {
try {
Tree antlrTree = getAntlrComputedExpressionTree();
@@ -226,7 +226,7 @@ public class JavascriptCompiler {
recursiveCompile(antlrTree, ComputedType.DOUBLE);
endCompile();
- Class<? extends Expression> evaluatorClass = new Loader(getClass().getClassLoader())
+ Class<? extends Expression> evaluatorClass = new Loader(parent)
.define(COMPILED_EXPRESSION_CLASS, classWriter.toByteArray());
Constructor<? extends Expression> constructor = evaluatorClass.getConstructor(String.class, String[].class);
return constructor.newInstance(sourceText, externalsMap.keySet().toArray(new String[externalsMap.size()]));
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 40157e7daea..23d0b162a48 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
@@ -33,7 +33,7 @@ public class TestCustomFunctions extends LuceneTestCase {
public void testEmpty() throws Exception {
Map<String,Method> functions = Collections.emptyMap();
try {
- JavascriptCompiler.compile("sqrt(20)", functions);
+ JavascriptCompiler.compile("sqrt(20)", functions, getClass().getClassLoader());
fail();
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("Unrecognized method"));
@@ -43,7 +43,7 @@ public class TestCustomFunctions extends LuceneTestCase {
/** using the default map explicitly */
public void testDefaultList() throws Exception {
Map<String,Method> functions = JavascriptCompiler.DEFAULT_FUNCTIONS;
- Expression expr = JavascriptCompiler.compile("sqrt(20)", functions);
+ Expression expr = JavascriptCompiler.compile("sqrt(20)", functions, getClass().getClassLoader());
assertEquals(Math.sqrt(20), expr.evaluate(0, null), DELTA);
}
@@ -53,7 +53,7 @@ public class TestCustomFunctions extends LuceneTestCase {
public void testNoArgMethod() throws Exception {
Map<String,Method> functions = new HashMap<String,Method>();
functions.put("foo", getClass().getMethod("zeroArgMethod"));
- Expression expr = JavascriptCompiler.compile("foo()", functions);
+ Expression expr = JavascriptCompiler.compile("foo()", functions, getClass().getClassLoader());
assertEquals(5, expr.evaluate(0, null), DELTA);
}
@@ -63,7 +63,7 @@ public class TestCustomFunctions extends LuceneTestCase {
public void testOneArgMethod() throws Exception {
Map<String,Method> functions = new HashMap<String,Method>();
functions.put("foo", getClass().getMethod("oneArgMethod", double.class));
- Expression expr = JavascriptCompiler.compile("foo(3)", functions);
+ Expression expr = JavascriptCompiler.compile("foo(3)", functions, getClass().getClassLoader());
assertEquals(6, expr.evaluate(0, null), DELTA);
}
@@ -73,7 +73,7 @@ public class TestCustomFunctions extends LuceneTestCase {
public void testThreeArgMethod() throws Exception {
Map<String,Method> functions = new HashMap<String,Method>();
functions.put("foo", getClass().getMethod("threeArgMethod", double.class, double.class, double.class));
- Expression expr = JavascriptCompiler.compile("foo(3, 4, 5)", functions);
+ Expression expr = JavascriptCompiler.compile("foo(3, 4, 5)", functions, getClass().getClassLoader());
assertEquals(12, expr.evaluate(0, null), DELTA);
}
@@ -82,7 +82,7 @@ public class TestCustomFunctions extends LuceneTestCase {
Map<String,Method> functions = new HashMap<String,Method>();
functions.put("foo", getClass().getMethod("zeroArgMethod"));
functions.put("bar", getClass().getMethod("oneArgMethod", double.class));
- Expression expr = JavascriptCompiler.compile("foo() + bar(3)", functions);
+ Expression expr = JavascriptCompiler.compile("foo() + bar(3)", functions, getClass().getClassLoader());
assertEquals(11, expr.evaluate(0, null), DELTA);
}
}