Package org.apache.lucene.expressions.js
Class JavascriptCompiler
- java.lang.Object
-
- org.apache.lucene.expressions.js.JavascriptCompiler
-
public final class JavascriptCompiler extends java.lang.ObjectAn expression compiler for javascript expressions.Example:
Expression foo = JavascriptCompiler.compile("((0.3*popularity)/10.0)+(0.7*score)");See the
package documentationfor 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 classJavascriptCompiler.Loader
-
Field Summary
Fields Modifier and Type Field Description private static intCLASSFILE_VERSIONprivate static java.lang.StringCOMPILED_EXPRESSION_CLASSprivate static java.lang.StringCOMPILED_EXPRESSION_INTERNALstatic java.util.Map<java.lang.String,java.lang.reflect.Method>DEFAULT_FUNCTIONSThe default set of functions available to expressions.(package private) static org.objectweb.asm.commons.MethodDOUBLE_VAL_METHODprivate static org.objectweb.asm.commons.MethodEVALUATE_METHODprivate static org.objectweb.asm.commons.MethodEXPRESSION_CTOR(package private) static org.objectweb.asm.TypeEXPRESSION_TYPE(package private) static org.objectweb.asm.TypeFUNCTION_VALUES_TYPE(package private) java.util.Map<java.lang.String,java.lang.reflect.Method>functionsprivate static intMAX_SOURCE_LENGTH(package private) java.lang.StringsourceText
-
Constructor Summary
Constructors Modifier Constructor Description privateJavascriptCompiler(java.lang.String sourceText)Constructs a compiler for expressions.privateJavascriptCompiler(java.lang.String sourceText, java.util.Map<java.lang.String,java.lang.reflect.Method> functions)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 voidcheckFunction(java.lang.reflect.Method method)Check Method signature for compatibility.private static voidcheckFunctionClassLoader(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 parentClassLoaderon string lookup.static Expressioncompile(java.lang.String sourceText)Compiles the given expression.static Expressioncompile(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.private ExpressioncompileExpression(java.lang.ClassLoader parent)Compiles the given expression with the specified parent classloader(package private) static intfindSingleQuoteStringEnd(java.lang.String text, int start)private voidgenerateClass(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.ParseTreegetAntlrParseTree()Parses the sourceText into an ANTLR 4 parse treeprivate static org.objectweb.asm.commons.MethodgetAsmMethod(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.StringnormalizeQuotes(java.lang.String text)private static voidunusedTestCompile()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
-
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 documentationfor a list.
-
-
Constructor Detail
-
JavascriptCompiler
private JavascriptCompiler(java.lang.String sourceText)
Constructs a compiler for expressions.- Parameters:
sourceText- The expression to compile
-
JavascriptCompiler
private JavascriptCompiler(java.lang.String sourceText, java.util.Map<java.lang.String,java.lang.reflect.Method> functions)Constructs a compiler for expressions with specific set of functions- Parameters:
sourceText- The expression to compile
-
-
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.- 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.Functions must be
public static, returndoubleand can take from zero to 256doubleparameters.- Parameters:
sourceText- The expression to compilefunctions- map of String names to functionsparent- aClassLoaderthat 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
-
unusedTestCompile
private static void unusedTestCompile() throws java.io.IOExceptionThis 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.ParseExceptionParses the sourceText into an ANTLR 4 parse tree- Returns:
- The ANTLR parse tree
- Throws:
java.text.ParseException- on failure to parse
-
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.ParseExceptionSends 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 parentClassLoaderon string lookup. This preventsNoClassDefFoundError.
-
-