rt/emul/compact/src/main/java/java/lang/invoke/MethodHandleNatives.java
branchjdk8-b132
changeset 1646 c880a8a8803b
child 1651 5c990ed353e9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/lang/invoke/MethodHandleNatives.java	Sat Aug 09 11:11:13 2014 +0200
     1.3 @@ -0,0 +1,505 @@
     1.4 +/*
     1.5 + * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang.invoke;
    1.30 +
    1.31 +import java.lang.invoke.MethodHandles.Lookup;
    1.32 +import java.lang.reflect.Field;
    1.33 +import static java.lang.invoke.MethodHandleNatives.Constants.*;
    1.34 +import static java.lang.invoke.MethodHandleStatics.*;
    1.35 +import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
    1.36 +
    1.37 +/**
    1.38 + * The JVM interface for the method handles package is all here.
    1.39 + * This is an interface internal and private to an implementation of JSR 292.
    1.40 + * <em>This class is not part of the JSR 292 standard.</em>
    1.41 + * @author jrose
    1.42 + */
    1.43 +class MethodHandleNatives {
    1.44 +
    1.45 +    private MethodHandleNatives() { } // static only
    1.46 +
    1.47 +    /// MemberName support
    1.48 +
    1.49 +    static native void init(MemberName self, Object ref);
    1.50 +    static native void expand(MemberName self);
    1.51 +    static native MemberName resolve(MemberName self, Class<?> caller) throws LinkageError;
    1.52 +    static native int getMembers(Class<?> defc, String matchName, String matchSig,
    1.53 +            int matchFlags, Class<?> caller, int skip, MemberName[] results);
    1.54 +
    1.55 +    /// Field layout queries parallel to sun.misc.Unsafe:
    1.56 +    static native long objectFieldOffset(MemberName self);  // e.g., returns vmindex
    1.57 +    static native long staticFieldOffset(MemberName self);  // e.g., returns vmindex
    1.58 +    static native Object staticFieldBase(MemberName self);  // e.g., returns clazz
    1.59 +    static native Object getMemberVMInfo(MemberName self);  // returns {vmindex,vmtarget}
    1.60 +
    1.61 +    /// MethodHandle support
    1.62 +
    1.63 +    /** Fetch MH-related JVM parameter.
    1.64 +     *  which=0 retrieves MethodHandlePushLimit
    1.65 +     *  which=1 retrieves stack slot push size (in address units)
    1.66 +     */
    1.67 +    static native int getConstant(int which);
    1.68 +
    1.69 +    static final boolean COUNT_GWT;
    1.70 +
    1.71 +    /// CallSite support
    1.72 +
    1.73 +    /** Tell the JVM that we need to change the target of a CallSite. */
    1.74 +    static native void setCallSiteTargetNormal(CallSite site, MethodHandle target);
    1.75 +    static native void setCallSiteTargetVolatile(CallSite site, MethodHandle target);
    1.76 +
    1.77 +    private static native void registerNatives();
    1.78 +    static {
    1.79 +        registerNatives();
    1.80 +        COUNT_GWT                   = getConstant(Constants.GC_COUNT_GWT) != 0;
    1.81 +
    1.82 +        // The JVM calls MethodHandleNatives.<clinit>.  Cascade the <clinit> calls as needed:
    1.83 +        MethodHandleImpl.initStatics();
    1.84 +}
    1.85 +
    1.86 +    // All compile-time constants go here.
    1.87 +    // There is an opportunity to check them against the JVM's idea of them.
    1.88 +    static class Constants {
    1.89 +        Constants() { } // static only
    1.90 +        // MethodHandleImpl
    1.91 +        static final int // for getConstant
    1.92 +                GC_COUNT_GWT = 4,
    1.93 +                GC_LAMBDA_SUPPORT = 5;
    1.94 +
    1.95 +        // MemberName
    1.96 +        // The JVM uses values of -2 and above for vtable indexes.
    1.97 +        // Field values are simple positive offsets.
    1.98 +        // Ref: src/share/vm/oops/methodOop.hpp
    1.99 +        // This value is negative enough to avoid such numbers,
   1.100 +        // but not too negative.
   1.101 +        static final int
   1.102 +                MN_IS_METHOD           = 0x00010000, // method (not constructor)
   1.103 +                MN_IS_CONSTRUCTOR      = 0x00020000, // constructor
   1.104 +                MN_IS_FIELD            = 0x00040000, // field
   1.105 +                MN_IS_TYPE             = 0x00080000, // nested type
   1.106 +                MN_CALLER_SENSITIVE    = 0x00100000, // @CallerSensitive annotation detected
   1.107 +                MN_REFERENCE_KIND_SHIFT = 24, // refKind
   1.108 +                MN_REFERENCE_KIND_MASK = 0x0F000000 >> MN_REFERENCE_KIND_SHIFT,
   1.109 +                // The SEARCH_* bits are not for MN.flags but for the matchFlags argument of MHN.getMembers:
   1.110 +                MN_SEARCH_SUPERCLASSES = 0x00100000,
   1.111 +                MN_SEARCH_INTERFACES   = 0x00200000;
   1.112 +
   1.113 +        /**
   1.114 +         * Basic types as encoded in the JVM.  These code values are not
   1.115 +         * intended for use outside this class.  They are used as part of
   1.116 +         * a private interface between the JVM and this class.
   1.117 +         */
   1.118 +        static final int
   1.119 +            T_BOOLEAN  =  4,
   1.120 +            T_CHAR     =  5,
   1.121 +            T_FLOAT    =  6,
   1.122 +            T_DOUBLE   =  7,
   1.123 +            T_BYTE     =  8,
   1.124 +            T_SHORT    =  9,
   1.125 +            T_INT      = 10,
   1.126 +            T_LONG     = 11,
   1.127 +            T_OBJECT   = 12,
   1.128 +            //T_ARRAY    = 13
   1.129 +            T_VOID     = 14,
   1.130 +            //T_ADDRESS  = 15
   1.131 +            T_ILLEGAL  = 99;
   1.132 +
   1.133 +        /**
   1.134 +         * Constant pool entry types.
   1.135 +         */
   1.136 +        static final byte
   1.137 +            CONSTANT_Utf8                = 1,
   1.138 +            CONSTANT_Integer             = 3,
   1.139 +            CONSTANT_Float               = 4,
   1.140 +            CONSTANT_Long                = 5,
   1.141 +            CONSTANT_Double              = 6,
   1.142 +            CONSTANT_Class               = 7,
   1.143 +            CONSTANT_String              = 8,
   1.144 +            CONSTANT_Fieldref            = 9,
   1.145 +            CONSTANT_Methodref           = 10,
   1.146 +            CONSTANT_InterfaceMethodref  = 11,
   1.147 +            CONSTANT_NameAndType         = 12,
   1.148 +            CONSTANT_MethodHandle        = 15,  // JSR 292
   1.149 +            CONSTANT_MethodType          = 16,  // JSR 292
   1.150 +            CONSTANT_InvokeDynamic       = 18,
   1.151 +            CONSTANT_LIMIT               = 19;   // Limit to tags found in classfiles
   1.152 +
   1.153 +        /**
   1.154 +         * Access modifier flags.
   1.155 +         */
   1.156 +        static final char
   1.157 +            ACC_PUBLIC                 = 0x0001,
   1.158 +            ACC_PRIVATE                = 0x0002,
   1.159 +            ACC_PROTECTED              = 0x0004,
   1.160 +            ACC_STATIC                 = 0x0008,
   1.161 +            ACC_FINAL                  = 0x0010,
   1.162 +            ACC_SYNCHRONIZED           = 0x0020,
   1.163 +            ACC_VOLATILE               = 0x0040,
   1.164 +            ACC_TRANSIENT              = 0x0080,
   1.165 +            ACC_NATIVE                 = 0x0100,
   1.166 +            ACC_INTERFACE              = 0x0200,
   1.167 +            ACC_ABSTRACT               = 0x0400,
   1.168 +            ACC_STRICT                 = 0x0800,
   1.169 +            ACC_SYNTHETIC              = 0x1000,
   1.170 +            ACC_ANNOTATION             = 0x2000,
   1.171 +            ACC_ENUM                   = 0x4000,
   1.172 +            // aliases:
   1.173 +            ACC_SUPER                  = ACC_SYNCHRONIZED,
   1.174 +            ACC_BRIDGE                 = ACC_VOLATILE,
   1.175 +            ACC_VARARGS                = ACC_TRANSIENT;
   1.176 +
   1.177 +        /**
   1.178 +         * Constant pool reference-kind codes, as used by CONSTANT_MethodHandle CP entries.
   1.179 +         */
   1.180 +        static final byte
   1.181 +            REF_NONE                    = 0,  // null value
   1.182 +            REF_getField                = 1,
   1.183 +            REF_getStatic               = 2,
   1.184 +            REF_putField                = 3,
   1.185 +            REF_putStatic               = 4,
   1.186 +            REF_invokeVirtual           = 5,
   1.187 +            REF_invokeStatic            = 6,
   1.188 +            REF_invokeSpecial           = 7,
   1.189 +            REF_newInvokeSpecial        = 8,
   1.190 +            REF_invokeInterface         = 9,
   1.191 +            REF_LIMIT                  = 10;
   1.192 +    }
   1.193 +
   1.194 +    static boolean refKindIsValid(int refKind) {
   1.195 +        return (refKind > REF_NONE && refKind < REF_LIMIT);
   1.196 +    }
   1.197 +    static boolean refKindIsField(byte refKind) {
   1.198 +        assert(refKindIsValid(refKind));
   1.199 +        return (refKind <= REF_putStatic);
   1.200 +    }
   1.201 +    static boolean refKindIsGetter(byte refKind) {
   1.202 +        assert(refKindIsValid(refKind));
   1.203 +        return (refKind <= REF_getStatic);
   1.204 +    }
   1.205 +    static boolean refKindIsSetter(byte refKind) {
   1.206 +        return refKindIsField(refKind) && !refKindIsGetter(refKind);
   1.207 +    }
   1.208 +    static boolean refKindIsMethod(byte refKind) {
   1.209 +        return !refKindIsField(refKind) && (refKind != REF_newInvokeSpecial);
   1.210 +    }
   1.211 +    static boolean refKindIsConstructor(byte refKind) {
   1.212 +        return (refKind == REF_newInvokeSpecial);
   1.213 +    }
   1.214 +    static boolean refKindHasReceiver(byte refKind) {
   1.215 +        assert(refKindIsValid(refKind));
   1.216 +        return (refKind & 1) != 0;
   1.217 +    }
   1.218 +    static boolean refKindIsStatic(byte refKind) {
   1.219 +        return !refKindHasReceiver(refKind) && (refKind != REF_newInvokeSpecial);
   1.220 +    }
   1.221 +    static boolean refKindDoesDispatch(byte refKind) {
   1.222 +        assert(refKindIsValid(refKind));
   1.223 +        return (refKind == REF_invokeVirtual ||
   1.224 +                refKind == REF_invokeInterface);
   1.225 +    }
   1.226 +    static {
   1.227 +        final int HR_MASK = ((1 << REF_getField) |
   1.228 +                             (1 << REF_putField) |
   1.229 +                             (1 << REF_invokeVirtual) |
   1.230 +                             (1 << REF_invokeSpecial) |
   1.231 +                             (1 << REF_invokeInterface)
   1.232 +                            );
   1.233 +        for (byte refKind = REF_NONE+1; refKind < REF_LIMIT; refKind++) {
   1.234 +            assert(refKindHasReceiver(refKind) == (((1<<refKind) & HR_MASK) != 0)) : refKind;
   1.235 +        }
   1.236 +    }
   1.237 +    static String refKindName(byte refKind) {
   1.238 +        assert(refKindIsValid(refKind));
   1.239 +        switch (refKind) {
   1.240 +        case REF_getField:          return "getField";
   1.241 +        case REF_getStatic:         return "getStatic";
   1.242 +        case REF_putField:          return "putField";
   1.243 +        case REF_putStatic:         return "putStatic";
   1.244 +        case REF_invokeVirtual:     return "invokeVirtual";
   1.245 +        case REF_invokeStatic:      return "invokeStatic";
   1.246 +        case REF_invokeSpecial:     return "invokeSpecial";
   1.247 +        case REF_newInvokeSpecial:  return "newInvokeSpecial";
   1.248 +        case REF_invokeInterface:   return "invokeInterface";
   1.249 +        default:                    return "REF_???";
   1.250 +        }
   1.251 +    }
   1.252 +
   1.253 +    private static native int getNamedCon(int which, Object[] name);
   1.254 +    static boolean verifyConstants() {
   1.255 +        Object[] box = { null };
   1.256 +        for (int i = 0; ; i++) {
   1.257 +            box[0] = null;
   1.258 +            int vmval = getNamedCon(i, box);
   1.259 +            if (box[0] == null)  break;
   1.260 +            String name = (String) box[0];
   1.261 +            try {
   1.262 +                Field con = Constants.class.getDeclaredField(name);
   1.263 +                int jval = con.getInt(null);
   1.264 +                if (jval == vmval)  continue;
   1.265 +                String err = (name+": JVM has "+vmval+" while Java has "+jval);
   1.266 +                if (name.equals("CONV_OP_LIMIT")) {
   1.267 +                    System.err.println("warning: "+err);
   1.268 +                    continue;
   1.269 +                }
   1.270 +                throw new InternalError(err);
   1.271 +            } catch (NoSuchFieldException | IllegalAccessException ex) {
   1.272 +                String err = (name+": JVM has "+vmval+" which Java does not define");
   1.273 +                // ignore exotic ops the JVM cares about; we just wont issue them
   1.274 +                //System.err.println("warning: "+err);
   1.275 +                continue;
   1.276 +            }
   1.277 +        }
   1.278 +        return true;
   1.279 +    }
   1.280 +    static {
   1.281 +        assert(verifyConstants());
   1.282 +    }
   1.283 +
   1.284 +    // Up-calls from the JVM.
   1.285 +    // These must NOT be public.
   1.286 +
   1.287 +    /**
   1.288 +     * The JVM is linking an invokedynamic instruction.  Create a reified call site for it.
   1.289 +     */
   1.290 +    static MemberName linkCallSite(Object callerObj,
   1.291 +                                   Object bootstrapMethodObj,
   1.292 +                                   Object nameObj, Object typeObj,
   1.293 +                                   Object staticArguments,
   1.294 +                                   Object[] appendixResult) {
   1.295 +        MethodHandle bootstrapMethod = (MethodHandle)bootstrapMethodObj;
   1.296 +        Class<?> caller = (Class<?>)callerObj;
   1.297 +        String name = nameObj.toString().intern();
   1.298 +        MethodType type = (MethodType)typeObj;
   1.299 +        CallSite callSite = CallSite.makeSite(bootstrapMethod,
   1.300 +                                              name,
   1.301 +                                              type,
   1.302 +                                              staticArguments,
   1.303 +                                              caller);
   1.304 +        if (callSite instanceof ConstantCallSite) {
   1.305 +            appendixResult[0] = callSite.dynamicInvoker();
   1.306 +            return Invokers.linkToTargetMethod(type);
   1.307 +        } else {
   1.308 +            appendixResult[0] = callSite;
   1.309 +            return Invokers.linkToCallSiteMethod(type);
   1.310 +        }
   1.311 +    }
   1.312 +
   1.313 +    /**
   1.314 +     * The JVM wants a pointer to a MethodType.  Oblige it by finding or creating one.
   1.315 +     */
   1.316 +    static MethodType findMethodHandleType(Class<?> rtype, Class<?>[] ptypes) {
   1.317 +        return MethodType.makeImpl(rtype, ptypes, true);
   1.318 +    }
   1.319 +
   1.320 +    /**
   1.321 +     * The JVM wants to link a call site that requires a dynamic type check.
   1.322 +     * Name is a type-checking invoker, invokeExact or invoke.
   1.323 +     * Return a JVM method (MemberName) to handle the invoking.
   1.324 +     * The method assumes the following arguments on the stack:
   1.325 +     * 0: the method handle being invoked
   1.326 +     * 1-N: the arguments to the method handle invocation
   1.327 +     * N+1: an optional, implicitly added argument (typically the given MethodType)
   1.328 +     * <p>
   1.329 +     * The nominal method at such a call site is an instance of
   1.330 +     * a signature-polymorphic method (see @PolymorphicSignature).
   1.331 +     * Such method instances are user-visible entities which are
   1.332 +     * "split" from the generic placeholder method in {@code MethodHandle}.
   1.333 +     * (Note that the placeholder method is not identical with any of
   1.334 +     * its instances.  If invoked reflectively, is guaranteed to throw an
   1.335 +     * {@code UnsupportedOperationException}.)
   1.336 +     * If the signature-polymorphic method instance is ever reified,
   1.337 +     * it appears as a "copy" of the original placeholder
   1.338 +     * (a native final member of {@code MethodHandle}) except
   1.339 +     * that its type descriptor has shape required by the instance,
   1.340 +     * and the method instance is <em>not</em> varargs.
   1.341 +     * The method instance is also marked synthetic, since the
   1.342 +     * method (by definition) does not appear in Java source code.
   1.343 +     * <p>
   1.344 +     * The JVM is allowed to reify this method as instance metadata.
   1.345 +     * For example, {@code invokeBasic} is always reified.
   1.346 +     * But the JVM may instead call {@code linkMethod}.
   1.347 +     * If the result is an * ordered pair of a {@code (method, appendix)},
   1.348 +     * the method gets all the arguments (0..N inclusive)
   1.349 +     * plus the appendix (N+1), and uses the appendix to complete the call.
   1.350 +     * In this way, one reusable method (called a "linker method")
   1.351 +     * can perform the function of any number of polymorphic instance
   1.352 +     * methods.
   1.353 +     * <p>
   1.354 +     * Linker methods are allowed to be weakly typed, with any or
   1.355 +     * all references rewritten to {@code Object} and any primitives
   1.356 +     * (except {@code long}/{@code float}/{@code double})
   1.357 +     * rewritten to {@code int}.
   1.358 +     * A linker method is trusted to return a strongly typed result,
   1.359 +     * according to the specific method type descriptor of the
   1.360 +     * signature-polymorphic instance it is emulating.
   1.361 +     * This can involve (as necessary) a dynamic check using
   1.362 +     * data extracted from the appendix argument.
   1.363 +     * <p>
   1.364 +     * The JVM does not inspect the appendix, other than to pass
   1.365 +     * it verbatim to the linker method at every call.
   1.366 +     * This means that the JDK runtime has wide latitude
   1.367 +     * for choosing the shape of each linker method and its
   1.368 +     * corresponding appendix.
   1.369 +     * Linker methods should be generated from {@code LambdaForm}s
   1.370 +     * so that they do not become visible on stack traces.
   1.371 +     * <p>
   1.372 +     * The {@code linkMethod} call is free to omit the appendix
   1.373 +     * (returning null) and instead emulate the required function
   1.374 +     * completely in the linker method.
   1.375 +     * As a corner case, if N==255, no appendix is possible.
   1.376 +     * In this case, the method returned must be custom-generated to
   1.377 +     * to perform any needed type checking.
   1.378 +     * <p>
   1.379 +     * If the JVM does not reify a method at a call site, but instead
   1.380 +     * calls {@code linkMethod}, the corresponding call represented
   1.381 +     * in the bytecodes may mention a valid method which is not
   1.382 +     * representable with a {@code MemberName}.
   1.383 +     * Therefore, use cases for {@code linkMethod} tend to correspond to
   1.384 +     * special cases in reflective code such as {@code findVirtual}
   1.385 +     * or {@code revealDirect}.
   1.386 +     */
   1.387 +    static MemberName linkMethod(Class<?> callerClass, int refKind,
   1.388 +                                 Class<?> defc, String name, Object type,
   1.389 +                                 Object[] appendixResult) {
   1.390 +        if (!TRACE_METHOD_LINKAGE)
   1.391 +            return linkMethodImpl(callerClass, refKind, defc, name, type, appendixResult);
   1.392 +        return linkMethodTracing(callerClass, refKind, defc, name, type, appendixResult);
   1.393 +    }
   1.394 +    static MemberName linkMethodImpl(Class<?> callerClass, int refKind,
   1.395 +                                     Class<?> defc, String name, Object type,
   1.396 +                                     Object[] appendixResult) {
   1.397 +        try {
   1.398 +            if (defc == MethodHandle.class && refKind == REF_invokeVirtual) {
   1.399 +                return Invokers.methodHandleInvokeLinkerMethod(name, fixMethodType(callerClass, type), appendixResult);
   1.400 +            }
   1.401 +        } catch (Throwable ex) {
   1.402 +            if (ex instanceof LinkageError)
   1.403 +                throw (LinkageError) ex;
   1.404 +            else
   1.405 +                throw new LinkageError(ex.getMessage(), ex);
   1.406 +        }
   1.407 +        throw new LinkageError("no such method "+defc.getName()+"."+name+type);
   1.408 +    }
   1.409 +    private static MethodType fixMethodType(Class<?> callerClass, Object type) {
   1.410 +        if (type instanceof MethodType)
   1.411 +            return (MethodType) type;
   1.412 +        else
   1.413 +            return MethodType.fromMethodDescriptorString((String)type, callerClass.getClassLoader());
   1.414 +    }
   1.415 +    // Tracing logic:
   1.416 +    static MemberName linkMethodTracing(Class<?> callerClass, int refKind,
   1.417 +                                        Class<?> defc, String name, Object type,
   1.418 +                                        Object[] appendixResult) {
   1.419 +        System.out.println("linkMethod "+defc.getName()+"."+
   1.420 +                           name+type+"/"+Integer.toHexString(refKind));
   1.421 +        try {
   1.422 +            MemberName res = linkMethodImpl(callerClass, refKind, defc, name, type, appendixResult);
   1.423 +            System.out.println("linkMethod => "+res+" + "+appendixResult[0]);
   1.424 +            return res;
   1.425 +        } catch (Throwable ex) {
   1.426 +            System.out.println("linkMethod => throw "+ex);
   1.427 +            throw ex;
   1.428 +        }
   1.429 +    }
   1.430 +
   1.431 +
   1.432 +    /**
   1.433 +     * The JVM is resolving a CONSTANT_MethodHandle CP entry.  And it wants our help.
   1.434 +     * It will make an up-call to this method.  (Do not change the name or signature.)
   1.435 +     * The type argument is a Class for field requests and a MethodType for non-fields.
   1.436 +     * <p>
   1.437 +     * Recent versions of the JVM may also pass a resolved MemberName for the type.
   1.438 +     * In that case, the name is ignored and may be null.
   1.439 +     */
   1.440 +    static MethodHandle linkMethodHandleConstant(Class<?> callerClass, int refKind,
   1.441 +                                                 Class<?> defc, String name, Object type) {
   1.442 +        try {
   1.443 +            Lookup lookup = IMPL_LOOKUP.in(callerClass);
   1.444 +            assert(refKindIsValid(refKind));
   1.445 +            return lookup.linkMethodHandleConstant((byte) refKind, defc, name, type);
   1.446 +        } catch (IllegalAccessException ex) {
   1.447 +            Throwable cause = ex.getCause();
   1.448 +            if (cause instanceof AbstractMethodError) {
   1.449 +                throw (AbstractMethodError) cause;
   1.450 +            } else {
   1.451 +                Error err = new IllegalAccessError(ex.getMessage());
   1.452 +                throw initCauseFrom(err, ex);
   1.453 +            }
   1.454 +        } catch (NoSuchMethodException ex) {
   1.455 +            Error err = new NoSuchMethodError(ex.getMessage());
   1.456 +            throw initCauseFrom(err, ex);
   1.457 +        } catch (NoSuchFieldException ex) {
   1.458 +            Error err = new NoSuchFieldError(ex.getMessage());
   1.459 +            throw initCauseFrom(err, ex);
   1.460 +        } catch (ReflectiveOperationException ex) {
   1.461 +            Error err = new IncompatibleClassChangeError();
   1.462 +            throw initCauseFrom(err, ex);
   1.463 +        }
   1.464 +    }
   1.465 +
   1.466 +    /**
   1.467 +     * Use best possible cause for err.initCause(), substituting the
   1.468 +     * cause for err itself if the cause has the same (or better) type.
   1.469 +     */
   1.470 +    static private Error initCauseFrom(Error err, Exception ex) {
   1.471 +        Throwable th = ex.getCause();
   1.472 +        if (err.getClass().isInstance(th))
   1.473 +           return (Error) th;
   1.474 +        err.initCause(th == null ? ex : th);
   1.475 +        return err;
   1.476 +    }
   1.477 +
   1.478 +    /**
   1.479 +     * Is this method a caller-sensitive method?
   1.480 +     * I.e., does it call Reflection.getCallerClass or a similer method
   1.481 +     * to ask about the identity of its caller?
   1.482 +     */
   1.483 +    static boolean isCallerSensitive(MemberName mem) {
   1.484 +        if (!mem.isInvocable())  return false;  // fields are not caller sensitive
   1.485 +
   1.486 +        return mem.isCallerSensitive() || canBeCalledVirtual(mem);
   1.487 +    }
   1.488 +
   1.489 +    static boolean canBeCalledVirtual(MemberName mem) {
   1.490 +        assert(mem.isInvocable());
   1.491 +        Class<?> defc = mem.getDeclaringClass();
   1.492 +        switch (mem.getName()) {
   1.493 +        case "checkMemberAccess":
   1.494 +            return canBeCalledVirtual(mem, java.lang.SecurityManager.class);
   1.495 +        case "getContextClassLoader":
   1.496 +            return canBeCalledVirtual(mem, java.lang.Thread.class);
   1.497 +        }
   1.498 +        return false;
   1.499 +    }
   1.500 +
   1.501 +    static boolean canBeCalledVirtual(MemberName symbolicRef, Class<?> definingClass) {
   1.502 +        Class<?> symbolicRefClass = symbolicRef.getDeclaringClass();
   1.503 +        if (symbolicRefClass == definingClass)  return true;
   1.504 +        if (symbolicRef.isStatic() || symbolicRef.isPrivate())  return false;
   1.505 +        return (definingClass.isAssignableFrom(symbolicRefClass) ||  // Msym overrides Mdef
   1.506 +                symbolicRefClass.isInterface());                     // Mdef implements Msym
   1.507 +    }
   1.508 +}