001 /*
002 * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.beans;
027
028 /**
029 * An <code>Expression</code> object represents a primitive expression
030 * in which a single method is applied to a target and a set of
031 * arguments to return a result - as in <code>"a.getFoo()"</code>.
032 * <p>
033 * In addition to the properties of the super class, the
034 * <code>Expression</code> object provides a <em>value</em> which
035 * is the object returned when this expression is evaluated.
036 * The return value is typically not provided by the caller and
037 * is instead computed by dynamically finding the method and invoking
038 * it when the first call to <code>getValue</code> is made.
039 *
040 * @see #getValue
041 * @see #setValue
042 *
043 * @since 1.4
044 *
045 * @version 1.3 11/15/00
046 * @author Philip Milne
047 */
048 public class Expression extends Statement {
049
050 private static Object unbound = new Object();
051
052 private Object value = unbound;
053
054 /**
055 * Creates a new <code>Statement</code> object with a <code>target</code>,
056 * <code>methodName</code> and <code>arguments</code> as per the parameters.
057 *
058 * @param target The target of this expression.
059 * @param methodName The methodName of this expression.
060 * @param arguments The arguments of this expression. If <code>null</code> then an empty array will be used.
061 *
062 * @see #getValue
063 */
064 @ConstructorProperties({"target","methodName","arguments"})
065 public Expression(Object target, String methodName,
066 Object[] arguments) {
067 super (target, methodName, arguments);
068 }
069
070 /**
071 * Creates a new <code>Expression</code> object for a method
072 * that returns a result. The result will never be calculated
073 * however, since this constructor uses the <code>value</code>
074 * parameter to set the value property by calling the
075 * <code>setValue</code> method.
076 *
077 * @param value The value of this expression.
078 * @param target The target of this expression.
079 * @param methodName The methodName of this expression.
080 * @param arguments The arguments of this expression. If <code>null</code> then an empty array will be used.
081 *
082 * @see #setValue
083 */
084 public Expression(Object value, Object target, String methodName,
085 Object[] arguments) {
086 this (target, methodName, arguments);
087 setValue(value);
088 }
089
090 /**
091 * If the value property of this instance is not already set,
092 * this method dynamically finds the method with the specified
093 * methodName on this target with these arguments and calls it.
094 * The result of the method invocation is first copied
095 * into the value property of this expression and then returned
096 * as the result of <code>getValue</code>. If the value property
097 * was already set, either by a call to <code>setValue</code>
098 * or a previous call to <code>getValue</code> then the value
099 * property is returned without either looking up or calling the method.
100 * <p>
101 * The value property of an <code>Expression</code> is set to
102 * a unique private (non-<code>null</code>) value by default and
103 * this value is used as an internal indication that the method
104 * has not yet been called. A return value of <code>null</code>
105 * replaces this default value in the same way that any other value
106 * would, ensuring that expressions are never evaluated more than once.
107 * <p>
108 * See the <code>excecute</code> method for details on how
109 * methods are chosen using the dynamic types of the target
110 * and arguments.
111 *
112 * @see Statement#execute
113 * @see #setValue
114 *
115 * @return The result of applying this method to these arguments.
116 */
117 public Object getValue() throws Exception {
118 if (value == unbound) {
119 setValue(invoke());
120 }
121 return value;
122 }
123
124 /**
125 * Sets the value of this expression to <code>value</code>.
126 * This value will be returned by the getValue method
127 * without calling the method associated with this
128 * expression.
129 *
130 * @param value The value of this expression.
131 *
132 * @see #getValue
133 */
134 public void setValue(Object value) {
135 this .value = value;
136 }
137
138 /*pp*/String instanceName(Object instance) {
139 return instance == unbound ? "<unbound>" : super
140 .instanceName(instance);
141 }
142
143 /**
144 * Prints the value of this expression using a Java-style syntax.
145 */
146 public String toString() {
147 return instanceName(value) + "=" + super.toString();
148 }
149 }
|