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@122
|
28 |
import java.io.InputStream;
|
jaroslav@56
|
29 |
import java.lang.annotation.Annotation;
|
jaroslav@225
|
30 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@56
|
31 |
|
jaroslav@56
|
32 |
/**
|
jaroslav@56
|
33 |
* Instances of the class {@code Class} represent classes and
|
jaroslav@56
|
34 |
* interfaces in a running Java application. An enum is a kind of
|
jaroslav@56
|
35 |
* class and an annotation is a kind of interface. Every array also
|
jaroslav@56
|
36 |
* belongs to a class that is reflected as a {@code Class} object
|
jaroslav@56
|
37 |
* that is shared by all arrays with the same element type and number
|
jaroslav@56
|
38 |
* of dimensions. The primitive Java types ({@code boolean},
|
jaroslav@56
|
39 |
* {@code byte}, {@code char}, {@code short},
|
jaroslav@56
|
40 |
* {@code int}, {@code long}, {@code float}, and
|
jaroslav@56
|
41 |
* {@code double}), and the keyword {@code void} are also
|
jaroslav@56
|
42 |
* represented as {@code Class} objects.
|
jaroslav@56
|
43 |
*
|
jaroslav@56
|
44 |
* <p> {@code Class} has no public constructor. Instead {@code Class}
|
jaroslav@56
|
45 |
* objects are constructed automatically by the Java Virtual Machine as classes
|
jaroslav@56
|
46 |
* are loaded and by calls to the {@code defineClass} method in the class
|
jaroslav@56
|
47 |
* loader.
|
jaroslav@56
|
48 |
*
|
jaroslav@56
|
49 |
* <p> The following example uses a {@code Class} object to print the
|
jaroslav@56
|
50 |
* class name of an object:
|
jaroslav@56
|
51 |
*
|
jaroslav@56
|
52 |
* <p> <blockquote><pre>
|
jaroslav@56
|
53 |
* void printClassName(Object obj) {
|
jaroslav@56
|
54 |
* System.out.println("The class of " + obj +
|
jaroslav@56
|
55 |
* " is " + obj.getClass().getName());
|
jaroslav@56
|
56 |
* }
|
jaroslav@56
|
57 |
* </pre></blockquote>
|
jaroslav@56
|
58 |
*
|
jaroslav@56
|
59 |
* <p> It is also possible to get the {@code Class} object for a named
|
jaroslav@56
|
60 |
* type (or for void) using a class literal. See Section 15.8.2 of
|
jaroslav@56
|
61 |
* <cite>The Java™ Language Specification</cite>.
|
jaroslav@56
|
62 |
* For example:
|
jaroslav@56
|
63 |
*
|
jaroslav@56
|
64 |
* <p> <blockquote>
|
jaroslav@56
|
65 |
* {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
|
jaroslav@56
|
66 |
* </blockquote>
|
jaroslav@56
|
67 |
*
|
jaroslav@56
|
68 |
* @param <T> the type of the class modeled by this {@code Class}
|
jaroslav@56
|
69 |
* object. For example, the type of {@code String.class} is {@code
|
jaroslav@56
|
70 |
* Class<String>}. Use {@code Class<?>} if the class being modeled is
|
jaroslav@56
|
71 |
* unknown.
|
jaroslav@56
|
72 |
*
|
jaroslav@56
|
73 |
* @author unascribed
|
jaroslav@56
|
74 |
* @see java.lang.ClassLoader#defineClass(byte[], int, int)
|
jaroslav@56
|
75 |
* @since JDK1.0
|
jaroslav@56
|
76 |
*/
|
jaroslav@56
|
77 |
public final
|
jaroslav@65
|
78 |
class Class<T> implements java.io.Serializable {
|
jaroslav@65
|
79 |
// java.lang.reflect.GenericDeclaration,
|
jaroslav@65
|
80 |
// java.lang.reflect.Type,
|
jaroslav@65
|
81 |
// java.lang.reflect.AnnotatedElement {
|
jaroslav@56
|
82 |
private static final int ANNOTATION= 0x00002000;
|
jaroslav@56
|
83 |
private static final int ENUM = 0x00004000;
|
jaroslav@56
|
84 |
private static final int SYNTHETIC = 0x00001000;
|
jaroslav@56
|
85 |
|
jaroslav@56
|
86 |
/*
|
jaroslav@56
|
87 |
* Constructor. Only the Java Virtual Machine creates Class
|
jaroslav@56
|
88 |
* objects.
|
jaroslav@56
|
89 |
*/
|
jaroslav@56
|
90 |
private Class() {}
|
jaroslav@56
|
91 |
|
jaroslav@56
|
92 |
|
jaroslav@56
|
93 |
/**
|
jaroslav@56
|
94 |
* Converts the object to a string. The string representation is the
|
jaroslav@56
|
95 |
* string "class" or "interface", followed by a space, and then by the
|
jaroslav@56
|
96 |
* fully qualified name of the class in the format returned by
|
jaroslav@56
|
97 |
* {@code getName}. If this {@code Class} object represents a
|
jaroslav@56
|
98 |
* primitive type, this method returns the name of the primitive type. If
|
jaroslav@56
|
99 |
* this {@code Class} object represents void this method returns
|
jaroslav@56
|
100 |
* "void".
|
jaroslav@56
|
101 |
*
|
jaroslav@56
|
102 |
* @return a string representation of this class object.
|
jaroslav@56
|
103 |
*/
|
jaroslav@56
|
104 |
public String toString() {
|
jaroslav@56
|
105 |
return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
|
jaroslav@56
|
106 |
+ getName();
|
jaroslav@56
|
107 |
}
|
jaroslav@56
|
108 |
|
jaroslav@56
|
109 |
|
jaroslav@56
|
110 |
/**
|
jaroslav@56
|
111 |
* Returns the {@code Class} object associated with the class or
|
jaroslav@56
|
112 |
* interface with the given string name. Invoking this method is
|
jaroslav@56
|
113 |
* equivalent to:
|
jaroslav@56
|
114 |
*
|
jaroslav@56
|
115 |
* <blockquote>
|
jaroslav@56
|
116 |
* {@code Class.forName(className, true, currentLoader)}
|
jaroslav@56
|
117 |
* </blockquote>
|
jaroslav@56
|
118 |
*
|
jaroslav@56
|
119 |
* where {@code currentLoader} denotes the defining class loader of
|
jaroslav@56
|
120 |
* the current class.
|
jaroslav@56
|
121 |
*
|
jaroslav@56
|
122 |
* <p> For example, the following code fragment returns the
|
jaroslav@56
|
123 |
* runtime {@code Class} descriptor for the class named
|
jaroslav@56
|
124 |
* {@code java.lang.Thread}:
|
jaroslav@56
|
125 |
*
|
jaroslav@56
|
126 |
* <blockquote>
|
jaroslav@56
|
127 |
* {@code Class t = Class.forName("java.lang.Thread")}
|
jaroslav@56
|
128 |
* </blockquote>
|
jaroslav@56
|
129 |
* <p>
|
jaroslav@56
|
130 |
* A call to {@code forName("X")} causes the class named
|
jaroslav@56
|
131 |
* {@code X} to be initialized.
|
jaroslav@56
|
132 |
*
|
jaroslav@56
|
133 |
* @param className the fully qualified name of the desired class.
|
jaroslav@56
|
134 |
* @return the {@code Class} object for the class with the
|
jaroslav@56
|
135 |
* specified name.
|
jaroslav@56
|
136 |
* @exception LinkageError if the linkage fails
|
jaroslav@56
|
137 |
* @exception ExceptionInInitializerError if the initialization provoked
|
jaroslav@56
|
138 |
* by this method fails
|
jaroslav@56
|
139 |
* @exception ClassNotFoundException if the class cannot be located
|
jaroslav@56
|
140 |
*/
|
jaroslav@56
|
141 |
public static Class<?> forName(String className)
|
jaroslav@56
|
142 |
throws ClassNotFoundException {
|
jaroslav@65
|
143 |
throw new UnsupportedOperationException();
|
jaroslav@56
|
144 |
}
|
jaroslav@56
|
145 |
|
jaroslav@56
|
146 |
|
jaroslav@56
|
147 |
/**
|
jaroslav@56
|
148 |
* Creates a new instance of the class represented by this {@code Class}
|
jaroslav@56
|
149 |
* object. The class is instantiated as if by a {@code new}
|
jaroslav@56
|
150 |
* expression with an empty argument list. The class is initialized if it
|
jaroslav@56
|
151 |
* has not already been initialized.
|
jaroslav@56
|
152 |
*
|
jaroslav@56
|
153 |
* <p>Note that this method propagates any exception thrown by the
|
jaroslav@56
|
154 |
* nullary constructor, including a checked exception. Use of
|
jaroslav@56
|
155 |
* this method effectively bypasses the compile-time exception
|
jaroslav@56
|
156 |
* checking that would otherwise be performed by the compiler.
|
jaroslav@56
|
157 |
* The {@link
|
jaroslav@56
|
158 |
* java.lang.reflect.Constructor#newInstance(java.lang.Object...)
|
jaroslav@56
|
159 |
* Constructor.newInstance} method avoids this problem by wrapping
|
jaroslav@56
|
160 |
* any exception thrown by the constructor in a (checked) {@link
|
jaroslav@56
|
161 |
* java.lang.reflect.InvocationTargetException}.
|
jaroslav@56
|
162 |
*
|
jaroslav@56
|
163 |
* @return a newly allocated instance of the class represented by this
|
jaroslav@56
|
164 |
* object.
|
jaroslav@56
|
165 |
* @exception IllegalAccessException if the class or its nullary
|
jaroslav@56
|
166 |
* constructor is not accessible.
|
jaroslav@56
|
167 |
* @exception InstantiationException
|
jaroslav@56
|
168 |
* if this {@code Class} represents an abstract class,
|
jaroslav@56
|
169 |
* an interface, an array class, a primitive type, or void;
|
jaroslav@56
|
170 |
* or if the class has no nullary constructor;
|
jaroslav@56
|
171 |
* or if the instantiation fails for some other reason.
|
jaroslav@56
|
172 |
* @exception ExceptionInInitializerError if the initialization
|
jaroslav@56
|
173 |
* provoked by this method fails.
|
jaroslav@56
|
174 |
* @exception SecurityException
|
jaroslav@56
|
175 |
* If a security manager, <i>s</i>, is present and any of the
|
jaroslav@56
|
176 |
* following conditions is met:
|
jaroslav@56
|
177 |
*
|
jaroslav@56
|
178 |
* <ul>
|
jaroslav@56
|
179 |
*
|
jaroslav@56
|
180 |
* <li> invocation of
|
jaroslav@56
|
181 |
* {@link SecurityManager#checkMemberAccess
|
jaroslav@56
|
182 |
* s.checkMemberAccess(this, Member.PUBLIC)} denies
|
jaroslav@56
|
183 |
* creation of new instances of this class
|
jaroslav@56
|
184 |
*
|
jaroslav@56
|
185 |
* <li> the caller's class loader is not the same as or an
|
jaroslav@56
|
186 |
* ancestor of the class loader for the current class and
|
jaroslav@56
|
187 |
* invocation of {@link SecurityManager#checkPackageAccess
|
jaroslav@56
|
188 |
* s.checkPackageAccess()} denies access to the package
|
jaroslav@56
|
189 |
* of this class
|
jaroslav@56
|
190 |
*
|
jaroslav@56
|
191 |
* </ul>
|
jaroslav@56
|
192 |
*
|
jaroslav@56
|
193 |
*/
|
jaroslav@56
|
194 |
public T newInstance()
|
jaroslav@56
|
195 |
throws InstantiationException, IllegalAccessException
|
jaroslav@56
|
196 |
{
|
jaroslav@65
|
197 |
throw new UnsupportedOperationException("Should be somehow supported");
|
jaroslav@56
|
198 |
}
|
jaroslav@56
|
199 |
|
jaroslav@56
|
200 |
/**
|
jaroslav@56
|
201 |
* Determines if the specified {@code Object} is assignment-compatible
|
jaroslav@56
|
202 |
* with the object represented by this {@code Class}. This method is
|
jaroslav@56
|
203 |
* the dynamic equivalent of the Java language {@code instanceof}
|
jaroslav@56
|
204 |
* operator. The method returns {@code true} if the specified
|
jaroslav@56
|
205 |
* {@code Object} argument is non-null and can be cast to the
|
jaroslav@56
|
206 |
* reference type represented by this {@code Class} object without
|
jaroslav@56
|
207 |
* raising a {@code ClassCastException.} It returns {@code false}
|
jaroslav@56
|
208 |
* otherwise.
|
jaroslav@56
|
209 |
*
|
jaroslav@56
|
210 |
* <p> Specifically, if this {@code Class} object represents a
|
jaroslav@56
|
211 |
* declared class, this method returns {@code true} if the specified
|
jaroslav@56
|
212 |
* {@code Object} argument is an instance of the represented class (or
|
jaroslav@56
|
213 |
* of any of its subclasses); it returns {@code false} otherwise. If
|
jaroslav@56
|
214 |
* this {@code Class} object represents an array class, this method
|
jaroslav@56
|
215 |
* returns {@code true} if the specified {@code Object} argument
|
jaroslav@56
|
216 |
* can be converted to an object of the array class by an identity
|
jaroslav@56
|
217 |
* conversion or by a widening reference conversion; it returns
|
jaroslav@56
|
218 |
* {@code false} otherwise. If this {@code Class} object
|
jaroslav@56
|
219 |
* represents an interface, this method returns {@code true} if the
|
jaroslav@56
|
220 |
* class or any superclass of the specified {@code Object} argument
|
jaroslav@56
|
221 |
* implements this interface; it returns {@code false} otherwise. If
|
jaroslav@56
|
222 |
* this {@code Class} object represents a primitive type, this method
|
jaroslav@56
|
223 |
* returns {@code false}.
|
jaroslav@56
|
224 |
*
|
jaroslav@56
|
225 |
* @param obj the object to check
|
jaroslav@56
|
226 |
* @return true if {@code obj} is an instance of this class
|
jaroslav@56
|
227 |
*
|
jaroslav@56
|
228 |
* @since JDK1.1
|
jaroslav@56
|
229 |
*/
|
jaroslav@56
|
230 |
public native boolean isInstance(Object obj);
|
jaroslav@56
|
231 |
|
jaroslav@56
|
232 |
|
jaroslav@56
|
233 |
/**
|
jaroslav@56
|
234 |
* Determines if the class or interface represented by this
|
jaroslav@56
|
235 |
* {@code Class} object is either the same as, or is a superclass or
|
jaroslav@56
|
236 |
* superinterface of, the class or interface represented by the specified
|
jaroslav@56
|
237 |
* {@code Class} parameter. It returns {@code true} if so;
|
jaroslav@56
|
238 |
* otherwise it returns {@code false}. If this {@code Class}
|
jaroslav@56
|
239 |
* object represents a primitive type, this method returns
|
jaroslav@56
|
240 |
* {@code true} if the specified {@code Class} parameter is
|
jaroslav@56
|
241 |
* exactly this {@code Class} object; otherwise it returns
|
jaroslav@56
|
242 |
* {@code false}.
|
jaroslav@56
|
243 |
*
|
jaroslav@56
|
244 |
* <p> Specifically, this method tests whether the type represented by the
|
jaroslav@56
|
245 |
* specified {@code Class} parameter can be converted to the type
|
jaroslav@56
|
246 |
* represented by this {@code Class} object via an identity conversion
|
jaroslav@56
|
247 |
* or via a widening reference conversion. See <em>The Java Language
|
jaroslav@56
|
248 |
* Specification</em>, sections 5.1.1 and 5.1.4 , for details.
|
jaroslav@56
|
249 |
*
|
jaroslav@56
|
250 |
* @param cls the {@code Class} object to be checked
|
jaroslav@56
|
251 |
* @return the {@code boolean} value indicating whether objects of the
|
jaroslav@56
|
252 |
* type {@code cls} can be assigned to objects of this class
|
jaroslav@56
|
253 |
* @exception NullPointerException if the specified Class parameter is
|
jaroslav@56
|
254 |
* null.
|
jaroslav@56
|
255 |
* @since JDK1.1
|
jaroslav@56
|
256 |
*/
|
jaroslav@56
|
257 |
public native boolean isAssignableFrom(Class<?> cls);
|
jaroslav@56
|
258 |
|
jaroslav@56
|
259 |
|
jaroslav@56
|
260 |
/**
|
jaroslav@56
|
261 |
* Determines if the specified {@code Class} object represents an
|
jaroslav@56
|
262 |
* interface type.
|
jaroslav@56
|
263 |
*
|
jaroslav@56
|
264 |
* @return {@code true} if this object represents an interface;
|
jaroslav@56
|
265 |
* {@code false} otherwise.
|
jaroslav@56
|
266 |
*/
|
jaroslav@56
|
267 |
public native boolean isInterface();
|
jaroslav@56
|
268 |
|
jaroslav@56
|
269 |
|
jaroslav@56
|
270 |
/**
|
jaroslav@56
|
271 |
* Determines if this {@code Class} object represents an array class.
|
jaroslav@56
|
272 |
*
|
jaroslav@56
|
273 |
* @return {@code true} if this object represents an array class;
|
jaroslav@56
|
274 |
* {@code false} otherwise.
|
jaroslav@56
|
275 |
* @since JDK1.1
|
jaroslav@56
|
276 |
*/
|
jaroslav@228
|
277 |
public boolean isArray() {
|
jaroslav@228
|
278 |
return false;
|
jaroslav@228
|
279 |
}
|
jaroslav@56
|
280 |
|
jaroslav@56
|
281 |
|
jaroslav@56
|
282 |
/**
|
jaroslav@56
|
283 |
* Determines if the specified {@code Class} object represents a
|
jaroslav@56
|
284 |
* primitive type.
|
jaroslav@56
|
285 |
*
|
jaroslav@56
|
286 |
* <p> There are nine predefined {@code Class} objects to represent
|
jaroslav@56
|
287 |
* the eight primitive types and void. These are created by the Java
|
jaroslav@56
|
288 |
* Virtual Machine, and have the same names as the primitive types that
|
jaroslav@56
|
289 |
* they represent, namely {@code boolean}, {@code byte},
|
jaroslav@56
|
290 |
* {@code char}, {@code short}, {@code int},
|
jaroslav@56
|
291 |
* {@code long}, {@code float}, and {@code double}.
|
jaroslav@56
|
292 |
*
|
jaroslav@56
|
293 |
* <p> These objects may only be accessed via the following public static
|
jaroslav@56
|
294 |
* final variables, and are the only {@code Class} objects for which
|
jaroslav@56
|
295 |
* this method returns {@code true}.
|
jaroslav@56
|
296 |
*
|
jaroslav@56
|
297 |
* @return true if and only if this class represents a primitive type
|
jaroslav@56
|
298 |
*
|
jaroslav@56
|
299 |
* @see java.lang.Boolean#TYPE
|
jaroslav@56
|
300 |
* @see java.lang.Character#TYPE
|
jaroslav@56
|
301 |
* @see java.lang.Byte#TYPE
|
jaroslav@56
|
302 |
* @see java.lang.Short#TYPE
|
jaroslav@56
|
303 |
* @see java.lang.Integer#TYPE
|
jaroslav@56
|
304 |
* @see java.lang.Long#TYPE
|
jaroslav@56
|
305 |
* @see java.lang.Float#TYPE
|
jaroslav@56
|
306 |
* @see java.lang.Double#TYPE
|
jaroslav@56
|
307 |
* @see java.lang.Void#TYPE
|
jaroslav@56
|
308 |
* @since JDK1.1
|
jaroslav@56
|
309 |
*/
|
jaroslav@56
|
310 |
public native boolean isPrimitive();
|
jaroslav@56
|
311 |
|
jaroslav@56
|
312 |
/**
|
jaroslav@56
|
313 |
* Returns true if this {@code Class} object represents an annotation
|
jaroslav@56
|
314 |
* type. Note that if this method returns true, {@link #isInterface()}
|
jaroslav@56
|
315 |
* would also return true, as all annotation types are also interfaces.
|
jaroslav@56
|
316 |
*
|
jaroslav@56
|
317 |
* @return {@code true} if this class object represents an annotation
|
jaroslav@56
|
318 |
* type; {@code false} otherwise
|
jaroslav@56
|
319 |
* @since 1.5
|
jaroslav@56
|
320 |
*/
|
jaroslav@56
|
321 |
public boolean isAnnotation() {
|
jaroslav@56
|
322 |
return (getModifiers() & ANNOTATION) != 0;
|
jaroslav@56
|
323 |
}
|
jaroslav@56
|
324 |
|
jaroslav@56
|
325 |
/**
|
jaroslav@56
|
326 |
* Returns {@code true} if this class is a synthetic class;
|
jaroslav@56
|
327 |
* returns {@code false} otherwise.
|
jaroslav@56
|
328 |
* @return {@code true} if and only if this class is a synthetic class as
|
jaroslav@56
|
329 |
* defined by the Java Language Specification.
|
jaroslav@56
|
330 |
* @since 1.5
|
jaroslav@56
|
331 |
*/
|
jaroslav@56
|
332 |
public boolean isSynthetic() {
|
jaroslav@56
|
333 |
return (getModifiers() & SYNTHETIC) != 0;
|
jaroslav@56
|
334 |
}
|
jaroslav@56
|
335 |
|
jaroslav@56
|
336 |
/**
|
jaroslav@56
|
337 |
* Returns the name of the entity (class, interface, array class,
|
jaroslav@56
|
338 |
* primitive type, or void) represented by this {@code Class} object,
|
jaroslav@56
|
339 |
* as a {@code String}.
|
jaroslav@56
|
340 |
*
|
jaroslav@56
|
341 |
* <p> If this class object represents a reference type that is not an
|
jaroslav@56
|
342 |
* array type then the binary name of the class is returned, as specified
|
jaroslav@56
|
343 |
* by
|
jaroslav@56
|
344 |
* <cite>The Java™ Language Specification</cite>.
|
jaroslav@56
|
345 |
*
|
jaroslav@56
|
346 |
* <p> If this class object represents a primitive type or void, then the
|
jaroslav@56
|
347 |
* name returned is a {@code String} equal to the Java language
|
jaroslav@56
|
348 |
* keyword corresponding to the primitive type or void.
|
jaroslav@56
|
349 |
*
|
jaroslav@56
|
350 |
* <p> If this class object represents a class of arrays, then the internal
|
jaroslav@56
|
351 |
* form of the name consists of the name of the element type preceded by
|
jaroslav@56
|
352 |
* one or more '{@code [}' characters representing the depth of the array
|
jaroslav@56
|
353 |
* nesting. The encoding of element type names is as follows:
|
jaroslav@56
|
354 |
*
|
jaroslav@56
|
355 |
* <blockquote><table summary="Element types and encodings">
|
jaroslav@56
|
356 |
* <tr><th> Element Type <th> <th> Encoding
|
jaroslav@56
|
357 |
* <tr><td> boolean <td> <td align=center> Z
|
jaroslav@56
|
358 |
* <tr><td> byte <td> <td align=center> B
|
jaroslav@56
|
359 |
* <tr><td> char <td> <td align=center> C
|
jaroslav@56
|
360 |
* <tr><td> class or interface
|
jaroslav@56
|
361 |
* <td> <td align=center> L<i>classname</i>;
|
jaroslav@56
|
362 |
* <tr><td> double <td> <td align=center> D
|
jaroslav@56
|
363 |
* <tr><td> float <td> <td align=center> F
|
jaroslav@56
|
364 |
* <tr><td> int <td> <td align=center> I
|
jaroslav@56
|
365 |
* <tr><td> long <td> <td align=center> J
|
jaroslav@56
|
366 |
* <tr><td> short <td> <td align=center> S
|
jaroslav@56
|
367 |
* </table></blockquote>
|
jaroslav@56
|
368 |
*
|
jaroslav@56
|
369 |
* <p> The class or interface name <i>classname</i> is the binary name of
|
jaroslav@56
|
370 |
* the class specified above.
|
jaroslav@56
|
371 |
*
|
jaroslav@56
|
372 |
* <p> Examples:
|
jaroslav@56
|
373 |
* <blockquote><pre>
|
jaroslav@56
|
374 |
* String.class.getName()
|
jaroslav@56
|
375 |
* returns "java.lang.String"
|
jaroslav@56
|
376 |
* byte.class.getName()
|
jaroslav@56
|
377 |
* returns "byte"
|
jaroslav@56
|
378 |
* (new Object[3]).getClass().getName()
|
jaroslav@56
|
379 |
* returns "[Ljava.lang.Object;"
|
jaroslav@56
|
380 |
* (new int[3][4][5][6][7][8][9]).getClass().getName()
|
jaroslav@56
|
381 |
* returns "[[[[[[[I"
|
jaroslav@56
|
382 |
* </pre></blockquote>
|
jaroslav@56
|
383 |
*
|
jaroslav@56
|
384 |
* @return the name of the class or interface
|
jaroslav@56
|
385 |
* represented by this object.
|
jaroslav@56
|
386 |
*/
|
jaroslav@56
|
387 |
public String getName() {
|
jaroslav@225
|
388 |
return jvmName().replace('/', '.');
|
jaroslav@56
|
389 |
}
|
jaroslav@56
|
390 |
|
jaroslav@225
|
391 |
@JavaScriptBody(args = "self", body = "return self.jvmName;")
|
jaroslav@225
|
392 |
private native String jvmName();
|
jaroslav@225
|
393 |
|
jaroslav@56
|
394 |
/**
|
jaroslav@56
|
395 |
* Returns the {@code Class} representing the superclass of the entity
|
jaroslav@56
|
396 |
* (class, interface, primitive type or void) represented by this
|
jaroslav@56
|
397 |
* {@code Class}. If this {@code Class} represents either the
|
jaroslav@56
|
398 |
* {@code Object} class, an interface, a primitive type, or void, then
|
jaroslav@56
|
399 |
* null is returned. If this object represents an array class then the
|
jaroslav@56
|
400 |
* {@code Class} object representing the {@code Object} class is
|
jaroslav@56
|
401 |
* returned.
|
jaroslav@56
|
402 |
*
|
jaroslav@56
|
403 |
* @return the superclass of the class represented by this object.
|
jaroslav@56
|
404 |
*/
|
jaroslav@56
|
405 |
public native Class<? super T> getSuperclass();
|
jaroslav@56
|
406 |
|
jaroslav@56
|
407 |
/**
|
jaroslav@56
|
408 |
* Returns the Java language modifiers for this class or interface, encoded
|
jaroslav@56
|
409 |
* in an integer. The modifiers consist of the Java Virtual Machine's
|
jaroslav@56
|
410 |
* constants for {@code public}, {@code protected},
|
jaroslav@56
|
411 |
* {@code private}, {@code final}, {@code static},
|
jaroslav@56
|
412 |
* {@code abstract} and {@code interface}; they should be decoded
|
jaroslav@56
|
413 |
* using the methods of class {@code Modifier}.
|
jaroslav@56
|
414 |
*
|
jaroslav@56
|
415 |
* <p> If the underlying class is an array class, then its
|
jaroslav@56
|
416 |
* {@code public}, {@code private} and {@code protected}
|
jaroslav@56
|
417 |
* modifiers are the same as those of its component type. If this
|
jaroslav@56
|
418 |
* {@code Class} represents a primitive type or void, its
|
jaroslav@56
|
419 |
* {@code public} modifier is always {@code true}, and its
|
jaroslav@56
|
420 |
* {@code protected} and {@code private} modifiers are always
|
jaroslav@56
|
421 |
* {@code false}. If this object represents an array class, a
|
jaroslav@56
|
422 |
* primitive type or void, then its {@code final} modifier is always
|
jaroslav@56
|
423 |
* {@code true} and its interface modifier is always
|
jaroslav@56
|
424 |
* {@code false}. The values of its other modifiers are not determined
|
jaroslav@56
|
425 |
* by this specification.
|
jaroslav@56
|
426 |
*
|
jaroslav@56
|
427 |
* <p> The modifier encodings are defined in <em>The Java Virtual Machine
|
jaroslav@56
|
428 |
* Specification</em>, table 4.1.
|
jaroslav@56
|
429 |
*
|
jaroslav@56
|
430 |
* @return the {@code int} representing the modifiers for this class
|
jaroslav@56
|
431 |
* @see java.lang.reflect.Modifier
|
jaroslav@56
|
432 |
* @since JDK1.1
|
jaroslav@56
|
433 |
*/
|
jaroslav@56
|
434 |
public native int getModifiers();
|
jaroslav@56
|
435 |
|
jaroslav@56
|
436 |
|
jaroslav@56
|
437 |
/**
|
jaroslav@56
|
438 |
* Returns the simple name of the underlying class as given in the
|
jaroslav@56
|
439 |
* source code. Returns an empty string if the underlying class is
|
jaroslav@56
|
440 |
* anonymous.
|
jaroslav@56
|
441 |
*
|
jaroslav@56
|
442 |
* <p>The simple name of an array is the simple name of the
|
jaroslav@56
|
443 |
* component type with "[]" appended. In particular the simple
|
jaroslav@56
|
444 |
* name of an array whose component type is anonymous is "[]".
|
jaroslav@56
|
445 |
*
|
jaroslav@56
|
446 |
* @return the simple name of the underlying class
|
jaroslav@56
|
447 |
* @since 1.5
|
jaroslav@56
|
448 |
*/
|
jaroslav@56
|
449 |
public String getSimpleName() {
|
jaroslav@229
|
450 |
if (isArray())
|
jaroslav@229
|
451 |
return getComponentType().getSimpleName()+"[]";
|
jaroslav@229
|
452 |
|
jaroslav@229
|
453 |
String simpleName = getSimpleBinaryName();
|
jaroslav@229
|
454 |
if (simpleName == null) { // top level class
|
jaroslav@229
|
455 |
simpleName = getName();
|
jaroslav@229
|
456 |
return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
|
jaroslav@229
|
457 |
}
|
jaroslav@229
|
458 |
// According to JLS3 "Binary Compatibility" (13.1) the binary
|
jaroslav@229
|
459 |
// name of non-package classes (not top level) is the binary
|
jaroslav@229
|
460 |
// name of the immediately enclosing class followed by a '$' followed by:
|
jaroslav@229
|
461 |
// (for nested and inner classes): the simple name.
|
jaroslav@229
|
462 |
// (for local classes): 1 or more digits followed by the simple name.
|
jaroslav@229
|
463 |
// (for anonymous classes): 1 or more digits.
|
jaroslav@229
|
464 |
|
jaroslav@229
|
465 |
// Since getSimpleBinaryName() will strip the binary name of
|
jaroslav@229
|
466 |
// the immediatly enclosing class, we are now looking at a
|
jaroslav@229
|
467 |
// string that matches the regular expression "\$[0-9]*"
|
jaroslav@229
|
468 |
// followed by a simple name (considering the simple of an
|
jaroslav@229
|
469 |
// anonymous class to be the empty string).
|
jaroslav@229
|
470 |
|
jaroslav@229
|
471 |
// Remove leading "\$[0-9]*" from the name
|
jaroslav@229
|
472 |
int length = simpleName.length();
|
jaroslav@229
|
473 |
if (length < 1 || simpleName.charAt(0) != '$')
|
jaroslav@229
|
474 |
throw new IllegalStateException("Malformed class name");
|
jaroslav@229
|
475 |
int index = 1;
|
jaroslav@229
|
476 |
while (index < length && isAsciiDigit(simpleName.charAt(index)))
|
jaroslav@229
|
477 |
index++;
|
jaroslav@229
|
478 |
// Eventually, this is the empty string iff this is an anonymous class
|
jaroslav@229
|
479 |
return simpleName.substring(index);
|
jaroslav@56
|
480 |
}
|
jaroslav@56
|
481 |
|
jaroslav@56
|
482 |
/**
|
jaroslav@229
|
483 |
* Returns the "simple binary name" of the underlying class, i.e.,
|
jaroslav@229
|
484 |
* the binary name without the leading enclosing class name.
|
jaroslav@229
|
485 |
* Returns {@code null} if the underlying class is a top level
|
jaroslav@229
|
486 |
* class.
|
jaroslav@229
|
487 |
*/
|
jaroslav@229
|
488 |
private String getSimpleBinaryName() {
|
jaroslav@229
|
489 |
Class<?> enclosingClass = null; // XXX getEnclosingClass();
|
jaroslav@229
|
490 |
if (enclosingClass == null) // top level class
|
jaroslav@229
|
491 |
return null;
|
jaroslav@229
|
492 |
// Otherwise, strip the enclosing class' name
|
jaroslav@229
|
493 |
try {
|
jaroslav@229
|
494 |
return getName().substring(enclosingClass.getName().length());
|
jaroslav@229
|
495 |
} catch (IndexOutOfBoundsException ex) {
|
jaroslav@229
|
496 |
throw new IllegalStateException("Malformed class name");
|
jaroslav@229
|
497 |
}
|
jaroslav@229
|
498 |
}
|
jaroslav@229
|
499 |
|
jaroslav@229
|
500 |
/**
|
jaroslav@56
|
501 |
* Character.isDigit answers {@code true} to some non-ascii
|
jaroslav@56
|
502 |
* digits. This one does not.
|
jaroslav@56
|
503 |
*/
|
jaroslav@56
|
504 |
private static boolean isAsciiDigit(char c) {
|
jaroslav@56
|
505 |
return '0' <= c && c <= '9';
|
jaroslav@56
|
506 |
}
|
jaroslav@56
|
507 |
|
jaroslav@56
|
508 |
/**
|
jaroslav@56
|
509 |
* Returns the canonical name of the underlying class as
|
jaroslav@56
|
510 |
* defined by the Java Language Specification. Returns null if
|
jaroslav@56
|
511 |
* the underlying class does not have a canonical name (i.e., if
|
jaroslav@56
|
512 |
* it is a local or anonymous class or an array whose component
|
jaroslav@56
|
513 |
* type does not have a canonical name).
|
jaroslav@56
|
514 |
* @return the canonical name of the underlying class if it exists, and
|
jaroslav@56
|
515 |
* {@code null} otherwise.
|
jaroslav@56
|
516 |
* @since 1.5
|
jaroslav@56
|
517 |
*/
|
jaroslav@56
|
518 |
public String getCanonicalName() {
|
jaroslav@228
|
519 |
if (isArray()) {
|
jaroslav@228
|
520 |
String canonicalName = getComponentType().getCanonicalName();
|
jaroslav@228
|
521 |
if (canonicalName != null)
|
jaroslav@228
|
522 |
return canonicalName + "[]";
|
jaroslav@228
|
523 |
else
|
jaroslav@228
|
524 |
return null;
|
jaroslav@228
|
525 |
}
|
jaroslav@65
|
526 |
// if (isLocalOrAnonymousClass())
|
jaroslav@65
|
527 |
// return null;
|
jaroslav@65
|
528 |
// Class<?> enclosingClass = getEnclosingClass();
|
jaroslav@228
|
529 |
Class<?> enclosingClass = null;
|
jaroslav@228
|
530 |
if (enclosingClass == null) { // top level class
|
jaroslav@228
|
531 |
return getName();
|
jaroslav@228
|
532 |
} else {
|
jaroslav@228
|
533 |
String enclosingName = enclosingClass.getCanonicalName();
|
jaroslav@228
|
534 |
if (enclosingName == null)
|
jaroslav@228
|
535 |
return null;
|
jaroslav@228
|
536 |
return enclosingName + "." + getSimpleName();
|
jaroslav@228
|
537 |
}
|
jaroslav@56
|
538 |
}
|
jaroslav@56
|
539 |
|
jaroslav@56
|
540 |
/**
|
jaroslav@56
|
541 |
* Finds a resource with a given name. The rules for searching resources
|
jaroslav@56
|
542 |
* associated with a given class are implemented by the defining
|
jaroslav@56
|
543 |
* {@linkplain ClassLoader class loader} of the class. This method
|
jaroslav@56
|
544 |
* delegates to this object's class loader. If this object was loaded by
|
jaroslav@56
|
545 |
* the bootstrap class loader, the method delegates to {@link
|
jaroslav@56
|
546 |
* ClassLoader#getSystemResourceAsStream}.
|
jaroslav@56
|
547 |
*
|
jaroslav@56
|
548 |
* <p> Before delegation, an absolute resource name is constructed from the
|
jaroslav@56
|
549 |
* given resource name using this algorithm:
|
jaroslav@56
|
550 |
*
|
jaroslav@56
|
551 |
* <ul>
|
jaroslav@56
|
552 |
*
|
jaroslav@56
|
553 |
* <li> If the {@code name} begins with a {@code '/'}
|
jaroslav@56
|
554 |
* (<tt>'\u002f'</tt>), then the absolute name of the resource is the
|
jaroslav@56
|
555 |
* portion of the {@code name} following the {@code '/'}.
|
jaroslav@56
|
556 |
*
|
jaroslav@56
|
557 |
* <li> Otherwise, the absolute name is of the following form:
|
jaroslav@56
|
558 |
*
|
jaroslav@56
|
559 |
* <blockquote>
|
jaroslav@56
|
560 |
* {@code modified_package_name/name}
|
jaroslav@56
|
561 |
* </blockquote>
|
jaroslav@56
|
562 |
*
|
jaroslav@56
|
563 |
* <p> Where the {@code modified_package_name} is the package name of this
|
jaroslav@56
|
564 |
* object with {@code '/'} substituted for {@code '.'}
|
jaroslav@56
|
565 |
* (<tt>'\u002e'</tt>).
|
jaroslav@56
|
566 |
*
|
jaroslav@56
|
567 |
* </ul>
|
jaroslav@56
|
568 |
*
|
jaroslav@56
|
569 |
* @param name name of the desired resource
|
jaroslav@56
|
570 |
* @return A {@link java.io.InputStream} object or {@code null} if
|
jaroslav@56
|
571 |
* no resource with this name is found
|
jaroslav@56
|
572 |
* @throws NullPointerException If {@code name} is {@code null}
|
jaroslav@56
|
573 |
* @since JDK1.1
|
jaroslav@56
|
574 |
*/
|
jaroslav@122
|
575 |
public InputStream getResourceAsStream(String name) {
|
jaroslav@122
|
576 |
name = resolveName(name);
|
jaroslav@122
|
577 |
ClassLoader cl = getClassLoader0();
|
jaroslav@122
|
578 |
if (cl==null) {
|
jaroslav@122
|
579 |
// A system class.
|
jaroslav@122
|
580 |
return ClassLoader.getSystemResourceAsStream(name);
|
jaroslav@122
|
581 |
}
|
jaroslav@122
|
582 |
return cl.getResourceAsStream(name);
|
jaroslav@122
|
583 |
}
|
jaroslav@56
|
584 |
|
jaroslav@56
|
585 |
/**
|
jaroslav@56
|
586 |
* Finds a resource with a given name. The rules for searching resources
|
jaroslav@56
|
587 |
* associated with a given class are implemented by the defining
|
jaroslav@56
|
588 |
* {@linkplain ClassLoader class loader} of the class. This method
|
jaroslav@56
|
589 |
* delegates to this object's class loader. If this object was loaded by
|
jaroslav@56
|
590 |
* the bootstrap class loader, the method delegates to {@link
|
jaroslav@56
|
591 |
* ClassLoader#getSystemResource}.
|
jaroslav@56
|
592 |
*
|
jaroslav@56
|
593 |
* <p> Before delegation, an absolute resource name is constructed from the
|
jaroslav@56
|
594 |
* given resource name using this algorithm:
|
jaroslav@56
|
595 |
*
|
jaroslav@56
|
596 |
* <ul>
|
jaroslav@56
|
597 |
*
|
jaroslav@56
|
598 |
* <li> If the {@code name} begins with a {@code '/'}
|
jaroslav@56
|
599 |
* (<tt>'\u002f'</tt>), then the absolute name of the resource is the
|
jaroslav@56
|
600 |
* portion of the {@code name} following the {@code '/'}.
|
jaroslav@56
|
601 |
*
|
jaroslav@56
|
602 |
* <li> Otherwise, the absolute name is of the following form:
|
jaroslav@56
|
603 |
*
|
jaroslav@56
|
604 |
* <blockquote>
|
jaroslav@56
|
605 |
* {@code modified_package_name/name}
|
jaroslav@56
|
606 |
* </blockquote>
|
jaroslav@56
|
607 |
*
|
jaroslav@56
|
608 |
* <p> Where the {@code modified_package_name} is the package name of this
|
jaroslav@56
|
609 |
* object with {@code '/'} substituted for {@code '.'}
|
jaroslav@56
|
610 |
* (<tt>'\u002e'</tt>).
|
jaroslav@56
|
611 |
*
|
jaroslav@56
|
612 |
* </ul>
|
jaroslav@56
|
613 |
*
|
jaroslav@56
|
614 |
* @param name name of the desired resource
|
jaroslav@56
|
615 |
* @return A {@link java.net.URL} object or {@code null} if no
|
jaroslav@56
|
616 |
* resource with this name is found
|
jaroslav@56
|
617 |
* @since JDK1.1
|
jaroslav@56
|
618 |
*/
|
jaroslav@122
|
619 |
public java.net.URL getResource(String name) {
|
jaroslav@122
|
620 |
name = resolveName(name);
|
jaroslav@122
|
621 |
ClassLoader cl = getClassLoader0();
|
jaroslav@122
|
622 |
if (cl==null) {
|
jaroslav@122
|
623 |
// A system class.
|
jaroslav@122
|
624 |
return ClassLoader.getSystemResource(name);
|
jaroslav@122
|
625 |
}
|
jaroslav@122
|
626 |
return cl.getResource(name);
|
jaroslav@122
|
627 |
}
|
jaroslav@56
|
628 |
|
jaroslav@56
|
629 |
|
jaroslav@122
|
630 |
/**
|
jaroslav@122
|
631 |
* Add a package name prefix if the name is not absolute Remove leading "/"
|
jaroslav@122
|
632 |
* if name is absolute
|
jaroslav@122
|
633 |
*/
|
jaroslav@122
|
634 |
private String resolveName(String name) {
|
jaroslav@122
|
635 |
if (name == null) {
|
jaroslav@122
|
636 |
return name;
|
jaroslav@122
|
637 |
}
|
jaroslav@122
|
638 |
if (!name.startsWith("/")) {
|
jaroslav@122
|
639 |
Class<?> c = this;
|
jaroslav@122
|
640 |
while (c.isArray()) {
|
jaroslav@122
|
641 |
c = c.getComponentType();
|
jaroslav@122
|
642 |
}
|
jaroslav@122
|
643 |
String baseName = c.getName();
|
jaroslav@122
|
644 |
int index = baseName.lastIndexOf('.');
|
jaroslav@122
|
645 |
if (index != -1) {
|
jaroslav@122
|
646 |
name = baseName.substring(0, index).replace('.', '/')
|
jaroslav@122
|
647 |
+"/"+name;
|
jaroslav@122
|
648 |
}
|
jaroslav@122
|
649 |
} else {
|
jaroslav@122
|
650 |
name = name.substring(1);
|
jaroslav@122
|
651 |
}
|
jaroslav@122
|
652 |
return name;
|
jaroslav@122
|
653 |
}
|
jaroslav@122
|
654 |
|
jaroslav@122
|
655 |
/**
|
jaroslav@122
|
656 |
* Returns the class loader for the class. Some implementations may use
|
jaroslav@122
|
657 |
* null to represent the bootstrap class loader. This method will return
|
jaroslav@122
|
658 |
* null in such implementations if this class was loaded by the bootstrap
|
jaroslav@122
|
659 |
* class loader.
|
jaroslav@122
|
660 |
*
|
jaroslav@122
|
661 |
* <p> If a security manager is present, and the caller's class loader is
|
jaroslav@122
|
662 |
* not null and the caller's class loader is not the same as or an ancestor of
|
jaroslav@122
|
663 |
* the class loader for the class whose class loader is requested, then
|
jaroslav@122
|
664 |
* this method calls the security manager's {@code checkPermission}
|
jaroslav@122
|
665 |
* method with a {@code RuntimePermission("getClassLoader")}
|
jaroslav@122
|
666 |
* permission to ensure it's ok to access the class loader for the class.
|
jaroslav@122
|
667 |
*
|
jaroslav@122
|
668 |
* <p>If this object
|
jaroslav@122
|
669 |
* represents a primitive type or void, null is returned.
|
jaroslav@122
|
670 |
*
|
jaroslav@122
|
671 |
* @return the class loader that loaded the class or interface
|
jaroslav@122
|
672 |
* represented by this object.
|
jaroslav@122
|
673 |
* @throws SecurityException
|
jaroslav@122
|
674 |
* if a security manager exists and its
|
jaroslav@122
|
675 |
* {@code checkPermission} method denies
|
jaroslav@122
|
676 |
* access to the class loader for the class.
|
jaroslav@122
|
677 |
* @see java.lang.ClassLoader
|
jaroslav@122
|
678 |
* @see SecurityManager#checkPermission
|
jaroslav@122
|
679 |
* @see java.lang.RuntimePermission
|
jaroslav@122
|
680 |
*/
|
jaroslav@122
|
681 |
public ClassLoader getClassLoader() {
|
jaroslav@122
|
682 |
throw new SecurityException();
|
jaroslav@122
|
683 |
}
|
jaroslav@122
|
684 |
|
jaroslav@122
|
685 |
// Package-private to allow ClassLoader access
|
jaroslav@122
|
686 |
native ClassLoader getClassLoader0();
|
jaroslav@56
|
687 |
|
jaroslav@122
|
688 |
/**
|
jaroslav@122
|
689 |
* Returns the {@code Class} representing the component type of an
|
jaroslav@122
|
690 |
* array. If this class does not represent an array class this method
|
jaroslav@122
|
691 |
* returns null.
|
jaroslav@122
|
692 |
*
|
jaroslav@122
|
693 |
* @return the {@code Class} representing the component type of this
|
jaroslav@122
|
694 |
* class if this class is an array
|
jaroslav@122
|
695 |
* @see java.lang.reflect.Array
|
jaroslav@122
|
696 |
* @since JDK1.1
|
jaroslav@122
|
697 |
*/
|
jaroslav@228
|
698 |
public Class<?> getComponentType() {
|
jaroslav@228
|
699 |
return null;
|
jaroslav@228
|
700 |
}
|
jaroslav@56
|
701 |
|
jaroslav@56
|
702 |
/**
|
jaroslav@56
|
703 |
* Returns true if and only if this class was declared as an enum in the
|
jaroslav@56
|
704 |
* source code.
|
jaroslav@56
|
705 |
*
|
jaroslav@56
|
706 |
* @return true if and only if this class was declared as an enum in the
|
jaroslav@56
|
707 |
* source code
|
jaroslav@56
|
708 |
* @since 1.5
|
jaroslav@56
|
709 |
*/
|
jaroslav@56
|
710 |
public boolean isEnum() {
|
jaroslav@56
|
711 |
// An enum must both directly extend java.lang.Enum and have
|
jaroslav@56
|
712 |
// the ENUM bit set; classes for specialized enum constants
|
jaroslav@56
|
713 |
// don't do the former.
|
jaroslav@56
|
714 |
return (this.getModifiers() & ENUM) != 0 &&
|
jaroslav@56
|
715 |
this.getSuperclass() == java.lang.Enum.class;
|
jaroslav@56
|
716 |
}
|
jaroslav@56
|
717 |
|
jaroslav@56
|
718 |
/**
|
jaroslav@56
|
719 |
* Casts an object to the class or interface represented
|
jaroslav@56
|
720 |
* by this {@code Class} object.
|
jaroslav@56
|
721 |
*
|
jaroslav@56
|
722 |
* @param obj the object to be cast
|
jaroslav@56
|
723 |
* @return the object after casting, or null if obj is null
|
jaroslav@56
|
724 |
*
|
jaroslav@56
|
725 |
* @throws ClassCastException if the object is not
|
jaroslav@56
|
726 |
* null and is not assignable to the type T.
|
jaroslav@56
|
727 |
*
|
jaroslav@56
|
728 |
* @since 1.5
|
jaroslav@56
|
729 |
*/
|
jaroslav@56
|
730 |
public T cast(Object obj) {
|
jaroslav@56
|
731 |
if (obj != null && !isInstance(obj))
|
jaroslav@56
|
732 |
throw new ClassCastException(cannotCastMsg(obj));
|
jaroslav@56
|
733 |
return (T) obj;
|
jaroslav@56
|
734 |
}
|
jaroslav@56
|
735 |
|
jaroslav@56
|
736 |
private String cannotCastMsg(Object obj) {
|
jaroslav@56
|
737 |
return "Cannot cast " + obj.getClass().getName() + " to " + getName();
|
jaroslav@56
|
738 |
}
|
jaroslav@56
|
739 |
|
jaroslav@56
|
740 |
/**
|
jaroslav@56
|
741 |
* Casts this {@code Class} object to represent a subclass of the class
|
jaroslav@56
|
742 |
* represented by the specified class object. Checks that that the cast
|
jaroslav@56
|
743 |
* is valid, and throws a {@code ClassCastException} if it is not. If
|
jaroslav@56
|
744 |
* this method succeeds, it always returns a reference to this class object.
|
jaroslav@56
|
745 |
*
|
jaroslav@56
|
746 |
* <p>This method is useful when a client needs to "narrow" the type of
|
jaroslav@56
|
747 |
* a {@code Class} object to pass it to an API that restricts the
|
jaroslav@56
|
748 |
* {@code Class} objects that it is willing to accept. A cast would
|
jaroslav@56
|
749 |
* generate a compile-time warning, as the correctness of the cast
|
jaroslav@56
|
750 |
* could not be checked at runtime (because generic types are implemented
|
jaroslav@56
|
751 |
* by erasure).
|
jaroslav@56
|
752 |
*
|
jaroslav@56
|
753 |
* @return this {@code Class} object, cast to represent a subclass of
|
jaroslav@56
|
754 |
* the specified class object.
|
jaroslav@56
|
755 |
* @throws ClassCastException if this {@code Class} object does not
|
jaroslav@56
|
756 |
* represent a subclass of the specified class (here "subclass" includes
|
jaroslav@56
|
757 |
* the class itself).
|
jaroslav@56
|
758 |
* @since 1.5
|
jaroslav@56
|
759 |
*/
|
jaroslav@56
|
760 |
public <U> Class<? extends U> asSubclass(Class<U> clazz) {
|
jaroslav@56
|
761 |
if (clazz.isAssignableFrom(this))
|
jaroslav@56
|
762 |
return (Class<? extends U>) this;
|
jaroslav@56
|
763 |
else
|
jaroslav@56
|
764 |
throw new ClassCastException(this.toString());
|
jaroslav@56
|
765 |
}
|
jaroslav@56
|
766 |
|
jaroslav@56
|
767 |
/**
|
jaroslav@56
|
768 |
* @throws NullPointerException {@inheritDoc}
|
jaroslav@56
|
769 |
* @since 1.5
|
jaroslav@56
|
770 |
*/
|
jaroslav@56
|
771 |
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
|
jaroslav@65
|
772 |
throw new UnsupportedOperationException();
|
jaroslav@56
|
773 |
}
|
jaroslav@56
|
774 |
|
jaroslav@56
|
775 |
/**
|
jaroslav@56
|
776 |
* @throws NullPointerException {@inheritDoc}
|
jaroslav@56
|
777 |
* @since 1.5
|
jaroslav@56
|
778 |
*/
|
jaroslav@56
|
779 |
public boolean isAnnotationPresent(
|
jaroslav@56
|
780 |
Class<? extends Annotation> annotationClass) {
|
jaroslav@56
|
781 |
if (annotationClass == null)
|
jaroslav@56
|
782 |
throw new NullPointerException();
|
jaroslav@56
|
783 |
|
jaroslav@56
|
784 |
return getAnnotation(annotationClass) != null;
|
jaroslav@56
|
785 |
}
|
jaroslav@56
|
786 |
|
jaroslav@56
|
787 |
|
jaroslav@56
|
788 |
/**
|
jaroslav@56
|
789 |
* @since 1.5
|
jaroslav@56
|
790 |
*/
|
jaroslav@56
|
791 |
public Annotation[] getAnnotations() {
|
jaroslav@65
|
792 |
throw new UnsupportedOperationException();
|
jaroslav@56
|
793 |
}
|
jaroslav@56
|
794 |
|
jaroslav@56
|
795 |
/**
|
jaroslav@56
|
796 |
* @since 1.5
|
jaroslav@56
|
797 |
*/
|
jaroslav@56
|
798 |
public Annotation[] getDeclaredAnnotations() {
|
jaroslav@65
|
799 |
throw new UnsupportedOperationException();
|
jaroslav@56
|
800 |
}
|
jaroslav@56
|
801 |
|
jaroslav@84
|
802 |
static Class getPrimitiveClass(String type) {
|
jaroslav@93
|
803 |
// XXX
|
jaroslav@93
|
804 |
return Object.class;
|
jaroslav@80
|
805 |
}
|
jaroslav@88
|
806 |
|
jaroslav@88
|
807 |
public boolean desiredAssertionStatus() {
|
jaroslav@88
|
808 |
return false;
|
jaroslav@88
|
809 |
}
|
jaroslav@56
|
810 |
}
|