1 /*
2 * Copyright (c) 2000, 2014, 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
509 * To create an {@code ActionListener} that shows a
510 * {@code JDialog} with {@code dialog.show()},
511 * one can write:
512 *
513 *<blockquote>
514 *<pre>
515 *EventHandler.create(ActionListener.class, dialog, "show")
516 *</pre>
517 *</blockquote>
518 *
519 * @param <T> the type to create
520 * @param listenerInterface the listener interface to create a proxy for
521 * @param target the object that will perform the action
522 * @param action the name of a (possibly qualified) property or method on
523 * the target
524 * @return an object that implements {@code listenerInterface}
525 *
526 * @throws NullPointerException if {@code listenerInterface} is null
527 * @throws NullPointerException if {@code target} is null
528 * @throws NullPointerException if {@code action} is null
529 *
530 * @see #create(Class, Object, String, String)
531 */
532 public static <T> T create(Class<T> listenerInterface,
533 Object target, String action)
534 {
535 return create(listenerInterface, target, action, null, null);
536 }
537
538 /**
539 /**
540 * Creates an implementation of {@code listenerInterface} in which
541 * <em>all</em> of the methods pass the value of the event
542 * expression, {@code eventPropertyName}, to the final method in the
543 * statement, {@code action}, which is applied to the {@code target}.
544 * This method is implemented by calling the
545 * more general, implementation of the {@code create} method with
546 * the {@code listenerMethodName} taking the value {@code null}.
547 * Refer to
548 * {@link java.beans.EventHandler#create(Class, Object, String, String)
549 * the general version of create} for a complete description of
550 * the {@code action} and {@code eventPropertyName} parameters.
567 *new ActionListener() {
568 * public void actionPerformed(ActionEvent event) {
569 * label.setText(((JTextField)(event.getSource())).getText());
570 * }
571 *};
572 *</pre>
573 *</blockquote>
574 *
575 * @param <T> the type to create
576 * @param listenerInterface the listener interface to create a proxy for
577 * @param target the object that will perform the action
578 * @param action the name of a (possibly qualified) property or method on
579 * the target
580 * @param eventPropertyName the (possibly qualified) name of a readable property of the incoming event
581 *
582 * @return an object that implements {@code listenerInterface}
583 *
584 * @throws NullPointerException if {@code listenerInterface} is null
585 * @throws NullPointerException if {@code target} is null
586 * @throws NullPointerException if {@code action} is null
587 *
588 * @see #create(Class, Object, String, String, String)
589 */
590 public static <T> T create(Class<T> listenerInterface,
591 Object target, String action,
592 String eventPropertyName)
593 {
594 return create(listenerInterface, target, action, eventPropertyName, null);
595 }
596
597 /**
598 * Creates an implementation of {@code listenerInterface} in which
599 * the method named {@code listenerMethodName}
600 * passes the value of the event expression, {@code eventPropertyName},
601 * to the final method in the statement, {@code action}, which
602 * is applied to the {@code target}. All of the other listener
603 * methods do nothing.
604 * <p>
605 * The {@code eventPropertyName} string is used to extract a value
606 * from the incoming event object that is passed to the target
607 * method. The common case is the target method takes no arguments, in
608 * which case a value of null should be used for the
658 * public void mousePressed(MouseEvent e) {
659 * target.setOrigin(e.getPoint());
660 * }
661 *};
662 * </pre>
663 *</blockquote>
664 *
665 * @param <T> the type to create
666 * @param listenerInterface the listener interface to create a proxy for
667 * @param target the object that will perform the action
668 * @param action the name of a (possibly qualified) property or method on
669 * the target
670 * @param eventPropertyName the (possibly qualified) name of a readable property of the incoming event
671 * @param listenerMethodName the name of the method in the listener interface that should trigger the action
672 *
673 * @return an object that implements {@code listenerInterface}
674 *
675 * @throws NullPointerException if {@code listenerInterface} is null
676 * @throws NullPointerException if {@code target} is null
677 * @throws NullPointerException if {@code action} is null
678 *
679 * @see EventHandler
680 */
681 public static <T> T create(Class<T> listenerInterface,
682 Object target, String action,
683 String eventPropertyName,
684 String listenerMethodName)
685 {
686 // Create this first to verify target/action are non-null
687 final EventHandler handler = new EventHandler(target, action,
688 eventPropertyName,
689 listenerMethodName);
690 if (listenerInterface == null) {
691 throw new NullPointerException(
692 "listenerInterface must be non-null");
693 }
694 final ClassLoader loader = getClassLoader(listenerInterface);
695 final Class<?>[] interfaces = {listenerInterface};
696 return AccessController.doPrivileged(new PrivilegedAction<T>() {
697 @SuppressWarnings("unchecked")
698 public T run() {
699 return (T) Proxy.newProxyInstance(loader, interfaces, handler);
|
1 /*
2 * Copyright (c) 2000, 2016, 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
509 * To create an {@code ActionListener} that shows a
510 * {@code JDialog} with {@code dialog.show()},
511 * one can write:
512 *
513 *<blockquote>
514 *<pre>
515 *EventHandler.create(ActionListener.class, dialog, "show")
516 *</pre>
517 *</blockquote>
518 *
519 * @param <T> the type to create
520 * @param listenerInterface the listener interface to create a proxy for
521 * @param target the object that will perform the action
522 * @param action the name of a (possibly qualified) property or method on
523 * the target
524 * @return an object that implements {@code listenerInterface}
525 *
526 * @throws NullPointerException if {@code listenerInterface} is null
527 * @throws NullPointerException if {@code target} is null
528 * @throws NullPointerException if {@code action} is null
529 * @throws IllegalArgumentException if creating a Proxy for
530 * {@code listenerInterface} fails for any of the restrictions
531 * specified by {@link Proxy#newProxyInstance}
532 * @see #create(Class, Object, String, String)
533 * @see Proxy#newProxyInstance
534 */
535 public static <T> T create(Class<T> listenerInterface,
536 Object target, String action)
537 {
538 return create(listenerInterface, target, action, null, null);
539 }
540
541 /**
542 /**
543 * Creates an implementation of {@code listenerInterface} in which
544 * <em>all</em> of the methods pass the value of the event
545 * expression, {@code eventPropertyName}, to the final method in the
546 * statement, {@code action}, which is applied to the {@code target}.
547 * This method is implemented by calling the
548 * more general, implementation of the {@code create} method with
549 * the {@code listenerMethodName} taking the value {@code null}.
550 * Refer to
551 * {@link java.beans.EventHandler#create(Class, Object, String, String)
552 * the general version of create} for a complete description of
553 * the {@code action} and {@code eventPropertyName} parameters.
570 *new ActionListener() {
571 * public void actionPerformed(ActionEvent event) {
572 * label.setText(((JTextField)(event.getSource())).getText());
573 * }
574 *};
575 *</pre>
576 *</blockquote>
577 *
578 * @param <T> the type to create
579 * @param listenerInterface the listener interface to create a proxy for
580 * @param target the object that will perform the action
581 * @param action the name of a (possibly qualified) property or method on
582 * the target
583 * @param eventPropertyName the (possibly qualified) name of a readable property of the incoming event
584 *
585 * @return an object that implements {@code listenerInterface}
586 *
587 * @throws NullPointerException if {@code listenerInterface} is null
588 * @throws NullPointerException if {@code target} is null
589 * @throws NullPointerException if {@code action} is null
590 * @throws IllegalArgumentException if creating a Proxy for
591 * {@code listenerInterface} fails for any of the restrictions
592 * specified by {@link Proxy#newProxyInstance}
593 * @see #create(Class, Object, String, String, String)
594 * @see Proxy#newProxyInstance
595 */
596 public static <T> T create(Class<T> listenerInterface,
597 Object target, String action,
598 String eventPropertyName)
599 {
600 return create(listenerInterface, target, action, eventPropertyName, null);
601 }
602
603 /**
604 * Creates an implementation of {@code listenerInterface} in which
605 * the method named {@code listenerMethodName}
606 * passes the value of the event expression, {@code eventPropertyName},
607 * to the final method in the statement, {@code action}, which
608 * is applied to the {@code target}. All of the other listener
609 * methods do nothing.
610 * <p>
611 * The {@code eventPropertyName} string is used to extract a value
612 * from the incoming event object that is passed to the target
613 * method. The common case is the target method takes no arguments, in
614 * which case a value of null should be used for the
664 * public void mousePressed(MouseEvent e) {
665 * target.setOrigin(e.getPoint());
666 * }
667 *};
668 * </pre>
669 *</blockquote>
670 *
671 * @param <T> the type to create
672 * @param listenerInterface the listener interface to create a proxy for
673 * @param target the object that will perform the action
674 * @param action the name of a (possibly qualified) property or method on
675 * the target
676 * @param eventPropertyName the (possibly qualified) name of a readable property of the incoming event
677 * @param listenerMethodName the name of the method in the listener interface that should trigger the action
678 *
679 * @return an object that implements {@code listenerInterface}
680 *
681 * @throws NullPointerException if {@code listenerInterface} is null
682 * @throws NullPointerException if {@code target} is null
683 * @throws NullPointerException if {@code action} is null
684 * @throws IllegalArgumentException if creating a Proxy for
685 * {@code listenerInterface} fails for any of the restrictions
686 * specified by {@link Proxy#newProxyInstance}
687 * @see EventHandler
688 * @see Proxy#newProxyInstance
689 */
690 public static <T> T create(Class<T> listenerInterface,
691 Object target, String action,
692 String eventPropertyName,
693 String listenerMethodName)
694 {
695 // Create this first to verify target/action are non-null
696 final EventHandler handler = new EventHandler(target, action,
697 eventPropertyName,
698 listenerMethodName);
699 if (listenerInterface == null) {
700 throw new NullPointerException(
701 "listenerInterface must be non-null");
702 }
703 final ClassLoader loader = getClassLoader(listenerInterface);
704 final Class<?>[] interfaces = {listenerInterface};
705 return AccessController.doPrivileged(new PrivilegedAction<T>() {
706 @SuppressWarnings("unchecked")
707 public T run() {
708 return (T) Proxy.newProxyInstance(loader, interfaces, handler);
|