public final class JavascriptCompiler
extends java.lang.Object
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());
Modifier and Type | Field and Description |
---|---|
static java.util.Map<java.lang.String,java.lang.reflect.Method> |
DEFAULT_FUNCTIONS
The default set of functions available to expressions.
|
Modifier and Type | Method and Description |
---|---|
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.
|
public static final java.util.Map<java.lang.String,java.lang.reflect.Method> DEFAULT_FUNCTIONS
See the package documentation
for a list.
public static Expression compile(java.lang.String sourceText) throws java.text.ParseException
sourceText
- The expression to compilejava.text.ParseException
- on failure to compilepublic 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
Functions must be public static
, return double
and
can take from zero to 256 double
parameters.
sourceText
- The expression to compilefunctions
- map of String names to functionsparent
- a ClassLoader
that should be used as the parent of the loaded class.
It must contain all classes referred to by the given functions
.java.text.ParseException
- on failure to compileCopyright © 2000–2019 The Apache Software Foundation. All rights reserved.