Java Code Examples for sun.invoke.util.VerifyAccess#isSamePackage()

The following examples show how to use sun.invoke.util.VerifyAccess#isSamePackage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InvokerBytecodeGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static boolean isStaticallyInvocable(MemberName member) {
    if (member == null)  return false;
    if (member.isConstructor())  return false;
    Class<?> cls = member.getDeclaringClass();
    if (cls.isArray() || cls.isPrimitive())
        return false;  // FIXME
    if (cls.isAnonymousClass() || cls.isLocalClass())
        return false;  // inner class of some sort
    if (cls.getClassLoader() != MethodHandle.class.getClassLoader())
        return false;  // not on BCP
    if (ReflectUtil.isVMAnonymousClass(cls)) // FIXME: switch to supported API once it is added
        return false;
    MethodType mtype = member.getMethodOrFieldType();
    if (!isStaticallyNameable(mtype.returnType()))
        return false;
    for (Class<?> ptype : mtype.parameterArray())
        if (!isStaticallyNameable(ptype))
            return false;
    if (!member.isPrivate() && VerifyAccess.isSamePackage(MethodHandle.class, cls))
        return true;   // in java.lang.invoke package
    if (member.isPublic() && isStaticallyNameable(cls))
        return true;
    return false;
}
 
Example 2
Source File: MethodHandles.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a lookup on the specified new lookup class.
 * The resulting object will report the specified
 * class as its own {@link #lookupClass lookupClass}.
 * <p>
 * However, the resulting {@code Lookup} object is guaranteed
 * to have no more access capabilities than the original.
 * In particular, access capabilities can be lost as follows:<ul>
 * <li>If the new lookup class differs from the old one,
 * protected members will not be accessible by virtue of inheritance.
 * (Protected members may continue to be accessible because of package sharing.)
 * <li>If the new lookup class is in a different package
 * than the old one, protected and default (package) members will not be accessible.
 * <li>If the new lookup class is not within the same package member
 * as the old one, private members will not be accessible.
 * <li>If the new lookup class is not accessible to the old lookup class,
 * then no members, not even public members, will be accessible.
 * (In all other cases, public members will continue to be accessible.)
 * </ul>
 *
 * @param requestedLookupClass the desired lookup class for the new lookup object
 * @return a lookup object which reports the desired lookup class
 * @throws NullPointerException if the argument is null
 */
public Lookup in(Class<?> requestedLookupClass) {
    requestedLookupClass.getClass();  // null check
    if (allowedModes == TRUSTED)  // IMPL_LOOKUP can make any lookup at all
        return new Lookup(requestedLookupClass, ALL_MODES);
    if (requestedLookupClass == this.lookupClass)
        return this;  // keep same capabilities
    int newModes = (allowedModes & (ALL_MODES & ~PROTECTED));
    if ((newModes & PACKAGE) != 0
        && !VerifyAccess.isSamePackage(this.lookupClass, requestedLookupClass)) {
        newModes &= ~(PACKAGE|PRIVATE);
    }
    // Allow nestmate lookups to be created without special privilege:
    if ((newModes & PRIVATE) != 0
        && !VerifyAccess.isSamePackageMember(this.lookupClass, requestedLookupClass)) {
        newModes &= ~PRIVATE;
    }
    if ((newModes & PUBLIC) != 0
        && !VerifyAccess.isClassAccessible(requestedLookupClass, this.lookupClass, allowedModes)) {
        // The requested class it not accessible from the lookup class.
        // No permissions.
        newModes = 0;
    }
    checkUnprivilegedlookupClass(requestedLookupClass, newModes);
    return new Lookup(requestedLookupClass, newModes);
}
 
Example 3
Source File: MethodHandles.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a lookup on the specified new lookup class.
 * The resulting object will report the specified
 * class as its own {@link #lookupClass lookupClass}.
 * <p>
 * However, the resulting {@code Lookup} object is guaranteed
 * to have no more access capabilities than the original.
 * In particular, access capabilities can be lost as follows:<ul>
 * <li>If the new lookup class differs from the old one,
 * protected members will not be accessible by virtue of inheritance.
 * (Protected members may continue to be accessible because of package sharing.)
 * <li>If the new lookup class is in a different package
 * than the old one, protected and default (package) members will not be accessible.
 * <li>If the new lookup class is not within the same package member
 * as the old one, private members will not be accessible.
 * <li>If the new lookup class is not accessible to the old lookup class,
 * then no members, not even public members, will be accessible.
 * (In all other cases, public members will continue to be accessible.)
 * </ul>
 *
 * @param requestedLookupClass the desired lookup class for the new lookup object
 * @return a lookup object which reports the desired lookup class
 * @throws NullPointerException if the argument is null
 */
public Lookup in(Class<?> requestedLookupClass) {
    requestedLookupClass.getClass();  // null check
    if (allowedModes == TRUSTED)  // IMPL_LOOKUP can make any lookup at all
        return new Lookup(requestedLookupClass, ALL_MODES);
    if (requestedLookupClass == this.lookupClass)
        return this;  // keep same capabilities
    int newModes = (allowedModes & (ALL_MODES & ~PROTECTED));
    if ((newModes & PACKAGE) != 0
        && !VerifyAccess.isSamePackage(this.lookupClass, requestedLookupClass)) {
        newModes &= ~(PACKAGE|PRIVATE);
    }
    // Allow nestmate lookups to be created without special privilege:
    if ((newModes & PRIVATE) != 0
        && !VerifyAccess.isSamePackageMember(this.lookupClass, requestedLookupClass)) {
        newModes &= ~PRIVATE;
    }
    if ((newModes & PUBLIC) != 0
        && !VerifyAccess.isClassAccessible(requestedLookupClass, this.lookupClass, allowedModes)) {
        // The requested class it not accessible from the lookup class.
        // No permissions.
        newModes = 0;
    }
    checkUnprivilegedlookupClass(requestedLookupClass, newModes);
    return new Lookup(requestedLookupClass, newModes);
}
 
Example 4
Source File: InvokerBytecodeGenerator.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static boolean isStaticallyNameable(Class<?> cls) {
    if (cls == Object.class)
        return true;
    while (cls.isArray())
        cls = cls.getComponentType();
    if (cls.isPrimitive())
        return true;  // int[].class, for example
    if (ReflectUtil.isVMAnonymousClass(cls)) // FIXME: switch to supported API once it is added
        return false;
    // could use VerifyAccess.isClassAccessible but the following is a safe approximation
    if (cls.getClassLoader() != Object.class.getClassLoader())
        return false;
    if (VerifyAccess.isSamePackage(MethodHandle.class, cls))
        return true;
    if (!Modifier.isPublic(cls.getModifiers()))
        return false;
    for (Class<?> pkgcls : STATICALLY_INVOCABLE_PACKAGES) {
        if (VerifyAccess.isSamePackage(pkgcls, cls))
            return true;
    }
    return false;
}
 
Example 5
Source File: InvokerBytecodeGenerator.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
static boolean isStaticallyNameable(Class<?> cls) {
    if (cls == Object.class)
        return true;
    while (cls.isArray())
        cls = cls.getComponentType();
    if (cls.isPrimitive())
        return true;  // int[].class, for example
    if (ReflectUtil.isVMAnonymousClass(cls)) // FIXME: switch to supported API once it is added
        return false;
    // could use VerifyAccess.isClassAccessible but the following is a safe approximation
    if (cls.getClassLoader() != Object.class.getClassLoader())
        return false;
    if (VerifyAccess.isSamePackage(MethodHandle.class, cls))
        return true;
    if (!Modifier.isPublic(cls.getModifiers()))
        return false;
    for (Class<?> pkgcls : STATICALLY_INVOCABLE_PACKAGES) {
        if (VerifyAccess.isSamePackage(pkgcls, cls))
            return true;
    }
    return false;
}
 
Example 6
Source File: InvokerBytecodeGenerator.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
static boolean isStaticallyNameable(Class<?> cls) {
    if (cls == Object.class)
        return true;
    while (cls.isArray())
        cls = cls.getComponentType();
    if (cls.isPrimitive())
        return true;  // int[].class, for example
    if (ReflectUtil.isVMAnonymousClass(cls)) // FIXME: switch to supported API once it is added
        return false;
    // could use VerifyAccess.isClassAccessible but the following is a safe approximation
    if (cls.getClassLoader() != Object.class.getClassLoader())
        return false;
    if (VerifyAccess.isSamePackage(MethodHandle.class, cls))
        return true;
    if (!Modifier.isPublic(cls.getModifiers()))
        return false;
    for (Class<?> pkgcls : STATICALLY_INVOCABLE_PACKAGES) {
        if (VerifyAccess.isSamePackage(pkgcls, cls))
            return true;
    }
    return false;
}
 
Example 7
Source File: InvokerBytecodeGenerator.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
static boolean isStaticallyInvocable(MemberName member) {
    if (member == null)  return false;
    if (member.isConstructor())  return false;
    Class<?> cls = member.getDeclaringClass();
    if (cls.isArray() || cls.isPrimitive())
        return false;  // FIXME
    if (cls.isAnonymousClass() || cls.isLocalClass())
        return false;  // inner class of some sort
    if (cls.getClassLoader() != MethodHandle.class.getClassLoader())
        return false;  // not on BCP
    if (ReflectUtil.isVMAnonymousClass(cls)) // FIXME: switch to supported API once it is added
        return false;
    MethodType mtype = member.getMethodOrFieldType();
    if (!isStaticallyNameable(mtype.returnType()))
        return false;
    for (Class<?> ptype : mtype.parameterArray())
        if (!isStaticallyNameable(ptype))
            return false;
    if (!member.isPrivate() && VerifyAccess.isSamePackage(MethodHandle.class, cls))
        return true;   // in java.lang.invoke package
    if (member.isPublic() && isStaticallyNameable(cls))
        return true;
    return false;
}
 
Example 8
Source File: InvokerBytecodeGenerator.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
static boolean isStaticallyInvocable(MemberName member) {
    if (member == null)  return false;
    if (member.isConstructor())  return false;
    Class<?> cls = member.getDeclaringClass();
    if (cls.isArray() || cls.isPrimitive())
        return false;  // FIXME
    if (cls.isAnonymousClass() || cls.isLocalClass())
        return false;  // inner class of some sort
    if (cls.getClassLoader() != MethodHandle.class.getClassLoader())
        return false;  // not on BCP
    if (ReflectUtil.isVMAnonymousClass(cls)) // FIXME: switch to supported API once it is added
        return false;
    MethodType mtype = member.getMethodOrFieldType();
    if (!isStaticallyNameable(mtype.returnType()))
        return false;
    for (Class<?> ptype : mtype.parameterArray())
        if (!isStaticallyNameable(ptype))
            return false;
    if (!member.isPrivate() && VerifyAccess.isSamePackage(MethodHandle.class, cls))
        return true;   // in java.lang.invoke package
    if (member.isPublic() && isStaticallyNameable(cls))
        return true;
    return false;
}
 
Example 9
Source File: InvokerBytecodeGenerator.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
static boolean isStaticallyInvocable(MemberName member) {
    if (member == null)  return false;
    if (member.isConstructor())  return false;
    Class<?> cls = member.getDeclaringClass();
    if (cls.isArray() || cls.isPrimitive())
        return false;  // FIXME
    if (cls.isAnonymousClass() || cls.isLocalClass())
        return false;  // inner class of some sort
    if (cls.getClassLoader() != MethodHandle.class.getClassLoader())
        return false;  // not on BCP
    if (ReflectUtil.isVMAnonymousClass(cls)) // FIXME: switch to supported API once it is added
        return false;
    MethodType mtype = member.getMethodOrFieldType();
    if (!isStaticallyNameable(mtype.returnType()))
        return false;
    for (Class<?> ptype : mtype.parameterArray())
        if (!isStaticallyNameable(ptype))
            return false;
    if (!member.isPrivate() && VerifyAccess.isSamePackage(MethodHandle.class, cls))
        return true;   // in java.lang.invoke package
    if (member.isPublic() && isStaticallyNameable(cls))
        return true;
    return false;
}
 
Example 10
Source File: MethodHandles.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a lookup on the specified new lookup class.
 * The resulting object will report the specified
 * class as its own {@link #lookupClass lookupClass}.
 * <p>
 * However, the resulting {@code Lookup} object is guaranteed
 * to have no more access capabilities than the original.
 * In particular, access capabilities can be lost as follows:<ul>
 * <li>If the new lookup class differs from the old one,
 * protected members will not be accessible by virtue of inheritance.
 * (Protected members may continue to be accessible because of package sharing.)
 * <li>If the new lookup class is in a different package
 * than the old one, protected and default (package) members will not be accessible.
 * <li>If the new lookup class is not within the same package member
 * as the old one, private members will not be accessible.
 * <li>If the new lookup class is not accessible to the old lookup class,
 * then no members, not even public members, will be accessible.
 * (In all other cases, public members will continue to be accessible.)
 * </ul>
 *
 * @param requestedLookupClass the desired lookup class for the new lookup object
 * @return a lookup object which reports the desired lookup class
 * @throws NullPointerException if the argument is null
 */
public Lookup in(Class<?> requestedLookupClass) {
    requestedLookupClass.getClass();  // null check
    if (allowedModes == TRUSTED)  // IMPL_LOOKUP can make any lookup at all
        return new Lookup(requestedLookupClass, ALL_MODES);
    if (requestedLookupClass == this.lookupClass)
        return this;  // keep same capabilities
    int newModes = (allowedModes & (ALL_MODES & ~PROTECTED));
    if ((newModes & PACKAGE) != 0
        && !VerifyAccess.isSamePackage(this.lookupClass, requestedLookupClass)) {
        newModes &= ~(PACKAGE|PRIVATE);
    }
    // Allow nestmate lookups to be created without special privilege:
    if ((newModes & PRIVATE) != 0
        && !VerifyAccess.isSamePackageMember(this.lookupClass, requestedLookupClass)) {
        newModes &= ~PRIVATE;
    }
    if ((newModes & PUBLIC) != 0
        && !VerifyAccess.isClassAccessible(requestedLookupClass, this.lookupClass, allowedModes)) {
        // The requested class it not accessible from the lookup class.
        // No permissions.
        newModes = 0;
    }
    checkUnprivilegedlookupClass(requestedLookupClass, newModes);
    return new Lookup(requestedLookupClass, newModes);
}
 
Example 11
Source File: MethodHandles.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a lookup on the specified new lookup class.
 * The resulting object will report the specified
 * class as its own {@link #lookupClass lookupClass}.
 * <p>
 * However, the resulting {@code Lookup} object is guaranteed
 * to have no more access capabilities than the original.
 * In particular, access capabilities can be lost as follows:<ul>
 * <li>If the new lookup class differs from the old one,
 * protected members will not be accessible by virtue of inheritance.
 * (Protected members may continue to be accessible because of package sharing.)
 * <li>If the new lookup class is in a different package
 * than the old one, protected and default (package) members will not be accessible.
 * <li>If the new lookup class is not within the same package member
 * as the old one, private members will not be accessible.
 * <li>If the new lookup class is not accessible to the old lookup class,
 * then no members, not even public members, will be accessible.
 * (In all other cases, public members will continue to be accessible.)
 * </ul>
 *
 * @param requestedLookupClass the desired lookup class for the new lookup object
 * @return a lookup object which reports the desired lookup class
 * @throws NullPointerException if the argument is null
 */
public Lookup in(Class<?> requestedLookupClass) {
    requestedLookupClass.getClass();  // null check
    // Android-changed: There's no notion of a trusted lookup.
    // if (allowedModes == TRUSTED)  // IMPL_LOOKUP can make any lookup at all
    //    return new Lookup(requestedLookupClass, ALL_MODES);

    if (requestedLookupClass == this.lookupClass)
        return this;  // keep same capabilities
    int newModes = (allowedModes & (ALL_MODES & ~PROTECTED));
    if ((newModes & PACKAGE) != 0
        && !VerifyAccess.isSamePackage(this.lookupClass, requestedLookupClass)) {
        newModes &= ~(PACKAGE|PRIVATE);
    }
    // Allow nestmate lookups to be created without special privilege:
    if ((newModes & PRIVATE) != 0
        && !VerifyAccess.isSamePackageMember(this.lookupClass, requestedLookupClass)) {
        newModes &= ~PRIVATE;
    }
    if ((newModes & PUBLIC) != 0
        && !VerifyAccess.isClassAccessible(requestedLookupClass, this.lookupClass, allowedModes)) {
        // The requested class it not accessible from the lookup class.
        // No permissions.
        newModes = 0;
    }
    checkUnprivilegedlookupClass(requestedLookupClass, newModes);
    return new Lookup(requestedLookupClass, newModes);
}
 
Example 12
Source File: DirectMethodHandle.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static
boolean shouldBeInitialized(MemberName member) {
    switch (member.getReferenceKind()) {
    case REF_invokeStatic:
    case REF_getStatic:
    case REF_putStatic:
    case REF_newInvokeSpecial:
        break;
    default:
        // No need to initialize the class on this kind of member.
        return false;
    }
    Class<?> cls = member.getDeclaringClass();
    if (cls == ValueConversions.class ||
        cls == MethodHandleImpl.class ||
        cls == Invokers.class) {
        // These guys have lots of <clinit> DMH creation but we know
        // the MHs will not be used until the system is booted.
        return false;
    }
    if (VerifyAccess.isSamePackage(MethodHandle.class, cls) ||
        VerifyAccess.isSamePackage(ValueConversions.class, cls)) {
        // It is a system class.  It is probably in the process of
        // being initialized, but we will help it along just to be safe.
        if (UNSAFE.shouldBeInitialized(cls)) {
            UNSAFE.ensureClassInitialized(cls);
        }
        return false;
    }
    return UNSAFE.shouldBeInitialized(cls);
}
 
Example 13
Source File: MethodHandles.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private boolean restrictProtectedReceiver(MemberName method) {
    // The accessing class only has the right to use a protected member
    // on itself or a subclass.  Enforce that restriction, from JVMS 5.4.4, etc.
    if (!method.isProtected() || method.isStatic()
        || allowedModes == TRUSTED
        || method.getDeclaringClass() == lookupClass()
        || VerifyAccess.isSamePackage(method.getDeclaringClass(), lookupClass())
        || (ALLOW_NESTMATE_ACCESS &&
            VerifyAccess.isSamePackageMember(method.getDeclaringClass(), lookupClass())))
        return false;
    return true;
}
 
Example 14
Source File: DirectMethodHandle.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static
boolean shouldBeInitialized(MemberName member) {
    switch (member.getReferenceKind()) {
    case REF_invokeStatic:
    case REF_getStatic:
    case REF_putStatic:
    case REF_newInvokeSpecial:
        break;
    default:
        // No need to initialize the class on this kind of member.
        return false;
    }
    Class<?> cls = member.getDeclaringClass();
    if (cls == ValueConversions.class ||
        cls == MethodHandleImpl.class ||
        cls == Invokers.class) {
        // These guys have lots of <clinit> DMH creation but we know
        // the MHs will not be used until the system is booted.
        return false;
    }
    if (VerifyAccess.isSamePackage(MethodHandle.class, cls) ||
        VerifyAccess.isSamePackage(ValueConversions.class, cls)) {
        // It is a system class.  It is probably in the process of
        // being initialized, but we will help it along just to be safe.
        if (UNSAFE.shouldBeInitialized(cls)) {
            UNSAFE.ensureClassInitialized(cls);
        }
        return false;
    }
    return UNSAFE.shouldBeInitialized(cls);
}
 
Example 15
Source File: MethodHandles.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private boolean restrictProtectedReceiver(MemberName method) {
    // The accessing class only has the right to use a protected member
    // on itself or a subclass.  Enforce that restriction, from JVMS 5.4.4, etc.
    if (!method.isProtected() || method.isStatic()
        || allowedModes == TRUSTED
        || method.getDeclaringClass() == lookupClass()
        || VerifyAccess.isSamePackage(method.getDeclaringClass(), lookupClass())
        || (ALLOW_NESTMATE_ACCESS &&
            VerifyAccess.isSamePackageMember(method.getDeclaringClass(), lookupClass())))
        return false;
    return true;
}
 
Example 16
Source File: MethodHandles.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private boolean restrictProtectedReceiver(MemberName method) {
    // The accessing class only has the right to use a protected member
    // on itself or a subclass.  Enforce that restriction, from JVMS 5.4.4, etc.
    if (!method.isProtected() || method.isStatic()
        || allowedModes == TRUSTED
        || method.getDeclaringClass() == lookupClass()
        || VerifyAccess.isSamePackage(method.getDeclaringClass(), lookupClass())
        || (ALLOW_NESTMATE_ACCESS &&
            VerifyAccess.isSamePackageMember(method.getDeclaringClass(), lookupClass())))
        return false;
    return true;
}
 
Example 17
Source File: DirectMethodHandle.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static
boolean shouldBeInitialized(MemberName member) {
    switch (member.getReferenceKind()) {
    case REF_invokeStatic:
    case REF_getStatic:
    case REF_putStatic:
    case REF_newInvokeSpecial:
        break;
    default:
        // No need to initialize the class on this kind of member.
        return false;
    }
    Class<?> cls = member.getDeclaringClass();
    if (cls == ValueConversions.class ||
        cls == MethodHandleImpl.class ||
        cls == Invokers.class) {
        // These guys have lots of <clinit> DMH creation but we know
        // the MHs will not be used until the system is booted.
        return false;
    }
    if (VerifyAccess.isSamePackage(MethodHandle.class, cls) ||
        VerifyAccess.isSamePackage(ValueConversions.class, cls)) {
        // It is a system class.  It is probably in the process of
        // being initialized, but we will help it along just to be safe.
        if (UNSAFE.shouldBeInitialized(cls)) {
            UNSAFE.ensureClassInitialized(cls);
        }
        return false;
    }
    return UNSAFE.shouldBeInitialized(cls);
}
 
Example 18
Source File: MethodHandles.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private boolean restrictProtectedReceiver(MemberName method) {
    // The accessing class only has the right to use a protected member
    // on itself or a subclass.  Enforce that restriction, from JVMS 5.4.4, etc.
    if (!method.isProtected() || method.isStatic()
        || allowedModes == TRUSTED
        || method.getDeclaringClass() == lookupClass()
        || VerifyAccess.isSamePackage(method.getDeclaringClass(), lookupClass())
        || (ALLOW_NESTMATE_ACCESS &&
            VerifyAccess.isSamePackageMember(method.getDeclaringClass(), lookupClass())))
        return false;
    return true;
}
 
Example 19
Source File: MethodHandles.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/** Check public/protected/private bits on the symbolic reference class and its member. */
void checkAccess(byte refKind, Class<?> refc, MemberName m) throws IllegalAccessException {
    assert(m.referenceKindIsConsistentWith(refKind) &&
           MethodHandleNatives.refKindIsValid(refKind) &&
           (MethodHandleNatives.refKindIsField(refKind) == m.isField()));
    int allowedModes = this.allowedModes;
    if (allowedModes == TRUSTED)  return;
    int mods = m.getModifiers();
    if (Modifier.isProtected(mods) &&
            refKind == REF_invokeVirtual &&
            m.getDeclaringClass() == Object.class &&
            m.getName().equals("clone") &&
            refc.isArray()) {
        // The JVM does this hack also.
        // (See ClassVerifier::verify_invoke_instructions
        // and LinkResolver::check_method_accessability.)
        // Because the JVM does not allow separate methods on array types,
        // there is no separate method for int[].clone.
        // All arrays simply inherit Object.clone.
        // But for access checking logic, we make Object.clone
        // (normally protected) appear to be public.
        // Later on, when the DirectMethodHandle is created,
        // its leading argument will be restricted to the
        // requested array type.
        // N.B. The return type is not adjusted, because
        // that is *not* the bytecode behavior.
        mods ^= Modifier.PROTECTED | Modifier.PUBLIC;
    }
    if (Modifier.isProtected(mods) && refKind == REF_newInvokeSpecial) {
        // cannot "new" a protected ctor in a different package
        mods ^= Modifier.PROTECTED;
    }
    if (Modifier.isFinal(mods) &&
            MethodHandleNatives.refKindIsSetter(refKind))
        throw m.makeAccessException("unexpected set of a final field", this);
    if (Modifier.isPublic(mods) && Modifier.isPublic(refc.getModifiers()) && allowedModes != 0)
        return;  // common case
    int requestedModes = fixmods(mods);  // adjust 0 => PACKAGE
    if ((requestedModes & allowedModes) != 0) {
        if (VerifyAccess.isMemberAccessible(refc, m.getDeclaringClass(),
                                            mods, lookupClass(), allowedModes))
            return;
    } else {
        // Protected members can also be checked as if they were package-private.
        if ((requestedModes & PROTECTED) != 0 && (allowedModes & PACKAGE) != 0
                && VerifyAccess.isSamePackage(m.getDeclaringClass(), lookupClass()))
            return;
    }
    throw m.makeAccessException(accessFailedMessage(refc, m), this);
}
 
Example 20
Source File: MethodHandles.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/** Check public/protected/private bits on the symbolic reference class and its member. */
void checkAccess(byte refKind, Class<?> refc, MemberName m) throws IllegalAccessException {
    assert(m.referenceKindIsConsistentWith(refKind) &&
           MethodHandleNatives.refKindIsValid(refKind) &&
           (MethodHandleNatives.refKindIsField(refKind) == m.isField()));
    int allowedModes = this.allowedModes;
    if (allowedModes == TRUSTED)  return;
    int mods = m.getModifiers();
    if (Modifier.isProtected(mods) &&
            refKind == REF_invokeVirtual &&
            m.getDeclaringClass() == Object.class &&
            m.getName().equals("clone") &&
            refc.isArray()) {
        // The JVM does this hack also.
        // (See ClassVerifier::verify_invoke_instructions
        // and LinkResolver::check_method_accessability.)
        // Because the JVM does not allow separate methods on array types,
        // there is no separate method for int[].clone.
        // All arrays simply inherit Object.clone.
        // But for access checking logic, we make Object.clone
        // (normally protected) appear to be public.
        // Later on, when the DirectMethodHandle is created,
        // its leading argument will be restricted to the
        // requested array type.
        // N.B. The return type is not adjusted, because
        // that is *not* the bytecode behavior.
        mods ^= Modifier.PROTECTED | Modifier.PUBLIC;
    }
    if (Modifier.isProtected(mods) && refKind == REF_newInvokeSpecial) {
        // cannot "new" a protected ctor in a different package
        mods ^= Modifier.PROTECTED;
    }
    if (Modifier.isFinal(mods) &&
            MethodHandleNatives.refKindIsSetter(refKind))
        throw m.makeAccessException("unexpected set of a final field", this);
    if (Modifier.isPublic(mods) && Modifier.isPublic(refc.getModifiers()) && allowedModes != 0)
        return;  // common case
    int requestedModes = fixmods(mods);  // adjust 0 => PACKAGE
    if ((requestedModes & allowedModes) != 0) {
        if (VerifyAccess.isMemberAccessible(refc, m.getDeclaringClass(),
                                            mods, lookupClass(), allowedModes))
            return;
    } else {
        // Protected members can also be checked as if they were package-private.
        if ((requestedModes & PROTECTED) != 0 && (allowedModes & PACKAGE) != 0
                && VerifyAccess.isSamePackage(m.getDeclaringClass(), lookupClass()))
            return;
    }
    throw m.makeAccessException(accessFailedMessage(refc, m), this);
}