1 |
/* java.lang.reflect.AccessibleObject |
/* java.lang.reflect.AccessibleObject |
2 |
Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
Copyright (C) 2001 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
7 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
8 |
the Free Software Foundation; either version 2, or (at your option) |
the Free Software Foundation; either version 2, or (at your option) |
9 |
any later version. |
any later version. |
10 |
|
|
11 |
GNU Classpath is distributed in the hope that it will be useful, but |
GNU Classpath is distributed in the hope that it will be useful, but |
12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
24 |
This exception does not however invalidate any other reasons why the |
This exception does not however invalidate any other reasons why the |
25 |
executable file might be covered by the GNU General Public License. */ |
executable file might be covered by the GNU General Public License. */ |
26 |
|
|
27 |
|
|
28 |
package java.lang.reflect; |
package java.lang.reflect; |
29 |
|
|
30 |
/** |
/** |
31 |
|
* This class is the superclass of various reflection classes, and |
32 |
|
* allows sufficiently trusted code to bypass normal restrictions to |
33 |
|
* do necessary things like invoke private methods outside of the |
34 |
|
* class during Serialization. If you don't have a good reason |
35 |
|
* to mess with this, don't try. Fortunately, there are adequate |
36 |
|
* security checks before you can set a reflection object as accessible. |
37 |
|
* |
38 |
* @author Tom Tromey <tromey@cygnus.com> |
* @author Tom Tromey <tromey@cygnus.com> |
39 |
* @date December 12, 1998 |
* @author Eric Blake <ebb9@email.byu.edu> |
40 |
*/ |
* @see Field |
41 |
/* Written using JDK 1.2 beta docs. |
* @see Constructor |
42 |
* Status: Believed complete and correct. |
* @see Method |
43 |
|
* @see ReflectPermission |
44 |
|
* @since 1.2 |
45 |
|
* @status updated to 1.4 |
46 |
*/ |
*/ |
|
|
|
47 |
public class AccessibleObject |
public class AccessibleObject |
48 |
{ |
{ |
49 |
protected AccessibleObject () |
/** |
50 |
|
* True if this object is marked accessible, which means the reflected |
51 |
|
* object bypasses normal security checks. <em>NOTE</em>Don't try messing |
52 |
|
* with this by reflection. You'll mess yourself up. |
53 |
|
*/ |
54 |
|
// default visibility for use by inherited classes |
55 |
|
boolean flag = false; |
56 |
|
|
57 |
|
/** |
58 |
|
* Only the three reflection classes that extend this can create an |
59 |
|
* accessible object. This is not serializable for security reasons. |
60 |
|
*/ |
61 |
|
protected AccessibleObject() |
62 |
{ |
{ |
|
flag = false; |
|
63 |
} |
} |
64 |
|
|
65 |
public boolean isAccessible () |
/** |
66 |
|
* Return the accessibility status of this object. |
67 |
|
* |
68 |
|
* @return true if this object bypasses security checks |
69 |
|
*/ |
70 |
|
public boolean isAccessible() |
71 |
{ |
{ |
72 |
return flag; |
return flag; |
73 |
} |
} |
74 |
|
|
75 |
public static void setAccessible (AccessibleObject[] array, boolean flag) |
/** |
76 |
|
* Convenience method to set the flag on a number of objects with a single |
77 |
|
* security check. If a security manager exists, it is checked for |
78 |
|
* <code>ReflectPermission("suppressAccessChecks")</code>.<p> |
79 |
|
* |
80 |
|
* If <code>flag</code> is true, and the initial security check succeeds, |
81 |
|
* this can still fail if a forbidden object is encountered, leaving the |
82 |
|
* array half-modified. At the moment, the forbidden members are:<br> |
83 |
|
* <ul> |
84 |
|
* <li>Any Constructor for java.lang.Class</li> |
85 |
|
* <li>Any AccessibleObject for java.lang.reflect.AccessibleObject |
86 |
|
* (this is not specified by Sun, but it closes a big security hole |
87 |
|
* where you can use reflection to bypass the security checks that |
88 |
|
* reflection is supposed to provide)</li> |
89 |
|
* </ul> |
90 |
|
* (Sun has not specified others, but good candidates might include |
91 |
|
* ClassLoader, String, and such. However, the more checks we do, the |
92 |
|
* slower this method gets). |
93 |
|
* |
94 |
|
* @param array the array of accessible objects |
95 |
|
* @param flag the desired state of accessibility, true to bypass security |
96 |
|
* @throws NullPointerException if array is null |
97 |
|
* @throws SecurityException if the request is denied |
98 |
|
* @see SecurityManager#checkPermission(java.security.Permission) |
99 |
|
* @see RuntimePermission |
100 |
|
*/ |
101 |
|
public static void setAccessible(AccessibleObject[] array, boolean flag) |
102 |
{ |
{ |
103 |
checkPermission (); |
checkPermission(); |
104 |
// FIXME: check for invalid changes in the loop. |
for (int i = 0; i < array.length; i++) |
105 |
// For instance, can't set this flag to true for a Constructor for |
array[i].secureSetAccessible(flag); |
|
// Class (example from the manual). |
|
|
for (int i = 0; i < array.length; ++i) |
|
|
array[i].flag = flag; |
|
|
} |
|
|
|
|
|
public void setAccessible (boolean flag) |
|
|
{ |
|
|
checkPermission (); |
|
|
// FIXME: check for invalid changes. |
|
|
// For instance, can't set this flag to true for a Constructor for |
|
|
// Class (example from the manual). |
|
|
this.flag = flag; |
|
106 |
} |
} |
107 |
|
|
108 |
private static final void checkPermission () |
/** |
109 |
|
* Sets the accessibility flag for this reflection object. If a security |
110 |
|
* manager exists, it is checked for |
111 |
|
* <code>ReflectPermission("suppressAccessChecks")</code>.<p> |
112 |
|
* |
113 |
|
* If <code>flag</code> is true, and the initial security check succeeds, |
114 |
|
* this will still fail for a forbidden object. At the moment, the |
115 |
|
* forbidden members are:<br> |
116 |
|
* <ul> |
117 |
|
* <li>Any Constructor for java.lang.Class</li> |
118 |
|
* <li>Any AccessibleObject for java.lang.reflect.AccessibleObject |
119 |
|
* (this is not specified by Sun, but it closes a big security hole |
120 |
|
* where you can use reflection to bypass the security checks that |
121 |
|
* reflection is supposed to provide)</li> |
122 |
|
* </ul> |
123 |
|
* (Sun has not specified others, but good candidates might include |
124 |
|
* ClassLoader, String, and such. However, the more checks we do, the |
125 |
|
* slower this method gets). |
126 |
|
* |
127 |
|
* @param flag the desired state of accessibility, true to bypass security |
128 |
|
* @throws NullPointerException if array is null |
129 |
|
* @throws SecurityException if the request is denied |
130 |
|
* @see SecurityManager#checkPermission(java.security.Permission) |
131 |
|
* @see RuntimePermission |
132 |
|
*/ |
133 |
|
public void setAccessible(boolean flag) |
134 |
|
{ |
135 |
|
checkPermission(); |
136 |
|
secureSetAccessible(flag); |
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
|
* Performs the specified security check, for |
141 |
|
* <code>ReflectPermission("suppressAccessChecks")</code>. |
142 |
|
* |
143 |
|
* @throws SecurityException if permission is denied |
144 |
|
*/ |
145 |
|
private static final void checkPermission() |
146 |
{ |
{ |
147 |
SecurityManager sm = System.getSecurityManager(); |
SecurityManager sm = System.getSecurityManager(); |
148 |
if (sm != null) |
if (sm != null) |
149 |
sm.checkPermission (new ReflectPermission ("suppressAccessChecks")); |
sm.checkPermission(new ReflectPermission("suppressAccessChecks")); |
150 |
} |
} |
151 |
|
|
152 |
private boolean flag; |
/** |
153 |
|
* Performs the actual accessibility change, this must always be invoked |
154 |
|
* after calling checkPermission. |
155 |
|
* |
156 |
|
* @param flag the desired status |
157 |
|
* @throws SecurityException if flag is true and this is one of the |
158 |
|
* forbidden members mentioned in {@link setAccessible(boolean)}. |
159 |
|
*/ |
160 |
|
private final void secureSetAccessible(boolean flag) |
161 |
|
{ |
162 |
|
if (flag && |
163 |
|
((this instanceof Constructor |
164 |
|
&& ((Constructor) this).getDeclaringClass() == Class.class) |
165 |
|
|| ((Member) this).getDeclaringClass() == AccessibleObject.class)) |
166 |
|
throw new SecurityException("Cannot make object accessible: " + this); |
167 |
|
this.flag = flag; |
168 |
|
} |
169 |
} |
} |