author | chegar |
Fri, 15 Apr 2016 16:19:15 +0100 | |
changeset 37363 | 329dba26ffd2 |
parent 36511 | 9d0388c6b336 |
child 41560 | a66e7ee16cf9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36511 | 2 |
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang.reflect; |
|
27 |
||
28 |
import java.security.AccessController; |
|
36511 | 29 |
|
37363
329dba26ffd2
8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents:
36511
diff
changeset
|
30 |
import jdk.internal.reflect.CallerSensitive; |
329dba26ffd2
8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents:
36511
diff
changeset
|
31 |
import jdk.internal.reflect.Reflection; |
329dba26ffd2
8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents:
36511
diff
changeset
|
32 |
import jdk.internal.reflect.ReflectionFactory; |
2 | 33 |
import java.lang.annotation.Annotation; |
34 |
||
35 |
/** |
|
36 |
* The AccessibleObject class is the base class for Field, Method and |
|
37 |
* Constructor objects. It provides the ability to flag a reflected |
|
38 |
* object as suppressing default Java language access control checks |
|
36511 | 39 |
* when it is used. The access checks -- <em>module boundaries</em>, |
40 |
* public, default (package) access, protected, and private members -- |
|
41 |
* are performed when Fields, Methods or Constructors are used to set |
|
42 |
* or get fields, to invoke methods or to create and initialize new |
|
43 |
* instances of classes, respectively. Unlike access control specified |
|
44 |
* in the <cite>The Java™ Language Specification</cite> and |
|
45 |
* <cite>The Java Virtual Machine Specification</cite>, access checks |
|
46 |
* with reflected objects assume {@link Module#canRead readability}. |
|
2 | 47 |
* |
48 |
* <p>Setting the {@code accessible} flag in a reflected object |
|
49 |
* permits sophisticated applications with sufficient privilege, such |
|
50 |
* as Java Object Serialization or other persistence mechanisms, to |
|
51 |
* manipulate objects in a manner that would normally be prohibited. |
|
52 |
* |
|
4042
6fc4d5c2a456
6648344: (reflect spec) State default of isAccessible for reflective objects
darcy
parents:
3959
diff
changeset
|
53 |
* <p>By default, a reflected object is <em>not</em> accessible. |
6fc4d5c2a456
6648344: (reflect spec) State default of isAccessible for reflective objects
darcy
parents:
3959
diff
changeset
|
54 |
* |
2 | 55 |
* @see Field |
56 |
* @see Method |
|
57 |
* @see Constructor |
|
58 |
* @see ReflectPermission |
|
59 |
* |
|
60 |
* @since 1.2 |
|
61 |
*/ |
|
62 |
public class AccessibleObject implements AnnotatedElement { |
|
63 |
||
64 |
/** |
|
65 |
* The Permission object that is used to check whether a client |
|
66 |
* has sufficient privilege to defeat Java language access |
|
67 |
* control checks. |
|
68 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
27783
diff
changeset
|
69 |
private static final java.security.Permission ACCESS_PERMISSION = |
2 | 70 |
new ReflectPermission("suppressAccessChecks"); |
71 |
||
36511 | 72 |
static void checkPermission() { |
73 |
SecurityManager sm = System.getSecurityManager(); |
|
74 |
if (sm != null) sm.checkPermission(ACCESS_PERMISSION); |
|
75 |
} |
|
76 |
||
2 | 77 |
/** |
78 |
* Convenience method to set the {@code accessible} flag for an |
|
79 |
* array of objects with a single security check (for efficiency). |
|
80 |
* |
|
36511 | 81 |
* <p>This method cannot be used to enable access to an object that is a |
82 |
* {@link Member member} of a class in a different module to the caller and |
|
83 |
* where the class is in a package that is not exported to the caller's |
|
84 |
* module. Additionally, this method cannot be used to enable access to |
|
85 |
* non-public members of {@code AccessibleObject} or {@link Module}. |
|
86 |
* |
|
87 |
* <p>If there is a security manager, its |
|
88 |
* {@code checkPermission} method is first called with a |
|
2 | 89 |
* {@code ReflectPermission("suppressAccessChecks")} permission. |
90 |
* |
|
36511 | 91 |
* <p>A {@code SecurityException} is also thrown if any of the elements of |
92 |
* the input {@code array} is a {@link java.lang.reflect.Constructor} |
|
93 |
* object for the class {@code java.lang.Class} and {@code flag} is true. |
|
2 | 94 |
* |
95 |
* @param array the array of AccessibleObjects |
|
96 |
* @param flag the new value for the {@code accessible} flag |
|
97 |
* in each object |
|
36511 | 98 |
* @throws InaccessibleObjectException if access cannot be enabled |
2 | 99 |
* @throws SecurityException if the request is denied. |
100 |
* @see SecurityManager#checkPermission |
|
36511 | 101 |
* @see ReflectPermission |
2 | 102 |
*/ |
36511 | 103 |
@CallerSensitive |
104 |
public static void setAccessible(AccessibleObject[] array, boolean flag) { |
|
105 |
checkPermission(); |
|
106 |
if (flag) { |
|
107 |
Class<?> caller = Reflection.getCallerClass(); |
|
108 |
array = array.clone(); |
|
109 |
for (AccessibleObject ao : array) { |
|
110 |
ao.checkCanSetAccessible(caller); |
|
111 |
} |
|
112 |
} |
|
22581
e868cde95050
8032779: Update code in java.lang to use newer language features
psandoz
parents:
16051
diff
changeset
|
113 |
for (AccessibleObject ao : array) { |
36511 | 114 |
ao.setAccessible0(flag); |
2 | 115 |
} |
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Set the {@code accessible} flag for this object to |
|
120 |
* the indicated boolean value. A value of {@code true} indicates that |
|
121 |
* the reflected object should suppress Java language access |
|
122 |
* checking when it is used. A value of {@code false} indicates |
|
36511 | 123 |
* that the reflected object should enforce Java language access checks |
124 |
* while assuming readability (as noted in the class description). |
|
2 | 125 |
* |
36511 | 126 |
* <p>This method cannot be used to enable access to an object that is a |
127 |
* {@link Member member} of a class in a different module to the caller and |
|
128 |
* where the class is in a package that is not exported to the caller's |
|
129 |
* module. Additionally, this method cannot be used to enable access to |
|
130 |
* non-public members of {@code AccessibleObject} or {@link Module}. |
|
2 | 131 |
* |
36511 | 132 |
* <p>If there is a security manager, its |
133 |
* {@code checkPermission} method is first called with a |
|
134 |
* {@code ReflectPermission("suppressAccessChecks")} permission. |
|
2 | 135 |
* |
136 |
* @param flag the new value for the {@code accessible} flag |
|
36511 | 137 |
* @throws InaccessibleObjectException if access cannot be enabled |
138 |
* @throws SecurityException if the request is denied |
|
2 | 139 |
* @see SecurityManager#checkPermission |
36511 | 140 |
* @see ReflectPermission |
2 | 141 |
*/ |
36511 | 142 |
public void setAccessible(boolean flag) { |
143 |
AccessibleObject.checkPermission(); |
|
144 |
setAccessible0(flag); |
|
145 |
} |
|
146 |
||
147 |
void setAccessible0(boolean flag) { |
|
148 |
this.override = flag; |
|
149 |
} |
|
150 |
||
151 |
/** |
|
152 |
* If the given AccessibleObject is a {@code Constructor}, {@code Method} |
|
153 |
* or {@code Field} then checks that its declaring class is in a package |
|
154 |
* that can be accessed by the given caller of setAccessible. |
|
155 |
*/ |
|
156 |
void checkCanSetAccessible(Class<?> caller) { |
|
157 |
// do nothing, needs to be overridden by Constructor, Method, Field |
|
2 | 158 |
} |
159 |
||
36511 | 160 |
void checkCanSetAccessible(Class<?> caller, Class<?> declaringClass) { |
161 |
Module callerModule = caller.getModule(); |
|
162 |
Module declaringModule = declaringClass.getModule(); |
|
163 |
||
164 |
if (callerModule != declaringModule |
|
165 |
&& callerModule != Object.class.getModule()) { |
|
166 |
||
167 |
// check exports to target module |
|
168 |
String pn = packageName(declaringClass); |
|
169 |
if (!declaringModule.isExported(pn, callerModule)) { |
|
170 |
String msg = "Unable to make member of " |
|
171 |
+ declaringClass + " accessible: " |
|
172 |
+ declaringModule + " does not export " |
|
173 |
+ pn + " to " + callerModule; |
|
174 |
Reflection.throwInaccessibleObjectException(msg); |
|
175 |
} |
|
176 |
||
177 |
} |
|
178 |
||
179 |
if (declaringClass == Module.class |
|
180 |
|| declaringClass == AccessibleObject.class) { |
|
181 |
int modifiers; |
|
182 |
if (this instanceof Executable) { |
|
183 |
modifiers = ((Executable) this).getModifiers(); |
|
184 |
} else { |
|
185 |
modifiers = ((Field) this).getModifiers(); |
|
186 |
} |
|
187 |
if (!Modifier.isPublic(modifiers)) { |
|
188 |
String msg = "Cannot make a non-public member of " |
|
189 |
+ declaringClass + " accessible"; |
|
190 |
Reflection.throwInaccessibleObjectException(msg); |
|
2 | 191 |
} |
192 |
} |
|
36511 | 193 |
} |
194 |
||
195 |
/** |
|
196 |
* Returns the package name of the given class. |
|
197 |
*/ |
|
198 |
private static String packageName(Class<?> c) { |
|
199 |
while (c.isArray()) { |
|
200 |
c = c.getComponentType(); |
|
201 |
} |
|
202 |
String pn = c.getPackageName(); |
|
203 |
return (pn != null) ? pn : ""; |
|
2 | 204 |
} |
205 |
||
206 |
/** |
|
207 |
* Get the value of the {@code accessible} flag for this object. |
|
208 |
* |
|
209 |
* @return the value of the object's {@code accessible} flag |
|
210 |
*/ |
|
211 |
public boolean isAccessible() { |
|
212 |
return override; |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Constructor: only used by the Java Virtual Machine. |
|
217 |
*/ |
|
218 |
protected AccessibleObject() {} |
|
219 |
||
220 |
// Indicates whether language-level access checks are overridden |
|
221 |
// by this object. Initializes to "false". This field is used by |
|
222 |
// Field, Method, and Constructor. |
|
223 |
// |
|
224 |
// NOTE: for security purposes, this field must not be visible |
|
225 |
// outside this package. |
|
226 |
boolean override; |
|
227 |
||
228 |
// Reflection factory used by subclasses for creating field, |
|
229 |
// method, and constructor accessors. Note that this is called |
|
230 |
// very early in the bootstrapping process. |
|
51 | 231 |
static final ReflectionFactory reflectionFactory = |
232 |
AccessController.doPrivileged( |
|
37363
329dba26ffd2
8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents:
36511
diff
changeset
|
233 |
new ReflectionFactory.GetReflectionFactoryAction()); |
2 | 234 |
|
235 |
/** |
|
236 |
* @throws NullPointerException {@inheritDoc} |
|
237 |
* @since 1.5 |
|
238 |
*/ |
|
239 |
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { |
|
240 |
throw new AssertionError("All subclasses should override this method"); |
|
241 |
} |
|
242 |
||
243 |
/** |
|
16051
649a03329639
8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents:
15659
diff
changeset
|
244 |
* {@inheritDoc} |
649a03329639
8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents:
15659
diff
changeset
|
245 |
* @throws NullPointerException {@inheritDoc} |
649a03329639
8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents:
15659
diff
changeset
|
246 |
* @since 1.5 |
649a03329639
8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents:
15659
diff
changeset
|
247 |
*/ |
649a03329639
8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents:
15659
diff
changeset
|
248 |
@Override |
649a03329639
8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents:
15659
diff
changeset
|
249 |
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { |
649a03329639
8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents:
15659
diff
changeset
|
250 |
return AnnotatedElement.super.isAnnotationPresent(annotationClass); |
649a03329639
8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents:
15659
diff
changeset
|
251 |
} |
649a03329639
8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents:
15659
diff
changeset
|
252 |
|
649a03329639
8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents:
15659
diff
changeset
|
253 |
/** |
2 | 254 |
* @throws NullPointerException {@inheritDoc} |
14676
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
255 |
* @since 1.8 |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
256 |
*/ |
15659
e575dab44ff5
8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents:
15534
diff
changeset
|
257 |
@Override |
e575dab44ff5
8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents:
15534
diff
changeset
|
258 |
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) { |
14676
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
259 |
throw new AssertionError("All subclasses should override this method"); |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
260 |
} |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
261 |
|
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
262 |
/** |
2 | 263 |
* @since 1.5 |
264 |
*/ |
|
265 |
public Annotation[] getAnnotations() { |
|
266 |
return getDeclaredAnnotations(); |
|
267 |
} |
|
268 |
||
269 |
/** |
|
14676
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
270 |
* @throws NullPointerException {@inheritDoc} |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
271 |
* @since 1.8 |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
272 |
*/ |
15659
e575dab44ff5
8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents:
15534
diff
changeset
|
273 |
@Override |
14676
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
274 |
public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) { |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
275 |
// Only annotations on classes are inherited, for all other |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
276 |
// objects getDeclaredAnnotation is the same as |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
277 |
// getAnnotation. |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
278 |
return getAnnotation(annotationClass); |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
279 |
} |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
280 |
|
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
281 |
/** |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
282 |
* @throws NullPointerException {@inheritDoc} |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
283 |
* @since 1.8 |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
284 |
*/ |
15659
e575dab44ff5
8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents:
15534
diff
changeset
|
285 |
@Override |
e575dab44ff5
8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents:
15534
diff
changeset
|
286 |
public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) { |
14676
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
287 |
// Only annotations on classes are inherited, for all other |
15659
e575dab44ff5
8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents:
15534
diff
changeset
|
288 |
// objects getDeclaredAnnotationsByType is the same as |
e575dab44ff5
8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents:
15534
diff
changeset
|
289 |
// getAnnotationsByType. |
e575dab44ff5
8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents:
15534
diff
changeset
|
290 |
return getAnnotationsByType(annotationClass); |
14676
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
291 |
} |
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
292 |
|
985410ec95e3
7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents:
14342
diff
changeset
|
293 |
/** |
2 | 294 |
* @since 1.5 |
295 |
*/ |
|
296 |
public Annotation[] getDeclaredAnnotations() { |
|
297 |
throw new AssertionError("All subclasses should override this method"); |
|
298 |
} |
|
9029
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
299 |
|
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
300 |
|
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
301 |
// Shared access checking logic. |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
302 |
|
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
303 |
// For non-public members or members in package-private classes, |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
304 |
// it is necessary to perform somewhat expensive security checks. |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
305 |
// If the security check succeeds for a given class, it will |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
306 |
// always succeed (it is not affected by the granting or revoking |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
307 |
// of permissions); we speed up the check in the common case by |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
308 |
// remembering the last Class for which the check succeeded. |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
309 |
// |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
310 |
// The simple security check for Constructor is to see if |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
311 |
// the caller has already been seen, verified, and cached. |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
312 |
// (See also Class.newInstance(), which uses a similar method.) |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
313 |
// |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
314 |
// A more complicated security check cache is needed for Method and Field |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
315 |
// The cache can be either null (empty cache), a 2-array of {caller,target}, |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
316 |
// or a caller (with target implicitly equal to this.clazz). |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
317 |
// In the 2-array case, the target is always different from the clazz. |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
318 |
volatile Object securityCheckCache; |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
319 |
|
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
320 |
void checkAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers) |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
321 |
throws IllegalAccessException |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
322 |
{ |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
323 |
if (caller == clazz) { // quick check |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
324 |
return; // ACCESS IS OK |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
325 |
} |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
326 |
Object cache = securityCheckCache; // read volatile |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
327 |
Class<?> targetClass = clazz; |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
328 |
if (obj != null |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
329 |
&& Modifier.isProtected(modifiers) |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
330 |
&& ((targetClass = obj.getClass()) != clazz)) { |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
331 |
// Must match a 2-list of { caller, targetClass }. |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
332 |
if (cache instanceof Class[]) { |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
333 |
Class<?>[] cache2 = (Class<?>[]) cache; |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
334 |
if (cache2[1] == targetClass && |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
335 |
cache2[0] == caller) { |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
336 |
return; // ACCESS IS OK |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
337 |
} |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
338 |
// (Test cache[1] first since range check for [1] |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
339 |
// subsumes range check for [0].) |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
340 |
} |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
341 |
} else if (cache == caller) { |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
342 |
// Non-protected case (or obj.class == this.clazz). |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
343 |
return; // ACCESS IS OK |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
344 |
} |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
345 |
|
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
346 |
// If no return, fall through to the slow path. |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
347 |
slowCheckMemberAccess(caller, clazz, obj, modifiers, targetClass); |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
348 |
} |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
349 |
|
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
350 |
// Keep all this slow stuff out of line: |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
351 |
void slowCheckMemberAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers, |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
352 |
Class<?> targetClass) |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
353 |
throws IllegalAccessException |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
354 |
{ |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
355 |
Reflection.ensureMemberAccess(caller, clazz, obj, modifiers); |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
356 |
|
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
357 |
// Success: Update the cache. |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
358 |
Object cache = ((targetClass == clazz) |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
359 |
? caller |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
360 |
: new Class<?>[] { caller, targetClass }); |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
361 |
|
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
362 |
// Note: The two cache elements are not volatile, |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
363 |
// but they are effectively final. The Java memory model |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
364 |
// guarantees that the initializing stores for the cache |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
365 |
// elements will occur before the volatile write. |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
366 |
securityCheckCache = cache; // write volatile |
e92fcf58f684
6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents:
5506
diff
changeset
|
367 |
} |
2 | 368 |
} |