From cdc36f41d2ed5d8526f2f7856ead5061ab605433 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Sat, 14 Sep 2013 19:22:23 +0000 Subject: [PATCH] LUCENE-5207: allow specifying classloader when using custom functions git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene5207@1523297 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/expressions/js/JavascriptCompiler.java | 14 +++++++------- .../lucene/expressions/js/TestCustomFunctions.java | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) 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. *

- * 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 functions) throws ParseException { + public static Expression compile(String sourceText, Map 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 evaluatorClass = new Loader(getClass().getClassLoader()) + Class evaluatorClass = new Loader(parent) .define(COMPILED_EXPRESSION_CLASS, classWriter.toByteArray()); Constructor 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 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 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 functions = new HashMap(); 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 functions = new HashMap(); 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 functions = new HashMap(); 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 functions = new HashMap(); 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); } }