Class 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  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private JavascriptCompiler​(java.lang.String sourceText)
      Constructs a compiler for expressions.
      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
    • 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 parent ClassLoader on string lookup.
      static Expression compile​(java.lang.String sourceText)
      Compiles the given expression.
      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.
      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 to ClassWriter.
      private org.antlr.v4.runtime.tree.ParseTree getAntlrParseTree()
      Parses the sourceText into an ANTLR 4 parse tree
      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.
      (package private) static java.lang.String normalizeQuotes​(java.lang.String text)  
      private static void unusedTestCompile()
      This method is unused, it is just here to make sure that the function signatures don't change.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • 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
      • 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 documentation for 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, 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:
        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
      • 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 to ClassWriter.
        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 parent ClassLoader on string lookup. This prevents NoClassDefFoundError.