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