st.extreme.jython.util
Class JyClass

java.lang.Object
  extended by st.extreme.jython.util.JyClass

public class JyClass
extends java.lang.Object

Utility class to:

The recommended way is to use forScript(String, Class), and newInstance(Class) afterwards.


Method Summary
static javax.script.CompiledScript compile(java.lang.String script, javax.script.Bindings bindings)
          Compile a script.
static JyClass forScript(java.lang.String script, java.lang.Class targetClass)
          Create a JyClass out of a script.
static
<T> T
newCompiledInstance(java.lang.Class<T> clazz, javax.script.CompiledScript compiledScript, javax.script.Bindings bindings, java.lang.String varName)
          Create a compiled instance of a class implemented by a compiled script.
static
<T> T
newCompiledInstance(java.lang.Class<T> clazz, javax.script.CompiledScript compiledScript, java.lang.String varName)
          Create a compiled instance of a class implemented by a compiled script.
<T> T
newInstance(java.lang.Class<T> clazz)
          Create a new instance.
<T> T
newInstance(java.lang.Class<T> clazz, java.lang.Object... params)
          Create a new instance.
static
<I> I
newInterpretedInstance(java.lang.Class<I> interfaze, java.lang.String script, javax.script.Bindings bindings, java.lang.String varName)
          Create an interpreted instance of a class implementing a given interface.
static
<I> I
newInterpretedInstance(java.lang.Class<I> interfaze, java.lang.String script, java.lang.String varName)
          Create an interpreted instance of a class implementing a given interface.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newInterpretedInstance

public static <I> I newInterpretedInstance(java.lang.Class<I> interfaze,
                                           java.lang.String script,
                                           java.lang.String varName)
                                throws javax.script.ScriptException
Create an interpreted instance of a class implementing a given interface.

For a more detailed description, see overloaded method.

Returns:
the instance, null if no instance found
Throws:
javax.script.ScriptException
See Also:
newInterpretedInstance(Class, String, Bindings, String)

newInterpretedInstance

public static <I> I newInterpretedInstance(java.lang.Class<I> interfaze,
                                           java.lang.String script,
                                           javax.script.Bindings bindings,
                                           java.lang.String varName)
                                throws javax.script.ScriptException
Create an interpreted instance of a class implementing a given interface.

The script is parsed, compiled and executed every time this method is called.

Example:

 from java.awt import Color
 from st.extreme.jython import SecondHand
 
 class MySimpleSecondHand(SecondHand):
   def getColor(self):
     return Color.RED
 
 # caller expects a variable named 'secondHand':
 secondHand = MySimpleSecondHand()
 

Parameters:
interfaze - The expected interface - it has to be an interface
script - The script defining and instantiating the class
bindings - The Bindings if present, null otherwise
varName - The name of the variable designating the instance
Returns:
the instance, null if no instance found
Throws:
javax.script.ScriptException
See Also:
Invocable.getInterface(Object, Class)

compile

public static javax.script.CompiledScript compile(java.lang.String script,
                                                  javax.script.Bindings bindings)
                                           throws javax.script.ScriptException
Compile a script.

A compiled script is only parsed and compiled once.

Parameters:
script - The script
bindings - The Bindings
Returns:
The compiled script
Throws:
javax.script.ScriptException
See Also:
newCompiledInstance(Class, CompiledScript, String)

newCompiledInstance

public static <T> T newCompiledInstance(java.lang.Class<T> clazz,
                                        javax.script.CompiledScript compiledScript,
                                        java.lang.String varName)
                             throws javax.script.ScriptException
Create a compiled instance of a class implemented by a compiled script.

For a more detailed description, see overloaded method.

Throws:
javax.script.ScriptException
See Also:
newCompiledInstance(Class, CompiledScript, Bindings, String)

newCompiledInstance

public static <T> T newCompiledInstance(java.lang.Class<T> clazz,
                                        javax.script.CompiledScript compiledScript,
                                        javax.script.Bindings bindings,
                                        java.lang.String varName)
                             throws javax.script.ScriptException
Create a compiled instance of a class implemented by a compiled script.

A compiled script is only parsed and compiled once.

Example:

 from java.awt import Color
 from st.extreme.jython import SecondHand
 
 class MySimpleSecondHand(SecondHand):
   def getColor(self):
     return Color.RED
 
 # caller expects a variable named 'secondHand':
 secondHand = MySimpleSecondHand()
 

Parameters:
clazz - The expected Class
compiledScript - The compiled script defining and instantiating the class
bindings - The Bindings if present, null otherwise
varName - The name of the variable designating the instance
Returns:
the instance, null if no instance found
Throws:
javax.script.ScriptException

forScript

public static JyClass forScript(java.lang.String script,
                                java.lang.Class targetClass)
                         throws javax.script.ScriptException
Create a JyClass out of a script.

The script is assumed to hold a class declaration only.

Multiple instances can be created afterwards, using the newInstance(Class) or newInstance(Class, Object[]) member methods. These instances will share the same Java class inside the JVM.

Example (no instantiation at the end):

 from java.awt import Color
 from st.extreme.jython import SecondHand
 
 class MySimpleSecondHand(SecondHand):
   def getColor(self):
     return Color.RED
 

Parameters:
script - The script defining the class
targetClass - The Java class implemented in this script
Returns:
The JyClass, ready to call newInstance() upon.
Throws:
javax.script.ScriptException
See Also:
newInstance(Class), newInstance(Class, Object[])

newInstance

public <T> T newInstance(java.lang.Class<T> clazz)
              throws javax.script.ScriptException
Create a new instance.

For a more detailed description, see overloaded method.

Throws:
javax.script.ScriptException
See Also:
newInstance(Class, Object[])

newInstance

public <T> T newInstance(java.lang.Class<T> clazz,
                         java.lang.Object... params)
              throws javax.script.ScriptException
Create a new instance.

JyClass is assumed to be created using forScript(String, Class).

Parameters:
clazz - The target Java class. This can be the class used when creating with forScript(String, Class), or a superclass of it.
params - Parameters necessary for creating the instance. Can be null.
Returns:
The instance
Throws:
javax.script.ScriptException
See Also:
forScript(String, Class)