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