| java.lang.Object instantj.expression.Expression
Expression | public class Expression implements Serializable(Code) | |
An Expression is a piece of Java-code that evaluates to a result
of a primitive- or Object-type. Therefor it doesn't have any
return-statements and does not end with ';'. Think of an expression
as something you can assign to a variable like this
Object foo = theExpression;
(Note that Object foo = and ; are NOT part of the expression).
Examples for expression are
"Hello World"
account.balance - 100
2 * Math.PI
count--; count<0
The last one is treated a little bit different - this expression consists
of an instruction count--; and the end-result count<0.
During evaluation the instruction(s) is/are executed first and the last
piece of the expression is returned as the result.
A java-expression can evaluate to a Object- or primitive-type. In
the latter case the evaluation of the Expression-object will wrap
the primitive result in a Java-type (e.g. Integer).
This type not only wraps the expression's body but alos its environment
properties. The properties are kept in a map of name/Class that keeps track
of those the expression can use.(e.g. String.class, Account.class or
Integer.TYPE). The expression can reference properties in its environment
as local variables (see above).
Once you have an instance of an expression you can evaluate it in the
environment of property values that you provide. The values are mapped
to the property-names and assigned to the expression's local variables
before execution.
Example 1:
// Our expression body
String body = " 3*4 ";
// Creating the Expression
Expression expression = new Expression(body);
// Evaluate it
Object result = expression.evaluate();
The result of this expression is
(java.lang.Integer) 12
Note: there's no exception-handling in this snippet
Example 2:
// The properties we want to use inside the expression
Hashtable properties = new Hashtable();
properties.put("count", Integer.TYPE);
properties.put("msg" , String.class);
// Our expression body
String body = " count++; msg+new java.util.Date() ";
// Creating the Expression
Expression expression = new Expression(body,properties);
// The values we give to the evaluation
Map values = new HashMap();
values.put("count", new Integer(3));
values.put("msg" , "Guybrush was here on " );
// Evaluate it
Object result = expression.getInstance(values).evaluate();
The result of this expression is
(java.lang.String) Guybrush was here on Dec 17 11:33:31 EST 2000
As an alternative to passing a value map into getInstance() you
can also provide the values for properties programmatically:
// Evaluate it
Object result = expression.getInstance().set("count", new Integer(3)).set("msg", "Guybrush was here on").evaluate();
Note: there's no exception-handling in this snippet; primitive-types
have to wrapped when provided; the time varies :)
author: Nils Meier |
Constructor Summary | |
protected | Expression() | public | Expression(String body) Constructor for a simple expression without properties. | public | Expression(String body, Map properties) Constructor for an expression with properties.
For more information about what an expression is, look at the
Expression class description Parameters: body - the expression body (Java) Parameters: properties - the property-names mapped to types (java.lang.Class or java.lang.String). | public | Expression(String body, Map props, Collection imports) Constructor for an expression with properties.
For more information about what an expression is, look at the
Expression class description Parameters: body - the expression body (Java) Parameters: props - the property-names mapped to types (java.lang.Class or java.lang.String). | public | Expression(String body, Map props, Collection imports, Collection library) Constructor for an expression with properties and a library.
For more information about what an expression is, look at the
Expression class description Parameters: body - the expression body (Java) Parameters: props - the property-names mapped to types (java.lang.Class or java.lang.String). | public | Expression(InputStream body) Constructor for a simple expression without properties. | public | Expression(InputStream body, Map properties) Constructor for an expression with properties.
For more information about what an expression is, look at the
Expression class description Parameters: body - the expression body (Java) Parameters: properties - the property-names mapped to types (java.lang.Class or java.lang.String). | public | Expression(InputStream body, Map properties, Collection imports) Constructor for an expression with properties.
For more information about what an expression is, look at the
Expression class description Parameters: body - the expression body (Java) Parameters: properties - the property-names mapped to types (java.lang.Class or java.lang.String). | public | Expression(InputStream body, Map properties, Collection imports, Collection library) Constructor for an expression with properties.
For more information about what an expression is, look at the
Expression class description Parameters: body - the expression body (Java) Parameters: properties - the property-names mapped to types (java.lang.Class or java.lang.String). |
PARAM_EXPRESSION_DIR | final public static String PARAM_EXPRESSION_DIR(Code) | | a VM flag that will turn on output of generated expressions in specified directory
|
Expression | protected Expression()(Code) | | Constructor for de-serialization
|
Expression | public Expression(String body, Map properties) throws CompilationFailedException(Code) | | Constructor for an expression with properties.
For more information about what an expression is, look at the
Expression class description Parameters: body - the expression body (Java) Parameters: properties - the property-names mapped to types (java.lang.Class or java.lang.String). These will be available to the evaluated expression. exception: CompilationFailedException - when the expression couldn't be compiled exception: IllegalArgumentException - when passed Hashtable does not map names (String) to types (Class) |
Expression | public Expression(String body, Map props, Collection imports) throws CompilationFailedException(Code) | | Constructor for an expression with properties.
For more information about what an expression is, look at the
Expression class description Parameters: body - the expression body (Java) Parameters: props - the property-names mapped to types (java.lang.Class or java.lang.String). These will be available to the evaluated expression. Parameters: imports - imports to use (e.g java.util.*, java.sql.*) exception: CompilationFailedException - when the expression couldn't be compiled exception: IllegalArgumentException - when passed Hashtable does not map names (String) to types (Class) |
Expression | public Expression(String body, Map props, Collection imports, Collection library) throws CompilationFailedException(Code) | | Constructor for an expression with properties and a library.
For more information about what an expression is, look at the
Expression class description Parameters: body - the expression body (Java) Parameters: props - the property-names mapped to types (java.lang.Class or java.lang.String). These will be available to the evaluated expression. Parameters: imports - imports to use (e.g java.util.*, java.sql.*) Parameters: library - of CompiledSources to use exception: CompilationFailedException - when the expression couldn't be compiled exception: IllegalArgumentException - when passed Hashtable does not map names (String) to types (Class) |
Expression | public Expression(InputStream body, Map properties) throws CompilationFailedException, IOException(Code) | | Constructor for an expression with properties.
For more information about what an expression is, look at the
Expression class description Parameters: body - the expression body (Java) Parameters: properties - the property-names mapped to types (java.lang.Class or java.lang.String). These will be available to the evaluated expression. exception: CompilationFailedException - when the expression couldn't be compiled exception: IllegalArgumentException - when passed Hashtable does not map names (String) to types (Class) |
Expression | public Expression(InputStream body, Map properties, Collection imports) throws CompilationFailedException, IOException(Code) | | Constructor for an expression with properties.
For more information about what an expression is, look at the
Expression class description Parameters: body - the expression body (Java) Parameters: properties - the property-names mapped to types (java.lang.Class or java.lang.String). These will be available to the evaluated expression. Parameters: imports - imports to use exception: CompilationFailedException - when the expression couldn't be compiled exception: IllegalArgumentException - when passed Hashtable does not map names (String) to types (Class) |
Expression | public Expression(InputStream body, Map properties, Collection imports, Collection library) throws CompilationFailedException, IOException(Code) | | Constructor for an expression with properties.
For more information about what an expression is, look at the
Expression class description Parameters: body - the expression body (Java) Parameters: properties - the property-names mapped to types (java.lang.Class or java.lang.String). These will be available to the evaluated expression. Parameters: imports - imports to use exception: CompilationFailedException - when the expression couldn't be compiled exception: IllegalArgumentException - when passed Hashtable does not map names (String) to types (Class) |
getInstance | public ExpressionInstance getInstance(Map context) throws IllegalPropertyException(Code) | | Returns an ExpressionInstance with its properties' preset to values
in given context.
Parameters: context - a context for the evaluation of the expression. Itmaps property-names to -values. All properties specified inthe expression's constructor will be preset with values fromthe map if possible.Either provide values of according property-type or values that can be used in a one-argument-constructor of the according property-type. exception: IllegalPropertyException - when a property doesn't match the expression's properties the result |
|
|