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
This commit is contained in:
Robert Muir 2013-09-14 19:22:23 +00:00
parent 04cd8c6db4
commit cdc36f41d2
2 changed files with 13 additions and 13 deletions

View File

@ -161,24 +161,24 @@ public class JavascriptCompiler {
* @throws ParseException on failure to compile * @throws ParseException on failure to compile
*/ */
public static Expression compile(String sourceText) throws ParseException { 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. * Compiles the given expression with the supplied custom functions.
* <p> * <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 sourceText The expression to compile
* @param functions map of String names to functions * @param functions map of String names to functions
* @return A new compiled expression * @return A new compiled expression
* @throws ParseException on failure to compile * @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()) { for (Method m : functions.values()) {
checkFunction(m); 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 * @return A new compiled expression
* @throws ParseException on failure to compile * @throws ParseException on failure to compile
*/ */
private Expression compileExpression() throws ParseException { private Expression compileExpression(ClassLoader parent) throws ParseException {
try { try {
Tree antlrTree = getAntlrComputedExpressionTree(); Tree antlrTree = getAntlrComputedExpressionTree();
@ -226,7 +226,7 @@ public class JavascriptCompiler {
recursiveCompile(antlrTree, ComputedType.DOUBLE); recursiveCompile(antlrTree, ComputedType.DOUBLE);
endCompile(); endCompile();
Class<? extends Expression> evaluatorClass = new Loader(getClass().getClassLoader()) Class<? extends Expression> evaluatorClass = new Loader(parent)
.define(COMPILED_EXPRESSION_CLASS, classWriter.toByteArray()); .define(COMPILED_EXPRESSION_CLASS, classWriter.toByteArray());
Constructor<? extends Expression> constructor = evaluatorClass.getConstructor(String.class, String[].class); Constructor<? extends Expression> constructor = evaluatorClass.getConstructor(String.class, String[].class);
return constructor.newInstance(sourceText, externalsMap.keySet().toArray(new String[externalsMap.size()])); return constructor.newInstance(sourceText, externalsMap.keySet().toArray(new String[externalsMap.size()]));

View File

@ -33,7 +33,7 @@ public class TestCustomFunctions extends LuceneTestCase {
public void testEmpty() throws Exception { public void testEmpty() throws Exception {
Map<String,Method> functions = Collections.emptyMap(); Map<String,Method> functions = Collections.emptyMap();
try { try {
JavascriptCompiler.compile("sqrt(20)", functions); JavascriptCompiler.compile("sqrt(20)", functions, getClass().getClassLoader());
fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("Unrecognized method")); assertTrue(e.getMessage().contains("Unrecognized method"));
@ -43,7 +43,7 @@ public class TestCustomFunctions extends LuceneTestCase {
/** using the default map explicitly */ /** using the default map explicitly */
public void testDefaultList() throws Exception { public void testDefaultList() throws Exception {
Map<String,Method> functions = JavascriptCompiler.DEFAULT_FUNCTIONS; 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); assertEquals(Math.sqrt(20), expr.evaluate(0, null), DELTA);
} }
@ -53,7 +53,7 @@ public class TestCustomFunctions extends LuceneTestCase {
public void testNoArgMethod() throws Exception { public void testNoArgMethod() throws Exception {
Map<String,Method> functions = new HashMap<String,Method>(); Map<String,Method> functions = new HashMap<String,Method>();
functions.put("foo", getClass().getMethod("zeroArgMethod")); 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); assertEquals(5, expr.evaluate(0, null), DELTA);
} }
@ -63,7 +63,7 @@ public class TestCustomFunctions extends LuceneTestCase {
public void testOneArgMethod() throws Exception { public void testOneArgMethod() throws Exception {
Map<String,Method> functions = new HashMap<String,Method>(); Map<String,Method> functions = new HashMap<String,Method>();
functions.put("foo", getClass().getMethod("oneArgMethod", double.class)); 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); assertEquals(6, expr.evaluate(0, null), DELTA);
} }
@ -73,7 +73,7 @@ public class TestCustomFunctions extends LuceneTestCase {
public void testThreeArgMethod() throws Exception { public void testThreeArgMethod() throws Exception {
Map<String,Method> functions = new HashMap<String,Method>(); Map<String,Method> functions = new HashMap<String,Method>();
functions.put("foo", getClass().getMethod("threeArgMethod", double.class, double.class, double.class)); 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); assertEquals(12, expr.evaluate(0, null), DELTA);
} }
@ -82,7 +82,7 @@ public class TestCustomFunctions extends LuceneTestCase {
Map<String,Method> functions = new HashMap<String,Method>(); Map<String,Method> functions = new HashMap<String,Method>();
functions.put("foo", getClass().getMethod("zeroArgMethod")); functions.put("foo", getClass().getMethod("zeroArgMethod"));
functions.put("bar", getClass().getMethod("oneArgMethod", double.class)); 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); assertEquals(11, expr.evaluate(0, null), DELTA);
} }
} }