mirror of
https://github.com/openjdk/jdk.git
synced 2026-07-19 07:29:08 +00:00
8023301: Enhance generic classes
Reviewed-by: mchung, hawtin
This commit is contained in:
parent
36b94773c9
commit
f32480245f
@ -28,7 +28,10 @@ package sun.reflect.generics.reflectiveObjects;
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.GenericDeclaration;
|
||||
import java.lang.reflect.Member;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -40,6 +43,7 @@ import sun.reflect.annotation.AnnotationType;
|
||||
import sun.reflect.generics.factory.GenericsFactory;
|
||||
import sun.reflect.generics.tree.FieldTypeSignature;
|
||||
import sun.reflect.generics.visitor.Reifier;
|
||||
import sun.reflect.misc.ReflectUtil;
|
||||
|
||||
/**
|
||||
* Implementation of <tt>java.lang.reflect.TypeVariable</tt> interface
|
||||
@ -95,6 +99,13 @@ public class TypeVariableImpl<D extends GenericDeclaration>
|
||||
TypeVariableImpl<T> make(T decl, String name,
|
||||
FieldTypeSignature[] bs,
|
||||
GenericsFactory f) {
|
||||
|
||||
if (!((decl instanceof Class) ||
|
||||
(decl instanceof Method) ||
|
||||
(decl instanceof Constructor))) {
|
||||
throw new AssertionError("Unexpected kind of GenericDeclaration" +
|
||||
decl.getClass().toString());
|
||||
}
|
||||
return new TypeVariableImpl<T>(decl, name, bs, f);
|
||||
}
|
||||
|
||||
@ -149,6 +160,13 @@ public class TypeVariableImpl<D extends GenericDeclaration>
|
||||
* @since 1.5
|
||||
*/
|
||||
public D getGenericDeclaration(){
|
||||
if (genericDeclaration instanceof Class)
|
||||
ReflectUtil.checkPackageAccess((Class)genericDeclaration);
|
||||
else if ((genericDeclaration instanceof Method) ||
|
||||
(genericDeclaration instanceof Constructor))
|
||||
ReflectUtil.conservativeCheckMemberAccess((Member)genericDeclaration);
|
||||
else
|
||||
throw new AssertionError("Unexpected kind of GenericDeclaration");
|
||||
return genericDeclaration;
|
||||
}
|
||||
|
||||
@ -164,7 +182,8 @@ public class TypeVariableImpl<D extends GenericDeclaration>
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof TypeVariable) {
|
||||
if (o instanceof TypeVariable &&
|
||||
o.getClass() == TypeVariableImpl.class) {
|
||||
TypeVariable<?> that = (TypeVariable<?>) o;
|
||||
|
||||
GenericDeclaration thatDecl = that.getGenericDeclaration();
|
||||
|
||||
@ -26,11 +26,13 @@
|
||||
|
||||
package sun.reflect.misc;
|
||||
|
||||
import java.lang.reflect.Member;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.Arrays;
|
||||
import sun.reflect.Reflection;
|
||||
import sun.security.util.SecurityConstants;
|
||||
|
||||
public final class ReflectUtil {
|
||||
|
||||
@ -117,6 +119,40 @@ public final class ReflectUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does a conservative approximation of member access check. Use this if
|
||||
* you don't have an actual 'userland' caller Class/ClassLoader available.
|
||||
* This might be more restrictive than a precise member access check where
|
||||
* you have a caller, but should never allow a member access that is
|
||||
* forbidden.
|
||||
*
|
||||
* @param m the {@code Member} about to be accessed
|
||||
*/
|
||||
public static void conservativeCheckMemberAccess(Member m) throws SecurityException{
|
||||
final SecurityManager sm = System.getSecurityManager();
|
||||
if (sm == null)
|
||||
return;
|
||||
|
||||
// Check for package access on the declaring class.
|
||||
//
|
||||
// In addition, unless the member and the declaring class are both
|
||||
// public check for access declared member permissions.
|
||||
//
|
||||
// This is done regardless of ClassLoader relations between the {@code
|
||||
// Member m} and any potential caller.
|
||||
|
||||
final Class<?> declaringClass = m.getDeclaringClass();
|
||||
|
||||
checkPackageAccess(declaringClass);
|
||||
|
||||
if (Modifier.isPublic(m.getModifiers()) &&
|
||||
Modifier.isPublic(declaringClass.getModifiers()))
|
||||
return;
|
||||
|
||||
// Check for declared member access.
|
||||
sm.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks package access on the given class.
|
||||
*
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user