Package org.apache.lucene.expressions.js
Class JavascriptCompiler
- java.lang.Object
-
- org.apache.lucene.expressions.js.JavascriptCompiler
-
public final class JavascriptCompiler extends java.lang.Object
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 Modifier and Type Class Description (package private) static class
JavascriptCompiler.Loader
-
Field Summary
Fields Modifier and Type Field Description private static int
CLASSFILE_VERSION
private static java.lang.String
COMPILED_EXPRESSION_CLASS
private static java.lang.String
COMPILED_EXPRESSION_INTERNAL
static java.util.Map<java.lang.String,java.lang.reflect.Method>
DEFAULT_FUNCTIONS
The default set of functions available to expressions.(package private) static org.objectweb.asm.commons.Method
DOUBLE_VAL_METHOD
private static org.objectweb.asm.commons.Method
EVALUATE_METHOD
private static org.objectweb.asm.commons.Method
EXPRESSION_CTOR
(package private) static org.objectweb.asm.Type
EXPRESSION_TYPE
(package private) static org.objectweb.asm.Type
FUNCTION_VALUES_TYPE
(package private) java.util.Map<java.lang.String,java.lang.reflect.Method>
functions
private static int
MAX_SOURCE_LENGTH
(package private) boolean
picky
(package private) java.lang.String
sourceText
-
Constructor Summary
Constructors Modifier Constructor Description private
JavascriptCompiler(java.lang.String sourceText, java.util.Map<java.lang.String,java.lang.reflect.Method> functions, boolean picky)
Constructs a compiler for expressions with specific set of functions
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static void
checkFunction(java.lang.reflect.Method method)
Check Method signature for compatibility.private static void
checkFunctionClassLoader(java.lang.reflect.Method method, java.lang.ClassLoader parent)
Cross check if declaring class of given method is the same as returned by the given parentClassLoader
on string lookup.static Expression
compile(java.lang.String sourceText)
Compiles the given expression using default compiler settings.static Expression
compile(java.lang.String sourceText, java.util.Map<java.lang.String,java.lang.reflect.Method> functions, java.lang.ClassLoader parent)
Compiles the given expression with the supplied custom functions using default compiler settings.(package private) static Expression
compile(java.lang.String sourceText, java.util.Map<java.lang.String,java.lang.reflect.Method> functions, java.lang.ClassLoader parent, boolean picky)
Compiles the given expression with the supplied custom functions.private Expression
compileExpression(java.lang.ClassLoader parent)
Compiles the given expression with the specified parent classloader(package private) static int
findSingleQuoteStringEnd(java.lang.String text, int start)
private void
generateClass(org.antlr.v4.runtime.tree.ParseTree parseTree, org.objectweb.asm.ClassWriter classWriter, java.util.Map<java.lang.String,java.lang.Integer> externalsMap)
Sends the bytecode of class file toClassWriter
.private org.antlr.v4.runtime.tree.ParseTree
getAntlrParseTree()
Parses the sourceText into an ANTLR 4 parse treeprivate static org.objectweb.asm.commons.Method
getAsmMethod(java.lang.Class<?> rtype, java.lang.String name, java.lang.Class<?>... ptypes)
create an ASM Method object from return type, method name, and parameters.(package private) static java.lang.String
normalizeQuotes(java.lang.String text)
private void
setupPicky(JavascriptParser parser)
private static void
unusedTestCompile()
This method is unused, it is just here to make sure that the function signatures don't change.
-
-
-
Field Detail
-
CLASSFILE_VERSION
private static final int CLASSFILE_VERSION
- See Also:
- Constant Field Values
-
COMPILED_EXPRESSION_CLASS
private static final java.lang.String COMPILED_EXPRESSION_CLASS
-
COMPILED_EXPRESSION_INTERNAL
private static final java.lang.String 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:
- Constant Field Values
-
sourceText
final java.lang.String sourceText
-
functions
final java.util.Map<java.lang.String,java.lang.reflect.Method> functions
-
picky
final boolean picky
-
DEFAULT_FUNCTIONS
public static final java.util.Map<java.lang.String,java.lang.reflect.Method> DEFAULT_FUNCTIONS
The default set of functions available to expressions.See the
package documentation
for a list.
-
-
Method Detail
-
getAsmMethod
private static org.objectweb.asm.commons.Method getAsmMethod(java.lang.Class<?> rtype, java.lang.String name, java.lang.Class<?>... ptypes)
create an ASM Method object from return type, method name, and parameters.
-
compile
public static Expression compile(java.lang.String sourceText) throws java.text.ParseException
Compiles the given expression using default compiler settings.- Parameters:
sourceText
- The expression to compile- Returns:
- A new compiled expression
- Throws:
java.text.ParseException
- on failure to compile
-
compile
public static Expression compile(java.lang.String sourceText, java.util.Map<java.lang.String,java.lang.reflect.Method> functions, java.lang.ClassLoader parent) throws java.text.ParseException
Compiles 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:
java.text.ParseException
- on failure to compile
-
compile
static Expression compile(java.lang.String sourceText, java.util.Map<java.lang.String,java.lang.reflect.Method> functions, java.lang.ClassLoader parent, boolean picky) throws java.text.ParseException
Compiles 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:
java.text.ParseException
- on failure to compile
-
unusedTestCompile
private static void unusedTestCompile() throws java.io.IOException
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:
java.io.IOException
-
compileExpression
private Expression compileExpression(java.lang.ClassLoader parent) throws java.text.ParseException
Compiles the given expression with the specified parent classloader- Returns:
- A new compiled expression
- Throws:
java.text.ParseException
- on failure to compile
-
getAntlrParseTree
private org.antlr.v4.runtime.tree.ParseTree getAntlrParseTree() throws java.text.ParseException
Parses the sourceText into an ANTLR 4 parse tree- Returns:
- The ANTLR parse tree
- Throws:
java.text.ParseException
- on failure to parse
-
setupPicky
private void setupPicky(JavascriptParser parser)
-
generateClass
private void generateClass(org.antlr.v4.runtime.tree.ParseTree parseTree, org.objectweb.asm.ClassWriter classWriter, java.util.Map<java.lang.String,java.lang.Integer> externalsMap) throws java.text.ParseException
Sends the bytecode of class file toClassWriter
.- Throws:
java.text.ParseException
-
normalizeQuotes
static java.lang.String normalizeQuotes(java.lang.String text)
-
findSingleQuoteStringEnd
static int findSingleQuoteStringEnd(java.lang.String text, int start)
-
checkFunction
private static void checkFunction(java.lang.reflect.Method method)
Check Method signature for compatibility.
-
checkFunctionClassLoader
private static void checkFunctionClassLoader(java.lang.reflect.Method method, java.lang.ClassLoader parent)
Cross check if declaring class of given method is the same as returned by the given parentClassLoader
on string lookup. This preventsNoClassDefFoundError
.
-
-