jdk/src/share/classes/java/beans/EventHandler.java
author darcy
Tue, 06 Aug 2013 16:01:39 -0700
changeset 19213 c360667a0da2
parent 11120 f8576c769572
child 20432 5060b1928712
permissions -rw-r--r--
8022406: Fix doclint issues in java.beans Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19213
c360667a0da2 8022406: Fix doclint issues in java.beans
darcy
parents: 11120
diff changeset
     2
 * Copyright (c) 2000, 2013, 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: 5185
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: 5185
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: 5185
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5185
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5185
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.beans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.reflect.InvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.reflect.misc.MethodUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The <code>EventHandler</code> class provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * support for dynamically generating event listeners whose methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * execute a simple statement involving an incoming event object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * and a target object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The <code>EventHandler</code> class is intended to be used by interactive tools, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * application builders, that allow developers to make connections between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * beans. Typically connections are made from a user interface bean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * (the event <em>source</em>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * to an application logic bean (the <em>target</em>). The most effective
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * connections of this kind isolate the application logic from the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * interface.  For example, the <code>EventHandler</code> for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * connection from a <code>JCheckBox</code> to a method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * that accepts a boolean value can deal with extracting the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * of the check box and passing it directly to the method so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * the method is isolated from the user interface layer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Inner classes are another, more general way to handle events from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * user interfaces.  The <code>EventHandler</code> class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * handles only a subset of what is possible using inner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * classes. However, <code>EventHandler</code> works better
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * with the long-term persistence scheme than inner classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Also, using <code>EventHandler</code> in large applications in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * which the same interface is implemented many times can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * reduce the disk and memory footprint of the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * The reason that listeners created with <code>EventHandler</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * have such a small
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * footprint is that the <code>Proxy</code> class, on which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * the <code>EventHandler</code> relies, shares implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * of identical
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * interfaces. For example, if you use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * the <code>EventHandler</code> <code>create</code> methods to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * all the <code>ActionListener</code>s in an application,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * all the action listeners will be instances of a single class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * (one created by the <code>Proxy</code> class).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * In general, listeners based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * the <code>Proxy</code> class require one listener class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * to be created per <em>listener type</em> (interface),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * whereas the inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * approach requires one class to be created per <em>listener</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * (object that implements the interface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * You don't generally deal directly with <code>EventHandler</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * Instead, you use one of the <code>EventHandler</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <code>create</code> methods to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * an object that implements a given listener interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * This listener object uses an <code>EventHandler</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * behind the scenes to encapsulate information about the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * event, the object to be sent a message when the event occurs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * the message (method) to be sent, and any argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * to the method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * The following section gives examples of how to create listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * objects using the <code>create</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <h2>Examples of Using EventHandler</h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * The simplest use of <code>EventHandler</code> is to install
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * a listener that calls a method on the target object with no arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * In the following example we create an <code>ActionListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * that invokes the <code>toFront</code> method on an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * of <code>javax.swing.JFrame</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *myButton.addActionListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *    (ActionListener)EventHandler.create(ActionListener.class, frame, "toFront"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * When <code>myButton</code> is pressed, the statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <code>frame.toFront()</code> will be executed.  One could get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * the same effect, with some additional compile-time type safety,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * by defining a new implementation of the <code>ActionListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * interface and adding an instance of it to the button:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
//Equivalent code using an inner class instead of EventHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *myButton.addActionListener(new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *        frame.toFront();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * The next simplest use of <code>EventHandler</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * to extract a property value from the first argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * of the method in the listener interface (typically an event object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * and use it to set the value of a property in the target object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * In the following example we create an <code>ActionListener</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * sets the <code>nextFocusableComponent</code> property of the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * (myButton) object to the value of the "source" property of the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *EventHandler.create(ActionListener.class, myButton, "nextFocusableComponent", "source")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * This would correspond to the following inner class implementation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
//Equivalent code using an inner class instead of EventHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *        myButton.setNextFocusableComponent((Component)e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * It's also possible to create an <code>EventHandler</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * just passes the incoming event object to the target's action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * If the fourth <code>EventHandler.create</code> argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * an empty string, then the event is just passed along:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *EventHandler.create(ActionListener.class, target, "doActionEvent", "")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * This would correspond to the following inner class implementation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
//Equivalent code using an inner class instead of EventHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *        target.doActionEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * Probably the most common use of <code>EventHandler</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * is to extract a property value from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * <em>source</em> of the event object and set this value as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * the value of a property of the target object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * In the following example we create an <code>ActionListener</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * sets the "label" property of the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * object to the value of the "text" property of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * source (the value of the "source" property) of the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *EventHandler.create(ActionListener.class, myButton, "label", "source.text")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * This would correspond to the following inner class implementation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
//Equivalent code using an inner class instead of EventHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *new ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *        myButton.setLabel(((JTextField)e.getSource()).getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * The event property may be "qualified" with an arbitrary number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * of property prefixes delimited with the "." character. The "qualifying"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * names that appear before the "." characters are taken as the names of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * properties that should be applied, left-most first, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * the event object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * For example, the following action listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 *EventHandler.create(ActionListener.class, target, "a", "b.c.d")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * might be written as the following inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * (assuming all the properties had canonical getter methods and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * returned the appropriate types):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
//Equivalent code using an inner class instead of EventHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 *new ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 *    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 *        target.setA(e.getB().getC().isD());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * The target property may also be "qualified" with an arbitrary number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * of property prefixs delimited with the "." character.  For example, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * following action listener:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *   EventHandler.create(ActionListener.class, target, "a.b", "c.d")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 * might be written as the following inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * (assuming all the properties had canonical getter methods and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * returned the appropriate types):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 *   //Equivalent code using an inner class instead of EventHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *   new ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *     public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 *         target.getA().setB(e.getC().isD());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * As <code>EventHandler</code> ultimately relies on reflection to invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * a method we recommend against targeting an overloaded method.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 * if the target is an instance of the class <code>MyTarget</code> which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * defined as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 *   public class MyTarget {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 *     public void doIt(String);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 *     public void doIt(Object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * Then the method <code>doIt</code> is overloaded.  EventHandler will invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * the method that is appropriate based on the source.  If the source is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * null, then either method is appropriate and the one that is invoked is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 * undefined.  For that reason we recommend against targeting overloaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 * @see java.lang.reflect.Proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 * @see java.util.EventObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 * @author Mark Davidson
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
public class EventHandler implements InvocationHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private Object target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private String action;
5185
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 1851
diff changeset
   281
    private final String eventPropertyName;
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 1851
diff changeset
   282
    private final String listenerMethodName;
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 1851
diff changeset
   283
    private final AccessControlContext acc = AccessController.getContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Creates a new <code>EventHandler</code> object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * you generally use one of the <code>create</code> methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * instead of invoking this constructor directly.  Refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * {@link java.beans.EventHandler#create(Class, Object, String, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * the general version of create} for a complete description of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * the <code>eventPropertyName</code> and <code>listenerMethodName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param target the object that will perform the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param action the name of a (possibly qualified) property or method on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *        the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param eventPropertyName the (possibly qualified) name of a readable property of the incoming event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param listenerMethodName the name of the method in the listener interface that should trigger the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @throws NullPointerException if <code>target</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @throws NullPointerException if <code>action</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @see EventHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @see #create(Class, Object, String, String, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @see #getTarget
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @see #getAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @see #getEventPropertyName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @see #getListenerMethodName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    @ConstructorProperties({"target", "action", "eventPropertyName", "listenerMethodName"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public EventHandler(Object target, String action, String eventPropertyName, String listenerMethodName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        this.action = action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            throw new NullPointerException("target must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (action == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            throw new NullPointerException("action must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        this.eventPropertyName = eventPropertyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        this.listenerMethodName = listenerMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Returns the object to which this event handler will send a message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @return the target of this event handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @see #EventHandler(Object, String, String, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public Object getTarget()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Returns the name of the target's writable property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * that this event handler will set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * or the name of the method that this event handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * will invoke on the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @return the action of this event handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @see #EventHandler(Object, String, String, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public String getAction()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Returns the property of the event that should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * used in the action applied to the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @return the property of the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see #EventHandler(Object, String, String, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public String getEventPropertyName()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return eventPropertyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Returns the name of the method that will trigger the action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * A return value of <code>null</code> signifies that all methods in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * listener interface trigger the action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @return the name of the method that will trigger the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @see #EventHandler(Object, String, String, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public String getListenerMethodName()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return listenerMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private Object applyGetters(Object target, String getters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (getters == null || getters.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        int firstDot = getters.indexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (firstDot == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            firstDot = getters.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        String first = getters.substring(0, firstDot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        String rest = getters.substring(Math.min(firstDot + 1, getters.length()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            Method getter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (target != null) {
1851
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 1299
diff changeset
   386
                getter = Statement.getMethod(target.getClass(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                      "get" + NameGenerator.capitalize(first),
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5506
diff changeset
   388
                                      new Class<?>[]{});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                if (getter == null) {
1851
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 1299
diff changeset
   390
                    getter = Statement.getMethod(target.getClass(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                   "is" + NameGenerator.capitalize(first),
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5506
diff changeset
   392
                                   new Class<?>[]{});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                if (getter == null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5506
diff changeset
   395
                    getter = Statement.getMethod(target.getClass(), first, new Class<?>[]{});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            if (getter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                throw new RuntimeException("No method called: " + first +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                                           " defined on " + target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            Object newTarget = MethodUtil.invoke(getter, target, new Object[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            return applyGetters(newTarget, rest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
1280
f58fc9f575e3 6351692: catch(Throwable) in java.beans.MetaData preventing thread shutdown
malenkov
parents: 674
diff changeset
   405
        catch (Exception e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            throw new RuntimeException("Failed to call method: " + first +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                       " on " + target, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Extract the appropriate property value from the event and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * pass it to the action associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * this <code>EventHandler</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @param proxy the proxy object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param method the method in the listener interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @return the result of applying the action to the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @see EventHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public Object invoke(final Object proxy, final Method method, final Object[] arguments) {
5185
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 1851
diff changeset
   423
        AccessControlContext acc = this.acc;
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 1851
diff changeset
   424
        if ((acc == null) && (System.getSecurityManager() != null)) {
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 1851
diff changeset
   425
            throw new SecurityException("AccessControlContext is not set");
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 1851
diff changeset
   426
        }
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 1851
diff changeset
   427
        return AccessController.doPrivileged(new PrivilegedAction<Object>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                return invokeInternal(proxy, method, arguments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    private Object invokeInternal(Object proxy, Method method, Object[] arguments) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        String methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (method.getDeclaringClass() == Object.class)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            // Handle the Object public methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            if (methodName.equals("hashCode"))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                return new Integer(System.identityHashCode(proxy));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            } else if (methodName.equals("equals")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                return (proxy == arguments[0] ? Boolean.TRUE : Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            } else if (methodName.equals("toString")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                return proxy.getClass().getName() + '@' + Integer.toHexString(proxy.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (listenerMethodName == null || listenerMethodName.equals(methodName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            Class[] argTypes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            Object[] newArgs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            if (eventPropertyName == null) {     // Nullary method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                newArgs = new Object[]{};
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5506
diff changeset
   453
                argTypes = new Class<?>[]{};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                Object input = applyGetters(arguments[0], getEventPropertyName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                newArgs = new Object[]{input};
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5506
diff changeset
   458
                argTypes = new Class<?>[]{input == null ? null :
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                       input.getClass()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                int lastDot = action.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                if (lastDot != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    target = applyGetters(target, action.substring(0, lastDot));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    action = action.substring(lastDot + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                }
1851
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 1299
diff changeset
   467
                Method targetMethod = Statement.getMethod(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                             target.getClass(), action, argTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                if (targetMethod == null) {
1851
8203c7eb8caf 6788531: java.beans.Statement imposes excessive access control
malenkov
parents: 1299
diff changeset
   470
                    targetMethod = Statement.getMethod(target.getClass(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                             "set" + NameGenerator.capitalize(action), argTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                if (targetMethod == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    String argTypeString = (argTypes.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        ? " with no arguments"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        : " with argument " + argTypes[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        "No method called " + action + " on " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                        target.getClass() + argTypeString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                return MethodUtil.invoke(targetMethod, target, newArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            catch (IllegalAccessException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                throw new RuntimeException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            catch (InvocationTargetException ex) {
5185
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 1851
diff changeset
   487
                Throwable th = ex.getTargetException();
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 1851
diff changeset
   488
                throw (th instanceof RuntimeException)
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 1851
diff changeset
   489
                        ? (RuntimeException) th
e29dad1b6945 6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents: 1851
diff changeset
   490
                        : new RuntimeException(th);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Creates an implementation of <code>listenerInterface</code> in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * <em>all</em> of the methods in the listener interface apply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * the handler's <code>action</code> to the <code>target</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * method is implemented by calling the other, more general,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * implementation of the <code>create</code> method with both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * the <code>eventPropertyName</code> and the <code>listenerMethodName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * taking the value <code>null</code>. Refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * {@link java.beans.EventHandler#create(Class, Object, String, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * the general version of create} for a complete description of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * the <code>action</code> parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * To create an <code>ActionListener</code> that shows a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * <code>JDialog</code> with <code>dialog.show()</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * one can write:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *<blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *EventHandler.create(ActionListener.class, dialog, "show")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *</blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
19213
c360667a0da2 8022406: Fix doclint issues in java.beans
darcy
parents: 11120
diff changeset
   518
     * @param <T> the type to create
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @param listenerInterface the listener interface to create a proxy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @param target the object that will perform the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @param action the name of a (possibly qualified) property or method on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *        the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @return an object that implements <code>listenerInterface</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @throws NullPointerException if <code>listenerInterface</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @throws NullPointerException if <code>target</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @throws NullPointerException if <code>action</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @see #create(Class, Object, String, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public static <T> T create(Class<T> listenerInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                               Object target, String action)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        return create(listenerInterface, target, action, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Creates an implementation of <code>listenerInterface</code> in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * <em>all</em> of the methods pass the value of the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * expression, <code>eventPropertyName</code>, to the final method in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * statement, <code>action</code>, which is applied to the <code>target</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * This method is implemented by calling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * more general, implementation of the <code>create</code> method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * the <code>listenerMethodName</code> taking the value <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * Refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * {@link java.beans.EventHandler#create(Class, Object, String, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * the general version of create} for a complete description of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * the <code>action</code> and <code>eventPropertyName</code> parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * To create an <code>ActionListener</code> that sets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * the text of a <code>JLabel</code> to the text value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * the <code>JTextField</code> source of the incoming event,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * you can use the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *<blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *EventHandler.create(ActionListener.class, label, "text", "source.text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *</blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * This is equivalent to the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *<blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
//Equivalent code using an inner class instead of EventHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *    public void actionPerformed(ActionEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *        label.setText(((JTextField)(event.getSource())).getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *</blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *
19213
c360667a0da2 8022406: Fix doclint issues in java.beans
darcy
parents: 11120
diff changeset
   574
     * @param <T> the type to create
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @param listenerInterface the listener interface to create a proxy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @param target the object that will perform the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @param action the name of a (possibly qualified) property or method on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *        the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @param eventPropertyName the (possibly qualified) name of a readable property of the incoming event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @return an object that implements <code>listenerInterface</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @throws NullPointerException if <code>listenerInterface</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @throws NullPointerException if <code>target</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @throws NullPointerException if <code>action</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @see #create(Class, Object, String, String, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public static <T> T create(Class<T> listenerInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                               Object target, String action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                               String eventPropertyName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return create(listenerInterface, target, action, eventPropertyName, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Creates an implementation of <code>listenerInterface</code> in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * the method named <code>listenerMethodName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * passes the value of the event expression, <code>eventPropertyName</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * to the final method in the statement, <code>action</code>, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * is applied to the <code>target</code>. All of the other listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * methods do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * The <code>eventPropertyName</code> string is used to extract a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * from the incoming event object that is passed to the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * method.  The common case is the target method takes no arguments, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * which case a value of null should be used for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * <code>eventPropertyName</code>.  Alternatively if you want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * the incoming event object passed directly to the target method use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * The format of the <code>eventPropertyName</code> string is a sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * methods or properties where each method or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * property is applied to the value returned by the preceeding method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * starting from the incoming event object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * The syntax is: <code>propertyName{.propertyName}*</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * where <code>propertyName</code> matches a method or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * property.  For example, to extract the <code>point</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * property from a <code>MouseEvent</code>, you could use either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * <code>"point"</code> or <code>"getPoint"</code> as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * <code>eventPropertyName</code>.  To extract the "text" property from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * a <code>MouseEvent</code> with a <code>JLabel</code> source use any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * of the following as <code>eventPropertyName</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * <code>"source.text"</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * <code>"getSource.text"</code> <code>"getSource.getText"</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <code>"source.getText"</code>.  If a method can not be found, or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * exception is generated as part of invoking a method a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * <code>RuntimeException</code> will be thrown at dispatch time.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * example, if the incoming event object is null, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <code>eventPropertyName</code> is non-null and not empty, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <code>RuntimeException</code> will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * The <code>action</code> argument is of the same format as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * <code>eventPropertyName</code> argument where the last property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * identifies either a method name or writable property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * If the <code>listenerMethodName</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * <em>all</em> methods in the interface trigger the <code>action</code> to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * executed on the <code>target</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * For example, to create a <code>MouseListener</code> that sets the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * object's <code>origin</code> property to the incoming <code>MouseEvent</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * location (that's the value of <code>mouseEvent.getPoint()</code>) each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * time a mouse button is pressed, one would write:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *<blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *<pre>
674
4867bdd4fa38 6668273: Example given in java.beans.EventHandler shows incorrect order of parameters
malenkov
parents: 2
diff changeset
   646
     *EventHandler.create(MouseListener.class, target, "origin", "point", "mousePressed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *</blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * This is comparable to writing a <code>MouseListener</code> in which all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * of the methods except <code>mousePressed</code> are no-ops:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *<blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
//Equivalent code using an inner class instead of EventHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *new MouseAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *    public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *        target.setOrigin(e.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *</blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
19213
c360667a0da2 8022406: Fix doclint issues in java.beans
darcy
parents: 11120
diff changeset
   664
     * @param <T> the type to create
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @param listenerInterface the listener interface to create a proxy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @param target the object that will perform the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @param action the name of a (possibly qualified) property or method on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *        the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @param eventPropertyName the (possibly qualified) name of a readable property of the incoming event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @param listenerMethodName the name of the method in the listener interface that should trigger the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @return an object that implements <code>listenerInterface</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @throws NullPointerException if <code>listenerInterface</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @throws NullPointerException if <code>target</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @throws NullPointerException if <code>action</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @see EventHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     */
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5506
diff changeset
   680
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    public static <T> T create(Class<T> listenerInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                               Object target, String action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                               String eventPropertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                               String listenerMethodName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        // Create this first to verify target/action are non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        EventHandler eventHandler = new EventHandler(target, action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                                                     eventPropertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                                                     listenerMethodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        if (listenerInterface == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            throw new NullPointerException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                          "listenerInterface must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        return (T)Proxy.newProxyInstance(target.getClass().getClassLoader(),
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5506
diff changeset
   695
                                         new Class<?>[] {listenerInterface},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                                         eventHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
}