1 |
14 |
jlechner |
/* java.lang.reflect.AccessibleObject
|
2 |
|
|
Copyright (C) 2001, 2005 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
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)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package java.lang.reflect;
|
40 |
|
|
|
41 |
|
|
/**
|
42 |
|
|
* This class is the superclass of various reflection classes, and
|
43 |
|
|
* allows sufficiently trusted code to bypass normal restrictions to
|
44 |
|
|
* do necessary things like invoke private methods outside of the
|
45 |
|
|
* class during Serialization. If you don't have a good reason
|
46 |
|
|
* to mess with this, don't try. Fortunately, there are adequate
|
47 |
|
|
* security checks before you can set a reflection object as accessible.
|
48 |
|
|
*
|
49 |
|
|
* @author Tom Tromey (tromey@cygnus.com)
|
50 |
|
|
* @author Eric Blake (ebb9@email.byu.edu)
|
51 |
|
|
* @see Field
|
52 |
|
|
* @see Constructor
|
53 |
|
|
* @see Method
|
54 |
|
|
* @see ReflectPermission
|
55 |
|
|
* @since 1.2
|
56 |
|
|
* @status updated to 1.4
|
57 |
|
|
*/
|
58 |
|
|
public class AccessibleObject
|
59 |
|
|
{
|
60 |
|
|
/**
|
61 |
|
|
* True if this object is marked accessible, which means the reflected
|
62 |
|
|
* object bypasses normal security checks.
|
63 |
|
|
*/
|
64 |
|
|
// default visibility for use by inherited classes
|
65 |
|
|
boolean flag = false;
|
66 |
|
|
|
67 |
|
|
/**
|
68 |
|
|
* Only the three reflection classes that extend this can create an
|
69 |
|
|
* accessible object. This is not serializable for security reasons.
|
70 |
|
|
*/
|
71 |
|
|
protected AccessibleObject()
|
72 |
|
|
{
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
|
|
* Return the accessibility status of this object.
|
77 |
|
|
*
|
78 |
|
|
* @return true if this object bypasses security checks
|
79 |
|
|
*/
|
80 |
|
|
public boolean isAccessible()
|
81 |
|
|
{
|
82 |
|
|
return flag;
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
/**
|
86 |
|
|
* Convenience method to set the flag on a number of objects with a single
|
87 |
|
|
* security check. If a security manager exists, it is checked for
|
88 |
|
|
* <code>ReflectPermission("suppressAccessChecks")</code>.<p>
|
89 |
|
|
*
|
90 |
|
|
* It is forbidden to set the accessibility flag to true on any constructor
|
91 |
|
|
* for java.lang.Class. This will result in a SecurityException. If the
|
92 |
|
|
* SecurityException is thrown for any of the passed AccessibleObjects,
|
93 |
|
|
* the accessibility flag will be set on AccessibleObjects in the array prior
|
94 |
|
|
* to the one which resulted in the exception.
|
95 |
|
|
*
|
96 |
|
|
* @param array the array of accessible objects
|
97 |
|
|
* @param flag the desired state of accessibility, true to bypass security
|
98 |
|
|
* @throws NullPointerException if array is null
|
99 |
|
|
* @throws SecurityException if the request is denied
|
100 |
|
|
* @see SecurityManager#checkPermission(java.security.Permission)
|
101 |
|
|
* @see RuntimePermission
|
102 |
|
|
*/
|
103 |
|
|
public static void setAccessible(AccessibleObject[] array, boolean flag)
|
104 |
|
|
{
|
105 |
|
|
checkPermission();
|
106 |
|
|
for (int i = 0; i < array.length; i++)
|
107 |
|
|
array[i].secureSetAccessible(flag);
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
/**
|
111 |
|
|
* Sets the accessibility flag for this reflection object. If a security
|
112 |
|
|
* manager exists, it is checked for
|
113 |
|
|
* <code>ReflectPermission("suppressAccessChecks")</code>.<p>
|
114 |
|
|
*
|
115 |
|
|
* It is forbidden to set the accessibility flag to true on any constructor for
|
116 |
|
|
* java.lang.Class. This will result in a SecurityException.
|
117 |
|
|
*
|
118 |
|
|
* @param flag the desired state of accessibility, true to bypass security
|
119 |
|
|
* @throws NullPointerException if array is null
|
120 |
|
|
* @throws SecurityException if the request is denied
|
121 |
|
|
* @see SecurityManager#checkPermission(java.security.Permission)
|
122 |
|
|
* @see RuntimePermission
|
123 |
|
|
*/
|
124 |
|
|
public void setAccessible(boolean flag)
|
125 |
|
|
{
|
126 |
|
|
checkPermission();
|
127 |
|
|
secureSetAccessible(flag);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
/**
|
131 |
|
|
* Performs the specified security check, for
|
132 |
|
|
* <code>ReflectPermission("suppressAccessChecks")</code>.
|
133 |
|
|
*
|
134 |
|
|
* @throws SecurityException if permission is denied
|
135 |
|
|
*/
|
136 |
|
|
private static void checkPermission()
|
137 |
|
|
{
|
138 |
|
|
SecurityManager sm = System.getSecurityManager();
|
139 |
|
|
if (sm != null)
|
140 |
|
|
sm.checkPermission(new ReflectPermission("suppressAccessChecks"));
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
/**
|
144 |
|
|
* Performs the actual accessibility change, this must always be invoked
|
145 |
|
|
* after calling checkPermission.
|
146 |
|
|
*
|
147 |
|
|
* @param flag the desired status
|
148 |
|
|
* @throws SecurityException if flag is true and this is a constructor
|
149 |
|
|
* for <code>java.lang.Class</code>.
|
150 |
|
|
*/
|
151 |
|
|
private void secureSetAccessible(boolean flag)
|
152 |
|
|
{
|
153 |
|
|
if (flag &&
|
154 |
|
|
(this instanceof Constructor
|
155 |
|
|
&& ((Constructor) this).getDeclaringClass() == Class.class))
|
156 |
|
|
throw new SecurityException("Cannot make object accessible: " + this);
|
157 |
|
|
this.flag = flag;
|
158 |
|
|
}
|
159 |
|
|
}
|