Interface IExpressionEvaluator

    • Field Detail

      • ANY_TYPE

        static final java.lang.Class ANY_TYPE
        Special value for setExpressionType(Class) that indicates that the expression may have any type.
    • Method Detail

      • setExpressionType

        void setExpressionType​(java.lang.Class expressionType)
        Define the type of the expression. The special type ANY_TYPE allows the expression to return any type (primitive or reference).

        If expressionType is Void.TYPE, then the expression must be an invocation of a void method.

        Defaults to ANY_TYPE.

      • setExpressionTypes

        void setExpressionTypes​(java.lang.Class[] expressionTypes)
        Same as setExpressionType(Class), but for multiple expressions.
      • evaluate

        java.lang.Object evaluate​(java.lang.Object[] arguments)
                           throws java.lang.reflect.InvocationTargetException
        Evaluates the expression with concrete parameter values.

        Each argument value must have the same type as specified through the "parameterTypes" parameter of IScriptEvaluator.setParameters(String[], Class[]).

        Arguments of primitive type must passed with their wrapper class objects.

        The object returned has the class as specified through setExpressionType(Class).

        This method is thread-safe.

        Specified by:
        evaluate in interface IScriptEvaluator
        Parameters:
        arguments - The actual parameter values.
        Throws:
        java.lang.reflect.InvocationTargetException
      • createFastEvaluator

        <T> java.lang.Object createFastEvaluator​(java.lang.String expression,
                                                 java.lang.Class<T> interfaceToImplement,
                                                 java.lang.String[] parameterNames)
                                          throws CompileException
        If the parameter and return types of the expression are known at compile time, then a "fast" expression evaluator can be instantiated through createFastEvaluator(String, Class, String[]). Expression evaluation is faster than through evaluate(Object[]), because it is not done through reflection but through direct method invocation.

        Example:

         public interface Foo {
             int bar(int a, int b);
         }
         ...
         ExpressionEvaluator ee = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();
        
         // Optionally configure the EE here...
         ee.setClassName("Bar");
         ee.setDefaultImports(new String[] { "java.util.*" });
         ee.setExtendedClass(SomeOtherClass.class);
         ee.setParentClassLoader(someClassLoader);
        
         // Optionally configure the EE here...
         Foo f = (Foo) ee.createFastEvaluator(
             "a + b",                    // expression to evaluate
             Foo.class,                  // interface that describes the expression's signature
             new String[] { "a", "b" }   // the parameters' names
         );
         System.out.println("1 + 2 = " + f.bar(1, 2)); // Evaluate the expression
         
        All other configuration (implemented type, static method, return type, method name, parameter names and types, thrown exceptions) are predetermined by the interfaceToImplement. Notice: The interfaceToImplement must be accessible by the compiled class, i.e. either be declared public, or with protected or default access in the package of the compiled class (see IClassBodyEvaluator.setClassName(String).
        Specified by:
        createFastEvaluator in interface IScriptEvaluator
        Parameters:
        expression - Contains the sequence of script tokens
        Throws:
        CompileException
        See Also:
        IScriptEvaluator.createFastEvaluator(Reader, Class, String[])
      • createFastEvaluator

        <T> java.lang.Object createFastEvaluator​(java.io.Reader reader,
                                                 java.lang.Class<T> interfaceToImplement,
                                                 java.lang.String[] parameterNames)
                                          throws CompileException,
                                                 java.io.IOException
        Description copied from interface: IScriptEvaluator
        If the parameter and return types of the expression are known at compile time, then a "fast" script evaluator can be instantiated through this method.

        Script evaluation is faster than through IScriptEvaluator.evaluate(Object[]), because it is not done through reflection but through direct method invocation.

        Example:

         public interface Foo {
             int bar(int a, int b);
         }
         ...
         IScriptEvaluator se = CompilerFactoryFactory.getDefaultCompilerFactory().newScriptEvaluator();
        
         // Optionally configure the SE her:
         se.setClassName("Bar");
         se.setDefaultImports(new String[] { "java.util.*" });
         se.setExtendedClass(SomeOtherClass.class);
         se.setParentClassLoader(someClassLoader);
        
         Foo f = (Foo) se.createFastScriptEvaluator(
             "return a - b;",
             Foo.class,
             new String[] { "a", "b" }
         );
         System.out.println("1 - 2 = " + f.bar(1, 2));
         
        All other configuration (implemented type, static method, return type, method name, parameter names and types, thrown exceptions) are predetermined by the interfaceToImplement. Notice: The interfaceToImplement must either be declared public, or with package scope in the same package as the generated class (see IClassBodyEvaluator.setClassName(String)).
        Specified by:
        createFastEvaluator in interface IScriptEvaluator
        Parameters:
        reader - Produces the stream of script tokens
        interfaceToImplement - Must declare exactly one method
        parameterNames - The names of the parameters of that method
        Returns:
        An object that implements the given interface
        Throws:
        CompileException
        java.io.IOException
        See Also:
        createFastEvaluator(String, Class, String[])