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