aboutsummaryrefslogtreecommitdiff
path: root/src/share/classes/sun/reflect
diff options
context:
space:
mode:
authorjfranck <none@none>2013-10-11 13:14:15 +0200
committerjfranck <none@none>2013-10-11 13:14:15 +0200
commitc89d987c4bd521e3a4506b499810cc441cb9037c (patch)
tree3e95a57f046b8ca8622a32a055a8f462f4693124 /src/share/classes/sun/reflect
parentdbe0333ceba87b3c0666338f886b7ff079432167 (diff)
8023301: Enhance generic classes
Reviewed-by: mchung, hawtin
Diffstat (limited to 'src/share/classes/sun/reflect')
-rw-r--r--src/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java21
-rw-r--r--src/share/classes/sun/reflect/misc/ReflectUtil.java36
2 files changed, 56 insertions, 1 deletions
diff --git a/src/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java b/src/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java
index 63da5ea75..4610a519b 100644
--- a/src/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java
+++ b/src/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java
@@ -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();
diff --git a/src/share/classes/sun/reflect/misc/ReflectUtil.java b/src/share/classes/sun/reflect/misc/ReflectUtil.java
index 1f871e8de..7e999cf21 100644
--- a/src/share/classes/sun/reflect/misc/ReflectUtil.java
+++ b/src/share/classes/sun/reflect/misc/ReflectUtil.java
@@ -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 {
@@ -118,6 +120,40 @@ public final class ReflectUtil {
}
/**
+ * 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.
*
* If it is a {@link Proxy#isProxyClass(java.lang.Class)} that implements