jdk/src/share/classes/java/beans/Expression.java
author mcimadamore
Thu, 01 Dec 2011 18:34:23 +0000
changeset 11120 f8576c769572
parent 5506 202f599c92aa
child 19213 c360667a0da2
permissions -rw-r--r--
7116954: Misc warnings in java.beans/java.beans.context Summary: Remove generic warnings form java.beans and java.beans.context Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5131
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5131
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5131
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5131
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5131
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5131
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.beans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * An <code>Expression</code> object represents a primitive expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * in which a single method is applied to a target and a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * arguments to return a result - as in <code>"a.getFoo()"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * In addition to the properties of the super class, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <code>Expression</code> object provides a <em>value</em> which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * is the object returned when this expression is evaluated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The return value is typically not provided by the caller and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * is instead computed by dynamically finding the method and invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * it when the first call to <code>getValue</code> is made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class Expression extends Statement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static Object unbound = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private Object value = unbound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    54
     * Creates a new {@link Expression} object
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    55
     * for the specified target object to invoke the method
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    56
     * specified by the name and by the array of arguments.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    57
     * <p>
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    58
     * The {@code target} and the {@code methodName} values should not be {@code null}.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    59
     * Otherwise an attempt to execute this {@code Expression}
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    60
     * will result in a {@code NullPointerException}.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    61
     * If the {@code arguments} value is {@code null},
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    62
     * an empty array is used as the value of the {@code arguments} property.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    64
     * @param target  the target object of this expression
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    65
     * @param methodName  the name of the method to invoke on the specified target
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    66
     * @param arguments  the array of arguments to invoke the specified method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    @ConstructorProperties({"target", "methodName", "arguments"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public Expression(Object target, String methodName, Object[] arguments) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super(target, methodName, arguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    76
     * Creates a new {@link Expression} object with the specified value
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    77
     * for the specified target object to invoke the  method
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    78
     * specified by the name and by the array of arguments.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    79
     * The {@code value} value is used as the value of the {@code value} property,
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    80
     * so the {@link #getValue} method will return it
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    81
     * without executing this {@code Expression}.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    82
     * <p>
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    83
     * The {@code target} and the {@code methodName} values should not be {@code null}.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    84
     * Otherwise an attempt to execute this {@code Expression}
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    85
     * will result in a {@code NullPointerException}.
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    86
     * If the {@code arguments} value is {@code null},
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    87
     * an empty array is used as the value of the {@code arguments} property.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
4845
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    89
     * @param value  the value of this expression
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    90
     * @param target  the target object of this expression
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    91
     * @param methodName  the name of the method to invoke on the specified target
64433d7632f2 4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents: 2
diff changeset
    92
     * @param arguments  the array of arguments to invoke the specified method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public Expression(Object value, Object target, String methodName, Object[] arguments) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this(target, methodName, arguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
5131
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   102
     * {@inheritDoc}
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   103
     * <p>
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   104
     * If the invoked method completes normally,
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   105
     * the value it returns is copied in the {@code value} property.
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   106
     * Note that the {@code value} property is set to {@code null},
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   107
     * if the return type of the underlying method is {@code void}.
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   108
     *
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   109
     * @throws NullPointerException if the value of the {@code target} or
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   110
     *                              {@code methodName} property is {@code null}
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   111
     * @throws NoSuchMethodException if a matching method is not found
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   112
     * @throws SecurityException if a security manager exists and
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   113
     *                           it denies the method invocation
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   114
     * @throws Exception that is thrown by the invoked method
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   115
     *
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   116
     * @see java.lang.reflect.Method
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   117
     * @since 1.7
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   118
     */
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   119
    @Override
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   120
    public void execute() throws Exception {
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   121
        setValue(invoke());
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   122
    }
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   123
03037c4967f4 6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents: 4845
diff changeset
   124
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * If the value property of this instance is not already set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * this method dynamically finds the method with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * methodName on this target with these arguments and calls it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * The result of the method invocation is first copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * into the value property of this expression and then returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * as the result of <code>getValue</code>. If the value property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * was already set, either by a call to <code>setValue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * or a previous call to <code>getValue</code> then the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * property is returned without either looking up or calling the method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * The value property of an <code>Expression</code> is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * a unique private (non-<code>null</code>) value by default and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * this value is used as an internal indication that the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * has not yet been called. A return value of <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * replaces this default value in the same way that any other value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * would, ensuring that expressions are never evaluated more than once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * See the <code>excecute</code> method for details on how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * methods are chosen using the dynamic types of the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * and arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @see Statement#execute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return The result of applying this method to these arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public Object getValue() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (value == unbound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            setValue(invoke());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Sets the value of this expression to <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * This value will be returned by the getValue method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * without calling the method associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * expression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param value The value of this expression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public void setValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /*pp*/ String instanceName(Object instance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return instance == unbound ? "<unbound>" : super.instanceName(instance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Prints the value of this expression using a Java-style syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return instanceName(value) + "=" + super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}