Package org.apache.lucene.expressions.js
Class JavascriptCompiler
java.lang.Object
org.apache.lucene.expressions.js.JavascriptCompiler
An expression compiler for javascript expressions.
Example:
Expression foo = JavascriptCompiler.compile("((0.3*popularity)/10.0)+(0.7*score)");
See the package documentation
for the supported
syntax and default functions.
You can compile with an alternate set of functions via compile(String, Map, ClassLoader)
. For example:
Map<String,Method> functions = new HashMap<>(); // add all the default functions functions.putAll(JavascriptCompiler.DEFAULT_FUNCTIONS); // add cbrt() functions.put("cbrt", Math.class.getMethod("cbrt", double.class)); // call compile with customized function map Expression foo = JavascriptCompiler.compile("cbrt(score)+ln(popularity)", functions, getClass().getClassLoader());
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private static final String
private static final String
The default set of functions available to expressions.(package private) static final org.objectweb.asm.commons.Method
private static final org.objectweb.asm.commons.Method
private static final org.objectweb.asm.commons.Method
(package private) static final org.objectweb.asm.Type
(package private) static final org.objectweb.asm.Type
private static final int
(package private) final boolean
(package private) final String
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
JavascriptCompiler
(String sourceText, Map<String, Method> functions, boolean picky) Constructs a compiler for expressions with specific set of functions -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
checkFunction
(Method method) Check Method signature for compatibility.private static void
checkFunctionClassLoader
(Method method, ClassLoader parent) Cross check if declaring class of given method is the same as returned by the given parentClassLoader
on string lookup.static Expression
Compiles the given expression using default compiler settings.static Expression
Compiles the given expression with the supplied custom functions using default compiler settings.(package private) static Expression
Compiles the given expression with the supplied custom functions.private Expression
compileExpression
(ClassLoader parent) Compiles the given expression with the specified parent classloader(package private) static int
findSingleQuoteStringEnd
(String text, int start) private void
generateClass
(org.antlr.v4.runtime.tree.ParseTree parseTree, org.objectweb.asm.ClassWriter classWriter, Map<String, Integer> externalsMap) Sends the bytecode of class file toClassWriter
.private org.antlr.v4.runtime.tree.ParseTree
Parses the sourceText into an ANTLR 4 parse treeprivate static org.objectweb.asm.commons.Method
getAsmMethod
(Class<?> rtype, String name, Class<?>... ptypes) create an ASM Method object from return type, method name, and parameters.(package private) static String
normalizeQuotes
(String text) private void
setupPicky
(JavascriptParser parser) private static void
This method is unused, it is just here to make sure that the function signatures don't change.
-
Field Details
-
CLASSFILE_VERSION
private static final int CLASSFILE_VERSION- See Also:
-
COMPILED_EXPRESSION_CLASS
-
COMPILED_EXPRESSION_INTERNAL
-
EXPRESSION_TYPE
static final org.objectweb.asm.Type EXPRESSION_TYPE -
FUNCTION_VALUES_TYPE
static final org.objectweb.asm.Type FUNCTION_VALUES_TYPE -
EXPRESSION_CTOR
private static final org.objectweb.asm.commons.Method EXPRESSION_CTOR -
EVALUATE_METHOD
private static final org.objectweb.asm.commons.Method EVALUATE_METHOD -
DOUBLE_VAL_METHOD
static final org.objectweb.asm.commons.Method DOUBLE_VAL_METHOD -
MAX_SOURCE_LENGTH
private static final int MAX_SOURCE_LENGTH- See Also:
-
sourceText
-
functions
-
picky
final boolean picky -
DEFAULT_FUNCTIONS
The default set of functions available to expressions.See the
package documentation
for a list.
-
-
Constructor Details
-
JavascriptCompiler
Constructs a compiler for expressions with specific set of functions- Parameters:
sourceText
- The expression to compile
-
-
Method Details
-
getAsmMethod
private static org.objectweb.asm.commons.Method getAsmMethod(Class<?> rtype, String name, Class<?>... ptypes) create an ASM Method object from return type, method name, and parameters. -
compile
Compiles the given expression using default compiler settings.- Parameters:
sourceText
- The expression to compile- Returns:
- A new compiled expression
- Throws:
ParseException
- on failure to compile
-
compile
public static Expression compile(String sourceText, Map<String, Method> functions, ClassLoader parent) throws ParseExceptionCompiles the given expression with the supplied custom functions using default compiler settings.Functions must be
public static
, returndouble
and can take from zero to 256double
parameters.- Parameters:
sourceText
- The expression to compilefunctions
- map of String names to functionsparent
- aClassLoader
that should be used as the parent of the loaded class. It must contain all classes referred to by the givenfunctions
.- Returns:
- A new compiled expression
- Throws:
ParseException
- on failure to compile
-
compile
static Expression compile(String sourceText, Map<String, Method> functions, ClassLoader parent, boolean picky) throws ParseExceptionCompiles the given expression with the supplied custom functions.Functions must be
public static
, returndouble
and can take from zero to 256double
parameters.- Parameters:
sourceText
- The expression to compilefunctions
- map of String names to functionsparent
- aClassLoader
that should be used as the parent of the loaded class. It must contain all classes referred to by the givenfunctions
.picky
- whether to throw exception on ambiguity or other internal parsing issues (this option makes things slower too, it is only for debugging).- Returns:
- A new compiled expression
- Throws:
ParseException
- on failure to compile
-
unusedTestCompile
This method is unused, it is just here to make sure that the function signatures don't change. If this method fails to compile, you also have to change the byte code generator to correctly use the FunctionValues class.- Throws:
IOException
-
compileExpression
Compiles the given expression with the specified parent classloader- Returns:
- A new compiled expression
- Throws:
ParseException
- on failure to compile
-
getAntlrParseTree
Parses the sourceText into an ANTLR 4 parse tree- Returns:
- The ANTLR parse tree
- Throws:
ParseException
- on failure to parse
-
setupPicky
-
generateClass
private void generateClass(org.antlr.v4.runtime.tree.ParseTree parseTree, org.objectweb.asm.ClassWriter classWriter, Map<String, Integer> externalsMap) throws ParseExceptionSends the bytecode of class file toClassWriter
.- Throws:
ParseException
-
normalizeQuotes
-
findSingleQuoteStringEnd
-
checkFunction
Check Method signature for compatibility. -
checkFunctionClassLoader
Cross check if declaring class of given method is the same as returned by the given parentClassLoader
on string lookup. This preventsNoClassDefFoundError
.
-