Interface IExpressionEvaluator

    • Field Detail

      • DEFAULT_EXPRESSION_TYPE

        static final java.lang.Class<?> DEFAULT_EXPRESSION_TYPE
        The type of all expressions that were not reconfigured with setExpressionTypes(Class[]).
      • ANY_TYPE

        @Deprecated
        static final java.lang.Class<?> ANY_TYPE
        Deprecated.
        Since autoboxing was introduced in JANINO, this feature is no longer necessary because you can use expression type Object.class
        Special value for setExpressionType(Class) that indicates that the expression may have any type.
    • Method Detail

      • setParentClassLoader

        void setParentClassLoader​(@Nullable
                                  java.lang.ClassLoader parentClassLoader)
        The "parent class loader" is used to load referenced classes. Useful values are:
        System.getSystemClassLoader() The running JVM's class path
        Thread.currentThread().getContextClassLoader() or null The class loader effective for the invoking thread
        ClassLoaders.BOOTCLASSPATH_CLASS_LOADER The running JVM's boot class path

        The parent class loader defaults to the current thread's context class loader.

      • setDebuggingInformation

        void setDebuggingInformation​(boolean debugSource,
                                     boolean debugLines,
                                     boolean debugVars)
        Determines what kind of debugging information is included in the generates classes. The default is typically "-g:none".
      • setWarningHandler

        void setWarningHandler​(@Nullable
                               WarningHandler warningHandler)
        By default, warnings are discarded, but an application my install a custom WarningHandler.
        Parameters:
        warningHandler - null to indicate that no warnings be issued
      • evaluate

        @Nullable
        java.lang.Object evaluate​(@Nullable
                                  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 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.

        Null arguments is equivalent with new Object[0].

        Parameters:
        arguments - The actual parameter values
        Throws:
        java.lang.reflect.InvocationTargetException
      • setDefaultExpressionType

        void setDefaultExpressionType​(java.lang.Class<?> defaultExpressionType)
        Reconfigures the "default expression type"; if no expression type is configured for an expression, then, when cooking this IExpressionEvaluator, the "default expression type" is used for the expression
      • getDefaultExpressionType

        java.lang.Class<?> getDefaultExpressionType()
        Returns:
        The currently configured "default expression type"
        See Also:
        setDefaultExpressionType(Class)
      • setImplementedInterfaces

        void setImplementedInterfaces​(java.lang.Class<?>[] implementedTypes)
        Configures the interfaces that the generated class implements.
      • setReturnType

        @Deprecated
        void setReturnType​(@Deprecated
                           java.lang.Class<?> returnType)
        Deprecated.
      • setExpressionType

        void setExpressionType​(java.lang.Class<?> expressionType)
        Defines the type of the expression.

        Defaults to Object.class, which, thanks to autoboxing, allows for any expression type (including primitive types).

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

      • setExpressionTypes

        void setExpressionTypes​(java.lang.Class<?>[] expressionTypes)
        Configures the types of the expressions.

        Unless this method is called, all expressions have type Object.class.

        If any expression has type void.class, then it must be an invocation of a void method.

      • evaluate

        @Nullable
        java.lang.Object evaluate​(int idx,
                                  @Nullable
                                  java.lang.Object... arguments)
                           throws java.lang.reflect.InvocationTargetException
        Null arguments is equivalent with new Object[0].
        Throws:
        java.lang.reflect.InvocationTargetException
      • createFastEvaluator

        <T> T createFastEvaluator​(java.lang.String expression,
                                  java.lang.Class<? extends 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 setClassName(String).

        Throws:
        CompileException