KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > beans > Expression


1 /*
2  * @(#)Expression.java 1.13 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.beans;
9
10 /**
11  * An <code>Expression</code> object represents a primitive expression
12  * in which a single method is applied to a target and a set of
13  * arguments to return a result - as in <code>"a.getFoo()"</code>.
14  * <p>
15  * In addition to the properties of the super class, the
16  * <code>Expression</code> object provides a <em>value</em> which
17  * is the object returned when this expression is evaluated.
18  * The return value is typically not provided by the caller and
19  * is instead computed by dynamically finding the method and invoking
20  * it when the first call to <code>getValue</code> is made.
21  *
22  * @see #getValue
23  * @see #setValue
24  *
25  * @since 1.4
26  *
27  * @version 1.3 11/15/00
28  * @author Philip Milne
29  */

30 public class Expression extends Statement JavaDoc {
31
32     private static Object JavaDoc unbound = new Object JavaDoc();
33     
34     private Object JavaDoc value = unbound;
35     
36     /**
37      * Creates a new <code>Statement</code> object with a <code>target</code>,
38      * <code>methodName</code> and <code>arguments</code> as per the parameters.
39      *
40      * @param target The target of this expression.
41      * @param methodName The methodName of this expression.
42      * @param arguments The arguments of this expression. If <code>null</code> then an empty array will be used.
43      *
44      * @see #getValue
45      */

46     public Expression(Object JavaDoc target, String JavaDoc methodName, Object JavaDoc[] arguments) {
47         super(target, methodName, arguments);
48     }
49     
50     /**
51      * Creates a new <code>Expression</code> object for a method
52      * that returns a result. The result will never be calculated
53      * however, since this constructor uses the <code>value</code>
54      * parameter to set the value property by calling the
55      * <code>setValue</code> method.
56      *
57      * @param value The value of this expression.
58      * @param target The target of this expression.
59      * @param methodName The methodName of this expression.
60      * @param arguments The arguments of this expression. If <code>null</code> then an empty array will be used.
61      *
62      * @see #setValue
63      */

64     public Expression(Object JavaDoc value, Object JavaDoc target, String JavaDoc methodName, Object JavaDoc[] arguments) {
65         this(target, methodName, arguments);
66         setValue(value);
67     }
68     
69     /**
70      * If the value property of this instance is not already set,
71      * this method dynamically finds the method with the specified
72      * methodName on this target with these arguments and calls it.
73      * The result of the method invocation is first copied
74      * into the value property of this expression and then returned
75      * as the result of <code>getValue</code>. If the value property
76      * was already set, either by a call to <code>setValue</code>
77      * or a previous call to <code>getValue</code> then the value
78      * property is returned without either looking up or calling the method.
79      * <p>
80      * The value property of an <code>Expression</code> is set to
81      * a unique private (non-<code>null</code>) value by default and
82      * this value is used as an internal indication that the method
83      * has not yet been called. A return value of <code>null</code>
84      * replaces this default value in the same way that any other value
85      * would, ensuring that expressions are never evaluated more than once.
86      * <p>
87      * See the <code>excecute<code> method for details on how
88      * methods are chosen using the dynamic types of the target
89      * and arguments.
90      *
91      * @see Statement#execute
92      * @see #setValue
93      *
94      * @return The result of applying this method to these arguments.
95      */

96     public Object JavaDoc getValue() throws Exception JavaDoc {
97         if (value == unbound) {
98             setValue(invoke());
99         }
100         return value;
101     }
102     
103     /**
104      * Sets the value of this expression to <code>value</code>.
105      * This value will be returned by the getValue method
106      * without calling the method associated with this
107      * expression.
108      *
109      * @param value The value of this expression.
110      *
111      * @see #getValue
112      */

113     public void setValue(Object JavaDoc value) {
114         this.value = value;
115     }
116     
117     /*pp*/ String JavaDoc instanceName(Object JavaDoc instance) {
118         return instance == unbound ? "<unbound>" : super.instanceName(instance);
119     }
120     
121     /**
122      * Prints the value of this expression using a Java-style syntax.
123      */

124     public String JavaDoc toString() {
125         return instanceName(value) + "=" + super.toString();
126     }
127 }
128
Popular Tags