author | alanb |
Fri, 02 Nov 2012 15:50:11 +0000 | |
changeset 14342 | 8435a30053c1 |
parent 10342 | ca0984bc9d32 |
child 16906 | 44dfee24cb71 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
10342
diff
changeset
|
2 |
* Copyright (c) 2001, 2011, 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 sun.reflect; |
|
27 |
||
28 |
import java.lang.reflect.*; |
|
29 |
import java.util.HashMap; |
|
30 |
import java.util.Map; |
|
31 |
||
32 |
/** Common utility routines used by both java.lang and |
|
33 |
java.lang.reflect */ |
|
34 |
||
35 |
public class Reflection { |
|
36 |
||
37 |
/** Used to filter out fields and methods from certain classes from public |
|
38 |
view, where they are sensitive or they may contain VM-internal objects. |
|
39 |
These Maps are updated very rarely. Rather than synchronize on |
|
40 |
each access, we use copy-on-write */ |
|
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
41 |
private static volatile Map<Class<?>,String[]> fieldFilterMap; |
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
42 |
private static volatile Map<Class<?>,String[]> methodFilterMap; |
2 | 43 |
|
44 |
static { |
|
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
45 |
Map<Class<?>,String[]> map = new HashMap<Class<?>,String[]>(); |
2 | 46 |
map.put(Reflection.class, |
47 |
new String[] {"fieldFilterMap", "methodFilterMap"}); |
|
48 |
map.put(System.class, new String[] {"security"}); |
|
49 |
fieldFilterMap = map; |
|
50 |
||
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
51 |
methodFilterMap = new HashMap<>(); |
2 | 52 |
} |
53 |
||
54 |
/** Returns the class of the method <code>realFramesToSkip</code> |
|
55 |
frames up the stack (zero-based), ignoring frames associated |
|
56 |
with java.lang.reflect.Method.invoke() and its implementation. |
|
57 |
The first frame is that associated with this method, so |
|
58 |
<code>getCallerClass(0)</code> returns the Class object for |
|
59 |
sun.reflect.Reflection. Frames associated with |
|
60 |
java.lang.reflect.Method.invoke() and its implementation are |
|
61 |
completely ignored and do not count toward the number of "real" |
|
62 |
frames skipped. */ |
|
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
63 |
public static native Class<?> getCallerClass(int realFramesToSkip); |
2 | 64 |
|
65 |
/** Retrieves the access flags written to the class file. For |
|
66 |
inner classes these flags may differ from those returned by |
|
67 |
Class.getModifiers(), which searches the InnerClasses |
|
68 |
attribute to find the source-level access flags. This is used |
|
69 |
instead of Class.getModifiers() for run-time access checks due |
|
70 |
to compatibility reasons; see 4471811. Only the values of the |
|
71 |
low 13 bits (i.e., a mask of 0x1FFF) are guaranteed to be |
|
72 |
valid. */ |
|
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
73 |
private static native int getClassAccessFlags(Class<?> c); |
2 | 74 |
|
75 |
/** A quick "fast-path" check to try to avoid getCallerClass() |
|
76 |
calls. */ |
|
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
77 |
public static boolean quickCheckMemberAccess(Class<?> memberClass, |
2 | 78 |
int modifiers) |
79 |
{ |
|
80 |
return Modifier.isPublic(getClassAccessFlags(memberClass) & modifiers); |
|
81 |
} |
|
82 |
||
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
83 |
public static void ensureMemberAccess(Class<?> currentClass, |
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
84 |
Class<?> memberClass, |
2 | 85 |
Object target, |
86 |
int modifiers) |
|
87 |
throws IllegalAccessException |
|
88 |
{ |
|
89 |
if (currentClass == null || memberClass == null) { |
|
90 |
throw new InternalError(); |
|
91 |
} |
|
92 |
||
93 |
if (!verifyMemberAccess(currentClass, memberClass, target, modifiers)) { |
|
94 |
throw new IllegalAccessException("Class " + currentClass.getName() + |
|
95 |
" can not access a member of class " + |
|
96 |
memberClass.getName() + |
|
97 |
" with modifiers \"" + |
|
98 |
Modifier.toString(modifiers) + |
|
99 |
"\""); |
|
100 |
} |
|
101 |
} |
|
102 |
||
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
103 |
public static boolean verifyMemberAccess(Class<?> currentClass, |
2 | 104 |
// Declaring class of field |
105 |
// or method |
|
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
106 |
Class<?> memberClass, |
2 | 107 |
// May be NULL in case of statics |
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
108 |
Object target, |
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
109 |
int modifiers) |
2 | 110 |
{ |
111 |
// Verify that currentClass can access a field, method, or |
|
112 |
// constructor of memberClass, where that member's access bits are |
|
113 |
// "modifiers". |
|
114 |
||
115 |
boolean gotIsSameClassPackage = false; |
|
116 |
boolean isSameClassPackage = false; |
|
117 |
||
118 |
if (currentClass == memberClass) { |
|
119 |
// Always succeeds |
|
120 |
return true; |
|
121 |
} |
|
122 |
||
123 |
if (!Modifier.isPublic(getClassAccessFlags(memberClass))) { |
|
124 |
isSameClassPackage = isSameClassPackage(currentClass, memberClass); |
|
125 |
gotIsSameClassPackage = true; |
|
126 |
if (!isSameClassPackage) { |
|
127 |
return false; |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
// At this point we know that currentClass can access memberClass. |
|
132 |
||
133 |
if (Modifier.isPublic(modifiers)) { |
|
134 |
return true; |
|
135 |
} |
|
136 |
||
137 |
boolean successSoFar = false; |
|
138 |
||
139 |
if (Modifier.isProtected(modifiers)) { |
|
140 |
// See if currentClass is a subclass of memberClass |
|
141 |
if (isSubclassOf(currentClass, memberClass)) { |
|
142 |
successSoFar = true; |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
if (!successSoFar && !Modifier.isPrivate(modifiers)) { |
|
147 |
if (!gotIsSameClassPackage) { |
|
148 |
isSameClassPackage = isSameClassPackage(currentClass, |
|
149 |
memberClass); |
|
150 |
gotIsSameClassPackage = true; |
|
151 |
} |
|
152 |
||
153 |
if (isSameClassPackage) { |
|
154 |
successSoFar = true; |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
if (!successSoFar) { |
|
159 |
return false; |
|
160 |
} |
|
161 |
||
162 |
if (Modifier.isProtected(modifiers)) { |
|
163 |
// Additional test for protected members: JLS 6.6.2 |
|
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
164 |
Class<?> targetClass = (target == null ? memberClass : target.getClass()); |
2 | 165 |
if (targetClass != currentClass) { |
166 |
if (!gotIsSameClassPackage) { |
|
167 |
isSameClassPackage = isSameClassPackage(currentClass, memberClass); |
|
168 |
gotIsSameClassPackage = true; |
|
169 |
} |
|
170 |
if (!isSameClassPackage) { |
|
171 |
if (!isSubclassOf(targetClass, currentClass)) { |
|
172 |
return false; |
|
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
return true; |
|
179 |
} |
|
180 |
||
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
181 |
private static boolean isSameClassPackage(Class<?> c1, Class<?> c2) { |
2 | 182 |
return isSameClassPackage(c1.getClassLoader(), c1.getName(), |
183 |
c2.getClassLoader(), c2.getName()); |
|
184 |
} |
|
185 |
||
186 |
/** Returns true if two classes are in the same package; classloader |
|
187 |
and classname information is enough to determine a class's package */ |
|
188 |
private static boolean isSameClassPackage(ClassLoader loader1, String name1, |
|
189 |
ClassLoader loader2, String name2) |
|
190 |
{ |
|
191 |
if (loader1 != loader2) { |
|
192 |
return false; |
|
193 |
} else { |
|
194 |
int lastDot1 = name1.lastIndexOf('.'); |
|
195 |
int lastDot2 = name2.lastIndexOf('.'); |
|
196 |
if ((lastDot1 == -1) || (lastDot2 == -1)) { |
|
197 |
// One of the two doesn't have a package. Only return true |
|
198 |
// if the other one also doesn't have a package. |
|
199 |
return (lastDot1 == lastDot2); |
|
200 |
} else { |
|
201 |
int idx1 = 0; |
|
202 |
int idx2 = 0; |
|
203 |
||
204 |
// Skip over '['s |
|
205 |
if (name1.charAt(idx1) == '[') { |
|
206 |
do { |
|
207 |
idx1++; |
|
208 |
} while (name1.charAt(idx1) == '['); |
|
209 |
if (name1.charAt(idx1) != 'L') { |
|
210 |
// Something is terribly wrong. Shouldn't be here. |
|
211 |
throw new InternalError("Illegal class name " + name1); |
|
212 |
} |
|
213 |
} |
|
214 |
if (name2.charAt(idx2) == '[') { |
|
215 |
do { |
|
216 |
idx2++; |
|
217 |
} while (name2.charAt(idx2) == '['); |
|
218 |
if (name2.charAt(idx2) != 'L') { |
|
219 |
// Something is terribly wrong. Shouldn't be here. |
|
220 |
throw new InternalError("Illegal class name " + name2); |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
// Check that package part is identical |
|
225 |
int length1 = lastDot1 - idx1; |
|
226 |
int length2 = lastDot2 - idx2; |
|
227 |
||
228 |
if (length1 != length2) { |
|
229 |
return false; |
|
230 |
} |
|
231 |
return name1.regionMatches(false, idx1, name2, idx2, length1); |
|
232 |
} |
|
233 |
} |
|
234 |
} |
|
235 |
||
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
236 |
static boolean isSubclassOf(Class<?> queryClass, |
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
237 |
Class<?> ofClass) |
2 | 238 |
{ |
239 |
while (queryClass != null) { |
|
240 |
if (queryClass == ofClass) { |
|
241 |
return true; |
|
242 |
} |
|
243 |
queryClass = queryClass.getSuperclass(); |
|
244 |
} |
|
245 |
return false; |
|
246 |
} |
|
247 |
||
248 |
// fieldNames must contain only interned Strings |
|
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
249 |
public static synchronized void registerFieldsToFilter(Class<?> containingClass, |
2 | 250 |
String ... fieldNames) { |
251 |
fieldFilterMap = |
|
252 |
registerFilter(fieldFilterMap, containingClass, fieldNames); |
|
253 |
} |
|
254 |
||
255 |
// methodNames must contain only interned Strings |
|
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
256 |
public static synchronized void registerMethodsToFilter(Class<?> containingClass, |
2 | 257 |
String ... methodNames) { |
258 |
methodFilterMap = |
|
259 |
registerFilter(methodFilterMap, containingClass, methodNames); |
|
260 |
} |
|
261 |
||
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
262 |
private static Map<Class<?>,String[]> registerFilter(Map<Class<?>,String[]> map, |
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
263 |
Class<?> containingClass, String ... names) { |
2 | 264 |
if (map.get(containingClass) != null) { |
265 |
throw new IllegalArgumentException |
|
266 |
("Filter already registered: " + containingClass); |
|
267 |
} |
|
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
268 |
map = new HashMap<Class<?>,String[]>(map); |
2 | 269 |
map.put(containingClass, names); |
270 |
return map; |
|
271 |
} |
|
272 |
||
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
273 |
public static Field[] filterFields(Class<?> containingClass, |
2 | 274 |
Field[] fields) { |
275 |
if (fieldFilterMap == null) { |
|
276 |
// Bootstrapping |
|
277 |
return fields; |
|
278 |
} |
|
279 |
return (Field[])filter(fields, fieldFilterMap.get(containingClass)); |
|
280 |
} |
|
281 |
||
10342
ca0984bc9d32
7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents:
5506
diff
changeset
|
282 |
public static Method[] filterMethods(Class<?> containingClass, Method[] methods) { |
2 | 283 |
if (methodFilterMap == null) { |
284 |
// Bootstrapping |
|
285 |
return methods; |
|
286 |
} |
|
287 |
return (Method[])filter(methods, methodFilterMap.get(containingClass)); |
|
288 |
} |
|
289 |
||
290 |
private static Member[] filter(Member[] members, String[] filteredNames) { |
|
291 |
if ((filteredNames == null) || (members.length == 0)) { |
|
292 |
return members; |
|
293 |
} |
|
294 |
int numNewMembers = 0; |
|
295 |
for (Member member : members) { |
|
296 |
boolean shouldSkip = false; |
|
297 |
for (String filteredName : filteredNames) { |
|
298 |
if (member.getName() == filteredName) { |
|
299 |
shouldSkip = true; |
|
300 |
break; |
|
301 |
} |
|
302 |
} |
|
303 |
if (!shouldSkip) { |
|
304 |
++numNewMembers; |
|
305 |
} |
|
306 |
} |
|
307 |
Member[] newMembers = |
|
308 |
(Member[])Array.newInstance(members[0].getClass(), numNewMembers); |
|
309 |
int destIdx = 0; |
|
310 |
for (Member member : members) { |
|
311 |
boolean shouldSkip = false; |
|
312 |
for (String filteredName : filteredNames) { |
|
313 |
if (member.getName() == filteredName) { |
|
314 |
shouldSkip = true; |
|
315 |
break; |
|
316 |
} |
|
317 |
} |
|
318 |
if (!shouldSkip) { |
|
319 |
newMembers[destIdx++] = member; |
|
320 |
} |
|
321 |
} |
|
322 |
return newMembers; |
|
323 |
} |
|
324 |
} |