Interface IScriptEvaluator

    • Method Detail

      • setOverrideMethod

        void setOverrideMethod​(boolean overrideMethod)
        Defines whether the generated method overrides a methods declared in a supertype.
      • setStaticMethod

        void setStaticMethod​(boolean staticMethod)
        Defines whether the generated method should be STATIC or not. Defaults to true.
      • setReturnType

        void setReturnType​(java.lang.Class returnType)
        Defines the return type of the generated method. The meaning of a null value is implementation-dependent.
      • setMethodName

        void setMethodName​(java.lang.String methodName)
        Define the name of the generated method. Defaults to an unspecified name.
      • setParameters

        void setParameters​(java.lang.String[] names,
                           java.lang.Class[] types)
        Define the names and types of the parameters of the generated method.

        names and types must have the same number of elements.

        The parameters can be of primitive type, e.g. double.class.

        The default is to have zero parameters.

      • setThrownExceptions

        void setThrownExceptions​(java.lang.Class[] thrownExceptions)
        Define the exceptions that the generated method may throw.
      • evaluate

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

        Each argument 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 setReturnType(Class).

        This method is thread-safe.

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

        java.lang.reflect.Method getMethod()
        Returns the loaded Method.

        This method must only be called after exactly one of the ICookable.cook(String, Reader) methods was called.

      • setOverrideMethod

        void setOverrideMethod​(boolean[] overrideMethod)
        Same as setOverrideMethod(boolean), but for multiple scripts.
      • setStaticMethod

        void setStaticMethod​(boolean[] staticMethod)
        Same as setStaticMethod(boolean), but for multiple scripts.
      • setReturnTypes

        void setReturnTypes​(java.lang.Class[] returnTypes)
        Defines the return types of the generated methods. The meaning of null elements is implementation-dependent.
      • setMethodNames

        void setMethodNames​(java.lang.String[] methodNames)
        Same as setMethodName(String), but for multiple scripts.

        Define the names of the generated methods. By default the methods have distinct and implementation-specific names.

        If two scripts have the same name, then they must have different parameter types (see setParameters(String[][], Class[][])).

      • setParameters

        void setParameters​(java.lang.String[][] names,
                           java.lang.Class[][] types)
        Same as setParameters(String[], Class[]), but for multiple scripts.
      • setThrownExceptions

        void setThrownExceptions​(java.lang.Class[][] thrownExceptions)
        Same as setThrownExceptions(Class[]), but for multiple scripts.
      • cook

        void cook​(java.lang.String[] optionalFileNames,
                  java.io.Reader[] readers)
           throws CompileException,
                  java.io.IOException
        Same as ICookable.cook(String, Reader), but cooks a set of scripts into one class. Notice that if any of the scripts causes trouble, the entire compilation will fail. If you need to report which of the scripts causes the exception, you may want to use the optionalFileNames parameter to distinguish between the individual token sources.

        If and only if the number of scanners is one, then that single script may contain leading IMPORT directives.

        Throws:
        java.lang.IllegalStateException - if any of the preceding set...() had an array size different from that of scanners
        CompileException
        java.io.IOException
      • evaluate

        java.lang.Object evaluate​(int idx,
                                  java.lang.Object[] arguments)
                           throws java.lang.reflect.InvocationTargetException
        Same as evaluate(Object[]), but for multiple scripts.
        Throws:
        java.lang.reflect.InvocationTargetException
      • getMethod

        java.lang.reflect.Method getMethod​(int idx)
        Same as getMethod(), but for multiple scripts.
      • createFastEvaluator

        <T> java.lang.Object createFastEvaluator​(java.io.Reader reader,
                                                 java.lang.Class<T> interfaceToImplement,
                                                 java.lang.String[] parameterNames)
                                          throws CompileException,
                                                 java.io.IOException
        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 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)).
        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