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