jaroslav@56
|
1 |
/*
|
jaroslav@56
|
2 |
* Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@56
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@56
|
4 |
*
|
jaroslav@56
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@56
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@56
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@56
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@56
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@56
|
10 |
*
|
jaroslav@56
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@56
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@56
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@56
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@56
|
15 |
* accompanied this code).
|
jaroslav@56
|
16 |
*
|
jaroslav@56
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@56
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@56
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@56
|
20 |
*
|
jaroslav@56
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@56
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@56
|
23 |
* questions.
|
jaroslav@56
|
24 |
*/
|
jaroslav@56
|
25 |
|
jaroslav@56
|
26 |
package java.lang;
|
jaroslav@56
|
27 |
|
jaroslav@424
|
28 |
import java.io.ByteArrayInputStream;
|
jaroslav@122
|
29 |
import java.io.InputStream;
|
jaroslav@56
|
30 |
import java.lang.annotation.Annotation;
|
jtulach@1483
|
31 |
import java.lang.reflect.Array;
|
jaroslav@1321
|
32 |
import java.lang.reflect.Constructor;
|
jaroslav@261
|
33 |
import java.lang.reflect.Field;
|
jaroslav@261
|
34 |
import java.lang.reflect.Method;
|
jaroslav@260
|
35 |
import java.lang.reflect.TypeVariable;
|
jaroslav@576
|
36 |
import java.net.URL;
|
jaroslav@1586
|
37 |
import org.apidesign.bck2brwsr.core.Exported;
|
jaroslav@225
|
38 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@1515
|
39 |
import org.apidesign.bck2brwsr.core.JavaScriptOnly;
|
jtulach@1483
|
40 |
import org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl;
|
jaroslav@555
|
41 |
import org.apidesign.bck2brwsr.emul.reflect.MethodImpl;
|
jaroslav@56
|
42 |
|
jaroslav@56
|
43 |
/**
|
jaroslav@56
|
44 |
* Instances of the class {@code Class} represent classes and
|
jaroslav@56
|
45 |
* interfaces in a running Java application. An enum is a kind of
|
jaroslav@56
|
46 |
* class and an annotation is a kind of interface. Every array also
|
jaroslav@56
|
47 |
* belongs to a class that is reflected as a {@code Class} object
|
jaroslav@56
|
48 |
* that is shared by all arrays with the same element type and number
|
jaroslav@56
|
49 |
* of dimensions. The primitive Java types ({@code boolean},
|
jaroslav@56
|
50 |
* {@code byte}, {@code char}, {@code short},
|
jaroslav@56
|
51 |
* {@code int}, {@code long}, {@code float}, and
|
jaroslav@56
|
52 |
* {@code double}), and the keyword {@code void} are also
|
jaroslav@56
|
53 |
* represented as {@code Class} objects.
|
jaroslav@56
|
54 |
*
|
jaroslav@56
|
55 |
* <p> {@code Class} has no public constructor. Instead {@code Class}
|
jaroslav@56
|
56 |
* objects are constructed automatically by the Java Virtual Machine as classes
|
jaroslav@56
|
57 |
* are loaded and by calls to the {@code defineClass} method in the class
|
jaroslav@56
|
58 |
* loader.
|
jaroslav@56
|
59 |
*
|
jaroslav@56
|
60 |
* <p> The following example uses a {@code Class} object to print the
|
jaroslav@56
|
61 |
* class name of an object:
|
jaroslav@56
|
62 |
*
|
jaroslav@56
|
63 |
* <p> <blockquote><pre>
|
jaroslav@56
|
64 |
* void printClassName(Object obj) {
|
jaroslav@56
|
65 |
* System.out.println("The class of " + obj +
|
jaroslav@56
|
66 |
* " is " + obj.getClass().getName());
|
jaroslav@56
|
67 |
* }
|
jaroslav@56
|
68 |
* </pre></blockquote>
|
jaroslav@56
|
69 |
*
|
jaroslav@56
|
70 |
* <p> It is also possible to get the {@code Class} object for a named
|
jaroslav@56
|
71 |
* type (or for void) using a class literal. See Section 15.8.2 of
|
jaroslav@56
|
72 |
* <cite>The Java™ Language Specification</cite>.
|
jaroslav@56
|
73 |
* For example:
|
jaroslav@56
|
74 |
*
|
jaroslav@56
|
75 |
* <p> <blockquote>
|
jaroslav@56
|
76 |
* {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
|
jaroslav@56
|
77 |
* </blockquote>
|
jaroslav@56
|
78 |
*
|
jaroslav@56
|
79 |
* @param <T> the type of the class modeled by this {@code Class}
|
jaroslav@56
|
80 |
* object. For example, the type of {@code String.class} is {@code
|
jaroslav@56
|
81 |
* Class<String>}. Use {@code Class<?>} if the class being modeled is
|
jaroslav@56
|
82 |
* unknown.
|
jaroslav@56
|
83 |
*
|
jaroslav@56
|
84 |
* @author unascribed
|
jaroslav@56
|
85 |
* @see java.lang.ClassLoader#defineClass(byte[], int, int)
|
jaroslav@56
|
86 |
* @since JDK1.0
|
jaroslav@56
|
87 |
*/
|
jaroslav@56
|
88 |
public final
|
jaroslav@260
|
89 |
class Class<T> implements java.io.Serializable,
|
jaroslav@260
|
90 |
java.lang.reflect.GenericDeclaration,
|
jaroslav@260
|
91 |
java.lang.reflect.Type,
|
jaroslav@260
|
92 |
java.lang.reflect.AnnotatedElement {
|
jaroslav@56
|
93 |
private static final int ANNOTATION= 0x00002000;
|
jaroslav@56
|
94 |
private static final int ENUM = 0x00004000;
|
jaroslav@56
|
95 |
private static final int SYNTHETIC = 0x00001000;
|
jaroslav@56
|
96 |
|
jaroslav@1653
|
97 |
/* Backing store of user-defined values pertaining to this class.
|
jaroslav@1653
|
98 |
* Maintained by the ClassValue class.
|
jaroslav@1653
|
99 |
*/
|
jaroslav@1653
|
100 |
transient Object classValueMap;
|
jaroslav@1653
|
101 |
|
jaroslav@56
|
102 |
/*
|
jaroslav@56
|
103 |
* Constructor. Only the Java Virtual Machine creates Class
|
jaroslav@56
|
104 |
* objects.
|
jaroslav@56
|
105 |
*/
|
jaroslav@56
|
106 |
private Class() {}
|
jaroslav@56
|
107 |
|
jaroslav@56
|
108 |
|
jaroslav@56
|
109 |
/**
|
jaroslav@56
|
110 |
* Converts the object to a string. The string representation is the
|
jaroslav@56
|
111 |
* string "class" or "interface", followed by a space, and then by the
|
jaroslav@56
|
112 |
* fully qualified name of the class in the format returned by
|
jaroslav@56
|
113 |
* {@code getName}. If this {@code Class} object represents a
|
jaroslav@56
|
114 |
* primitive type, this method returns the name of the primitive type. If
|
jaroslav@56
|
115 |
* this {@code Class} object represents void this method returns
|
jaroslav@56
|
116 |
* "void".
|
jaroslav@56
|
117 |
*
|
jaroslav@56
|
118 |
* @return a string representation of this class object.
|
jaroslav@56
|
119 |
*/
|
jaroslav@56
|
120 |
public String toString() {
|
jaroslav@56
|
121 |
return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
|
jaroslav@56
|
122 |
+ getName();
|
jaroslav@56
|
123 |
}
|
jaroslav@56
|
124 |
|
jaroslav@56
|
125 |
|
jaroslav@56
|
126 |
/**
|
jaroslav@56
|
127 |
* Returns the {@code Class} object associated with the class or
|
jaroslav@56
|
128 |
* interface with the given string name. Invoking this method is
|
jaroslav@56
|
129 |
* equivalent to:
|
jaroslav@56
|
130 |
*
|
jaroslav@56
|
131 |
* <blockquote>
|
jaroslav@56
|
132 |
* {@code Class.forName(className, true, currentLoader)}
|
jaroslav@56
|
133 |
* </blockquote>
|
jaroslav@56
|
134 |
*
|
jaroslav@56
|
135 |
* where {@code currentLoader} denotes the defining class loader of
|
jaroslav@56
|
136 |
* the current class.
|
jaroslav@56
|
137 |
*
|
jaroslav@56
|
138 |
* <p> For example, the following code fragment returns the
|
jaroslav@56
|
139 |
* runtime {@code Class} descriptor for the class named
|
jaroslav@56
|
140 |
* {@code java.lang.Thread}:
|
jaroslav@56
|
141 |
*
|
jaroslav@56
|
142 |
* <blockquote>
|
jaroslav@56
|
143 |
* {@code Class t = Class.forName("java.lang.Thread")}
|
jaroslav@56
|
144 |
* </blockquote>
|
jaroslav@56
|
145 |
* <p>
|
jaroslav@56
|
146 |
* A call to {@code forName("X")} causes the class named
|
jaroslav@56
|
147 |
* {@code X} to be initialized.
|
jaroslav@56
|
148 |
*
|
jaroslav@56
|
149 |
* @param className the fully qualified name of the desired class.
|
jaroslav@56
|
150 |
* @return the {@code Class} object for the class with the
|
jaroslav@56
|
151 |
* specified name.
|
jaroslav@56
|
152 |
* @exception LinkageError if the linkage fails
|
jaroslav@56
|
153 |
* @exception ExceptionInInitializerError if the initialization provoked
|
jaroslav@56
|
154 |
* by this method fails
|
jaroslav@56
|
155 |
* @exception ClassNotFoundException if the class cannot be located
|
jaroslav@56
|
156 |
*/
|
jaroslav@56
|
157 |
public static Class<?> forName(String className)
|
jaroslav@450
|
158 |
throws ClassNotFoundException {
|
jaroslav@450
|
159 |
if (className.startsWith("[")) {
|
jaroslav@1532
|
160 |
Class<?> arrType = defineArray(className, null);
|
jaroslav@452
|
161 |
Class<?> c = arrType;
|
jaroslav@452
|
162 |
while (c != null && c.isArray()) {
|
jaroslav@452
|
163 |
c = c.getComponentType0(); // verify component type is sane
|
jaroslav@452
|
164 |
}
|
jaroslav@452
|
165 |
return arrType;
|
jaroslav@450
|
166 |
}
|
jaroslav@1252
|
167 |
try {
|
jaroslav@1887
|
168 |
final String inJsName = className.replace('.', '_');
|
jaroslav@1887
|
169 |
Class<?> c = loadCls(className, inJsName);
|
jaroslav@1887
|
170 |
if (c == null) {
|
jaroslav@1887
|
171 |
c = loadCls(className, inJsName);
|
jaroslav@1887
|
172 |
}
|
jaroslav@1252
|
173 |
if (c == null) {
|
jaroslav@1252
|
174 |
throw new ClassNotFoundException(className);
|
jaroslav@1252
|
175 |
}
|
jaroslav@1252
|
176 |
return c;
|
jaroslav@1252
|
177 |
} catch (Throwable ex) {
|
jaroslav@1252
|
178 |
throw new ClassNotFoundException(className, ex);
|
jaroslav@321
|
179 |
}
|
jaroslav@56
|
180 |
}
|
jaroslav@562
|
181 |
|
jaroslav@562
|
182 |
|
jaroslav@562
|
183 |
/**
|
jaroslav@562
|
184 |
* Returns the {@code Class} object associated with the class or
|
jaroslav@562
|
185 |
* interface with the given string name, using the given class loader.
|
jaroslav@562
|
186 |
* Given the fully qualified name for a class or interface (in the same
|
jaroslav@562
|
187 |
* format returned by {@code getName}) this method attempts to
|
jaroslav@562
|
188 |
* locate, load, and link the class or interface. The specified class
|
jaroslav@562
|
189 |
* loader is used to load the class or interface. If the parameter
|
jaroslav@562
|
190 |
* {@code loader} is null, the class is loaded through the bootstrap
|
jaroslav@562
|
191 |
* class loader. The class is initialized only if the
|
jaroslav@562
|
192 |
* {@code initialize} parameter is {@code true} and if it has
|
jaroslav@562
|
193 |
* not been initialized earlier.
|
jaroslav@562
|
194 |
*
|
jaroslav@562
|
195 |
* <p> If {@code name} denotes a primitive type or void, an attempt
|
jaroslav@562
|
196 |
* will be made to locate a user-defined class in the unnamed package whose
|
jaroslav@562
|
197 |
* name is {@code name}. Therefore, this method cannot be used to
|
jaroslav@562
|
198 |
* obtain any of the {@code Class} objects representing primitive
|
jaroslav@562
|
199 |
* types or void.
|
jaroslav@562
|
200 |
*
|
jaroslav@562
|
201 |
* <p> If {@code name} denotes an array class, the component type of
|
jaroslav@562
|
202 |
* the array class is loaded but not initialized.
|
jaroslav@562
|
203 |
*
|
jaroslav@562
|
204 |
* <p> For example, in an instance method the expression:
|
jaroslav@562
|
205 |
*
|
jaroslav@562
|
206 |
* <blockquote>
|
jaroslav@562
|
207 |
* {@code Class.forName("Foo")}
|
jaroslav@562
|
208 |
* </blockquote>
|
jaroslav@562
|
209 |
*
|
jaroslav@562
|
210 |
* is equivalent to:
|
jaroslav@562
|
211 |
*
|
jaroslav@562
|
212 |
* <blockquote>
|
jaroslav@562
|
213 |
* {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
|
jaroslav@562
|
214 |
* </blockquote>
|
jaroslav@562
|
215 |
*
|
jaroslav@562
|
216 |
* Note that this method throws errors related to loading, linking or
|
jaroslav@562
|
217 |
* initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
|
jaroslav@562
|
218 |
* Java Language Specification</em>.
|
jaroslav@562
|
219 |
* Note that this method does not check whether the requested class
|
jaroslav@562
|
220 |
* is accessible to its caller.
|
jaroslav@562
|
221 |
*
|
jaroslav@562
|
222 |
* <p> If the {@code loader} is {@code null}, and a security
|
jaroslav@562
|
223 |
* manager is present, and the caller's class loader is not null, then this
|
jaroslav@562
|
224 |
* method calls the security manager's {@code checkPermission} method
|
jaroslav@562
|
225 |
* with a {@code RuntimePermission("getClassLoader")} permission to
|
jaroslav@562
|
226 |
* ensure it's ok to access the bootstrap class loader.
|
jaroslav@562
|
227 |
*
|
jaroslav@562
|
228 |
* @param name fully qualified name of the desired class
|
jaroslav@562
|
229 |
* @param initialize whether the class must be initialized
|
jaroslav@562
|
230 |
* @param loader class loader from which the class must be loaded
|
jaroslav@562
|
231 |
* @return class object representing the desired class
|
jaroslav@562
|
232 |
*
|
jaroslav@562
|
233 |
* @exception LinkageError if the linkage fails
|
jaroslav@562
|
234 |
* @exception ExceptionInInitializerError if the initialization provoked
|
jaroslav@562
|
235 |
* by this method fails
|
jaroslav@562
|
236 |
* @exception ClassNotFoundException if the class cannot be located by
|
jaroslav@562
|
237 |
* the specified class loader
|
jaroslav@562
|
238 |
*
|
jaroslav@562
|
239 |
* @see java.lang.Class#forName(String)
|
jaroslav@562
|
240 |
* @see java.lang.ClassLoader
|
jaroslav@562
|
241 |
* @since 1.2
|
jaroslav@562
|
242 |
*/
|
jaroslav@562
|
243 |
public static Class<?> forName(String name, boolean initialize,
|
jaroslav@562
|
244 |
ClassLoader loader)
|
jaroslav@562
|
245 |
throws ClassNotFoundException
|
jaroslav@562
|
246 |
{
|
jaroslav@562
|
247 |
return forName(name);
|
jaroslav@562
|
248 |
}
|
jaroslav@321
|
249 |
|
jaroslav@322
|
250 |
@JavaScriptBody(args = {"n", "c" }, body =
|
jaroslav@1585
|
251 |
"var m = vm[c];\n"
|
jaroslav@1585
|
252 |
+ "if (!m) {\n"
|
jaroslav@1585
|
253 |
+ " var l = vm.loadClass ? vm.loadClass : exports ? exports.loadClass : null;\n"
|
jaroslav@1585
|
254 |
+ " if (l) {\n"
|
jaroslav@1585
|
255 |
+ " l(n);\n"
|
jaroslav@658
|
256 |
+ " }\n"
|
jaroslav@1585
|
257 |
+ " if (vm[c]) m = vm[c];\n"
|
jaroslav@1585
|
258 |
+ " else if (exports[c]) m = exports[c];\n"
|
jaroslav@322
|
259 |
+ "}\n"
|
jaroslav@1585
|
260 |
+ "if (!m) return null;"
|
jaroslav@1585
|
261 |
+ "m(false);"
|
jaroslav@1887
|
262 |
+ "return m.$class ? m.$class : null;"
|
jaroslav@321
|
263 |
)
|
jaroslav@322
|
264 |
private static native Class<?> loadCls(String n, String c);
|
jaroslav@56
|
265 |
|
jaroslav@56
|
266 |
|
jaroslav@56
|
267 |
/**
|
jaroslav@56
|
268 |
* Creates a new instance of the class represented by this {@code Class}
|
jaroslav@56
|
269 |
* object. The class is instantiated as if by a {@code new}
|
jaroslav@56
|
270 |
* expression with an empty argument list. The class is initialized if it
|
jaroslav@56
|
271 |
* has not already been initialized.
|
jaroslav@56
|
272 |
*
|
jaroslav@56
|
273 |
* <p>Note that this method propagates any exception thrown by the
|
jaroslav@56
|
274 |
* nullary constructor, including a checked exception. Use of
|
jaroslav@56
|
275 |
* this method effectively bypasses the compile-time exception
|
jaroslav@56
|
276 |
* checking that would otherwise be performed by the compiler.
|
jaroslav@56
|
277 |
* The {@link
|
jaroslav@56
|
278 |
* java.lang.reflect.Constructor#newInstance(java.lang.Object...)
|
jaroslav@56
|
279 |
* Constructor.newInstance} method avoids this problem by wrapping
|
jaroslav@56
|
280 |
* any exception thrown by the constructor in a (checked) {@link
|
jaroslav@56
|
281 |
* java.lang.reflect.InvocationTargetException}.
|
jaroslav@56
|
282 |
*
|
jaroslav@56
|
283 |
* @return a newly allocated instance of the class represented by this
|
jaroslav@56
|
284 |
* object.
|
jaroslav@56
|
285 |
* @exception IllegalAccessException if the class or its nullary
|
jaroslav@56
|
286 |
* constructor is not accessible.
|
jaroslav@56
|
287 |
* @exception InstantiationException
|
jaroslav@56
|
288 |
* if this {@code Class} represents an abstract class,
|
jaroslav@56
|
289 |
* an interface, an array class, a primitive type, or void;
|
jaroslav@56
|
290 |
* or if the class has no nullary constructor;
|
jaroslav@56
|
291 |
* or if the instantiation fails for some other reason.
|
jaroslav@56
|
292 |
* @exception ExceptionInInitializerError if the initialization
|
jaroslav@56
|
293 |
* provoked by this method fails.
|
jaroslav@56
|
294 |
* @exception SecurityException
|
jaroslav@56
|
295 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@56
|
296 |
* following conditions is met:
|
jaroslav@56
|
297 |
*
|
jaroslav@56
|
298 |
* <ul>
|
jaroslav@56
|
299 |
*
|
jaroslav@56
|
300 |
* <li> invocation of
|
jaroslav@56
|
301 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@56
|
302 |
* s.checkMemberAccess(this, Member.PUBLIC)} denies
|
jaroslav@56
|
303 |
* creation of new instances of this class
|
jaroslav@56
|
304 |
*
|
jaroslav@56
|
305 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@56
|
306 |
* ancestor of the class loader for the current class and
|
jaroslav@56
|
307 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@56
|
308 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@56
|
309 |
* of this class
|
jaroslav@56
|
310 |
*
|
jaroslav@56
|
311 |
* </ul>
|
jaroslav@56
|
312 |
*
|
jaroslav@56
|
313 |
*/
|
jaroslav@397
|
314 |
@JavaScriptBody(args = { "self", "illegal" }, body =
|
jaroslav@397
|
315 |
"\nvar c = self.cnstr;"
|
jaroslav@397
|
316 |
+ "\nif (c['cons__V']) {"
|
jaroslav@1612
|
317 |
+ "\n if ((c['cons__V'].access & 0x1) != 0) {"
|
jaroslav@397
|
318 |
+ "\n var inst = c();"
|
jaroslav@1612
|
319 |
+ "\n c['cons__V'].call(inst);"
|
jaroslav@397
|
320 |
+ "\n return inst;"
|
jaroslav@397
|
321 |
+ "\n }"
|
jaroslav@397
|
322 |
+ "\n return illegal;"
|
jaroslav@397
|
323 |
+ "\n}"
|
jaroslav@397
|
324 |
+ "\nreturn null;"
|
jaroslav@231
|
325 |
)
|
jaroslav@397
|
326 |
private static native Object newInstance0(Class<?> self, Object illegal);
|
jaroslav@397
|
327 |
|
jaroslav@56
|
328 |
public T newInstance()
|
jaroslav@56
|
329 |
throws InstantiationException, IllegalAccessException
|
jaroslav@56
|
330 |
{
|
jaroslav@397
|
331 |
Object illegal = new Object();
|
jaroslav@397
|
332 |
Object inst = newInstance0(this, illegal);
|
jaroslav@397
|
333 |
if (inst == null) {
|
jaroslav@397
|
334 |
throw new InstantiationException(getName());
|
jaroslav@397
|
335 |
}
|
jaroslav@397
|
336 |
if (inst == illegal) {
|
jaroslav@397
|
337 |
throw new IllegalAccessException();
|
jaroslav@397
|
338 |
}
|
jaroslav@397
|
339 |
return (T)inst;
|
jaroslav@56
|
340 |
}
|
jaroslav@56
|
341 |
|
jaroslav@56
|
342 |
/**
|
jaroslav@56
|
343 |
* Determines if the specified {@code Object} is assignment-compatible
|
jaroslav@56
|
344 |
* with the object represented by this {@code Class}. This method is
|
jaroslav@56
|
345 |
* the dynamic equivalent of the Java language {@code instanceof}
|
jaroslav@56
|
346 |
* operator. The method returns {@code true} if the specified
|
jaroslav@56
|
347 |
* {@code Object} argument is non-null and can be cast to the
|
jaroslav@56
|
348 |
* reference type represented by this {@code Class} object without
|
jaroslav@56
|
349 |
* raising a {@code ClassCastException.} It returns {@code false}
|
jaroslav@56
|
350 |
* otherwise.
|
jaroslav@56
|
351 |
*
|
jaroslav@56
|
352 |
* <p> Specifically, if this {@code Class} object represents a
|
jaroslav@56
|
353 |
* declared class, this method returns {@code true} if the specified
|
jaroslav@56
|
354 |
* {@code Object} argument is an instance of the represented class (or
|
jaroslav@56
|
355 |
* of any of its subclasses); it returns {@code false} otherwise. If
|
jaroslav@56
|
356 |
* this {@code Class} object represents an array class, this method
|
jaroslav@56
|
357 |
* returns {@code true} if the specified {@code Object} argument
|
jaroslav@56
|
358 |
* can be converted to an object of the array class by an identity
|
jaroslav@56
|
359 |
* conversion or by a widening reference conversion; it returns
|
jaroslav@56
|
360 |
* {@code false} otherwise. If this {@code Class} object
|
jaroslav@56
|
361 |
* represents an interface, this method returns {@code true} if the
|
jaroslav@56
|
362 |
* class or any superclass of the specified {@code Object} argument
|
jaroslav@56
|
363 |
* implements this interface; it returns {@code false} otherwise. If
|
jaroslav@56
|
364 |
* this {@code Class} object represents a primitive type, this method
|
jaroslav@56
|
365 |
* returns {@code false}.
|
jaroslav@56
|
366 |
*
|
jaroslav@56
|
367 |
* @param obj the object to check
|
jaroslav@56
|
368 |
* @return true if {@code obj} is an instance of this class
|
jaroslav@56
|
369 |
*
|
jaroslav@56
|
370 |
* @since JDK1.1
|
jaroslav@56
|
371 |
*/
|
jaroslav@434
|
372 |
public boolean isInstance(Object obj) {
|
jaroslav@643
|
373 |
if (obj == null) {
|
jaroslav@643
|
374 |
return false;
|
jaroslav@643
|
375 |
}
|
jaroslav@571
|
376 |
if (isArray()) {
|
jaroslav@571
|
377 |
return isAssignableFrom(obj.getClass());
|
jaroslav@571
|
378 |
}
|
jaroslav@571
|
379 |
|
jaroslav@434
|
380 |
String prop = "$instOf_" + getName().replace('.', '_');
|
jaroslav@434
|
381 |
return hasProperty(obj, prop);
|
jaroslav@434
|
382 |
}
|
jaroslav@434
|
383 |
|
jaroslav@434
|
384 |
@JavaScriptBody(args = { "who", "prop" }, body =
|
jaroslav@434
|
385 |
"if (who[prop]) return true; else return false;"
|
jaroslav@434
|
386 |
)
|
jaroslav@434
|
387 |
private static native boolean hasProperty(Object who, String prop);
|
jaroslav@56
|
388 |
|
jaroslav@56
|
389 |
|
jaroslav@56
|
390 |
/**
|
jaroslav@56
|
391 |
* Determines if the class or interface represented by this
|
jaroslav@56
|
392 |
* {@code Class} object is either the same as, or is a superclass or
|
jaroslav@56
|
393 |
* superinterface of, the class or interface represented by the specified
|
jaroslav@56
|
394 |
* {@code Class} parameter. It returns {@code true} if so;
|
jaroslav@56
|
395 |
* otherwise it returns {@code false}. If this {@code Class}
|
jaroslav@56
|
396 |
* object represents a primitive type, this method returns
|
jaroslav@56
|
397 |
* {@code true} if the specified {@code Class} parameter is
|
jaroslav@56
|
398 |
* exactly this {@code Class} object; otherwise it returns
|
jaroslav@56
|
399 |
* {@code false}.
|
jaroslav@56
|
400 |
*
|
jaroslav@56
|
401 |
* <p> Specifically, this method tests whether the type represented by the
|
jaroslav@56
|
402 |
* specified {@code Class} parameter can be converted to the type
|
jaroslav@56
|
403 |
* represented by this {@code Class} object via an identity conversion
|
jaroslav@56
|
404 |
* or via a widening reference conversion. See <em>The Java Language
|
jaroslav@56
|
405 |
* Specification</em>, sections 5.1.1 and 5.1.4 , for details.
|
jaroslav@56
|
406 |
*
|
jaroslav@56
|
407 |
* @param cls the {@code Class} object to be checked
|
jaroslav@56
|
408 |
* @return the {@code boolean} value indicating whether objects of the
|
jaroslav@56
|
409 |
* type {@code cls} can be assigned to objects of this class
|
jaroslav@56
|
410 |
* @exception NullPointerException if the specified Class parameter is
|
jaroslav@56
|
411 |
* null.
|
jaroslav@56
|
412 |
* @since JDK1.1
|
jaroslav@56
|
413 |
*/
|
jaroslav@571
|
414 |
public boolean isAssignableFrom(Class<?> cls) {
|
jaroslav@571
|
415 |
if (this == cls) {
|
jaroslav@571
|
416 |
return true;
|
jaroslav@571
|
417 |
}
|
jaroslav@1634
|
418 |
if (this == Object.class) {
|
jaroslav@1634
|
419 |
return true;
|
jaroslav@1634
|
420 |
}
|
jaroslav@571
|
421 |
|
jaroslav@571
|
422 |
if (isArray()) {
|
jaroslav@571
|
423 |
final Class<?> cmpType = cls.getComponentType();
|
jaroslav@571
|
424 |
if (isPrimitive()) {
|
jaroslav@571
|
425 |
return this == cmpType;
|
jaroslav@571
|
426 |
}
|
jaroslav@571
|
427 |
return cmpType != null && getComponentType().isAssignableFrom(cmpType);
|
jaroslav@571
|
428 |
}
|
jaroslav@886
|
429 |
if (isPrimitive()) {
|
jaroslav@886
|
430 |
return false;
|
jaroslav@886
|
431 |
} else {
|
jaroslav@886
|
432 |
if (cls.isPrimitive()) {
|
jaroslav@886
|
433 |
return false;
|
jaroslav@886
|
434 |
}
|
jaroslav@1634
|
435 |
if (cls.isArray()) {
|
jaroslav@1634
|
436 |
return false;
|
jaroslav@1634
|
437 |
}
|
jaroslav@886
|
438 |
String prop = "$instOf_" + getName().replace('.', '_');
|
jaroslav@886
|
439 |
return hasCnstrProperty(cls, prop);
|
jaroslav@886
|
440 |
}
|
jaroslav@571
|
441 |
}
|
jaroslav@56
|
442 |
|
jaroslav@733
|
443 |
@JavaScriptBody(args = { "who", "prop" }, body =
|
jaroslav@733
|
444 |
"if (who.cnstr.prototype[prop]) return true; else return false;"
|
jaroslav@733
|
445 |
)
|
jaroslav@733
|
446 |
private static native boolean hasCnstrProperty(Object who, String prop);
|
jaroslav@733
|
447 |
|
jaroslav@733
|
448 |
|
jaroslav@56
|
449 |
/**
|
jaroslav@56
|
450 |
* Determines if the specified {@code Class} object represents an
|
jaroslav@56
|
451 |
* interface type.
|
jaroslav@56
|
452 |
*
|
jaroslav@56
|
453 |
* @return {@code true} if this object represents an interface;
|
jaroslav@56
|
454 |
* {@code false} otherwise.
|
jaroslav@56
|
455 |
*/
|
jaroslav@355
|
456 |
public boolean isInterface() {
|
jaroslav@355
|
457 |
return (getAccess() & 0x200) != 0;
|
jaroslav@355
|
458 |
}
|
jaroslav@355
|
459 |
|
jaroslav@443
|
460 |
@JavaScriptBody(args = {}, body = "return this.access;")
|
jaroslav@355
|
461 |
private native int getAccess();
|
jaroslav@56
|
462 |
|
jaroslav@56
|
463 |
|
jaroslav@56
|
464 |
/**
|
jaroslav@56
|
465 |
* Determines if this {@code Class} object represents an array class.
|
jaroslav@56
|
466 |
*
|
jaroslav@56
|
467 |
* @return {@code true} if this object represents an array class;
|
jaroslav@56
|
468 |
* {@code false} otherwise.
|
jaroslav@56
|
469 |
* @since JDK1.1
|
jaroslav@56
|
470 |
*/
|
jaroslav@228
|
471 |
public boolean isArray() {
|
jaroslav@448
|
472 |
return hasProperty(this, "array"); // NOI18N
|
jaroslav@228
|
473 |
}
|
jaroslav@56
|
474 |
|
jaroslav@56
|
475 |
|
jaroslav@56
|
476 |
/**
|
jaroslav@56
|
477 |
* Determines if the specified {@code Class} object represents a
|
jaroslav@56
|
478 |
* primitive type.
|
jaroslav@56
|
479 |
*
|
jaroslav@56
|
480 |
* <p> There are nine predefined {@code Class} objects to represent
|
jaroslav@56
|
481 |
* the eight primitive types and void. These are created by the Java
|
jaroslav@56
|
482 |
* Virtual Machine, and have the same names as the primitive types that
|
jaroslav@56
|
483 |
* they represent, namely {@code boolean}, {@code byte},
|
jaroslav@56
|
484 |
* {@code char}, {@code short}, {@code int},
|
jaroslav@56
|
485 |
* {@code long}, {@code float}, and {@code double}.
|
jaroslav@56
|
486 |
*
|
jaroslav@56
|
487 |
* <p> These objects may only be accessed via the following public static
|
jaroslav@56
|
488 |
* final variables, and are the only {@code Class} objects for which
|
jaroslav@56
|
489 |
* this method returns {@code true}.
|
jaroslav@56
|
490 |
*
|
jaroslav@56
|
491 |
* @return true if and only if this class represents a primitive type
|
jaroslav@56
|
492 |
*
|
jaroslav@56
|
493 |
* @see java.lang.Boolean#TYPE
|
jaroslav@56
|
494 |
* @see java.lang.Character#TYPE
|
jaroslav@56
|
495 |
* @see java.lang.Byte#TYPE
|
jaroslav@56
|
496 |
* @see java.lang.Short#TYPE
|
jaroslav@56
|
497 |
* @see java.lang.Integer#TYPE
|
jaroslav@56
|
498 |
* @see java.lang.Long#TYPE
|
jaroslav@56
|
499 |
* @see java.lang.Float#TYPE
|
jaroslav@56
|
500 |
* @see java.lang.Double#TYPE
|
jaroslav@56
|
501 |
* @see java.lang.Void#TYPE
|
jaroslav@56
|
502 |
* @since JDK1.1
|
jaroslav@56
|
503 |
*/
|
jaroslav@443
|
504 |
@JavaScriptBody(args = {}, body =
|
jaroslav@443
|
505 |
"if (this.primitive) return true;"
|
jaroslav@354
|
506 |
+ "else return false;"
|
jaroslav@354
|
507 |
)
|
jaroslav@56
|
508 |
public native boolean isPrimitive();
|
jaroslav@56
|
509 |
|
jaroslav@56
|
510 |
/**
|
jaroslav@56
|
511 |
* Returns true if this {@code Class} object represents an annotation
|
jaroslav@56
|
512 |
* type. Note that if this method returns true, {@link #isInterface()}
|
jaroslav@56
|
513 |
* would also return true, as all annotation types are also interfaces.
|
jaroslav@56
|
514 |
*
|
jaroslav@56
|
515 |
* @return {@code true} if this class object represents an annotation
|
jaroslav@56
|
516 |
* type; {@code false} otherwise
|
jaroslav@56
|
517 |
* @since 1.5
|
jaroslav@56
|
518 |
*/
|
jaroslav@56
|
519 |
public boolean isAnnotation() {
|
jaroslav@56
|
520 |
return (getModifiers() & ANNOTATION) != 0;
|
jaroslav@56
|
521 |
}
|
jaroslav@56
|
522 |
|
jaroslav@56
|
523 |
/**
|
jaroslav@56
|
524 |
* Returns {@code true} if this class is a synthetic class;
|
jaroslav@56
|
525 |
* returns {@code false} otherwise.
|
jaroslav@56
|
526 |
* @return {@code true} if and only if this class is a synthetic class as
|
jaroslav@56
|
527 |
* defined by the Java Language Specification.
|
jaroslav@56
|
528 |
* @since 1.5
|
jaroslav@56
|
529 |
*/
|
jaroslav@56
|
530 |
public boolean isSynthetic() {
|
jaroslav@56
|
531 |
return (getModifiers() & SYNTHETIC) != 0;
|
jaroslav@56
|
532 |
}
|
jaroslav@56
|
533 |
|
jaroslav@56
|
534 |
/**
|
jaroslav@56
|
535 |
* Returns the name of the entity (class, interface, array class,
|
jaroslav@56
|
536 |
* primitive type, or void) represented by this {@code Class} object,
|
jaroslav@56
|
537 |
* as a {@code String}.
|
jaroslav@56
|
538 |
*
|
jaroslav@56
|
539 |
* <p> If this class object represents a reference type that is not an
|
jaroslav@56
|
540 |
* array type then the binary name of the class is returned, as specified
|
jaroslav@56
|
541 |
* by
|
jaroslav@56
|
542 |
* <cite>The Java™ Language Specification</cite>.
|
jaroslav@56
|
543 |
*
|
jaroslav@56
|
544 |
* <p> If this class object represents a primitive type or void, then the
|
jaroslav@56
|
545 |
* name returned is a {@code String} equal to the Java language
|
jaroslav@56
|
546 |
* keyword corresponding to the primitive type or void.
|
jaroslav@56
|
547 |
*
|
jaroslav@56
|
548 |
* <p> If this class object represents a class of arrays, then the internal
|
jaroslav@56
|
549 |
* form of the name consists of the name of the element type preceded by
|
jaroslav@56
|
550 |
* one or more '{@code [}' characters representing the depth of the array
|
jaroslav@56
|
551 |
* nesting. The encoding of element type names is as follows:
|
jaroslav@56
|
552 |
*
|
jaroslav@56
|
553 |
* <blockquote><table summary="Element types and encodings">
|
jaroslav@56
|
554 |
* <tr><th> Element Type <th> <th> Encoding
|
jaroslav@56
|
555 |
* <tr><td> boolean <td> <td align=center> Z
|
jaroslav@56
|
556 |
* <tr><td> byte <td> <td align=center> B
|
jaroslav@56
|
557 |
* <tr><td> char <td> <td align=center> C
|
jaroslav@56
|
558 |
* <tr><td> class or interface
|
jaroslav@56
|
559 |
* <td> <td align=center> L<i>classname</i>;
|
jaroslav@56
|
560 |
* <tr><td> double <td> <td align=center> D
|
jaroslav@56
|
561 |
* <tr><td> float <td> <td align=center> F
|
jaroslav@56
|
562 |
* <tr><td> int <td> <td align=center> I
|
jaroslav@56
|
563 |
* <tr><td> long <td> <td align=center> J
|
jaroslav@56
|
564 |
* <tr><td> short <td> <td align=center> S
|
jaroslav@56
|
565 |
* </table></blockquote>
|
jaroslav@56
|
566 |
*
|
jaroslav@56
|
567 |
* <p> The class or interface name <i>classname</i> is the binary name of
|
jaroslav@56
|
568 |
* the class specified above.
|
jaroslav@56
|
569 |
*
|
jaroslav@56
|
570 |
* <p> Examples:
|
jaroslav@56
|
571 |
* <blockquote><pre>
|
jaroslav@56
|
572 |
* String.class.getName()
|
jaroslav@56
|
573 |
* returns "java.lang.String"
|
jaroslav@56
|
574 |
* byte.class.getName()
|
jaroslav@56
|
575 |
* returns "byte"
|
jaroslav@56
|
576 |
* (new Object[3]).getClass().getName()
|
jaroslav@56
|
577 |
* returns "[Ljava.lang.Object;"
|
jaroslav@56
|
578 |
* (new int[3][4][5][6][7][8][9]).getClass().getName()
|
jaroslav@56
|
579 |
* returns "[[[[[[[I"
|
jaroslav@56
|
580 |
* </pre></blockquote>
|
jaroslav@56
|
581 |
*
|
jaroslav@56
|
582 |
* @return the name of the class or interface
|
jaroslav@56
|
583 |
* represented by this object.
|
jaroslav@56
|
584 |
*/
|
jaroslav@56
|
585 |
public String getName() {
|
jaroslav@225
|
586 |
return jvmName().replace('/', '.');
|
jaroslav@56
|
587 |
}
|
jaroslav@56
|
588 |
|
jaroslav@443
|
589 |
@JavaScriptBody(args = {}, body = "return this.jvmName;")
|
jaroslav@225
|
590 |
private native String jvmName();
|
jaroslav@225
|
591 |
|
jaroslav@260
|
592 |
|
jaroslav@260
|
593 |
/**
|
jaroslav@260
|
594 |
* Returns an array of {@code TypeVariable} objects that represent the
|
jaroslav@260
|
595 |
* type variables declared by the generic declaration represented by this
|
jaroslav@260
|
596 |
* {@code GenericDeclaration} object, in declaration order. Returns an
|
jaroslav@260
|
597 |
* array of length 0 if the underlying generic declaration declares no type
|
jaroslav@260
|
598 |
* variables.
|
jaroslav@260
|
599 |
*
|
jaroslav@260
|
600 |
* @return an array of {@code TypeVariable} objects that represent
|
jaroslav@260
|
601 |
* the type variables declared by this generic declaration
|
jaroslav@260
|
602 |
* @throws java.lang.reflect.GenericSignatureFormatError if the generic
|
jaroslav@260
|
603 |
* signature of this generic declaration does not conform to
|
jaroslav@260
|
604 |
* the format specified in
|
jaroslav@260
|
605 |
* <cite>The Java™ Virtual Machine Specification</cite>
|
jaroslav@260
|
606 |
* @since 1.5
|
jaroslav@260
|
607 |
*/
|
jaroslav@260
|
608 |
public TypeVariable<Class<T>>[] getTypeParameters() {
|
jaroslav@260
|
609 |
throw new UnsupportedOperationException();
|
jaroslav@260
|
610 |
}
|
jaroslav@260
|
611 |
|
jaroslav@56
|
612 |
/**
|
jaroslav@56
|
613 |
* Returns the {@code Class} representing the superclass of the entity
|
jaroslav@56
|
614 |
* (class, interface, primitive type or void) represented by this
|
jaroslav@56
|
615 |
* {@code Class}. If this {@code Class} represents either the
|
jaroslav@56
|
616 |
* {@code Object} class, an interface, a primitive type, or void, then
|
jaroslav@56
|
617 |
* null is returned. If this object represents an array class then the
|
jaroslav@56
|
618 |
* {@code Class} object representing the {@code Object} class is
|
jaroslav@56
|
619 |
* returned.
|
jaroslav@56
|
620 |
*
|
jaroslav@56
|
621 |
* @return the superclass of the class represented by this object.
|
jaroslav@56
|
622 |
*/
|
jaroslav@443
|
623 |
@JavaScriptBody(args = {}, body = "return this.superclass;")
|
jaroslav@56
|
624 |
public native Class<? super T> getSuperclass();
|
jaroslav@56
|
625 |
|
jaroslav@56
|
626 |
/**
|
jaroslav@56
|
627 |
* Returns the Java language modifiers for this class or interface, encoded
|
jaroslav@56
|
628 |
* in an integer. The modifiers consist of the Java Virtual Machine's
|
jaroslav@56
|
629 |
* constants for {@code public}, {@code protected},
|
jaroslav@56
|
630 |
* {@code private}, {@code final}, {@code static},
|
jaroslav@56
|
631 |
* {@code abstract} and {@code interface}; they should be decoded
|
jaroslav@56
|
632 |
* using the methods of class {@code Modifier}.
|
jaroslav@56
|
633 |
*
|
jaroslav@56
|
634 |
* <p> If the underlying class is an array class, then its
|
jaroslav@56
|
635 |
* {@code public}, {@code private} and {@code protected}
|
jaroslav@56
|
636 |
* modifiers are the same as those of its component type. If this
|
jaroslav@56
|
637 |
* {@code Class} represents a primitive type or void, its
|
jaroslav@56
|
638 |
* {@code public} modifier is always {@code true}, and its
|
jaroslav@56
|
639 |
* {@code protected} and {@code private} modifiers are always
|
jaroslav@56
|
640 |
* {@code false}. If this object represents an array class, a
|
jaroslav@56
|
641 |
* primitive type or void, then its {@code final} modifier is always
|
jaroslav@56
|
642 |
* {@code true} and its interface modifier is always
|
jaroslav@56
|
643 |
* {@code false}. The values of its other modifiers are not determined
|
jaroslav@56
|
644 |
* by this specification.
|
jaroslav@56
|
645 |
*
|
jaroslav@56
|
646 |
* <p> The modifier encodings are defined in <em>The Java Virtual Machine
|
jaroslav@56
|
647 |
* Specification</em>, table 4.1.
|
jaroslav@56
|
648 |
*
|
jaroslav@56
|
649 |
* @return the {@code int} representing the modifiers for this class
|
jaroslav@56
|
650 |
* @see java.lang.reflect.Modifier
|
jaroslav@56
|
651 |
* @since JDK1.1
|
jaroslav@56
|
652 |
*/
|
jaroslav@586
|
653 |
public int getModifiers() {
|
jaroslav@586
|
654 |
return getAccess();
|
jaroslav@586
|
655 |
}
|
jaroslav@56
|
656 |
|
jaroslav@1419
|
657 |
/**
|
jaroslav@1419
|
658 |
* If the class or interface represented by this {@code Class} object
|
jaroslav@1419
|
659 |
* is a member of another class, returns the {@code Class} object
|
jaroslav@1419
|
660 |
* representing the class in which it was declared. This method returns
|
jaroslav@1419
|
661 |
* null if this class or interface is not a member of any other class. If
|
jaroslav@1419
|
662 |
* this {@code Class} object represents an array class, a primitive
|
jaroslav@1419
|
663 |
* type, or void,then this method returns null.
|
jaroslav@1419
|
664 |
*
|
jaroslav@1419
|
665 |
* @return the declaring class for this class
|
jaroslav@1419
|
666 |
* @since JDK1.1
|
jaroslav@1419
|
667 |
*/
|
jaroslav@1419
|
668 |
public Class<?> getDeclaringClass() {
|
jaroslav@1419
|
669 |
throw new SecurityException();
|
jaroslav@1419
|
670 |
}
|
jaroslav@56
|
671 |
|
jaroslav@56
|
672 |
/**
|
jaroslav@56
|
673 |
* Returns the simple name of the underlying class as given in the
|
jaroslav@56
|
674 |
* source code. Returns an empty string if the underlying class is
|
jaroslav@56
|
675 |
* anonymous.
|
jaroslav@56
|
676 |
*
|
jaroslav@56
|
677 |
* <p>The simple name of an array is the simple name of the
|
jaroslav@56
|
678 |
* component type with "[]" appended. In particular the simple
|
jaroslav@56
|
679 |
* name of an array whose component type is anonymous is "[]".
|
jaroslav@56
|
680 |
*
|
jaroslav@56
|
681 |
* @return the simple name of the underlying class
|
jaroslav@56
|
682 |
* @since 1.5
|
jaroslav@56
|
683 |
*/
|
jaroslav@56
|
684 |
public String getSimpleName() {
|
jaroslav@229
|
685 |
if (isArray())
|
jaroslav@229
|
686 |
return getComponentType().getSimpleName()+"[]";
|
jaroslav@229
|
687 |
|
jaroslav@229
|
688 |
String simpleName = getSimpleBinaryName();
|
jaroslav@229
|
689 |
if (simpleName == null) { // top level class
|
jaroslav@229
|
690 |
simpleName = getName();
|
jaroslav@229
|
691 |
return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
|
jaroslav@229
|
692 |
}
|
jaroslav@229
|
693 |
// According to JLS3 "Binary Compatibility" (13.1) the binary
|
jaroslav@229
|
694 |
// name of non-package classes (not top level) is the binary
|
jaroslav@229
|
695 |
// name of the immediately enclosing class followed by a '$' followed by:
|
jaroslav@229
|
696 |
// (for nested and inner classes): the simple name.
|
jaroslav@229
|
697 |
// (for local classes): 1 or more digits followed by the simple name.
|
jaroslav@229
|
698 |
// (for anonymous classes): 1 or more digits.
|
jaroslav@229
|
699 |
|
jaroslav@229
|
700 |
// Since getSimpleBinaryName() will strip the binary name of
|
jaroslav@229
|
701 |
// the immediatly enclosing class, we are now looking at a
|
jaroslav@229
|
702 |
// string that matches the regular expression "\$[0-9]*"
|
jaroslav@229
|
703 |
// followed by a simple name (considering the simple of an
|
jaroslav@229
|
704 |
// anonymous class to be the empty string).
|
jaroslav@229
|
705 |
|
jaroslav@229
|
706 |
// Remove leading "\$[0-9]*" from the name
|
jaroslav@229
|
707 |
int length = simpleName.length();
|
jaroslav@229
|
708 |
if (length < 1 || simpleName.charAt(0) != '$')
|
jaroslav@229
|
709 |
throw new IllegalStateException("Malformed class name");
|
jaroslav@229
|
710 |
int index = 1;
|
jaroslav@229
|
711 |
while (index < length && isAsciiDigit(simpleName.charAt(index)))
|
jaroslav@229
|
712 |
index++;
|
jaroslav@229
|
713 |
// Eventually, this is the empty string iff this is an anonymous class
|
jaroslav@229
|
714 |
return simpleName.substring(index);
|
jaroslav@56
|
715 |
}
|
jaroslav@56
|
716 |
|
jaroslav@56
|
717 |
/**
|
jaroslav@229
|
718 |
* Returns the "simple binary name" of the underlying class, i.e.,
|
jaroslav@229
|
719 |
* the binary name without the leading enclosing class name.
|
jaroslav@229
|
720 |
* Returns {@code null} if the underlying class is a top level
|
jaroslav@229
|
721 |
* class.
|
jaroslav@229
|
722 |
*/
|
jaroslav@229
|
723 |
private String getSimpleBinaryName() {
|
jaroslav@1898
|
724 |
final String name = getName();
|
jaroslav@1898
|
725 |
int dolar = name.lastIndexOf('$');
|
jaroslav@1898
|
726 |
if (dolar == -1) {
|
jaroslav@229
|
727 |
return null;
|
jaroslav@229
|
728 |
}
|
jaroslav@1898
|
729 |
return name.substring(dolar);
|
jaroslav@229
|
730 |
}
|
jaroslav@261
|
731 |
|
jaroslav@261
|
732 |
/**
|
jaroslav@261
|
733 |
* Returns an array containing {@code Field} objects reflecting all
|
jaroslav@261
|
734 |
* the accessible public fields of the class or interface represented by
|
jaroslav@261
|
735 |
* this {@code Class} object. The elements in the array returned are
|
jaroslav@261
|
736 |
* not sorted and are not in any particular order. This method returns an
|
jaroslav@261
|
737 |
* array of length 0 if the class or interface has no accessible public
|
jaroslav@261
|
738 |
* fields, or if it represents an array class, a primitive type, or void.
|
jaroslav@261
|
739 |
*
|
jaroslav@261
|
740 |
* <p> Specifically, if this {@code Class} object represents a class,
|
jaroslav@261
|
741 |
* this method returns the public fields of this class and of all its
|
jaroslav@261
|
742 |
* superclasses. If this {@code Class} object represents an
|
jaroslav@261
|
743 |
* interface, this method returns the fields of this interface and of all
|
jaroslav@261
|
744 |
* its superinterfaces.
|
jaroslav@261
|
745 |
*
|
jaroslav@261
|
746 |
* <p> The implicit length field for array class is not reflected by this
|
jaroslav@261
|
747 |
* method. User code should use the methods of class {@code Array} to
|
jaroslav@261
|
748 |
* manipulate arrays.
|
jaroslav@261
|
749 |
*
|
jaroslav@261
|
750 |
* <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
|
jaroslav@261
|
751 |
*
|
jaroslav@261
|
752 |
* @return the array of {@code Field} objects representing the
|
jaroslav@261
|
753 |
* public fields
|
jaroslav@261
|
754 |
* @exception SecurityException
|
jaroslav@261
|
755 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@261
|
756 |
* following conditions is met:
|
jaroslav@261
|
757 |
*
|
jaroslav@261
|
758 |
* <ul>
|
jaroslav@261
|
759 |
*
|
jaroslav@261
|
760 |
* <li> invocation of
|
jaroslav@261
|
761 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@261
|
762 |
* s.checkMemberAccess(this, Member.PUBLIC)} denies
|
jaroslav@261
|
763 |
* access to the fields within this class
|
jaroslav@261
|
764 |
*
|
jaroslav@261
|
765 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@261
|
766 |
* ancestor of the class loader for the current class and
|
jaroslav@261
|
767 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@261
|
768 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@261
|
769 |
* of this class
|
jaroslav@261
|
770 |
*
|
jaroslav@261
|
771 |
* </ul>
|
jaroslav@261
|
772 |
*
|
jaroslav@261
|
773 |
* @since JDK1.1
|
jaroslav@261
|
774 |
*/
|
jaroslav@261
|
775 |
public Field[] getFields() throws SecurityException {
|
jaroslav@261
|
776 |
throw new SecurityException();
|
jaroslav@261
|
777 |
}
|
jaroslav@261
|
778 |
|
jaroslav@261
|
779 |
/**
|
jaroslav@261
|
780 |
* Returns an array containing {@code Method} objects reflecting all
|
jaroslav@261
|
781 |
* the public <em>member</em> methods of the class or interface represented
|
jaroslav@261
|
782 |
* by this {@code Class} object, including those declared by the class
|
jaroslav@261
|
783 |
* or interface and those inherited from superclasses and
|
jaroslav@261
|
784 |
* superinterfaces. Array classes return all the (public) member methods
|
jaroslav@261
|
785 |
* inherited from the {@code Object} class. The elements in the array
|
jaroslav@261
|
786 |
* returned are not sorted and are not in any particular order. This
|
jaroslav@261
|
787 |
* method returns an array of length 0 if this {@code Class} object
|
jaroslav@261
|
788 |
* represents a class or interface that has no public member methods, or if
|
jaroslav@261
|
789 |
* this {@code Class} object represents a primitive type or void.
|
jaroslav@261
|
790 |
*
|
jaroslav@261
|
791 |
* <p> The class initialization method {@code <clinit>} is not
|
jaroslav@261
|
792 |
* included in the returned array. If the class declares multiple public
|
jaroslav@261
|
793 |
* member methods with the same parameter types, they are all included in
|
jaroslav@261
|
794 |
* the returned array.
|
jaroslav@261
|
795 |
*
|
jaroslav@261
|
796 |
* <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
|
jaroslav@261
|
797 |
*
|
jaroslav@261
|
798 |
* @return the array of {@code Method} objects representing the
|
jaroslav@261
|
799 |
* public methods of this class
|
jaroslav@261
|
800 |
* @exception SecurityException
|
jaroslav@261
|
801 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@261
|
802 |
* following conditions is met:
|
jaroslav@261
|
803 |
*
|
jaroslav@261
|
804 |
* <ul>
|
jaroslav@261
|
805 |
*
|
jaroslav@261
|
806 |
* <li> invocation of
|
jaroslav@261
|
807 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@261
|
808 |
* s.checkMemberAccess(this, Member.PUBLIC)} denies
|
jaroslav@261
|
809 |
* access to the methods within this class
|
jaroslav@261
|
810 |
*
|
jaroslav@261
|
811 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@261
|
812 |
* ancestor of the class loader for the current class and
|
jaroslav@261
|
813 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@261
|
814 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@261
|
815 |
* of this class
|
jaroslav@261
|
816 |
*
|
jaroslav@261
|
817 |
* </ul>
|
jaroslav@261
|
818 |
*
|
jaroslav@261
|
819 |
* @since JDK1.1
|
jaroslav@261
|
820 |
*/
|
jaroslav@261
|
821 |
public Method[] getMethods() throws SecurityException {
|
jaroslav@392
|
822 |
return MethodImpl.findMethods(this, 0x01);
|
jaroslav@261
|
823 |
}
|
jaroslav@261
|
824 |
|
jaroslav@261
|
825 |
/**
|
jaroslav@261
|
826 |
* Returns a {@code Field} object that reflects the specified public
|
jaroslav@261
|
827 |
* member field of the class or interface represented by this
|
jaroslav@261
|
828 |
* {@code Class} object. The {@code name} parameter is a
|
jaroslav@261
|
829 |
* {@code String} specifying the simple name of the desired field.
|
jaroslav@261
|
830 |
*
|
jaroslav@261
|
831 |
* <p> The field to be reflected is determined by the algorithm that
|
jaroslav@261
|
832 |
* follows. Let C be the class represented by this object:
|
jaroslav@261
|
833 |
* <OL>
|
jaroslav@261
|
834 |
* <LI> If C declares a public field with the name specified, that is the
|
jaroslav@261
|
835 |
* field to be reflected.</LI>
|
jaroslav@261
|
836 |
* <LI> If no field was found in step 1 above, this algorithm is applied
|
jaroslav@261
|
837 |
* recursively to each direct superinterface of C. The direct
|
jaroslav@261
|
838 |
* superinterfaces are searched in the order they were declared.</LI>
|
jaroslav@261
|
839 |
* <LI> If no field was found in steps 1 and 2 above, and C has a
|
jaroslav@261
|
840 |
* superclass S, then this algorithm is invoked recursively upon S.
|
jaroslav@261
|
841 |
* If C has no superclass, then a {@code NoSuchFieldException}
|
jaroslav@261
|
842 |
* is thrown.</LI>
|
jaroslav@261
|
843 |
* </OL>
|
jaroslav@261
|
844 |
*
|
jaroslav@261
|
845 |
* <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
|
jaroslav@261
|
846 |
*
|
jaroslav@261
|
847 |
* @param name the field name
|
jaroslav@261
|
848 |
* @return the {@code Field} object of this class specified by
|
jaroslav@261
|
849 |
* {@code name}
|
jaroslav@261
|
850 |
* @exception NoSuchFieldException if a field with the specified name is
|
jaroslav@261
|
851 |
* not found.
|
jaroslav@261
|
852 |
* @exception NullPointerException if {@code name} is {@code null}
|
jaroslav@261
|
853 |
* @exception SecurityException
|
jaroslav@261
|
854 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@261
|
855 |
* following conditions is met:
|
jaroslav@261
|
856 |
*
|
jaroslav@261
|
857 |
* <ul>
|
jaroslav@261
|
858 |
*
|
jaroslav@261
|
859 |
* <li> invocation of
|
jaroslav@261
|
860 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@261
|
861 |
* s.checkMemberAccess(this, Member.PUBLIC)} denies
|
jaroslav@261
|
862 |
* access to the field
|
jaroslav@261
|
863 |
*
|
jaroslav@261
|
864 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@261
|
865 |
* ancestor of the class loader for the current class and
|
jaroslav@261
|
866 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@261
|
867 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@261
|
868 |
* of this class
|
jaroslav@261
|
869 |
*
|
jaroslav@261
|
870 |
* </ul>
|
jaroslav@261
|
871 |
*
|
jaroslav@261
|
872 |
* @since JDK1.1
|
jaroslav@261
|
873 |
*/
|
jaroslav@261
|
874 |
public Field getField(String name)
|
jaroslav@261
|
875 |
throws SecurityException {
|
jaroslav@261
|
876 |
throw new SecurityException();
|
jaroslav@261
|
877 |
}
|
jaroslav@229
|
878 |
|
jaroslav@261
|
879 |
|
jaroslav@261
|
880 |
/**
|
jaroslav@261
|
881 |
* Returns a {@code Method} object that reflects the specified public
|
jaroslav@261
|
882 |
* member method of the class or interface represented by this
|
jaroslav@261
|
883 |
* {@code Class} object. The {@code name} parameter is a
|
jaroslav@261
|
884 |
* {@code String} specifying the simple name of the desired method. The
|
jaroslav@261
|
885 |
* {@code parameterTypes} parameter is an array of {@code Class}
|
jaroslav@261
|
886 |
* objects that identify the method's formal parameter types, in declared
|
jaroslav@261
|
887 |
* order. If {@code parameterTypes} is {@code null}, it is
|
jaroslav@261
|
888 |
* treated as if it were an empty array.
|
jaroslav@261
|
889 |
*
|
jaroslav@261
|
890 |
* <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
|
jaroslav@261
|
891 |
* {@code NoSuchMethodException} is raised. Otherwise, the method to
|
jaroslav@261
|
892 |
* be reflected is determined by the algorithm that follows. Let C be the
|
jaroslav@261
|
893 |
* class represented by this object:
|
jaroslav@261
|
894 |
* <OL>
|
jaroslav@261
|
895 |
* <LI> C is searched for any <I>matching methods</I>. If no matching
|
jaroslav@261
|
896 |
* method is found, the algorithm of step 1 is invoked recursively on
|
jaroslav@261
|
897 |
* the superclass of C.</LI>
|
jaroslav@261
|
898 |
* <LI> If no method was found in step 1 above, the superinterfaces of C
|
jaroslav@261
|
899 |
* are searched for a matching method. If any such method is found, it
|
jaroslav@261
|
900 |
* is reflected.</LI>
|
jaroslav@261
|
901 |
* </OL>
|
jaroslav@261
|
902 |
*
|
jaroslav@261
|
903 |
* To find a matching method in a class C: If C declares exactly one
|
jaroslav@261
|
904 |
* public method with the specified name and exactly the same formal
|
jaroslav@261
|
905 |
* parameter types, that is the method reflected. If more than one such
|
jaroslav@261
|
906 |
* method is found in C, and one of these methods has a return type that is
|
jaroslav@261
|
907 |
* more specific than any of the others, that method is reflected;
|
jaroslav@261
|
908 |
* otherwise one of the methods is chosen arbitrarily.
|
jaroslav@261
|
909 |
*
|
jaroslav@261
|
910 |
* <p>Note that there may be more than one matching method in a
|
jaroslav@261
|
911 |
* class because while the Java language forbids a class to
|
jaroslav@261
|
912 |
* declare multiple methods with the same signature but different
|
jaroslav@261
|
913 |
* return types, the Java virtual machine does not. This
|
jaroslav@261
|
914 |
* increased flexibility in the virtual machine can be used to
|
jaroslav@261
|
915 |
* implement various language features. For example, covariant
|
jaroslav@261
|
916 |
* returns can be implemented with {@linkplain
|
jaroslav@261
|
917 |
* java.lang.reflect.Method#isBridge bridge methods}; the bridge
|
jaroslav@261
|
918 |
* method and the method being overridden would have the same
|
jaroslav@261
|
919 |
* signature but different return types.
|
jaroslav@261
|
920 |
*
|
jaroslav@261
|
921 |
* <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
|
jaroslav@261
|
922 |
*
|
jaroslav@261
|
923 |
* @param name the name of the method
|
jaroslav@261
|
924 |
* @param parameterTypes the list of parameters
|
jaroslav@261
|
925 |
* @return the {@code Method} object that matches the specified
|
jaroslav@261
|
926 |
* {@code name} and {@code parameterTypes}
|
jaroslav@261
|
927 |
* @exception NoSuchMethodException if a matching method is not found
|
jaroslav@261
|
928 |
* or if the name is "<init>"or "<clinit>".
|
jaroslav@261
|
929 |
* @exception NullPointerException if {@code name} is {@code null}
|
jaroslav@261
|
930 |
* @exception SecurityException
|
jaroslav@261
|
931 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@261
|
932 |
* following conditions is met:
|
jaroslav@261
|
933 |
*
|
jaroslav@261
|
934 |
* <ul>
|
jaroslav@261
|
935 |
*
|
jaroslav@261
|
936 |
* <li> invocation of
|
jaroslav@261
|
937 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@261
|
938 |
* s.checkMemberAccess(this, Member.PUBLIC)} denies
|
jaroslav@261
|
939 |
* access to the method
|
jaroslav@261
|
940 |
*
|
jaroslav@261
|
941 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@261
|
942 |
* ancestor of the class loader for the current class and
|
jaroslav@261
|
943 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@261
|
944 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@261
|
945 |
* of this class
|
jaroslav@261
|
946 |
*
|
jaroslav@261
|
947 |
* </ul>
|
jaroslav@261
|
948 |
*
|
jaroslav@261
|
949 |
* @since JDK1.1
|
jaroslav@261
|
950 |
*/
|
jaroslav@261
|
951 |
public Method getMethod(String name, Class<?>... parameterTypes)
|
jaroslav@420
|
952 |
throws SecurityException, NoSuchMethodException {
|
jaroslav@391
|
953 |
Method m = MethodImpl.findMethod(this, name, parameterTypes);
|
jaroslav@262
|
954 |
if (m == null) {
|
jaroslav@420
|
955 |
StringBuilder sb = new StringBuilder();
|
jaroslav@420
|
956 |
sb.append(getName()).append('.').append(name).append('(');
|
jaroslav@420
|
957 |
String sep = "";
|
jaroslav@420
|
958 |
for (int i = 0; i < parameterTypes.length; i++) {
|
jaroslav@420
|
959 |
sb.append(sep).append(parameterTypes[i].getName());
|
jaroslav@420
|
960 |
sep = ", ";
|
jaroslav@420
|
961 |
}
|
jaroslav@420
|
962 |
sb.append(')');
|
jaroslav@420
|
963 |
throw new NoSuchMethodException(sb.toString());
|
jaroslav@262
|
964 |
}
|
jaroslav@262
|
965 |
return m;
|
jaroslav@261
|
966 |
}
|
jaroslav@604
|
967 |
|
jaroslav@604
|
968 |
/**
|
jaroslav@604
|
969 |
* Returns an array of {@code Method} objects reflecting all the
|
jaroslav@604
|
970 |
* methods declared by the class or interface represented by this
|
jaroslav@604
|
971 |
* {@code Class} object. This includes public, protected, default
|
jaroslav@604
|
972 |
* (package) access, and private methods, but excludes inherited methods.
|
jaroslav@604
|
973 |
* The elements in the array returned are not sorted and are not in any
|
jaroslav@604
|
974 |
* particular order. This method returns an array of length 0 if the class
|
jaroslav@604
|
975 |
* or interface declares no methods, or if this {@code Class} object
|
jaroslav@604
|
976 |
* represents a primitive type, an array class, or void. The class
|
jaroslav@604
|
977 |
* initialization method {@code <clinit>} is not included in the
|
jaroslav@604
|
978 |
* returned array. If the class declares multiple public member methods
|
jaroslav@604
|
979 |
* with the same parameter types, they are all included in the returned
|
jaroslav@604
|
980 |
* array.
|
jaroslav@604
|
981 |
*
|
jaroslav@604
|
982 |
* <p> See <em>The Java Language Specification</em>, section 8.2.
|
jaroslav@604
|
983 |
*
|
jaroslav@604
|
984 |
* @return the array of {@code Method} objects representing all the
|
jaroslav@604
|
985 |
* declared methods of this class
|
jaroslav@604
|
986 |
* @exception SecurityException
|
jaroslav@604
|
987 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@604
|
988 |
* following conditions is met:
|
jaroslav@604
|
989 |
*
|
jaroslav@604
|
990 |
* <ul>
|
jaroslav@604
|
991 |
*
|
jaroslav@604
|
992 |
* <li> invocation of
|
jaroslav@604
|
993 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@604
|
994 |
* s.checkMemberAccess(this, Member.DECLARED)} denies
|
jaroslav@604
|
995 |
* access to the declared methods within this class
|
jaroslav@604
|
996 |
*
|
jaroslav@604
|
997 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@604
|
998 |
* ancestor of the class loader for the current class and
|
jaroslav@604
|
999 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@604
|
1000 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@604
|
1001 |
* of this class
|
jaroslav@604
|
1002 |
*
|
jaroslav@604
|
1003 |
* </ul>
|
jaroslav@604
|
1004 |
*
|
jaroslav@604
|
1005 |
* @since JDK1.1
|
jaroslav@604
|
1006 |
*/
|
jaroslav@604
|
1007 |
public Method[] getDeclaredMethods() throws SecurityException {
|
jaroslav@1900
|
1008 |
Method[] arr = getMethods();
|
jaroslav@1900
|
1009 |
int cnt = 0;
|
jaroslav@1900
|
1010 |
for (int i = 0; i < arr.length; i++) {
|
jaroslav@1900
|
1011 |
if (arr[i].getDeclaringClass() == this) {
|
jaroslav@1900
|
1012 |
cnt++;
|
jaroslav@1900
|
1013 |
}
|
jaroslav@1900
|
1014 |
}
|
jaroslav@1900
|
1015 |
Method[] ret = new Method[cnt];
|
jaroslav@1900
|
1016 |
cnt = 0;
|
jaroslav@1900
|
1017 |
for (int i = 0; i < arr.length; i++) {
|
jaroslav@1900
|
1018 |
if (arr[i].getDeclaringClass() == this) {
|
jaroslav@1900
|
1019 |
ret[cnt++] = arr[i];
|
jaroslav@1900
|
1020 |
}
|
jaroslav@1900
|
1021 |
}
|
jaroslav@1900
|
1022 |
return ret;
|
jaroslav@604
|
1023 |
}
|
jaroslav@604
|
1024 |
|
jaroslav@229
|
1025 |
/**
|
jaroslav@1312
|
1026 |
* Returns an array of {@code Field} objects reflecting all the fields
|
jaroslav@1312
|
1027 |
* declared by the class or interface represented by this
|
jaroslav@1312
|
1028 |
* {@code Class} object. This includes public, protected, default
|
jaroslav@1312
|
1029 |
* (package) access, and private fields, but excludes inherited fields.
|
jaroslav@1312
|
1030 |
* The elements in the array returned are not sorted and are not in any
|
jaroslav@1312
|
1031 |
* particular order. This method returns an array of length 0 if the class
|
jaroslav@1312
|
1032 |
* or interface declares no fields, or if this {@code Class} object
|
jaroslav@1312
|
1033 |
* represents a primitive type, an array class, or void.
|
jaroslav@1312
|
1034 |
*
|
jaroslav@1312
|
1035 |
* <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
|
jaroslav@1312
|
1036 |
*
|
jaroslav@1312
|
1037 |
* @return the array of {@code Field} objects representing all the
|
jaroslav@1312
|
1038 |
* declared fields of this class
|
jaroslav@1312
|
1039 |
* @exception SecurityException
|
jaroslav@1312
|
1040 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@1312
|
1041 |
* following conditions is met:
|
jaroslav@1312
|
1042 |
*
|
jaroslav@1312
|
1043 |
* <ul>
|
jaroslav@1312
|
1044 |
*
|
jaroslav@1312
|
1045 |
* <li> invocation of
|
jaroslav@1312
|
1046 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@1312
|
1047 |
* s.checkMemberAccess(this, Member.DECLARED)} denies
|
jaroslav@1312
|
1048 |
* access to the declared fields within this class
|
jaroslav@1312
|
1049 |
*
|
jaroslav@1312
|
1050 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@1312
|
1051 |
* ancestor of the class loader for the current class and
|
jaroslav@1312
|
1052 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@1312
|
1053 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@1312
|
1054 |
* of this class
|
jaroslav@1312
|
1055 |
*
|
jaroslav@1312
|
1056 |
* </ul>
|
jaroslav@1312
|
1057 |
*
|
jaroslav@1312
|
1058 |
* @since JDK1.1
|
jaroslav@1312
|
1059 |
*/
|
jaroslav@1312
|
1060 |
public Field[] getDeclaredFields() throws SecurityException {
|
jaroslav@1312
|
1061 |
throw new SecurityException();
|
jaroslav@1312
|
1062 |
}
|
jaroslav@1312
|
1063 |
|
jaroslav@1312
|
1064 |
/**
|
jaroslav@1312
|
1065 |
* <b>Bck2Brwsr</b> emulation can only seek public methods, otherwise it
|
jaroslav@1312
|
1066 |
* throws a {@code SecurityException}.
|
jaroslav@1312
|
1067 |
* <p>
|
jaroslav@1312
|
1068 |
* Returns a {@code Method} object that reflects the specified
|
jaroslav@1312
|
1069 |
* declared method of the class or interface represented by this
|
jaroslav@1312
|
1070 |
* {@code Class} object. The {@code name} parameter is a
|
jaroslav@1312
|
1071 |
* {@code String} that specifies the simple name of the desired
|
jaroslav@1312
|
1072 |
* method, and the {@code parameterTypes} parameter is an array of
|
jaroslav@1312
|
1073 |
* {@code Class} objects that identify the method's formal parameter
|
jaroslav@1312
|
1074 |
* types, in declared order. If more than one method with the same
|
jaroslav@1312
|
1075 |
* parameter types is declared in a class, and one of these methods has a
|
jaroslav@1312
|
1076 |
* return type that is more specific than any of the others, that method is
|
jaroslav@1312
|
1077 |
* returned; otherwise one of the methods is chosen arbitrarily. If the
|
jaroslav@1312
|
1078 |
* name is "<init>"or "<clinit>" a {@code NoSuchMethodException}
|
jaroslav@1312
|
1079 |
* is raised.
|
jaroslav@1312
|
1080 |
*
|
jaroslav@1312
|
1081 |
* @param name the name of the method
|
jaroslav@1312
|
1082 |
* @param parameterTypes the parameter array
|
jaroslav@1312
|
1083 |
* @return the {@code Method} object for the method of this class
|
jaroslav@1312
|
1084 |
* matching the specified name and parameters
|
jaroslav@1312
|
1085 |
* @exception NoSuchMethodException if a matching method is not found.
|
jaroslav@1312
|
1086 |
* @exception NullPointerException if {@code name} is {@code null}
|
jaroslav@1312
|
1087 |
* @exception SecurityException
|
jaroslav@1312
|
1088 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@1312
|
1089 |
* following conditions is met:
|
jaroslav@1312
|
1090 |
*
|
jaroslav@1312
|
1091 |
* <ul>
|
jaroslav@1312
|
1092 |
*
|
jaroslav@1312
|
1093 |
* <li> invocation of
|
jaroslav@1312
|
1094 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@1312
|
1095 |
* s.checkMemberAccess(this, Member.DECLARED)} denies
|
jaroslav@1312
|
1096 |
* access to the declared method
|
jaroslav@1312
|
1097 |
*
|
jaroslav@1312
|
1098 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@1312
|
1099 |
* ancestor of the class loader for the current class and
|
jaroslav@1312
|
1100 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@1312
|
1101 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@1312
|
1102 |
* of this class
|
jaroslav@1312
|
1103 |
*
|
jaroslav@1312
|
1104 |
* </ul>
|
jaroslav@1312
|
1105 |
*
|
jaroslav@1312
|
1106 |
* @since JDK1.1
|
jaroslav@1312
|
1107 |
*/
|
jaroslav@1312
|
1108 |
public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
|
jaroslav@1312
|
1109 |
throws NoSuchMethodException, SecurityException {
|
jaroslav@1312
|
1110 |
try {
|
jaroslav@1312
|
1111 |
return getMethod(name, parameterTypes);
|
jaroslav@1312
|
1112 |
} catch (NoSuchMethodException ex) {
|
jaroslav@1312
|
1113 |
throw new SecurityException();
|
jaroslav@1312
|
1114 |
}
|
jaroslav@1312
|
1115 |
}
|
jaroslav@1312
|
1116 |
|
jaroslav@1312
|
1117 |
/**
|
jaroslav@1312
|
1118 |
* Returns a {@code Field} object that reflects the specified declared
|
jaroslav@1312
|
1119 |
* field of the class or interface represented by this {@code Class}
|
jaroslav@1312
|
1120 |
* object. The {@code name} parameter is a {@code String} that
|
jaroslav@1312
|
1121 |
* specifies the simple name of the desired field. Note that this method
|
jaroslav@1312
|
1122 |
* will not reflect the {@code length} field of an array class.
|
jaroslav@1312
|
1123 |
*
|
jaroslav@1312
|
1124 |
* @param name the name of the field
|
jaroslav@1312
|
1125 |
* @return the {@code Field} object for the specified field in this
|
jaroslav@1312
|
1126 |
* class
|
jaroslav@1312
|
1127 |
* @exception NoSuchFieldException if a field with the specified name is
|
jaroslav@1312
|
1128 |
* not found.
|
jaroslav@1312
|
1129 |
* @exception NullPointerException if {@code name} is {@code null}
|
jaroslav@1312
|
1130 |
* @exception SecurityException
|
jaroslav@1312
|
1131 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@1312
|
1132 |
* following conditions is met:
|
jaroslav@1312
|
1133 |
*
|
jaroslav@1312
|
1134 |
* <ul>
|
jaroslav@1312
|
1135 |
*
|
jaroslav@1312
|
1136 |
* <li> invocation of
|
jaroslav@1312
|
1137 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@1312
|
1138 |
* s.checkMemberAccess(this, Member.DECLARED)} denies
|
jaroslav@1312
|
1139 |
* access to the declared field
|
jaroslav@1312
|
1140 |
*
|
jaroslav@1312
|
1141 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@1312
|
1142 |
* ancestor of the class loader for the current class and
|
jaroslav@1312
|
1143 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@1312
|
1144 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@1312
|
1145 |
* of this class
|
jaroslav@1312
|
1146 |
*
|
jaroslav@1312
|
1147 |
* </ul>
|
jaroslav@1312
|
1148 |
*
|
jaroslav@1312
|
1149 |
* @since JDK1.1
|
jaroslav@1312
|
1150 |
*/
|
jaroslav@1312
|
1151 |
public Field getDeclaredField(String name)
|
jaroslav@1312
|
1152 |
throws SecurityException {
|
jaroslav@1312
|
1153 |
throw new SecurityException();
|
jaroslav@1312
|
1154 |
}
|
jaroslav@1312
|
1155 |
|
jaroslav@1312
|
1156 |
/**
|
jaroslav@1321
|
1157 |
* Returns an array containing {@code Constructor} objects reflecting
|
jaroslav@1321
|
1158 |
* all the public constructors of the class represented by this
|
jaroslav@1321
|
1159 |
* {@code Class} object. An array of length 0 is returned if the
|
jaroslav@1321
|
1160 |
* class has no public constructors, or if the class is an array class, or
|
jaroslav@1321
|
1161 |
* if the class reflects a primitive type or void.
|
jaroslav@1321
|
1162 |
*
|
jaroslav@1321
|
1163 |
* Note that while this method returns an array of {@code
|
jaroslav@1321
|
1164 |
* Constructor<T>} objects (that is an array of constructors from
|
jaroslav@1321
|
1165 |
* this class), the return type of this method is {@code
|
jaroslav@1321
|
1166 |
* Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
|
jaroslav@1321
|
1167 |
* might be expected. This less informative return type is
|
jaroslav@1321
|
1168 |
* necessary since after being returned from this method, the
|
jaroslav@1321
|
1169 |
* array could be modified to hold {@code Constructor} objects for
|
jaroslav@1321
|
1170 |
* different classes, which would violate the type guarantees of
|
jaroslav@1321
|
1171 |
* {@code Constructor<T>[]}.
|
jaroslav@1321
|
1172 |
*
|
jaroslav@1321
|
1173 |
* @return the array of {@code Constructor} objects representing the
|
jaroslav@1321
|
1174 |
* public constructors of this class
|
jaroslav@1321
|
1175 |
* @exception SecurityException
|
jaroslav@1321
|
1176 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@1321
|
1177 |
* following conditions is met:
|
jaroslav@1321
|
1178 |
*
|
jaroslav@1321
|
1179 |
* <ul>
|
jaroslav@1321
|
1180 |
*
|
jaroslav@1321
|
1181 |
* <li> invocation of
|
jaroslav@1321
|
1182 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@1321
|
1183 |
* s.checkMemberAccess(this, Member.PUBLIC)} denies
|
jaroslav@1321
|
1184 |
* access to the constructors within this class
|
jaroslav@1321
|
1185 |
*
|
jaroslav@1321
|
1186 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@1321
|
1187 |
* ancestor of the class loader for the current class and
|
jaroslav@1321
|
1188 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@1321
|
1189 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@1321
|
1190 |
* of this class
|
jaroslav@1321
|
1191 |
*
|
jaroslav@1321
|
1192 |
* </ul>
|
jaroslav@1321
|
1193 |
*
|
jaroslav@1321
|
1194 |
* @since JDK1.1
|
jaroslav@1321
|
1195 |
*/
|
jaroslav@1321
|
1196 |
public Constructor<?>[] getConstructors() throws SecurityException {
|
jaroslav@1321
|
1197 |
return MethodImpl.findConstructors(this, 0x01);
|
jaroslav@1321
|
1198 |
}
|
jaroslav@1321
|
1199 |
|
jaroslav@1321
|
1200 |
/**
|
jaroslav@1321
|
1201 |
* Returns a {@code Constructor} object that reflects the specified
|
jaroslav@1321
|
1202 |
* public constructor of the class represented by this {@code Class}
|
jaroslav@1321
|
1203 |
* object. The {@code parameterTypes} parameter is an array of
|
jaroslav@1321
|
1204 |
* {@code Class} objects that identify the constructor's formal
|
jaroslav@1321
|
1205 |
* parameter types, in declared order.
|
jaroslav@1321
|
1206 |
*
|
jaroslav@1321
|
1207 |
* If this {@code Class} object represents an inner class
|
jaroslav@1321
|
1208 |
* declared in a non-static context, the formal parameter types
|
jaroslav@1321
|
1209 |
* include the explicit enclosing instance as the first parameter.
|
jaroslav@1321
|
1210 |
*
|
jaroslav@1321
|
1211 |
* <p> The constructor to reflect is the public constructor of the class
|
jaroslav@1321
|
1212 |
* represented by this {@code Class} object whose formal parameter
|
jaroslav@1321
|
1213 |
* types match those specified by {@code parameterTypes}.
|
jaroslav@1321
|
1214 |
*
|
jaroslav@1321
|
1215 |
* @param parameterTypes the parameter array
|
jaroslav@1321
|
1216 |
* @return the {@code Constructor} object of the public constructor that
|
jaroslav@1321
|
1217 |
* matches the specified {@code parameterTypes}
|
jaroslav@1321
|
1218 |
* @exception NoSuchMethodException if a matching method is not found.
|
jaroslav@1321
|
1219 |
* @exception SecurityException
|
jaroslav@1321
|
1220 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@1321
|
1221 |
* following conditions is met:
|
jaroslav@1321
|
1222 |
*
|
jaroslav@1321
|
1223 |
* <ul>
|
jaroslav@1321
|
1224 |
*
|
jaroslav@1321
|
1225 |
* <li> invocation of
|
jaroslav@1321
|
1226 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@1321
|
1227 |
* s.checkMemberAccess(this, Member.PUBLIC)} denies
|
jaroslav@1321
|
1228 |
* access to the constructor
|
jaroslav@1321
|
1229 |
*
|
jaroslav@1321
|
1230 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@1321
|
1231 |
* ancestor of the class loader for the current class and
|
jaroslav@1321
|
1232 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@1321
|
1233 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@1321
|
1234 |
* of this class
|
jaroslav@1321
|
1235 |
*
|
jaroslav@1321
|
1236 |
* </ul>
|
jaroslav@1321
|
1237 |
*
|
jaroslav@1321
|
1238 |
* @since JDK1.1
|
jaroslav@1321
|
1239 |
*/
|
jaroslav@1321
|
1240 |
public Constructor<T> getConstructor(Class<?>... parameterTypes)
|
jaroslav@1321
|
1241 |
throws NoSuchMethodException, SecurityException {
|
jaroslav@1321
|
1242 |
Constructor c = MethodImpl.findConstructor(this, parameterTypes);
|
jaroslav@1321
|
1243 |
if (c == null) {
|
jaroslav@1321
|
1244 |
StringBuilder sb = new StringBuilder();
|
jaroslav@1321
|
1245 |
sb.append(getName()).append('(');
|
jaroslav@1321
|
1246 |
String sep = "";
|
jaroslav@1321
|
1247 |
for (int i = 0; i < parameterTypes.length; i++) {
|
jaroslav@1321
|
1248 |
sb.append(sep).append(parameterTypes[i].getName());
|
jaroslav@1321
|
1249 |
sep = ", ";
|
jaroslav@1321
|
1250 |
}
|
jaroslav@1321
|
1251 |
sb.append(')');
|
jaroslav@1321
|
1252 |
throw new NoSuchMethodException(sb.toString());
|
jaroslav@1321
|
1253 |
}
|
jaroslav@1321
|
1254 |
return c;
|
jaroslav@1321
|
1255 |
}
|
jaroslav@1321
|
1256 |
|
jaroslav@1321
|
1257 |
/**
|
jaroslav@1321
|
1258 |
* Returns an array of {@code Constructor} objects reflecting all the
|
jaroslav@1321
|
1259 |
* constructors declared by the class represented by this
|
jaroslav@1321
|
1260 |
* {@code Class} object. These are public, protected, default
|
jaroslav@1321
|
1261 |
* (package) access, and private constructors. The elements in the array
|
jaroslav@1321
|
1262 |
* returned are not sorted and are not in any particular order. If the
|
jaroslav@1321
|
1263 |
* class has a default constructor, it is included in the returned array.
|
jaroslav@1321
|
1264 |
* This method returns an array of length 0 if this {@code Class}
|
jaroslav@1321
|
1265 |
* object represents an interface, a primitive type, an array class, or
|
jaroslav@1321
|
1266 |
* void.
|
jaroslav@1321
|
1267 |
*
|
jaroslav@1321
|
1268 |
* <p> See <em>The Java Language Specification</em>, section 8.2.
|
jaroslav@1321
|
1269 |
*
|
jaroslav@1321
|
1270 |
* @return the array of {@code Constructor} objects representing all the
|
jaroslav@1321
|
1271 |
* declared constructors of this class
|
jaroslav@1321
|
1272 |
* @exception SecurityException
|
jaroslav@1321
|
1273 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@1321
|
1274 |
* following conditions is met:
|
jaroslav@1321
|
1275 |
*
|
jaroslav@1321
|
1276 |
* <ul>
|
jaroslav@1321
|
1277 |
*
|
jaroslav@1321
|
1278 |
* <li> invocation of
|
jaroslav@1321
|
1279 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@1321
|
1280 |
* s.checkMemberAccess(this, Member.DECLARED)} denies
|
jaroslav@1321
|
1281 |
* access to the declared constructors within this class
|
jaroslav@1321
|
1282 |
*
|
jaroslav@1321
|
1283 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@1321
|
1284 |
* ancestor of the class loader for the current class and
|
jaroslav@1321
|
1285 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@1321
|
1286 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@1321
|
1287 |
* of this class
|
jaroslav@1321
|
1288 |
*
|
jaroslav@1321
|
1289 |
* </ul>
|
jaroslav@1321
|
1290 |
*
|
jaroslav@1321
|
1291 |
* @since JDK1.1
|
jaroslav@1321
|
1292 |
*/
|
jaroslav@1321
|
1293 |
public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
|
jaroslav@1321
|
1294 |
throw new SecurityException();
|
jaroslav@1321
|
1295 |
}
|
jaroslav@1321
|
1296 |
/**
|
jaroslav@1321
|
1297 |
* Returns a {@code Constructor} object that reflects the specified
|
jaroslav@1321
|
1298 |
* constructor of the class or interface represented by this
|
jaroslav@1321
|
1299 |
* {@code Class} object. The {@code parameterTypes} parameter is
|
jaroslav@1321
|
1300 |
* an array of {@code Class} objects that identify the constructor's
|
jaroslav@1321
|
1301 |
* formal parameter types, in declared order.
|
jaroslav@1321
|
1302 |
*
|
jaroslav@1321
|
1303 |
* If this {@code Class} object represents an inner class
|
jaroslav@1321
|
1304 |
* declared in a non-static context, the formal parameter types
|
jaroslav@1321
|
1305 |
* include the explicit enclosing instance as the first parameter.
|
jaroslav@1321
|
1306 |
*
|
jaroslav@1321
|
1307 |
* @param parameterTypes the parameter array
|
jaroslav@1321
|
1308 |
* @return The {@code Constructor} object for the constructor with the
|
jaroslav@1321
|
1309 |
* specified parameter list
|
jaroslav@1321
|
1310 |
* @exception NoSuchMethodException if a matching method is not found.
|
jaroslav@1321
|
1311 |
* @exception SecurityException
|
jaroslav@1321
|
1312 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@1321
|
1313 |
* following conditions is met:
|
jaroslav@1321
|
1314 |
*
|
jaroslav@1321
|
1315 |
* <ul>
|
jaroslav@1321
|
1316 |
*
|
jaroslav@1321
|
1317 |
* <li> invocation of
|
jaroslav@1321
|
1318 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@1321
|
1319 |
* s.checkMemberAccess(this, Member.DECLARED)} denies
|
jaroslav@1321
|
1320 |
* access to the declared constructor
|
jaroslav@1321
|
1321 |
*
|
jaroslav@1321
|
1322 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@1321
|
1323 |
* ancestor of the class loader for the current class and
|
jaroslav@1321
|
1324 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@1321
|
1325 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@1321
|
1326 |
* of this class
|
jaroslav@1321
|
1327 |
*
|
jaroslav@1321
|
1328 |
* </ul>
|
jaroslav@1321
|
1329 |
*
|
jaroslav@1321
|
1330 |
* @since JDK1.1
|
jaroslav@1321
|
1331 |
*/
|
jaroslav@1321
|
1332 |
public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
|
jaroslav@1321
|
1333 |
throws NoSuchMethodException, SecurityException {
|
jaroslav@1321
|
1334 |
return getConstructor(parameterTypes);
|
jaroslav@1321
|
1335 |
}
|
jaroslav@1321
|
1336 |
|
jaroslav@1321
|
1337 |
|
jaroslav@1321
|
1338 |
/**
|
jaroslav@56
|
1339 |
* Character.isDigit answers {@code true} to some non-ascii
|
jaroslav@56
|
1340 |
* digits. This one does not.
|
jaroslav@56
|
1341 |
*/
|
jaroslav@56
|
1342 |
private static boolean isAsciiDigit(char c) {
|
jaroslav@56
|
1343 |
return '0' <= c && c <= '9';
|
jaroslav@56
|
1344 |
}
|
jaroslav@56
|
1345 |
|
jaroslav@56
|
1346 |
/**
|
jaroslav@56
|
1347 |
* Returns the canonical name of the underlying class as
|
jaroslav@56
|
1348 |
* defined by the Java Language Specification. Returns null if
|
jaroslav@56
|
1349 |
* the underlying class does not have a canonical name (i.e., if
|
jaroslav@56
|
1350 |
* it is a local or anonymous class or an array whose component
|
jaroslav@56
|
1351 |
* type does not have a canonical name).
|
jaroslav@56
|
1352 |
* @return the canonical name of the underlying class if it exists, and
|
jaroslav@56
|
1353 |
* {@code null} otherwise.
|
jaroslav@56
|
1354 |
* @since 1.5
|
jaroslav@56
|
1355 |
*/
|
jaroslav@56
|
1356 |
public String getCanonicalName() {
|
jaroslav@228
|
1357 |
if (isArray()) {
|
jaroslav@228
|
1358 |
String canonicalName = getComponentType().getCanonicalName();
|
jaroslav@228
|
1359 |
if (canonicalName != null)
|
jaroslav@228
|
1360 |
return canonicalName + "[]";
|
jaroslav@228
|
1361 |
else
|
jaroslav@228
|
1362 |
return null;
|
jaroslav@228
|
1363 |
}
|
jaroslav@1898
|
1364 |
if (isLocalOrAnonymousClass())
|
jaroslav@1898
|
1365 |
return null;
|
jaroslav@65
|
1366 |
// Class<?> enclosingClass = getEnclosingClass();
|
jaroslav@228
|
1367 |
Class<?> enclosingClass = null;
|
jaroslav@228
|
1368 |
if (enclosingClass == null) { // top level class
|
jaroslav@228
|
1369 |
return getName();
|
jaroslav@228
|
1370 |
} else {
|
jaroslav@228
|
1371 |
String enclosingName = enclosingClass.getCanonicalName();
|
jaroslav@228
|
1372 |
if (enclosingName == null)
|
jaroslav@228
|
1373 |
return null;
|
jaroslav@228
|
1374 |
return enclosingName + "." + getSimpleName();
|
jaroslav@228
|
1375 |
}
|
jaroslav@56
|
1376 |
}
|
jaroslav@56
|
1377 |
|
jaroslav@56
|
1378 |
/**
|
jaroslav@1898
|
1379 |
* Returns {@code true} if and only if the underlying class is an anonymous
|
jaroslav@1898
|
1380 |
* class.
|
jaroslav@1898
|
1381 |
*
|
jaroslav@1898
|
1382 |
* @return {@code true} if and only if this class is an anonymous class.
|
jaroslav@1898
|
1383 |
* @since 1.5
|
jaroslav@1898
|
1384 |
*/
|
jaroslav@1898
|
1385 |
public boolean isAnonymousClass() {
|
jaroslav@1898
|
1386 |
return "".equals(getSimpleName());
|
jaroslav@1898
|
1387 |
}
|
jaroslav@1898
|
1388 |
|
jaroslav@1898
|
1389 |
/**
|
jaroslav@1898
|
1390 |
* Returns {@code true} if and only if the underlying class is a local
|
jaroslav@1898
|
1391 |
* class.
|
jaroslav@1898
|
1392 |
*
|
jaroslav@1898
|
1393 |
* @return {@code true} if and only if this class is a local class.
|
jaroslav@1898
|
1394 |
* @since 1.5
|
jaroslav@1898
|
1395 |
*/
|
jaroslav@1898
|
1396 |
public boolean isLocalClass() {
|
jaroslav@1898
|
1397 |
return isLocalOrAnonymousClass() && !isAnonymousClass();
|
jaroslav@1898
|
1398 |
}
|
jaroslav@1898
|
1399 |
|
jaroslav@1898
|
1400 |
/**
|
jaroslav@1898
|
1401 |
* Returns {@code true} if and only if the underlying class is a member
|
jaroslav@1898
|
1402 |
* class.
|
jaroslav@1898
|
1403 |
*
|
jaroslav@1898
|
1404 |
* @return {@code true} if and only if this class is a member class.
|
jaroslav@1898
|
1405 |
* @since 1.5
|
jaroslav@1898
|
1406 |
*/
|
jaroslav@1898
|
1407 |
public boolean isMemberClass() {
|
jaroslav@1898
|
1408 |
return getSimpleBinaryName() != null && !isLocalOrAnonymousClass();
|
jaroslav@1898
|
1409 |
}
|
jaroslav@1898
|
1410 |
|
jaroslav@1898
|
1411 |
/**
|
jaroslav@1898
|
1412 |
* Returns {@code true} if this is a local class or an anonymous class.
|
jaroslav@1898
|
1413 |
* Returns {@code false} otherwise.
|
jaroslav@1898
|
1414 |
*/
|
jaroslav@1898
|
1415 |
private boolean isLocalOrAnonymousClass() {
|
jaroslav@1898
|
1416 |
return (getAccess() & 0x10000) != 0;
|
jaroslav@1898
|
1417 |
}
|
jaroslav@1898
|
1418 |
|
jaroslav@1898
|
1419 |
/**
|
jaroslav@56
|
1420 |
* Finds a resource with a given name. The rules for searching resources
|
jaroslav@56
|
1421 |
* associated with a given class are implemented by the defining
|
jaroslav@56
|
1422 |
* {@linkplain ClassLoader class loader} of the class. This method
|
jaroslav@56
|
1423 |
* delegates to this object's class loader. If this object was loaded by
|
jaroslav@56
|
1424 |
* the bootstrap class loader, the method delegates to {@link
|
jaroslav@56
|
1425 |
* ClassLoader#getSystemResourceAsStream}.
|
jaroslav@56
|
1426 |
*
|
jaroslav@56
|
1427 |
* <p> Before delegation, an absolute resource name is constructed from the
|
jaroslav@56
|
1428 |
* given resource name using this algorithm:
|
jaroslav@56
|
1429 |
*
|
jaroslav@56
|
1430 |
* <ul>
|
jaroslav@56
|
1431 |
*
|
jaroslav@56
|
1432 |
* <li> If the {@code name} begins with a {@code '/'}
|
jaroslav@56
|
1433 |
* (<tt>'\u002f'</tt>), then the absolute name of the resource is the
|
jaroslav@56
|
1434 |
* portion of the {@code name} following the {@code '/'}.
|
jaroslav@56
|
1435 |
*
|
jaroslav@56
|
1436 |
* <li> Otherwise, the absolute name is of the following form:
|
jaroslav@56
|
1437 |
*
|
jaroslav@56
|
1438 |
* <blockquote>
|
jaroslav@56
|
1439 |
* {@code modified_package_name/name}
|
jaroslav@56
|
1440 |
* </blockquote>
|
jaroslav@56
|
1441 |
*
|
jaroslav@56
|
1442 |
* <p> Where the {@code modified_package_name} is the package name of this
|
jaroslav@56
|
1443 |
* object with {@code '/'} substituted for {@code '.'}
|
jaroslav@56
|
1444 |
* (<tt>'\u002e'</tt>).
|
jaroslav@56
|
1445 |
*
|
jaroslav@56
|
1446 |
* </ul>
|
jaroslav@56
|
1447 |
*
|
jaroslav@56
|
1448 |
* @param name name of the desired resource
|
jaroslav@56
|
1449 |
* @return A {@link java.io.InputStream} object or {@code null} if
|
jaroslav@56
|
1450 |
* no resource with this name is found
|
jaroslav@56
|
1451 |
* @throws NullPointerException If {@code name} is {@code null}
|
jaroslav@56
|
1452 |
* @since JDK1.1
|
jaroslav@56
|
1453 |
*/
|
jaroslav@122
|
1454 |
public InputStream getResourceAsStream(String name) {
|
jaroslav@122
|
1455 |
name = resolveName(name);
|
jaroslav@1375
|
1456 |
byte[] arr = ClassLoader.getResourceAsStream0(name, 0);
|
jaroslav@424
|
1457 |
return arr == null ? null : new ByteArrayInputStream(arr);
|
jaroslav@424
|
1458 |
}
|
jaroslav@1375
|
1459 |
|
jaroslav@56
|
1460 |
/**
|
jaroslav@56
|
1461 |
* Finds a resource with a given name. The rules for searching resources
|
jaroslav@56
|
1462 |
* associated with a given class are implemented by the defining
|
jaroslav@56
|
1463 |
* {@linkplain ClassLoader class loader} of the class. This method
|
jaroslav@56
|
1464 |
* delegates to this object's class loader. If this object was loaded by
|
jaroslav@56
|
1465 |
* the bootstrap class loader, the method delegates to {@link
|
jaroslav@56
|
1466 |
* ClassLoader#getSystemResource}.
|
jaroslav@56
|
1467 |
*
|
jaroslav@56
|
1468 |
* <p> Before delegation, an absolute resource name is constructed from the
|
jaroslav@56
|
1469 |
* given resource name using this algorithm:
|
jaroslav@56
|
1470 |
*
|
jaroslav@56
|
1471 |
* <ul>
|
jaroslav@56
|
1472 |
*
|
jaroslav@56
|
1473 |
* <li> If the {@code name} begins with a {@code '/'}
|
jaroslav@56
|
1474 |
* (<tt>'\u002f'</tt>), then the absolute name of the resource is the
|
jaroslav@56
|
1475 |
* portion of the {@code name} following the {@code '/'}.
|
jaroslav@56
|
1476 |
*
|
jaroslav@56
|
1477 |
* <li> Otherwise, the absolute name is of the following form:
|
jaroslav@56
|
1478 |
*
|
jaroslav@56
|
1479 |
* <blockquote>
|
jaroslav@56
|
1480 |
* {@code modified_package_name/name}
|
jaroslav@56
|
1481 |
* </blockquote>
|
jaroslav@56
|
1482 |
*
|
jaroslav@56
|
1483 |
* <p> Where the {@code modified_package_name} is the package name of this
|
jaroslav@56
|
1484 |
* object with {@code '/'} substituted for {@code '.'}
|
jaroslav@56
|
1485 |
* (<tt>'\u002e'</tt>).
|
jaroslav@56
|
1486 |
*
|
jaroslav@56
|
1487 |
* </ul>
|
jaroslav@56
|
1488 |
*
|
jaroslav@56
|
1489 |
* @param name name of the desired resource
|
jaroslav@56
|
1490 |
* @return A {@link java.net.URL} object or {@code null} if no
|
jaroslav@56
|
1491 |
* resource with this name is found
|
jaroslav@56
|
1492 |
* @since JDK1.1
|
jaroslav@56
|
1493 |
*/
|
jaroslav@122
|
1494 |
public java.net.URL getResource(String name) {
|
jaroslav@1717
|
1495 |
name = resolveName(name);
|
jaroslav@1717
|
1496 |
byte[] arr = ClassLoader.getResourceAsStream0(name, 0);
|
jaroslav@1717
|
1497 |
return arr == null ? null : newResourceURL(name, arr);
|
jaroslav@1375
|
1498 |
}
|
jaroslav@1375
|
1499 |
|
jaroslav@1717
|
1500 |
static URL newResourceURL(String name, byte[] arr) {
|
jaroslav@1717
|
1501 |
return newResourceURL0(URL.class, "res:/" + name, arr);
|
jaroslav@122
|
1502 |
}
|
jaroslav@576
|
1503 |
|
jaroslav@1717
|
1504 |
@JavaScriptBody(args = { "url", "spec", "arr" }, body =
|
jaroslav@576
|
1505 |
"var u = url.cnstr(true);\n"
|
jaroslav@1717
|
1506 |
+ "u.constructor.cons__VLjava_lang_String_2_3B.call(u, spec, arr);\n"
|
jaroslav@576
|
1507 |
+ "return u;"
|
jaroslav@576
|
1508 |
)
|
jaroslav@1717
|
1509 |
private static native URL newResourceURL0(Class<URL> url, String spec, byte[] arr);
|
jaroslav@56
|
1510 |
|
jaroslav@122
|
1511 |
/**
|
jaroslav@122
|
1512 |
* Add a package name prefix if the name is not absolute Remove leading "/"
|
jaroslav@122
|
1513 |
* if name is absolute
|
jaroslav@122
|
1514 |
*/
|
jaroslav@122
|
1515 |
private String resolveName(String name) {
|
jaroslav@122
|
1516 |
if (name == null) {
|
jaroslav@122
|
1517 |
return name;
|
jaroslav@122
|
1518 |
}
|
jaroslav@122
|
1519 |
if (!name.startsWith("/")) {
|
jaroslav@122
|
1520 |
Class<?> c = this;
|
jaroslav@122
|
1521 |
while (c.isArray()) {
|
jaroslav@122
|
1522 |
c = c.getComponentType();
|
jaroslav@122
|
1523 |
}
|
jaroslav@122
|
1524 |
String baseName = c.getName();
|
jaroslav@122
|
1525 |
int index = baseName.lastIndexOf('.');
|
jaroslav@122
|
1526 |
if (index != -1) {
|
jaroslav@122
|
1527 |
name = baseName.substring(0, index).replace('.', '/')
|
jaroslav@122
|
1528 |
+"/"+name;
|
jaroslav@122
|
1529 |
}
|
jaroslav@122
|
1530 |
} else {
|
jaroslav@122
|
1531 |
name = name.substring(1);
|
jaroslav@122
|
1532 |
}
|
jaroslav@122
|
1533 |
return name;
|
jaroslav@122
|
1534 |
}
|
jaroslav@122
|
1535 |
|
jaroslav@122
|
1536 |
/**
|
jaroslav@122
|
1537 |
* Returns the class loader for the class. Some implementations may use
|
jaroslav@122
|
1538 |
* null to represent the bootstrap class loader. This method will return
|
jaroslav@122
|
1539 |
* null in such implementations if this class was loaded by the bootstrap
|
jaroslav@122
|
1540 |
* class loader.
|
jaroslav@122
|
1541 |
*
|
jaroslav@122
|
1542 |
* <p> If a security manager is present, and the caller's class loader is
|
jaroslav@122
|
1543 |
* not null and the caller's class loader is not the same as or an ancestor of
|
jaroslav@122
|
1544 |
* the class loader for the class whose class loader is requested, then
|
jaroslav@122
|
1545 |
* this method calls the security manager's {@code checkPermission}
|
jaroslav@122
|
1546 |
* method with a {@code RuntimePermission("getClassLoader")}
|
jaroslav@122
|
1547 |
* permission to ensure it's ok to access the class loader for the class.
|
jaroslav@122
|
1548 |
*
|
jaroslav@122
|
1549 |
* <p>If this object
|
jaroslav@122
|
1550 |
* represents a primitive type or void, null is returned.
|
jaroslav@122
|
1551 |
*
|
jaroslav@122
|
1552 |
* @return the class loader that loaded the class or interface
|
jaroslav@122
|
1553 |
* represented by this object.
|
jaroslav@122
|
1554 |
* @throws SecurityException
|
jaroslav@122
|
1555 |
* if a security manager exists and its
|
jaroslav@122
|
1556 |
* {@code checkPermission} method denies
|
jaroslav@122
|
1557 |
* access to the class loader for the class.
|
jaroslav@122
|
1558 |
* @see java.lang.ClassLoader
|
jaroslav@122
|
1559 |
* @see SecurityManager#checkPermission
|
jaroslav@122
|
1560 |
* @see java.lang.RuntimePermission
|
jaroslav@122
|
1561 |
*/
|
jaroslav@122
|
1562 |
public ClassLoader getClassLoader() {
|
jaroslav@1333
|
1563 |
return ClassLoader.getSystemClassLoader();
|
jaroslav@122
|
1564 |
}
|
jaroslav@604
|
1565 |
|
jaroslav@604
|
1566 |
/**
|
jaroslav@604
|
1567 |
* Determines the interfaces implemented by the class or interface
|
jaroslav@604
|
1568 |
* represented by this object.
|
jaroslav@604
|
1569 |
*
|
jaroslav@604
|
1570 |
* <p> If this object represents a class, the return value is an array
|
jaroslav@604
|
1571 |
* containing objects representing all interfaces implemented by the
|
jaroslav@604
|
1572 |
* class. The order of the interface objects in the array corresponds to
|
jaroslav@604
|
1573 |
* the order of the interface names in the {@code implements} clause
|
jaroslav@604
|
1574 |
* of the declaration of the class represented by this object. For
|
jaroslav@604
|
1575 |
* example, given the declaration:
|
jaroslav@604
|
1576 |
* <blockquote>
|
jaroslav@604
|
1577 |
* {@code class Shimmer implements FloorWax, DessertTopping { ... }}
|
jaroslav@604
|
1578 |
* </blockquote>
|
jaroslav@604
|
1579 |
* suppose the value of {@code s} is an instance of
|
jaroslav@604
|
1580 |
* {@code Shimmer}; the value of the expression:
|
jaroslav@604
|
1581 |
* <blockquote>
|
jaroslav@604
|
1582 |
* {@code s.getClass().getInterfaces()[0]}
|
jaroslav@604
|
1583 |
* </blockquote>
|
jaroslav@604
|
1584 |
* is the {@code Class} object that represents interface
|
jaroslav@604
|
1585 |
* {@code FloorWax}; and the value of:
|
jaroslav@604
|
1586 |
* <blockquote>
|
jaroslav@604
|
1587 |
* {@code s.getClass().getInterfaces()[1]}
|
jaroslav@604
|
1588 |
* </blockquote>
|
jaroslav@604
|
1589 |
* is the {@code Class} object that represents interface
|
jaroslav@604
|
1590 |
* {@code DessertTopping}.
|
jaroslav@604
|
1591 |
*
|
jaroslav@604
|
1592 |
* <p> If this object represents an interface, the array contains objects
|
jaroslav@604
|
1593 |
* representing all interfaces extended by the interface. The order of the
|
jaroslav@604
|
1594 |
* interface objects in the array corresponds to the order of the interface
|
jaroslav@604
|
1595 |
* names in the {@code extends} clause of the declaration of the
|
jaroslav@604
|
1596 |
* interface represented by this object.
|
jaroslav@604
|
1597 |
*
|
jaroslav@604
|
1598 |
* <p> If this object represents a class or interface that implements no
|
jaroslav@604
|
1599 |
* interfaces, the method returns an array of length 0.
|
jaroslav@604
|
1600 |
*
|
jaroslav@604
|
1601 |
* <p> If this object represents a primitive type or void, the method
|
jaroslav@604
|
1602 |
* returns an array of length 0.
|
jaroslav@604
|
1603 |
*
|
jaroslav@604
|
1604 |
* @return an array of interfaces implemented by this class.
|
jaroslav@604
|
1605 |
*/
|
jaroslav@1727
|
1606 |
@JavaScriptBody(args = { }, body = "return this.interfaces();")
|
jaroslav@604
|
1607 |
public native Class<?>[] getInterfaces();
|
jaroslav@122
|
1608 |
|
jaroslav@122
|
1609 |
/**
|
jaroslav@122
|
1610 |
* Returns the {@code Class} representing the component type of an
|
jaroslav@122
|
1611 |
* array. If this class does not represent an array class this method
|
jaroslav@122
|
1612 |
* returns null.
|
jaroslav@122
|
1613 |
*
|
jaroslav@122
|
1614 |
* @return the {@code Class} representing the component type of this
|
jaroslav@122
|
1615 |
* class if this class is an array
|
jaroslav@122
|
1616 |
* @see java.lang.reflect.Array
|
jaroslav@122
|
1617 |
* @since JDK1.1
|
jaroslav@122
|
1618 |
*/
|
jaroslav@228
|
1619 |
public Class<?> getComponentType() {
|
jaroslav@451
|
1620 |
if (isArray()) {
|
jaroslav@451
|
1621 |
try {
|
jaroslav@1532
|
1622 |
Class<?> c = getComponentTypeByFnc();
|
jaroslav@1532
|
1623 |
return c != null ? c : getComponentType0();
|
jaroslav@451
|
1624 |
} catch (ClassNotFoundException cnfe) {
|
jaroslav@451
|
1625 |
throw new IllegalStateException(cnfe);
|
jaroslav@451
|
1626 |
}
|
jaroslav@451
|
1627 |
}
|
jaroslav@228
|
1628 |
return null;
|
jaroslav@228
|
1629 |
}
|
jaroslav@1532
|
1630 |
|
jaroslav@1532
|
1631 |
@JavaScriptBody(args = { }, body =
|
jaroslav@1532
|
1632 |
"if (this.fnc) {\n"
|
jaroslav@1532
|
1633 |
+ " var c = this.fnc(false).constructor.$class;\n"
|
jaroslav@1532
|
1634 |
// + " java.lang.System.out.println('will call: ' + (!!this.fnc) + ' res: ' + c.jvmName);\n"
|
jaroslav@1532
|
1635 |
+ " if (c) return c;\n"
|
jaroslav@1532
|
1636 |
+ "}\n"
|
jaroslav@1532
|
1637 |
+ "return null;"
|
jaroslav@1532
|
1638 |
)
|
jaroslav@1532
|
1639 |
private native Class<?> getComponentTypeByFnc();
|
jaroslav@56
|
1640 |
|
jaroslav@451
|
1641 |
private Class<?> getComponentType0() throws ClassNotFoundException {
|
jaroslav@451
|
1642 |
String n = getName().substring(1);
|
jaroslav@451
|
1643 |
switch (n.charAt(0)) {
|
jaroslav@451
|
1644 |
case 'L':
|
jaroslav@451
|
1645 |
n = n.substring(1, n.length() - 1);
|
jaroslav@451
|
1646 |
return Class.forName(n);
|
jaroslav@451
|
1647 |
case 'I':
|
jaroslav@451
|
1648 |
return Integer.TYPE;
|
jaroslav@451
|
1649 |
case 'J':
|
jaroslav@451
|
1650 |
return Long.TYPE;
|
jaroslav@451
|
1651 |
case 'D':
|
jaroslav@451
|
1652 |
return Double.TYPE;
|
jaroslav@451
|
1653 |
case 'F':
|
jaroslav@451
|
1654 |
return Float.TYPE;
|
jaroslav@451
|
1655 |
case 'B':
|
jaroslav@451
|
1656 |
return Byte.TYPE;
|
jaroslav@451
|
1657 |
case 'Z':
|
jaroslav@451
|
1658 |
return Boolean.TYPE;
|
jaroslav@451
|
1659 |
case 'S':
|
jaroslav@451
|
1660 |
return Short.TYPE;
|
jaroslav@451
|
1661 |
case 'V':
|
jaroslav@451
|
1662 |
return Void.TYPE;
|
jaroslav@451
|
1663 |
case 'C':
|
jaroslav@451
|
1664 |
return Character.TYPE;
|
jaroslav@451
|
1665 |
case '[':
|
jaroslav@1532
|
1666 |
return defineArray(n, null);
|
jaroslav@451
|
1667 |
default:
|
jaroslav@451
|
1668 |
throw new ClassNotFoundException("Unknown component type of " + getName());
|
jaroslav@451
|
1669 |
}
|
jaroslav@451
|
1670 |
}
|
jaroslav@451
|
1671 |
|
jaroslav@1532
|
1672 |
@JavaScriptBody(args = { "sig", "fnc" }, body =
|
jaroslav@933
|
1673 |
"if (!sig) sig = '[Ljava/lang/Object;';\n" +
|
jaroslav@450
|
1674 |
"var c = Array[sig];\n" +
|
jaroslav@1532
|
1675 |
"if (!c) {\n" +
|
jaroslav@1532
|
1676 |
" c = vm.java_lang_Class(true);\n" +
|
jaroslav@1702
|
1677 |
" Object.defineProperty(c, 'jvmName', { 'configurable': true, 'writable': true, 'value': sig });\n" +
|
jaroslav@1702
|
1678 |
" Object.defineProperty(c, 'superclass', { 'configurable': true, 'writable': true, 'value' : vm.java_lang_Object(false).$class });\n" +
|
jaroslav@1702
|
1679 |
" Object.defineProperty(c, 'array', { 'configurable': true, 'writable': true, 'value': true });\n" +
|
jaroslav@1532
|
1680 |
" Array[sig] = c;\n" +
|
jaroslav@1532
|
1681 |
"}\n" +
|
jaroslav@1702
|
1682 |
"if (!c.fnc) Object.defineProperty(c, 'fnc', { 'configurable': true, 'writable': true, 'value' : fnc });\n" +
|
jaroslav@450
|
1683 |
"return c;"
|
jaroslav@450
|
1684 |
)
|
jaroslav@1532
|
1685 |
private static native Class<?> defineArray(String sig, Object fnc);
|
jaroslav@450
|
1686 |
|
jaroslav@56
|
1687 |
/**
|
jaroslav@56
|
1688 |
* Returns true if and only if this class was declared as an enum in the
|
jaroslav@56
|
1689 |
* source code.
|
jaroslav@56
|
1690 |
*
|
jaroslav@56
|
1691 |
* @return true if and only if this class was declared as an enum in the
|
jaroslav@56
|
1692 |
* source code
|
jaroslav@56
|
1693 |
* @since 1.5
|
jaroslav@56
|
1694 |
*/
|
jaroslav@56
|
1695 |
public boolean isEnum() {
|
jaroslav@56
|
1696 |
// An enum must both directly extend java.lang.Enum and have
|
jaroslav@56
|
1697 |
// the ENUM bit set; classes for specialized enum constants
|
jaroslav@56
|
1698 |
// don't do the former.
|
jaroslav@56
|
1699 |
return (this.getModifiers() & ENUM) != 0 &&
|
jaroslav@1623
|
1700 |
this.getSuperclass() == java.lang.Enum.class;
|
jaroslav@56
|
1701 |
}
|
jaroslav@56
|
1702 |
|
jaroslav@56
|
1703 |
/**
|
jaroslav@56
|
1704 |
* Casts an object to the class or interface represented
|
jaroslav@56
|
1705 |
* by this {@code Class} object.
|
jaroslav@56
|
1706 |
*
|
jaroslav@56
|
1707 |
* @param obj the object to be cast
|
jaroslav@56
|
1708 |
* @return the object after casting, or null if obj is null
|
jaroslav@56
|
1709 |
*
|
jaroslav@56
|
1710 |
* @throws ClassCastException if the object is not
|
jaroslav@56
|
1711 |
* null and is not assignable to the type T.
|
jaroslav@56
|
1712 |
*
|
jaroslav@56
|
1713 |
* @since 1.5
|
jaroslav@56
|
1714 |
*/
|
jaroslav@56
|
1715 |
public T cast(Object obj) {
|
jaroslav@56
|
1716 |
if (obj != null && !isInstance(obj))
|
jaroslav@56
|
1717 |
throw new ClassCastException(cannotCastMsg(obj));
|
jaroslav@56
|
1718 |
return (T) obj;
|
jaroslav@56
|
1719 |
}
|
jaroslav@56
|
1720 |
|
jaroslav@56
|
1721 |
private String cannotCastMsg(Object obj) {
|
jaroslav@56
|
1722 |
return "Cannot cast " + obj.getClass().getName() + " to " + getName();
|
jaroslav@56
|
1723 |
}
|
jaroslav@56
|
1724 |
|
jaroslav@56
|
1725 |
/**
|
jaroslav@56
|
1726 |
* Casts this {@code Class} object to represent a subclass of the class
|
jaroslav@56
|
1727 |
* represented by the specified class object. Checks that that the cast
|
jaroslav@56
|
1728 |
* is valid, and throws a {@code ClassCastException} if it is not. If
|
jaroslav@56
|
1729 |
* this method succeeds, it always returns a reference to this class object.
|
jaroslav@56
|
1730 |
*
|
jaroslav@56
|
1731 |
* <p>This method is useful when a client needs to "narrow" the type of
|
jaroslav@56
|
1732 |
* a {@code Class} object to pass it to an API that restricts the
|
jaroslav@56
|
1733 |
* {@code Class} objects that it is willing to accept. A cast would
|
jaroslav@56
|
1734 |
* generate a compile-time warning, as the correctness of the cast
|
jaroslav@56
|
1735 |
* could not be checked at runtime (because generic types are implemented
|
jaroslav@56
|
1736 |
* by erasure).
|
jaroslav@56
|
1737 |
*
|
jaroslav@56
|
1738 |
* @return this {@code Class} object, cast to represent a subclass of
|
jaroslav@56
|
1739 |
* the specified class object.
|
jaroslav@56
|
1740 |
* @throws ClassCastException if this {@code Class} object does not
|
jaroslav@56
|
1741 |
* represent a subclass of the specified class (here "subclass" includes
|
jaroslav@56
|
1742 |
* the class itself).
|
jaroslav@56
|
1743 |
* @since 1.5
|
jaroslav@56
|
1744 |
*/
|
jaroslav@56
|
1745 |
public <U> Class<? extends U> asSubclass(Class<U> clazz) {
|
jaroslav@56
|
1746 |
if (clazz.isAssignableFrom(this))
|
jaroslav@56
|
1747 |
return (Class<? extends U>) this;
|
jaroslav@56
|
1748 |
else
|
jaroslav@56
|
1749 |
throw new ClassCastException(this.toString());
|
jaroslav@56
|
1750 |
}
|
jaroslav@56
|
1751 |
|
jaroslav@443
|
1752 |
@JavaScriptBody(args = { "ac" },
|
jaroslav@235
|
1753 |
body =
|
jaroslav@1370
|
1754 |
"if (this.anno) {\n"
|
jaroslav@1370
|
1755 |
+ " var r = this.anno['L' + ac.jvmName + ';'];\n"
|
jaroslav@1370
|
1756 |
+ " if (typeof r === 'undefined') r = null;\n"
|
jaroslav@1370
|
1757 |
+ " return r;\n"
|
jaroslav@1370
|
1758 |
+ "} else return null;\n"
|
jaroslav@235
|
1759 |
)
|
jaroslav@235
|
1760 |
private Object getAnnotationData(Class<?> annotationClass) {
|
jaroslav@235
|
1761 |
throw new UnsupportedOperationException();
|
jaroslav@235
|
1762 |
}
|
jaroslav@237
|
1763 |
/**
|
jaroslav@237
|
1764 |
* @throws NullPointerException {@inheritDoc}
|
jaroslav@237
|
1765 |
* @since 1.5
|
jaroslav@237
|
1766 |
*/
|
jaroslav@56
|
1767 |
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
|
jaroslav@235
|
1768 |
Object data = getAnnotationData(annotationClass);
|
jaroslav@235
|
1769 |
return data == null ? null : AnnotationImpl.create(annotationClass, data);
|
jaroslav@56
|
1770 |
}
|
jaroslav@56
|
1771 |
|
jaroslav@56
|
1772 |
/**
|
jaroslav@56
|
1773 |
* @throws NullPointerException {@inheritDoc}
|
jaroslav@56
|
1774 |
* @since 1.5
|
jaroslav@56
|
1775 |
*/
|
jaroslav@443
|
1776 |
@JavaScriptBody(args = { "ac" },
|
jaroslav@443
|
1777 |
body = "if (this.anno && this.anno['L' + ac.jvmName + ';']) { return true; }"
|
jaroslav@235
|
1778 |
+ "else return false;"
|
jaroslav@235
|
1779 |
)
|
jaroslav@56
|
1780 |
public boolean isAnnotationPresent(
|
jaroslav@56
|
1781 |
Class<? extends Annotation> annotationClass) {
|
jaroslav@56
|
1782 |
if (annotationClass == null)
|
jaroslav@56
|
1783 |
throw new NullPointerException();
|
jaroslav@56
|
1784 |
|
jaroslav@56
|
1785 |
return getAnnotation(annotationClass) != null;
|
jaroslav@56
|
1786 |
}
|
jaroslav@56
|
1787 |
|
jaroslav@1901
|
1788 |
@JavaScriptBody(args = {}, body = "return this.anno ? this.anno : null;")
|
jaroslav@1901
|
1789 |
private native Object getAnnotationData();
|
jaroslav@56
|
1790 |
|
jaroslav@56
|
1791 |
/**
|
jaroslav@56
|
1792 |
* @since 1.5
|
jaroslav@56
|
1793 |
*/
|
jaroslav@56
|
1794 |
public Annotation[] getAnnotations() {
|
jaroslav@238
|
1795 |
Object data = getAnnotationData();
|
jaroslav@238
|
1796 |
return data == null ? new Annotation[0] : AnnotationImpl.create(data);
|
jaroslav@56
|
1797 |
}
|
jaroslav@56
|
1798 |
|
jaroslav@56
|
1799 |
/**
|
jaroslav@56
|
1800 |
* @since 1.5
|
jaroslav@56
|
1801 |
*/
|
jaroslav@56
|
1802 |
public Annotation[] getDeclaredAnnotations() {
|
jaroslav@1899
|
1803 |
return getAnnotations();
|
jaroslav@56
|
1804 |
}
|
jaroslav@56
|
1805 |
|
jaroslav@353
|
1806 |
@JavaScriptBody(args = "type", body = ""
|
jaroslav@353
|
1807 |
+ "var c = vm.java_lang_Class(true);"
|
jaroslav@353
|
1808 |
+ "c.jvmName = type;"
|
jaroslav@354
|
1809 |
+ "c.primitive = true;"
|
jaroslav@353
|
1810 |
+ "return c;"
|
jaroslav@353
|
1811 |
)
|
jaroslav@353
|
1812 |
native static Class getPrimitiveClass(String type);
|
jaroslav@88
|
1813 |
|
jaroslav@517
|
1814 |
@JavaScriptBody(args = {}, body =
|
jaroslav@1586
|
1815 |
"return this['desiredAssertionStatus'] ? this['desiredAssertionStatus'] : false;"
|
jaroslav@517
|
1816 |
)
|
jaroslav@517
|
1817 |
public native boolean desiredAssertionStatus();
|
jaroslav@1607
|
1818 |
|
jaroslav@1607
|
1819 |
public boolean equals(Object obj) {
|
jaroslav@1607
|
1820 |
if (isPrimitive() && obj instanceof Class) {
|
jaroslav@1607
|
1821 |
Class c = ((Class)obj);
|
jaroslav@1607
|
1822 |
return c.isPrimitive() && getName().equals(c.getName());
|
jaroslav@1607
|
1823 |
}
|
jaroslav@1607
|
1824 |
return super.equals(obj);
|
jaroslav@1607
|
1825 |
}
|
jtulach@1483
|
1826 |
|
jtulach@1483
|
1827 |
static void registerNatives() {
|
jtulach@1483
|
1828 |
boolean assertsOn = false;
|
jtulach@1484
|
1829 |
// assert assertsOn = true;
|
jtulach@1483
|
1830 |
if (assertsOn) {
|
jtulach@1483
|
1831 |
try {
|
jtulach@1483
|
1832 |
Array.get(null, 0);
|
jtulach@1483
|
1833 |
} catch (Throwable ex) {
|
jtulach@1483
|
1834 |
// ignore
|
jtulach@1483
|
1835 |
}
|
jtulach@1483
|
1836 |
}
|
jtulach@1483
|
1837 |
}
|
jtulach@1483
|
1838 |
|
jaroslav@1935
|
1839 |
@JavaScriptBody(args = {}, body = "var p = java_lang_Object(false);"
|
jtulach@1483
|
1840 |
+ "p.toString = function() { return this.toString__Ljava_lang_String_2(); };"
|
jtulach@1483
|
1841 |
)
|
jtulach@1483
|
1842 |
static native void registerToString();
|
jtulach@1483
|
1843 |
|
jtulach@1483
|
1844 |
@JavaScriptBody(args = {"self"}, body
|
jtulach@1483
|
1845 |
= "var c = self.constructor.$class;\n"
|
jtulach@1483
|
1846 |
+ "return c ? c : null;\n"
|
jtulach@1483
|
1847 |
)
|
jtulach@1483
|
1848 |
static native Class<?> classFor(Object self);
|
jtulach@1483
|
1849 |
|
jaroslav@1586
|
1850 |
@Exported
|
jtulach@1483
|
1851 |
@JavaScriptBody(args = { "self" }, body
|
jaroslav@1586
|
1852 |
= "if (self['$hashCode']) return self['$hashCode'];\n"
|
jaroslav@1586
|
1853 |
+ "var h = self['computeHashCode__I'] ? self['computeHashCode__I']() : Math.random() * Math.pow(2, 31);\n"
|
jaroslav@1586
|
1854 |
+ "return self['$hashCode'] = h & h;"
|
jtulach@1483
|
1855 |
)
|
jtulach@1483
|
1856 |
static native int defaultHashCode(Object self);
|
jtulach@1483
|
1857 |
|
jtulach@1483
|
1858 |
@JavaScriptBody(args = "self", body
|
jaroslav@1513
|
1859 |
= "\nif (!self['$instOf_java_lang_Cloneable']) {"
|
jtulach@1483
|
1860 |
+ "\n return null;"
|
jtulach@1483
|
1861 |
+ "\n} else {"
|
jtulach@1483
|
1862 |
+ "\n var clone = self.constructor(true);"
|
jtulach@1483
|
1863 |
+ "\n var props = Object.getOwnPropertyNames(self);"
|
jtulach@1483
|
1864 |
+ "\n for (var i = 0; i < props.length; i++) {"
|
jtulach@1483
|
1865 |
+ "\n var p = props[i];"
|
jtulach@1483
|
1866 |
+ "\n clone[p] = self[p];"
|
jtulach@1483
|
1867 |
+ "\n };"
|
jtulach@1483
|
1868 |
+ "\n return clone;"
|
jtulach@1483
|
1869 |
+ "\n}"
|
jtulach@1483
|
1870 |
)
|
jtulach@1483
|
1871 |
static native Object clone(Object self) throws CloneNotSupportedException;
|
jaroslav@1515
|
1872 |
|
jaroslav@1958
|
1873 |
@JavaScriptOnly(name = "toJS", value = "function convToJS(v) {\n"
|
jaroslav@1515
|
1874 |
+ " if (v === null) return null;\n"
|
jaroslav@1515
|
1875 |
+ " if (Object.prototype.toString.call(v) === '[object Array]') {\n"
|
jaroslav@1958
|
1876 |
+ " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).convArray__Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_Object_2(v, convToJS);\n"
|
jaroslav@1515
|
1877 |
+ " }\n"
|
jaroslav@1919
|
1878 |
+ " if (v instanceof Date) return v;\n"
|
jaroslav@1515
|
1879 |
+ " return v.valueOf();\n"
|
jaroslav@1515
|
1880 |
+ "}\n"
|
jaroslav@1515
|
1881 |
)
|
jaroslav@1515
|
1882 |
static native int toJS();
|
jaroslav@1515
|
1883 |
|
jaroslav@1586
|
1884 |
@Exported
|
jaroslav@1515
|
1885 |
@JavaScriptOnly(name = "activate__Ljava_io_Closeable_2Lorg_apidesign_html_boot_spi_Fn$Presenter_2", value = "function() {\n"
|
jaroslav@1515
|
1886 |
+ " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).activate__Ljava_io_Closeable_2();"
|
jaroslav@1515
|
1887 |
+ "}\n"
|
jaroslav@1515
|
1888 |
)
|
jaroslav@1515
|
1889 |
static native int activate();
|
jaroslav@1676
|
1890 |
|
jaroslav@1676
|
1891 |
@Exported
|
jaroslav@1676
|
1892 |
@JavaScriptOnly(name = "activate__Ljava_io_Closeable_2Lorg_netbeans_html_boot_spi_Fn$Presenter_2", value = "function() {\n"
|
jaroslav@1676
|
1893 |
+ " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).activate__Ljava_io_Closeable_2();"
|
jaroslav@1676
|
1894 |
+ "}\n"
|
jaroslav@1676
|
1895 |
)
|
jaroslav@1676
|
1896 |
static native int activateNew();
|
jaroslav@1522
|
1897 |
|
jaroslav@1522
|
1898 |
private static Object bck2BrwsrCnvrt(Object o) {
|
jaroslav@1522
|
1899 |
if (o instanceof Throwable) {
|
jaroslav@1522
|
1900 |
return o;
|
jaroslav@1522
|
1901 |
}
|
jaroslav@1522
|
1902 |
final String msg = msg(o);
|
jaroslav@1522
|
1903 |
if (msg == null || msg.startsWith("TypeError")) {
|
jaroslav@1522
|
1904 |
return new NullPointerException(msg);
|
jaroslav@1522
|
1905 |
}
|
jaroslav@1522
|
1906 |
return new Throwable(msg);
|
jaroslav@1522
|
1907 |
}
|
jaroslav@1522
|
1908 |
|
jaroslav@1522
|
1909 |
@JavaScriptBody(args = {"o"}, body = "return o ? o.toString() : null;")
|
jaroslav@1522
|
1910 |
private static native String msg(Object o);
|
jaroslav@1522
|
1911 |
|
jaroslav@1586
|
1912 |
@Exported
|
jaroslav@1522
|
1913 |
@JavaScriptOnly(name = "bck2BrwsrThrwrbl", value = "c.bck2BrwsrCnvrt__Ljava_lang_Object_2Ljava_lang_Object_2")
|
jaroslav@1522
|
1914 |
private static void bck2BrwsrCnvrtVM() {
|
jaroslav@1522
|
1915 |
}
|
jaroslav@1774
|
1916 |
|
jaroslav@1774
|
1917 |
@Exported
|
jaroslav@1930
|
1918 |
@JavaScriptOnly(name = "castEx", value = ""
|
jaroslav@1930
|
1919 |
+ "function(obj, type) {\n"
|
jaroslav@1930
|
1920 |
+ " var realType = obj.getClass__Ljava_lang_Class_2().getName__Ljava_lang_String_2();\n"
|
jaroslav@1930
|
1921 |
+ " var msg = realType + ' cannot be cast to ' + type;\n"
|
jaroslav@1930
|
1922 |
+ " var ex = vm.java_lang_ClassCastException(true);\n"
|
jaroslav@1930
|
1923 |
+ " ex.constructor.cons__VLjava_lang_String_2.call(ex, msg);;\n"
|
jaroslav@1930
|
1924 |
+ " throw ex;\n"
|
jaroslav@1930
|
1925 |
+ "}\n"
|
jaroslav@1930
|
1926 |
+ ""
|
jaroslav@1930
|
1927 |
)
|
jaroslav@1774
|
1928 |
private static void castEx() {
|
jaroslav@1774
|
1929 |
}
|
jaroslav@1522
|
1930 |
|
jaroslav@56
|
1931 |
}
|