jdk/src/share/classes/sun/swing/AccessibleMethod.java
author tbell
Fri, 03 Jul 2009 09:15:02 -0700
changeset 3081 079050a02eba
parent 1639 a97859015238
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1639
a97859015238 6785258: Update copyright year
xdono
parents: 1301
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A utility for accessing and invoking methods, via reflection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * that would otherwise be unaccessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author Shannon Hickey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class AccessibleMethod {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private final Method method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Construct an instance for the given params.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * @param klass the class to which the method belongs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * @param methodName the name of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * @param paramTypes the paramater type array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * @throws NullPointerException if <code>klass</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *         or <code>name</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * @throws NoSuchMethodException if the method can't be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public AccessibleMethod(Class klass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                            String methodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                            Class ... paramTypes) throws NoSuchMethodException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            method = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                new AccessMethodAction(klass, methodName, paramTypes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            throw (NoSuchMethodException)e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Invoke the method that this object represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Has the same behavior and throws the same exceptions as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <code>java.lang.reflect.Method.invoke</code> with one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * exception: This method does not throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * <code>IllegalAccessException</code> since the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * method has already been made accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param obj the object the underlying method is invoked from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param args the arguments used for the method call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @return the result of dispatching the method represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *         this object on <code>obj</code> with parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *         <code>args</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @see java.lang.reflect.Method#invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public Object invoke(Object obj, Object ... args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throws IllegalArgumentException, InvocationTargetException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return method.invoke(obj, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            // should never happen since we've made it accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new AssertionError("accessible method inaccessible");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Invoke the method that this object represents, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * expectation that the method being called throws no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * checked exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Simply calls <code>this.invoke(obj, args)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * but catches any <code>InvocationTargetException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * and returns the cause wrapped in a runtime exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param obj the object the underlying method is invoked from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param args the arguments used for the method call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @return the result of dispatching the method represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *         this object on <code>obj</code> with parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *         <code>args</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @see #invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public Object invokeNoChecked(Object obj, Object ... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return invoke(obj, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } catch (InvocationTargetException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (ex.getCause() instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                throw (RuntimeException)ex.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                throw new RuntimeException(ex.getCause());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /** The action used to fetch the method and make it accessible */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private static class AccessMethodAction implements PrivilegedExceptionAction<Method> {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   117
        private final Class<?> klass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        private final String methodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        private final Class[] paramTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        public AccessMethodAction(Class klass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                  String methodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                  Class ... paramTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            this.klass = klass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            this.methodName = methodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            this.paramTypes = paramTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        public Method run() throws NoSuchMethodException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            Method method = klass.getDeclaredMethod(methodName, paramTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            method.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
}