Class JavascriptCompiler

java.lang.Object
org.apache.lucene.expressions.js.JavascriptCompiler

public final class JavascriptCompiler extends 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());
 
  • Field Details

    • CLASSFILE_VERSION

      private static final int CLASSFILE_VERSION
      See Also:
    • COMPILED_EXPRESSION_CLASS

      private static final String COMPILED_EXPRESSION_CLASS
    • COMPILED_EXPRESSION_INTERNAL

      private static final 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:
    • sourceText

      final String sourceText
    • functions

      final Map<String,Method> functions
    • picky

      final boolean picky
    • DEFAULT_FUNCTIONS

      public static final Map<String,Method> DEFAULT_FUNCTIONS
      The default set of functions available to expressions.

      See the package documentation for a list.

  • Constructor Details

    • JavascriptCompiler

      private JavascriptCompiler(String sourceText, Map<String,Method> functions, boolean picky)
      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

      public static Expression compile(String sourceText) throws ParseException
      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 ParseException
      Compiles the given expression with the supplied custom functions using default compiler settings.

      Functions must be public static, return double and can take from zero to 256 double parameters.

      Parameters:
      sourceText - The expression to compile
      functions - map of String names to functions
      parent - a ClassLoader that should be used as the parent of the loaded class. It must contain all classes referred to by the given functions.
      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 ParseException
      Compiles the given expression with the supplied custom functions.

      Functions must be public static, return double and can take from zero to 256 double parameters.

      Parameters:
      sourceText - The expression to compile
      functions - map of String names to functions
      parent - a ClassLoader that should be used as the parent of the loaded class. It must contain all classes referred to by the given functions.
      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

      private static void unusedTestCompile() throws 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:
      IOException
    • compileExpression

      private Expression compileExpression(ClassLoader parent) throws ParseException
      Compiles the given expression with the specified parent classloader
      Returns:
      A new compiled expression
      Throws:
      ParseException - on failure to compile
    • getAntlrParseTree

      private org.antlr.v4.runtime.tree.ParseTree getAntlrParseTree() throws ParseException
      Parses the sourceText into an ANTLR 4 parse tree
      Returns:
      The ANTLR parse tree
      Throws:
      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, Map<String,Integer> externalsMap) throws ParseException
      Sends the bytecode of class file to ClassWriter.
      Throws:
      ParseException
    • normalizeQuotes

      static String normalizeQuotes(String text)
    • findSingleQuoteStringEnd

      static int findSingleQuoteStringEnd(String text, int start)
    • checkFunction

      private static void checkFunction(Method method)
      Check Method signature for compatibility.
    • checkFunctionClassLoader

      private static void checkFunctionClassLoader(Method method, ClassLoader parent)
      Cross check if declaring class of given method is the same as returned by the given parent ClassLoader on string lookup. This prevents NoClassDefFoundError.