jdk/src/share/classes/sun/reflect/misc/MethodUtil.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 4188 f67abce80f05
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.reflect.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.AllPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PermissionCollection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.SecureClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.HttpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.lang.reflect.AccessibleObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
class Trampoline {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static Object invoke(Method m, Object obj, Object[] params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        throws InvocationTargetException, IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        return m.invoke(obj, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Create a trampoline class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public final class MethodUtil extends SecureClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static String MISC_PKG = "sun.reflect.misc.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static String TRAMPOLINE = MISC_PKG + "Trampoline";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static Method bounce = getTrampoline();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private MethodUtil() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    70
    public static Method getMethod(Class<?> cls, String name, Class[] args)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        throws NoSuchMethodException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        ReflectUtil.checkPackageAccess(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return cls.getMethod(name, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static Method[] getMethods(Class cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        ReflectUtil.checkPackageAccess(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return cls.getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Discover the public methods on public classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * and interfaces accessible to any caller by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Class.getMethods() and walking towards Object until
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     public static Method[] getPublicMethods(Class cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // compatibility for update release
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (System.getSecurityManager() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return cls.getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    92
        Map<Signature, Method> sigs = new HashMap<Signature, Method>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        while (cls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            boolean done = getInternalPublicMethods(cls, sigs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            getInterfaceMethods(cls, sigs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            cls = cls.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   101
        return sigs.values().toArray(new Method[sigs.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Process the immediate interfaces of this class or interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   107
    private static void getInterfaceMethods(Class cls,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   108
                                            Map<Signature, Method> sigs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        Class[] intfs = cls.getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        for (int i=0; i < intfs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            Class intf = intfs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            boolean done = getInternalPublicMethods(intf, sigs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                getInterfaceMethods(intf, sigs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Process the methods in this class or interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   123
    private static boolean getInternalPublicMethods(Class cls,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   124
                                                    Map<Signature, Method> sigs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        Method[] methods = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
             * This class or interface is non-public so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
             * can't use any of it's methods. Go back and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
             * try again with a superclass or superinterface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (!Modifier.isPublic(cls.getModifiers())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (!ReflectUtil.isPackageAccessible(cls)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            methods = cls.getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         * Check for inherited methods with non-public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         * declaring classes. They might override and hide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         * methods from their superclasses or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         * superinterfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        boolean done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        for (int i=0; i < methods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            Class dc = methods[i].getDeclaringClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (!Modifier.isPublic(dc.getModifiers())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
             * We're done. Spray all the methods into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
             * the list and then we're out of here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            for (int i=0; i < methods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                addMethod(sigs, methods[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
             * Simulate cls.getDeclaredMethods() by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
             * stripping away inherited methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            for (int i=0; i < methods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                Class dc = methods[i].getDeclaringClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (cls.equals(dc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    addMethod(sigs, methods[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   182
    private static void addMethod(Map<Signature, Method> sigs, Method method) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        Signature signature = new Signature(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (!sigs.containsKey(signature)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            sigs.put(signature, method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } else if (!method.getDeclaringClass().isInterface()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
             * Superclasses beat interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
             */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   190
            Method old = sigs.get(signature);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (old.getDeclaringClass().isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                sigs.put(signature, method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * A class that represents the unique elements of a method that will be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * key in the method cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private static class Signature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        private String methodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        private Class[] argClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        private volatile int hashCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        Signature(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            this.methodName = m.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            this.argClasses = m.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        public boolean equals(Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (this == o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            Signature that = (Signature)o2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (!(methodName.equals(that.methodName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (argClasses.length != that.argClasses.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            for (int i = 0; i < argClasses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                if (!(argClasses[i] == that.argClasses[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         * Hash code computed using algorithm suggested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         * Effective Java, Item 8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (hashCode == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                result = 37 * result + methodName.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                if (argClasses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    for (int i = 0; i < argClasses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        result = 37 * result + ((argClasses[i] == null) ? 0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                            argClasses[i].hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                hashCode = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Bounce through the trampoline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public static Object invoke(Method m, Object obj, Object[] params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        throws InvocationTargetException, IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (m.getDeclaringClass().equals(AccessController.class) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            m.getDeclaringClass().equals(Method.class))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            throw new InvocationTargetException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                new UnsupportedOperationException("invocation not supported"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return bounce.invoke(null, new Object[] {m, obj, params});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } catch (InvocationTargetException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            Throwable t = ie.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (t instanceof InvocationTargetException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                throw (InvocationTargetException)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            } else if (t instanceof IllegalAccessException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                throw (IllegalAccessException)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            } else if (t instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                throw (RuntimeException)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            } else if (t instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                throw (Error)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                throw new Error("Unexpected invocation error", t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            // this can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new Error("Unexpected invocation error", iae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private static Method getTrampoline() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   285
            return AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   286
                new PrivilegedExceptionAction<Method>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   287
                    public Method run() throws Exception {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   288
                        Class<?> t = getTrampolineClass();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   289
                        Class[] types = {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   290
                            Method.class, Object.class, Object[].class
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   291
                        };
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   292
                        Method b = t.getDeclaredMethod("invoke", types);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    ((AccessibleObject)b).setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw new InternalError("bouncer cannot be found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    protected synchronized Class loadClass(String name, boolean resolve)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // First, check if the class has already been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        ReflectUtil.checkPackageAccess(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        Class c = findLoadedClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                c = findClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                // Fall through ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                c = getParent().loadClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (resolve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            resolveClass(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return c;
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
    protected Class findClass(final String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (!name.startsWith(MISC_PKG)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        String path = name.replace('.', '/').concat(".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        URL res = getResource(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                return defineClass(name, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                throw new ClassNotFoundException(name, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Define the proxy classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private Class defineClass(String name, URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        byte[] b = getBytes(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        CodeSource cs = new CodeSource(null, (java.security.cert.Certificate[])null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (!name.equals(TRAMPOLINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            throw new IOException("MethodUtil: bad name " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return defineClass(name, b, 0, b.length, cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Returns the contents of the specified URL as an array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private static byte[] getBytes(URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        URLConnection uc = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (uc instanceof java.net.HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            java.net.HttpURLConnection huc = (java.net.HttpURLConnection) uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            int code = huc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (code >= java.net.HttpURLConnection.HTTP_BAD_REQUEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                throw new IOException("open HTTP connection failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        int len = uc.getContentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        InputStream in = new BufferedInputStream(uc.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        byte[] b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if (len != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                // Read exactly len bytes from the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                b = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    int n = in.read(b, b.length - len, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    if (n == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        throw new IOException("unexpected EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    len -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                b = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                while ((len = in.read(b, total, b.length - total)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    total += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    if (total >= b.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        byte[] tmp = new byte[total * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        System.arraycopy(b, 0, tmp, 0, total);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        b = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                // Trim array to correct size, if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                if (total != b.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    byte[] tmp = new byte[total];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    System.arraycopy(b, 0, tmp, 0, total);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    b = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    protected PermissionCollection getPermissions(CodeSource codesource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        PermissionCollection perms = super.getPermissions(codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        perms.add(new AllPermission());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private static Class getTrampolineClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            return Class.forName(TRAMPOLINE, true, new MethodUtil());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
}