1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
$FreeBSD$
--- ../../deploy/src/plugin/src/share/classes/sun/plugin/javascript/ReflectUtil.java 1 Jan 1970 00:00:00 -0000
+++ ../../deploy/src/plugin/src/share/classes/sun/plugin/javascript/ReflectUtil.java 3 Dec 2004 03:56:58 -0000 1.1
@@ -0,0 +1,307 @@
+/*
+ * @(#)ReflectUtil.java 1.1 04/06/20
+ *
+ * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
+ * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ */
+package sun.plugin.javascript;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Iterator;
+import sun.plugin.javascript.JSClassLoader;
+
+public class ReflectUtil {
+ /*
+ * Discover the public methods on public classes
+ * and interfaces accessible to the calling
+ * JavaScript code.
+ */
+ public static Method[] getJScriptMethods(Class cls) {
+ List m = new ArrayList(); /* the valid methods we find */
+
+ /*
+ * Temporary map of method signatures when we decide
+ * that a simple call to target.getMethods() returns
+ * inaccessible methods and we must search for alternative
+ * supermethods that might be accessible. We can toss
+ * this when we're done searching.
+ */
+ Map sigs = new HashMap();
+
+ while (cls != null) {
+ boolean done = getPublicMethods(cls, m, sigs);
+ if (done) {
+ break;
+ }
+ getJScriptInterfaceMethods(cls, m, sigs);
+ cls = cls.getSuperclass();
+ }
+ return (Method[]) m.toArray(new Method[m.size()]);
+ }
+
+ /*
+ * Process the immediate interfaces of this class or interface.
+ */
+ private static void getJScriptInterfaceMethods(Class cls, List m, Map sigs) {
+ Class[] intfs = cls.getInterfaces();
+ for (int i=0; i < intfs.length; i++) {
+ Class intf = intfs[i];
+ boolean done = getPublicMethods(intf, m, sigs);
+ if (!done) {
+ getJScriptInterfaceMethods(intf, m, sigs);
+ }
+ }
+ }
+
+ /*
+ *
+ * Process the methods in this class or interface
+ */
+ private static boolean getPublicMethods(Class cls, List m, Map sigs) {
+ Method[] methods = null;
+ try {
+
+ /*
+ * This class or interface is non-public so we
+ * can't use any of it's methods. Go back and
+ * try again with a superclass or superinterface.
+ */
+ if (!Modifier.isPublic(cls.getModifiers())) {
+ return false;
+ }
+
+ if (!JSClassLoader.isPackageAccessible(cls)) {
+ return false;
+ }
+
+ methods = cls.getMethods();
+ } catch (SecurityException se) {
+ return false;
+ }
+
+ /*
+ * Check for inherited methods with non-public
+ * declaring classes. They might override and hide
+ * methods from their superclasses or
+ * superinterfaces.
+ */
+ boolean done = true;
+ for (int i=0; i < methods.length; i++) {
+ Class dc = methods[i].getDeclaringClass();
+ if (!Modifier.isPublic(dc.getModifiers())) {
+ done = false;
+ break;
+ }
+ }
+
+ /*
+ * Belatedly initialize the signature map if
+ * this is not the first time here.
+ */
+ if (sigs.isEmpty() && !m.isEmpty()) {
+ initSignatureMap(m, sigs);
+ }
+
+ if (done) {
+ /*
+ * We're done. Spray all the methods into
+ * the list and then we're out of here.
+ */
+ for (int i=0; i < methods.length; i++) {
+ addMethod(m, sigs, methods[i]);
+ }
+ } else {
+ /*
+ * Simulate cls.getDeclaredMethods() by
+ * stripping away inherited methods.
+ */
+ for (int i=0; i < methods.length; i++) {
+ Class dc = methods[i].getDeclaringClass();
+ if (cls.equals(dc)) {
+ addMethod(m, sigs, methods[i]);
+ }
+ }
+ }
+ return done;
+ }
+
+ private static void initSignatureMap(List m, Map sigs) {
+ Iterator i = m.iterator();
+ while (i.hasNext()) {
+ Method entry = (Method) i.next();
+ sigs.put(getSignature(entry), entry);
+ }
+ }
+
+ private static void addMethod(List m, Map sigs, Method method) {
+ /*
+ * Avoid work. We ignore the signature matching
+ * until the map is initialized in initSignatureMap.
+ * This has the effect of avoiding the signature
+ * work for the first call of getPublicMethods().
+ */
+ if (sigs.isEmpty()) {
+ m.add(method);
+ return;
+ }
+
+ /*
+ * Avoid adding duplicate accessible methods on
+ * the list.
+ */
+ String signature = getSignature(method);
+ if (!sigs.containsKey(signature)) {
+ m.add(method);
+ sigs.put(signature, method);
+ }
+ }
+
+ /*
+ * Return a canonical method signature for the method.
+ * We care only about the simple method name and the
+ * the number, type and order of the parameters.
+ * Exception declarations are not part of a method
+ * signature nor is the return type.
+ */
+ private static String getSignature(Method method) {
+ StringBuffer sb = new StringBuffer();
+
+ sb.append(method.getName());
+ Class[] params = method.getParameterTypes();
+ sb.append('(');
+ if (params.length > 0) {
+ sb.append(params[0].getName());
+ }
+ for (int i=1; i < params.length; i++) {
+ sb.append(',');
+ sb.append(params[i].getName());
+ }
+ sb.append(')');
+
+ return sb.toString();
+ }
+
+ /*
+ * Discover the public fields on public classes
+ * and interfaces accessible to the calling
+ * JavaScript code.
+ */
+ public static Field[] getJScriptFields(Class cls) {
+ List m = new ArrayList(); /* the valid fields we find */
+
+ /*
+ * Temporary map of field name when we decide
+ * that a simple call to target.getFields() returns
+ * inaccessible fields and we must search for alternative
+ * supermethods that might be accessible. We can toss
+ * this when we're done searching.
+ */
+ Map names = new HashMap();
+
+ while (cls != null) {
+ boolean done = getPublicFields(cls, m, names);
+ if (done) {
+ break;
+ }
+ getJScriptInterfaceFields(cls, m, names);
+ cls = cls.getSuperclass();
+ }
+ return (Field[]) m.toArray(new Field[m.size()]);
+ }
+
+ /*
+ * Process the immediate interfaces of this class or interface.
+ */
+ private static void getJScriptInterfaceFields(Class cls, List m, Map names) {
+ Class[] intfs = cls.getInterfaces();
+ for (int i=0; i < intfs.length; i++) {
+ Class intf = intfs[i];
+ boolean done = getPublicFields(intf, m, names);
+ if (!done) {
+ getJScriptInterfaceFields(intf, m, names);
+ }
+ }
+ }
+
+ /*
+ *
+ * Process the fields in this class or interface
+ */
+ private static boolean getPublicFields(Class cls, List m, Map names) {
+ Field[] fields = null;
+ try {
+
+ /*
+ * This class or interface is non-public so we
+ * can't use any of it's fields. Go back and
+ * try again with a superclass or superinterface.
+ */
+ if (!Modifier.isPublic(cls.getModifiers())) {
+ return false;
+ }
+
+ if (!JSClassLoader.isPackageAccessible(cls)) {
+ return false;
+ }
+ fields = cls.getFields();
+ } catch (SecurityException se) {
+ return false;
+ }
+
+ /*
+ * Check for inherited fields with non-public
+ * declaring classes. They might hide
+ * fields from public classes or interfaces.
+ */
+ boolean done = true;
+ for (int i=0; i < fields.length; i++) {
+ Class dc = fields[i].getDeclaringClass();
+ if (!Modifier.isPublic(dc.getModifiers())) {
+ done = false;
+ break;
+ }
+ }
+ if (done) {
+ /*
+ * We're done. Spray all the fields into
+ * the list and then we're out of here.
+ */
+ for (int i=0; i < fields.length; i++) {
+ addField(m, names, fields[i]);
+ }
+ } else {
+ /*
+ * Simulate cls.getDeclaredFields() by
+ * stripping away inherited fields.
+ */
+ for (int i=0; i < fields.length; i++) {
+ Class dc = fields[i].getDeclaringClass();
+ if (cls.equals(dc)) {
+ addField(m, names, fields[i]);
+ }
+ }
+ }
+ return done;
+ }
+
+ private static void addField(List m, Map names, Field field) {
+ /*
+ * Avoid adding duplicate accessible fields on
+ * the list.
+ */
+ String name = field.getName();
+ if (!names.containsKey(name)) {
+ m.add(field);
+ names.put(name, field);
+ }
+ }
+}
+
+
|