author | alanb |
Wed, 19 Sep 2018 08:49:07 +0100 | |
changeset 51798 | f55a4bc91ef4 |
parent 50091 | 05979f6ba560 |
child 52020 | 4c247dde38ed |
permissions | -rw-r--r-- |
50091
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
1 |
/* |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
4 |
* |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
8 |
* |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
13 |
* accompanied this code). |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
14 |
* |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
18 |
* |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
21 |
* questions. |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
22 |
*/ |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
23 |
|
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
24 |
import java.lang.reflect.Constructor; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
25 |
import java.lang.reflect.Field; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
26 |
import java.lang.reflect.Method; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
27 |
import java.lang.reflect.Modifier; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
28 |
import java.util.concurrent.Callable; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
29 |
|
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
30 |
/** |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
31 |
* Each nested class tests a member of a specific access. |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
32 |
* |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
33 |
* Caller class is cached when setAccessible is not used to suppress the access |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
34 |
* check and only access allowed. If not accessible, caller class is not cached. |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
35 |
*/ |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
36 |
public class AccessTest { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
37 |
public static class PublicConstructor implements Callable<Object> { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
38 |
public Object call() throws Exception { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
39 |
Constructor c = Members.class.getConstructor(); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
40 |
return c.newInstance(); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
41 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
42 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
43 |
|
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
44 |
public static class PublicMethod extends Members implements Callable<Void> { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
45 |
public Void call() throws Exception { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
46 |
Method m = Members.class.getDeclaredMethod("publicMethod"); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
47 |
m.invoke(new PublicMethod()); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
48 |
return null; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
49 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
50 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
51 |
|
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
52 |
public static class ProtectedMethod extends Members implements Callable<Void> { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
53 |
public Void call() throws Exception { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
54 |
Method m = Members.class.getDeclaredMethod("protectedMethod"); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
55 |
m.invoke(new ProtectedMethod()); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
56 |
return null; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
57 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
58 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
59 |
|
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
60 |
/* |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
61 |
* private field is not accessible. So caller class is not cached. |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
62 |
*/ |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
63 |
public static class PrivateMethod extends Members implements Callable<Void> { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
64 |
public Void call() throws Exception { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
65 |
Method m = Members.class.getDeclaredMethod("privateMethod"); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
66 |
try { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
67 |
m.invoke(new ProtectedMethod()); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
68 |
} catch (IllegalAccessException e) { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
69 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
70 |
return null; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
71 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
72 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
73 |
|
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
74 |
public static class PublicField extends Members implements Callable<Void> { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
75 |
public Void call() throws Exception { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
76 |
Field f = Members.class.getDeclaredField("publicField"); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
77 |
f.get(new PublicField()); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
78 |
return null; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
79 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
80 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
81 |
|
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
82 |
public static class ProtectedField extends Members implements Callable<Void> { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
83 |
public Void call() throws Exception { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
84 |
Field f = Members.class.getDeclaredField("protectedField"); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
85 |
f.get(new ProtectedField()); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
86 |
return null; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
87 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
88 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
89 |
|
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
90 |
/* |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
91 |
* private field is not accessible. So caller class is not cached. |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
92 |
*/ |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
93 |
public static class PrivateField implements Callable<Void> { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
94 |
public Void call() throws Exception { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
95 |
Field f = Members.class.getDeclaredField("privateField"); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
96 |
try { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
97 |
f.get(new Members()); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
98 |
} catch (IllegalAccessException e) { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
99 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
100 |
return null; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
101 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
102 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
103 |
|
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
104 |
/* |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
105 |
* Validate final field |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
106 |
*/ |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
107 |
public static class FinalField implements Callable<Void> { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
108 |
final Field f; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
109 |
final boolean isStatic; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
110 |
public FinalField(String name) throws Exception { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
111 |
this.f = Members.class.getDeclaredField(name); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
112 |
this.isStatic = Modifier.isStatic(f.getModifiers()); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
113 |
if (!Modifier.isFinal(f.getModifiers())) { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
114 |
throw new RuntimeException("not a final field"); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
115 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
116 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
117 |
public Void call() throws Exception { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
118 |
Members obj = isStatic ? null : new Members(); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
119 |
try { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
120 |
f.set(obj, 20); |
51798
f55a4bc91ef4
8210496: Improve filtering for classes with security sensitive fields
alanb
parents:
50091
diff
changeset
|
121 |
throw new RuntimeException("IllegalAccessException expected"); |
50091
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
122 |
} catch (IllegalAccessException e) { |
51798
f55a4bc91ef4
8210496: Improve filtering for classes with security sensitive fields
alanb
parents:
50091
diff
changeset
|
123 |
// expected |
50091
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
124 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
125 |
return null; |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
126 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
127 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
128 |
|
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
129 |
public static class PublicFinalField extends FinalField { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
130 |
public PublicFinalField() throws Exception { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
131 |
super("publicFinalField"); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
132 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
133 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
134 |
|
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
135 |
public static class PrivateFinalField extends FinalField { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
136 |
public PrivateFinalField() throws Exception { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
137 |
super("privateFinalField"); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
138 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
139 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
140 |
|
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
141 |
public static class PublicStaticFinalField extends FinalField { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
142 |
public PublicStaticFinalField() throws Exception { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
143 |
super("publicStaticFinalField"); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
144 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
145 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
146 |
|
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
147 |
public static class PrivateStaticFinalField extends FinalField { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
148 |
public PrivateStaticFinalField() throws Exception { |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
149 |
super("privateStaticFinalField"); |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
150 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
151 |
} |
05979f6ba560
8202113: Reflection API is causing caller classes to leak
mchung
parents:
diff
changeset
|
152 |
} |