|
1 "{ Package: 'stx:goodies/petitparser/islands/tests' }" |
|
2 |
|
3 PPCompositeParserTest subclass:#JavaParserTest |
|
4 instanceVariableNames:'debugResult context' |
|
5 classVariableNames:'' |
|
6 poolDictionaries:'' |
|
7 category:'PetitIslands-Examples' |
|
8 ! |
|
9 |
|
10 JavaParserTest comment:'' |
|
11 ! |
|
12 |
|
13 |
|
14 !JavaParserTest class methodsFor:'as yet unclassified'! |
|
15 |
|
16 classJavaLangClass |
|
17 ^ self new classJavaLangClass |
|
18 ! |
|
19 |
|
20 classJavaLangObject |
|
21 ^ self new classJavaLangObject |
|
22 ! ! |
|
23 |
|
24 !JavaParserTest methodsFor:'as yet unclassified'! |
|
25 |
|
26 context |
|
27 context ifNil: [ |
|
28 context := PPContext new. |
|
29 ]. |
|
30 ^ context |
|
31 |
|
32 |
|
33 ! |
|
34 |
|
35 createContext: anInput |
|
36 | context | |
|
37 context := PPContext on: self parserInstance stream: anInput asPetitStream. |
|
38 self parserInstance updateContext: context. |
|
39 |
|
40 ^ context |
|
41 |
|
42 |
|
43 ! |
|
44 |
|
45 parserClass |
|
46 ^ JavaParser |
|
47 ! |
|
48 |
|
49 setUp |
|
50 super setUp. |
|
51 context := nil. |
|
52 ! ! |
|
53 |
|
54 !JavaParserTest methodsFor:'src'! |
|
55 |
|
56 classJavaIoInputStream |
|
57 ^ |
|
58 ' |
|
59 /* |
|
60 * @(#)InputStream.java 1.45 04/02/19 |
|
61 * |
|
62 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. |
|
63 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
|
64 */ |
|
65 |
|
66 package java.io; |
|
67 |
|
68 /** |
|
69 * This abstract class is the superclass of all classes representing |
|
70 * an input stream of bytes. |
|
71 * |
|
72 * <p> Applications that need to define a subclass of <code>InputStream</code> |
|
73 * must always provide a method that returns the next byte of input. |
|
74 * |
|
75 * @author Arthur van Hoff |
|
76 * @version 1.45, 02/19/04 |
|
77 * @see java.io.BufferedInputStream |
|
78 * @see java.io.ByteArrayInputStream |
|
79 * @see java.io.DataInputStream |
|
80 * @see java.io.FilterInputStream |
|
81 * @see java.io.InputStream#read() |
|
82 * @see java.io.OutputStream |
|
83 * @see java.io.PushbackInputStream |
|
84 * @since JDK1.0 |
|
85 */ |
|
86 public abstract class InputStream implements Closeable { |
|
87 |
|
88 // SKIP_BUFFER_SIZE is used to determine the size of skipBuffer |
|
89 private static final int SKIP_BUFFER_SIZE = 2048; |
|
90 // skipBuffer is initialized in skip(long), if needed. |
|
91 private static byte[] skipBuffer; |
|
92 |
|
93 /** |
|
94 * Reads the next byte of data from the input stream. The value byte is |
|
95 * returned as an <code>int</code> in the range <code>0</code> to |
|
96 * <code>255</code>. If no byte is available because the end of the stream |
|
97 * has been reached, the value <code>-1</code> is returned. This method |
|
98 * blocks until input data is available, the end of the stream is detected, |
|
99 * or an exception is thrown. |
|
100 * |
|
101 * <p> A subclass must provide an implementation of this method. |
|
102 * |
|
103 * @return the next byte of data, or <code>-1</code> if the end of the |
|
104 * stream is reached. |
|
105 * @exception IOException if an I/O error occurs. |
|
106 */ |
|
107 public abstract int read() throws IOException; |
|
108 |
|
109 /** |
|
110 * Reads some number of bytes from the input stream and stores them into |
|
111 * the buffer array <code>b</code>. The number of bytes actually read is |
|
112 * returned as an integer. This method blocks until input data is |
|
113 * available, end of file is detected, or an exception is thrown. |
|
114 * |
|
115 * <p> If <code>b</code> is <code>null</code>, a |
|
116 * <code>NullPointerException</code> is thrown. If the length of |
|
117 * <code>b</code> is zero, then no bytes are read and <code>0</code> is |
|
118 * returned; otherwise, there is an attempt to read at least one byte. If |
|
119 * no byte is available because the stream is at end of file, the value |
|
120 * <code>-1</code> is returned; otherwise, at least one byte is read and |
|
121 * stored into <code>b</code>. |
|
122 * |
|
123 * <p> The first byte read is stored into element <code>b[0]</code>, the |
|
124 * next one into <code>b[1]</code>, and so on. The number of bytes read is, |
|
125 * at most, equal to the length of <code>b</code>. Let <i>k</i> be the |
|
126 * number of bytes actually read; these bytes will be stored in elements |
|
127 * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>, |
|
128 * leaving elements <code>b[</code><i>k</i><code>]</code> through |
|
129 * <code>b[b.length-1]</code> unaffected. |
|
130 * |
|
131 * <p> If the first byte cannot be read for any reason other than end of |
|
132 * file, then an <code>IOException</code> is thrown. In particular, an |
|
133 * <code>IOException</code> is thrown if the input stream has been closed. |
|
134 * |
|
135 * <p> The <code>read(b)</code> method for class <code>InputStream</code> |
|
136 * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre> |
|
137 * |
|
138 * @param b the buffer into which the data is read. |
|
139 * @return the total number of bytes read into the buffer, or |
|
140 * <code>-1</code> is there is no more data because the end of |
|
141 * the stream has been reached. |
|
142 * @exception IOException if an I/O error occurs. |
|
143 * @exception NullPointerException if <code>b</code> is <code>null</code>. |
|
144 * @see java.io.InputStream#read(byte[], int, int) |
|
145 */ |
|
146 public int read(byte b[]) throws IOException { |
|
147 return read(b, 0, b.length); |
|
148 } |
|
149 |
|
150 /** |
|
151 * Reads up to <code>len</code> bytes of data from the input stream into |
|
152 * an array of bytes. An attempt is made to read as many as |
|
153 * <code>len</code> bytes, but a smaller number may be read. |
|
154 * The number of bytes actually read is returned as an integer. |
|
155 * |
|
156 * <p> This method blocks until input data is available, end of file is |
|
157 * detected, or an exception is thrown. |
|
158 * |
|
159 * <p> If <code>b</code> is <code>null</code>, a |
|
160 * <code>NullPointerException</code> is thrown. |
|
161 * |
|
162 * <p> If <code>off</code> is negative, or <code>len</code> is negative, or |
|
163 * <code>off+len</code> is greater than the length of the array |
|
164 * <code>b</code>, then an <code>IndexOutOfBoundsException</code> is |
|
165 * thrown. |
|
166 * |
|
167 * <p> If <code>len</code> is zero, then no bytes are read and |
|
168 * <code>0</code> is returned; otherwise, there is an attempt to read at |
|
169 * least one byte. If no byte is available because the stream is at end of |
|
170 * file, the value <code>-1</code> is returned; otherwise, at least one |
|
171 * byte is read and stored into <code>b</code>. |
|
172 * |
|
173 * <p> The first byte read is stored into element <code>b[off]</code>, the |
|
174 * next one into <code>b[off+1]</code>, and so on. The number of bytes read |
|
175 * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of |
|
176 * bytes actually read; these bytes will be stored in elements |
|
177 * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>, |
|
178 * leaving elements <code>b[off+</code><i>k</i><code>]</code> through |
|
179 * <code>b[off+len-1]</code> unaffected. |
|
180 * |
|
181 * <p> In every case, elements <code>b[0]</code> through |
|
182 * <code>b[off]</code> and elements <code>b[off+len]</code> through |
|
183 * <code>b[b.length-1]</code> are unaffected. |
|
184 * |
|
185 * <p> If the first byte cannot be read for any reason other than end of |
|
186 * file, then an <code>IOException</code> is thrown. In particular, an |
|
187 * <code>IOException</code> is thrown if the input stream has been closed. |
|
188 * |
|
189 * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method |
|
190 * for class <code>InputStream</code> simply calls the method |
|
191 * <code>read()</code> repeatedly. If the first such call results in an |
|
192 * <code>IOException</code>, that exception is returned from the call to |
|
193 * the <code>read(b,</code> <code>off,</code> <code>len)</code> method. If |
|
194 * any subsequent call to <code>read()</code> results in a |
|
195 * <code>IOException</code>, the exception is caught and treated as if it |
|
196 * were end of file; the bytes read up to that point are stored into |
|
197 * <code>b</code> and the number of bytes read before the exception |
|
198 * occurred is returned. Subclasses are encouraged to provide a more |
|
199 * efficient implementation of this method. |
|
200 * |
|
201 * @param b the buffer into which the data is read. |
|
202 * @param off the start offset in array <code>b</code> |
|
203 * at which the data is written. |
|
204 * @param len the maximum number of bytes to read. |
|
205 * @return the total number of bytes read into the buffer, or |
|
206 * <code>-1</code> if there is no more data because the end of |
|
207 * the stream has been reached. |
|
208 * @exception IOException if an I/O error occurs. |
|
209 * @exception NullPointerException if <code>b</code> is <code>null</code>. |
|
210 * @see java.io.InputStream#read() |
|
211 */ |
|
212 public int read(byte b[], int off, int len) throws IOException { |
|
213 if (b == null) { |
|
214 throw new NullPointerException(); |
|
215 } else if ((off < 0) || (off > b.length) || (len < 0) || |
|
216 ((off + len) > b.length) || ((off + len) < 0)) { |
|
217 throw new IndexOutOfBoundsException(); |
|
218 } else if (len == 0) { |
|
219 return 0; |
|
220 } |
|
221 |
|
222 int c = read(); |
|
223 if (c == -1) { |
|
224 return -1; |
|
225 } |
|
226 b[off] = (byte)c; |
|
227 |
|
228 int i = 1; |
|
229 try { |
|
230 for (; i < len ; i++) { |
|
231 c = read(); |
|
232 if (c == -1) { |
|
233 break; |
|
234 } |
|
235 if (b !!= null) { |
|
236 b[off + i] = (byte)c; |
|
237 } |
|
238 } |
|
239 } catch (IOException ee) { |
|
240 } |
|
241 return i; |
|
242 } |
|
243 |
|
244 /** |
|
245 * Skips over and discards <code>n</code> bytes of data from this input |
|
246 * stream. The <code>skip</code> method may, for a variety of reasons, end |
|
247 * up skipping over some smaller number of bytes, possibly <code>0</code>. |
|
248 * This may result from any of a number of conditions; reaching end of file |
|
249 * before <code>n</code> bytes have been skipped is only one possibility. |
|
250 * The actual number of bytes skipped is returned. If <code>n</code> is |
|
251 * negative, no bytes are skipped. |
|
252 * |
|
253 * <p> The <code>skip</code> method of <code>InputStream</code> creates a |
|
254 * byte array and then repeatedly reads into it until <code>n</code> bytes |
|
255 * have been read or the end of the stream has been reached. Subclasses are |
|
256 * encouraged to provide a more efficient implementation of this method. |
|
257 * |
|
258 * @param n the number of bytes to be skipped. |
|
259 * @return the actual number of bytes skipped. |
|
260 * @exception IOException if an I/O error occurs. |
|
261 */ |
|
262 public long skip(long n) throws IOException { |
|
263 |
|
264 long remaining = n; |
|
265 int nr; |
|
266 if (skipBuffer == null) |
|
267 skipBuffer = new byte[SKIP_BUFFER_SIZE]; |
|
268 |
|
269 byte[] localSkipBuffer = skipBuffer; |
|
270 |
|
271 if (n <= 0) { |
|
272 return 0; |
|
273 } |
|
274 |
|
275 while (remaining > 0) { |
|
276 nr = read(localSkipBuffer, 0, |
|
277 (int) Math.min(SKIP_BUFFER_SIZE, remaining)); |
|
278 if (nr < 0) { |
|
279 break; |
|
280 } |
|
281 remaining -= nr; |
|
282 } |
|
283 |
|
284 return n - remaining; |
|
285 } |
|
286 |
|
287 /** |
|
288 * Returns the number of bytes that can be read (or skipped over) from |
|
289 * this input stream without blocking by the next caller of a method for |
|
290 * this input stream. The next caller might be the same thread or |
|
291 * another thread. |
|
292 * |
|
293 * <p> The <code>available</code> method for class <code>InputStream</code> |
|
294 * always returns <code>0</code>. |
|
295 * |
|
296 * <p> This method should be overridden by subclasses. |
|
297 * |
|
298 * @return the number of bytes that can be read from this input stream |
|
299 * without blocking. |
|
300 * @exception IOException if an I/O error occurs. |
|
301 */ |
|
302 public int available() throws IOException { |
|
303 return 0; |
|
304 } |
|
305 |
|
306 /** |
|
307 * Closes this input stream and releases any system resources associated |
|
308 * with the stream. |
|
309 * |
|
310 * <p> The <code>close</code> method of <code>InputStream</code> does |
|
311 * nothing. |
|
312 * |
|
313 * @exception IOException if an I/O error occurs. |
|
314 */ |
|
315 public void close() throws IOException {} |
|
316 |
|
317 /** |
|
318 * Marks the current position in this input stream. A subsequent call to |
|
319 * the <code>reset</code> method repositions this stream at the last marked |
|
320 * position so that subsequent reads re-read the same bytes. |
|
321 * |
|
322 * <p> The <code>readlimit</code> arguments tells this input stream to |
|
323 * allow that many bytes to be read before the mark position gets |
|
324 * invalidated. |
|
325 * |
|
326 * <p> The general contract of <code>mark</code> is that, if the method |
|
327 * <code>markSupported</code> returns <code>true</code>, the stream somehow |
|
328 * remembers all the bytes read after the call to <code>mark</code> and |
|
329 * stands ready to supply those same bytes again if and whenever the method |
|
330 * <code>reset</code> is called. However, the stream is not required to |
|
331 * remember any data at all if more than <code>readlimit</code> bytes are |
|
332 * read from the stream before <code>reset</code> is called. |
|
333 * |
|
334 * <p> The <code>mark</code> method of <code>InputStream</code> does |
|
335 * nothing. |
|
336 * |
|
337 * @param readlimit the maximum limit of bytes that can be read before |
|
338 * the mark position becomes invalid. |
|
339 * @see java.io.InputStream#reset() |
|
340 */ |
|
341 public synchronized void mark(int readlimit) {} |
|
342 |
|
343 /** |
|
344 * Repositions this stream to the position at the time the |
|
345 * <code>mark</code> method was last called on this input stream. |
|
346 * |
|
347 * <p> The general contract of <code>reset</code> is: |
|
348 * |
|
349 * <p><ul> |
|
350 * |
|
351 * <li> If the method <code>markSupported</code> returns |
|
352 * <code>true</code>, then: |
|
353 * |
|
354 * <ul><li> If the method <code>mark</code> has not been called since |
|
355 * the stream was created, or the number of bytes read from the stream |
|
356 * since <code>mark</code> was last called is larger than the argument |
|
357 * to <code>mark</code> at that last call, then an |
|
358 * <code>IOException</code> might be thrown. |
|
359 * |
|
360 * <li> If such an <code>IOException</code> is not thrown, then the |
|
361 * stream is reset to a state such that all the bytes read since the |
|
362 * most recent call to <code>mark</code> (or since the start of the |
|
363 * file, if <code>mark</code> has not been called) will be resupplied |
|
364 * to subsequent callers of the <code>read</code> method, followed by |
|
365 * any bytes that otherwise would have been the next input data as of |
|
366 * the time of the call to <code>reset</code>. </ul> |
|
367 * |
|
368 * <li> If the method <code>markSupported</code> returns |
|
369 * <code>false</code>, then: |
|
370 * |
|
371 * <ul><li> The call to <code>reset</code> may throw an |
|
372 * <code>IOException</code>. |
|
373 * |
|
374 * <li> If an <code>IOException</code> is not thrown, then the stream |
|
375 * is reset to a fixed state that depends on the particular type of the |
|
376 * input stream and how it was created. The bytes that will be supplied |
|
377 * to subsequent callers of the <code>read</code> method depend on the |
|
378 * particular type of the input stream. </ul></ul> |
|
379 * |
|
380 * <p>The method <code>reset</code> for class <code>InputStream</code> |
|
381 * does nothing except throw an <code>IOException</code>. |
|
382 * |
|
383 * @exception IOException if this stream has not been marked or if the |
|
384 * mark has been invalidated. |
|
385 * @see java.io.InputStream#mark(int) |
|
386 * @see java.io.IOException |
|
387 */ |
|
388 public synchronized void reset() throws IOException { |
|
389 throw new IOException("mark/reset not supported"); |
|
390 } |
|
391 |
|
392 /** |
|
393 * Tests if this input stream supports the <code>mark</code> and |
|
394 * <code>reset</code> methods. Whether or not <code>mark</code> and |
|
395 * <code>reset</code> are supported is an invariant property of a |
|
396 * particular input stream instance. The <code>markSupported</code> method |
|
397 * of <code>InputStream</code> returns <code>false</code>. |
|
398 * |
|
399 * @return <code>true</code> if this stream instance supports the mark |
|
400 * and reset methods; <code>false</code> otherwise. |
|
401 * @see java.io.InputStream#mark(int) |
|
402 * @see java.io.InputStream#reset() |
|
403 */ |
|
404 public boolean markSupported() { |
|
405 return false; |
|
406 } |
|
407 |
|
408 } |
|
409 ' |
|
410 ! |
|
411 |
|
412 classJavaLangClass |
|
413 ^ |
|
414 '/* |
|
415 * Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved. |
|
416 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
417 * |
|
418 * This code is free software; you can redistribute it and/or modify it |
|
419 * under the terms of the GNU General Public License version 2 only, as |
|
420 * published by the Free Software Foundation. Oracle designates this |
|
421 * particular file as subject to the "Classpath" exception as provided |
|
422 * by Oracle in the LICENSE file that accompanied this code. |
|
423 * |
|
424 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
425 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
426 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
427 * version 2 for more details (a copy is included in the LICENSE file that |
|
428 * accompanied this code). |
|
429 * |
|
430 * You should have received a copy of the GNU General Public License version |
|
431 * 2 along with this work; if not, write to the Free Software Foundation, |
|
432 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
433 * |
|
434 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
435 * or visit www.oracle.com if you need additional information or have any |
|
436 * questions. |
|
437 */ |
|
438 |
|
439 package java.lang; |
|
440 |
|
441 import java.lang.reflect.Array; |
|
442 import java.lang.reflect.GenericArrayType; |
|
443 import java.lang.reflect.Member; |
|
444 import java.lang.reflect.Field; |
|
445 import java.lang.reflect.Method; |
|
446 import java.lang.reflect.Constructor; |
|
447 import java.lang.reflect.GenericDeclaration; |
|
448 import java.lang.reflect.Modifier; |
|
449 import java.lang.reflect.Type; |
|
450 import java.lang.reflect.TypeVariable; |
|
451 import java.lang.reflect.InvocationTargetException; |
|
452 import java.lang.ref.SoftReference; |
|
453 import java.io.InputStream; |
|
454 import java.io.ObjectStreamField; |
|
455 import java.security.AccessController; |
|
456 import java.security.PrivilegedAction; |
|
457 import java.util.ArrayList; |
|
458 import java.util.Arrays; |
|
459 import java.util.Collection; |
|
460 import java.util.HashSet; |
|
461 import java.util.Iterator; |
|
462 import java.util.List; |
|
463 import java.util.LinkedList; |
|
464 import java.util.LinkedHashSet; |
|
465 import java.util.Set; |
|
466 import java.util.Map; |
|
467 import java.util.HashMap; |
|
468 import sun.misc.Unsafe; |
|
469 import sun.reflect.ConstantPool; |
|
470 import sun.reflect.Reflection; |
|
471 import sun.reflect.ReflectionFactory; |
|
472 import sun.reflect.SignatureIterator; |
|
473 import sun.reflect.generics.factory.CoreReflectionFactory; |
|
474 import sun.reflect.generics.factory.GenericsFactory; |
|
475 import sun.reflect.generics.repository.ClassRepository; |
|
476 import sun.reflect.generics.repository.MethodRepository; |
|
477 import sun.reflect.generics.repository.ConstructorRepository; |
|
478 import sun.reflect.generics.scope.ClassScope; |
|
479 import sun.security.util.SecurityConstants; |
|
480 import java.lang.annotation.Annotation; |
|
481 import sun.reflect.annotation.*; |
|
482 |
|
483 /** |
|
484 * Instances of the class {@code Class} represent classes and |
|
485 * interfaces in a running Java application. An enum is a kind of |
|
486 * class and an annotation is a kind of interface. Every array also |
|
487 * belongs to a class that is reflected as a {@code Class} object |
|
488 * that is shared by all arrays with the same element type and number |
|
489 * of dimensions. The primitive Java types ({@code boolean}, |
|
490 * {@code byte}, {@code char}, {@code short}, |
|
491 * {@code int}, {@code long}, {@code float}, and |
|
492 * {@code double}), and the keyword {@code void} are also |
|
493 * represented as {@code Class} objects. |
|
494 * |
|
495 * <p> {@code Class} has no public constructor. Instead {@code Class} |
|
496 * objects are constructed automatically by the Java Virtual Machine as classes |
|
497 * are loaded and by calls to the {@code defineClass} method in the class |
|
498 * loader. |
|
499 * |
|
500 * <p> The following example uses a {@code Class} object to print the |
|
501 * class name of an object: |
|
502 * |
|
503 * <p> <blockquote><pre> |
|
504 * void printClassName(Object obj) { |
|
505 * System.out.println("The class of " + obj + |
|
506 * " is " + obj.getClass().getName()); |
|
507 * } |
|
508 * </pre></blockquote> |
|
509 * |
|
510 * <p> It is also possible to get the {@code Class} object for a named |
|
511 * type (or for void) using a class literal |
|
512 * (JLS Section <A HREF="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#251530">15.8.2</A>). |
|
513 * For example: |
|
514 * |
|
515 * <p> <blockquote> |
|
516 * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());} |
|
517 * </blockquote> |
|
518 * |
|
519 * @param <T> the type of the class modeled by this {@code Class} |
|
520 * object. For example, the type of {@code String.class} is {@code |
|
521 * Class<String>}. Use {@code Class<?>} if the class being modeled is |
|
522 * unknown. |
|
523 * |
|
524 * @author unascribed |
|
525 * @see java.lang.ClassLoader#defineClass(byte[], int, int) |
|
526 * @since JDK1.0 |
|
527 */ |
|
528 public final |
|
529 class Class<T> implements java.io.Serializable, |
|
530 java.lang.reflect.GenericDeclaration, |
|
531 java.lang.reflect.Type, |
|
532 java.lang.reflect.AnnotatedElement { |
|
533 private static final int ANNOTATION= 0x00002000; |
|
534 private static final int ENUM = 0x00004000; |
|
535 private static final int SYNTHETIC = 0x00001000; |
|
536 |
|
537 private static native void registerNatives(); |
|
538 static { |
|
539 registerNatives(); |
|
540 } |
|
541 |
|
542 /* |
|
543 * Constructor. Only the Java Virtual Machine creates Class |
|
544 * objects. |
|
545 */ |
|
546 private Class() {} |
|
547 |
|
548 |
|
549 /** |
|
550 * Converts the object to a string. The string representation is the |
|
551 * string "class" or "interface", followed by a space, and then by the |
|
552 * fully qualified name of the class in the format returned by |
|
553 * {@code getName}. If this {@code Class} object represents a |
|
554 * primitive type, this method returns the name of the primitive type. If |
|
555 * this {@code Class} object represents void this method returns |
|
556 * "void". |
|
557 * |
|
558 * @return a string representation of this class object. |
|
559 */ |
|
560 public String toString() { |
|
561 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) |
|
562 + getName(); |
|
563 } |
|
564 |
|
565 |
|
566 /** |
|
567 * Returns the {@code Class} object associated with the class or |
|
568 * interface with the given string name. Invoking this method is |
|
569 * equivalent to: |
|
570 * |
|
571 * <blockquote> |
|
572 * {@code Class.forName(className, true, currentLoader)} |
|
573 * </blockquote> |
|
574 * |
|
575 * where {@code currentLoader} denotes the defining class loader of |
|
576 * the current class. |
|
577 * |
|
578 * <p> For example, the following code fragment returns the |
|
579 * runtime {@code Class} descriptor for the class named |
|
580 * {@code java.lang.Thread}: |
|
581 * |
|
582 * <blockquote> |
|
583 * {@code Class t = Class.forName("java.lang.Thread")} |
|
584 * </blockquote> |
|
585 * <p> |
|
586 * A call to {@code forName("X")} causes the class named |
|
587 * {@code X} to be initialized. |
|
588 * |
|
589 * @param className the fully qualified name of the desired class. |
|
590 * @return the {@code Class} object for the class with the |
|
591 * specified name. |
|
592 * @exception LinkageError if the linkage fails |
|
593 * @exception ExceptionInInitializerError if the initialization provoked |
|
594 * by this method fails |
|
595 * @exception ClassNotFoundException if the class cannot be located |
|
596 */ |
|
597 public static Class<?> forName(String className) |
|
598 throws ClassNotFoundException { |
|
599 return forName0(className, true, ClassLoader.getCallerClassLoader()); |
|
600 } |
|
601 |
|
602 |
|
603 /** |
|
604 * Returns the {@code Class} object associated with the class or |
|
605 * interface with the given string name, using the given class loader. |
|
606 * Given the fully qualified name for a class or interface (in the same |
|
607 * format returned by {@code getName}) this method attempts to |
|
608 * locate, load, and link the class or interface. The specified class |
|
609 * loader is used to load the class or interface. If the parameter |
|
610 * {@code loader} is null, the class is loaded through the bootstrap |
|
611 * class loader. The class is initialized only if the |
|
612 * {@code initialize} parameter is {@code true} and if it has |
|
613 * not been initialized earlier. |
|
614 * |
|
615 * <p> If {@code name} denotes a primitive type or void, an attempt |
|
616 * will be made to locate a user-defined class in the unnamed package whose |
|
617 * name is {@code name}. Therefore, this method cannot be used to |
|
618 * obtain any of the {@code Class} objects representing primitive |
|
619 * types or void. |
|
620 * |
|
621 * <p> If {@code name} denotes an array class, the component type of |
|
622 * the array class is loaded but not initialized. |
|
623 * |
|
624 * <p> For example, in an instance method the expression: |
|
625 * |
|
626 * <blockquote> |
|
627 * {@code Class.forName("Foo")} |
|
628 * </blockquote> |
|
629 * |
|
630 * is equivalent to: |
|
631 * |
|
632 * <blockquote> |
|
633 * {@code Class.forName("Foo", true, this.getClass().getClassLoader())} |
|
634 * </blockquote> |
|
635 * |
|
636 * Note that this method throws errors related to loading, linking or |
|
637 * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The |
|
638 * Java Language Specification</em>. |
|
639 * Note that this method does not check whether the requested class |
|
640 * is accessible to its caller. |
|
641 * |
|
642 * <p> If the {@code loader} is {@code null}, and a security |
|
643 * manager is present, and the caller''s class loader is not null, then this |
|
644 * method calls the security manager''s {@code checkPermission} method |
|
645 * with a {@code RuntimePermission("getClassLoader")} permission to |
|
646 * ensure it''s ok to access the bootstrap class loader. |
|
647 * |
|
648 * @param name fully qualified name of the desired class |
|
649 * @param initialize whether the class must be initialized |
|
650 * @param loader class loader from which the class must be loaded |
|
651 * @return class object representing the desired class |
|
652 * |
|
653 * @exception LinkageError if the linkage fails |
|
654 * @exception ExceptionInInitializerError if the initialization provoked |
|
655 * by this method fails |
|
656 * @exception ClassNotFoundException if the class cannot be located by |
|
657 * the specified class loader |
|
658 * |
|
659 * @see java.lang.Class#forName(String) |
|
660 * @see java.lang.ClassLoader |
|
661 * @since 1.2 |
|
662 */ |
|
663 public static Class<?> forName(String name, boolean initialize, |
|
664 ClassLoader loader) |
|
665 throws ClassNotFoundException |
|
666 { |
|
667 if (loader == null) { |
|
668 SecurityManager sm = System.getSecurityManager(); |
|
669 if (sm !!= null) { |
|
670 ClassLoader ccl = ClassLoader.getCallerClassLoader(); |
|
671 if (ccl !!= null) { |
|
672 sm.checkPermission( |
|
673 SecurityConstants.GET_CLASSLOADER_PERMISSION); |
|
674 } |
|
675 } |
|
676 } |
|
677 return forName0(name, initialize, loader); |
|
678 } |
|
679 |
|
680 /** Called after security checks have been made. */ |
|
681 private static native Class forName0(String name, boolean initialize, |
|
682 ClassLoader loader) |
|
683 throws ClassNotFoundException; |
|
684 |
|
685 /** |
|
686 * Creates a new instance of the class represented by this {@code Class} |
|
687 * object. The class is instantiated as if by a {@code new} |
|
688 * expression with an empty argument list. The class is initialized if it |
|
689 * has not already been initialized. |
|
690 * |
|
691 * <p>Note that this method propagates any exception thrown by the |
|
692 * nullary constructor, including a checked exception. Use of |
|
693 * this method effectively bypasses the compile-time exception |
|
694 * checking that would otherwise be performed by the compiler. |
|
695 * The {@link |
|
696 * java.lang.reflect.Constructor#newInstance(java.lang.Object...) |
|
697 * Constructor.newInstance} method avoids this problem by wrapping |
|
698 * any exception thrown by the constructor in a (checked) {@link |
|
699 * java.lang.reflect.InvocationTargetException}. |
|
700 * |
|
701 * @return a newly allocated instance of the class represented by this |
|
702 * object. |
|
703 * @exception IllegalAccessException if the class or its nullary |
|
704 * constructor is not accessible. |
|
705 * @exception InstantiationException |
|
706 * if this {@code Class} represents an abstract class, |
|
707 * an interface, an array class, a primitive type, or void; |
|
708 * or if the class has no nullary constructor; |
|
709 * or if the instantiation fails for some other reason. |
|
710 * @exception ExceptionInInitializerError if the initialization |
|
711 * provoked by this method fails. |
|
712 * @exception SecurityException |
|
713 * If a security manager, <i>s</i>, is present and any of the |
|
714 * following conditions is met: |
|
715 * |
|
716 * <ul> |
|
717 * |
|
718 * <li> invocation of |
|
719 * {@link SecurityManager#checkMemberAccess |
|
720 * s.checkMemberAccess(this, Member.PUBLIC)} denies |
|
721 * creation of new instances of this class |
|
722 * |
|
723 * <li> the caller''s class loader is not the same as or an |
|
724 * ancestor of the class loader for the current class and |
|
725 * invocation of {@link SecurityManager#checkPackageAccess |
|
726 * s.checkPackageAccess()} denies access to the package |
|
727 * of this class |
|
728 * |
|
729 * </ul> |
|
730 * |
|
731 */ |
|
732 public T newInstance() |
|
733 throws InstantiationException, IllegalAccessException |
|
734 { |
|
735 if (System.getSecurityManager() !!= null) { |
|
736 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); |
|
737 } |
|
738 return newInstance0(); |
|
739 } |
|
740 |
|
741 private T newInstance0() |
|
742 throws InstantiationException, IllegalAccessException |
|
743 { |
|
744 // NOTE: the following code may not be strictly correct under |
|
745 // the current Java memory model. |
|
746 |
|
747 // Constructor lookup |
|
748 if (cachedConstructor == null) { |
|
749 if (this == Class.class) { |
|
750 throw new IllegalAccessException( |
|
751 "Can not call newInstance() on the Class for java.lang.Class" |
|
752 ); |
|
753 } |
|
754 try { |
|
755 Class[] empty = {}; |
|
756 final Constructor<T> c = getConstructor0(empty, Member.DECLARED); |
|
757 // Disable accessibility checks on the constructor |
|
758 // since we have to do the security check here anyway |
|
759 // (the stack depth is wrong for the Constructor''s |
|
760 // security check to work) |
|
761 java.security.AccessController.doPrivileged |
|
762 (new java.security.PrivilegedAction() { |
|
763 public Object run() { |
|
764 c.setAccessible(true); |
|
765 return null; |
|
766 } |
|
767 }); |
|
768 cachedConstructor = c; |
|
769 } catch (NoSuchMethodException e) { |
|
770 throw new InstantiationException(getName()); |
|
771 } |
|
772 } |
|
773 Constructor<T> tmpConstructor = cachedConstructor; |
|
774 // Security check (same as in java.lang.reflect.Constructor) |
|
775 int modifiers = tmpConstructor.getModifiers(); |
|
776 if (!!Reflection.quickCheckMemberAccess(this, modifiers)) { |
|
777 Class caller = Reflection.getCallerClass(3); |
|
778 if (newInstanceCallerCache !!= caller) { |
|
779 Reflection.ensureMemberAccess(caller, this, null, modifiers); |
|
780 newInstanceCallerCache = caller; |
|
781 } |
|
782 } |
|
783 // Run constructor |
|
784 try { |
|
785 return tmpConstructor.newInstance((Object[])null); |
|
786 } catch (InvocationTargetException e) { |
|
787 Unsafe.getUnsafe().throwException(e.getTargetException()); |
|
788 // Not reached |
|
789 return null; |
|
790 } |
|
791 } |
|
792 private volatile transient Constructor<T> cachedConstructor; |
|
793 private volatile transient Class newInstanceCallerCache; |
|
794 |
|
795 |
|
796 /** |
|
797 * Determines if the specified {@code Object} is assignment-compatible |
|
798 * with the object represented by this {@code Class}. This method is |
|
799 * the dynamic equivalent of the Java language {@code instanceof} |
|
800 * operator. The method returns {@code true} if the specified |
|
801 * {@code Object} argument is non-null and can be cast to the |
|
802 * reference type represented by this {@code Class} object without |
|
803 * raising a {@code ClassCastException.} It returns {@code false} |
|
804 * otherwise. |
|
805 * |
|
806 * <p> Specifically, if this {@code Class} object represents a |
|
807 * declared class, this method returns {@code true} if the specified |
|
808 * {@code Object} argument is an instance of the represented class (or |
|
809 * of any of its subclasses); it returns {@code false} otherwise. If |
|
810 * this {@code Class} object represents an array class, this method |
|
811 * returns {@code true} if the specified {@code Object} argument |
|
812 * can be converted to an object of the array class by an identity |
|
813 * conversion or by a widening reference conversion; it returns |
|
814 * {@code false} otherwise. If this {@code Class} object |
|
815 * represents an interface, this method returns {@code true} if the |
|
816 * class or any superclass of the specified {@code Object} argument |
|
817 * implements this interface; it returns {@code false} otherwise. If |
|
818 * this {@code Class} object represents a primitive type, this method |
|
819 * returns {@code false}. |
|
820 * |
|
821 * @param obj the object to check |
|
822 * @return true if {@code obj} is an instance of this class |
|
823 * |
|
824 * @since JDK1.1 |
|
825 */ |
|
826 public native boolean isInstance(Object obj); |
|
827 |
|
828 |
|
829 /** |
|
830 * Determines if the class or interface represented by this |
|
831 * {@code Class} object is either the same as, or is a superclass or |
|
832 * superinterface of, the class or interface represented by the specified |
|
833 * {@code Class} parameter. It returns {@code true} if so; |
|
834 * otherwise it returns {@code false}. If this {@code Class} |
|
835 * object represents a primitive type, this method returns |
|
836 * {@code true} if the specified {@code Class} parameter is |
|
837 * exactly this {@code Class} object; otherwise it returns |
|
838 * {@code false}. |
|
839 * |
|
840 * <p> Specifically, this method tests whether the type represented by the |
|
841 * specified {@code Class} parameter can be converted to the type |
|
842 * represented by this {@code Class} object via an identity conversion |
|
843 * or via a widening reference conversion. See <em>The Java Language |
|
844 * Specification</em>, sections 5.1.1 and 5.1.4 , for details. |
|
845 * |
|
846 * @param cls the {@code Class} object to be checked |
|
847 * @return the {@code boolean} value indicating whether objects of the |
|
848 * type {@code cls} can be assigned to objects of this class |
|
849 * @exception NullPointerException if the specified Class parameter is |
|
850 * null. |
|
851 * @since JDK1.1 |
|
852 */ |
|
853 public native boolean isAssignableFrom(Class<?> cls); |
|
854 |
|
855 |
|
856 /** |
|
857 * Determines if the specified {@code Class} object represents an |
|
858 * interface type. |
|
859 * |
|
860 * @return {@code true} if this object represents an interface; |
|
861 * {@code false} otherwise. |
|
862 */ |
|
863 public native boolean isInterface(); |
|
864 |
|
865 |
|
866 /** |
|
867 * Determines if this {@code Class} object represents an array class. |
|
868 * |
|
869 * @return {@code true} if this object represents an array class; |
|
870 * {@code false} otherwise. |
|
871 * @since JDK1.1 |
|
872 */ |
|
873 public native boolean isArray(); |
|
874 |
|
875 |
|
876 /** |
|
877 * Determines if the specified {@code Class} object represents a |
|
878 * primitive type. |
|
879 * |
|
880 * <p> There are nine predefined {@code Class} objects to represent |
|
881 * the eight primitive types and void. These are created by the Java |
|
882 * Virtual Machine, and have the same names as the primitive types that |
|
883 * they represent, namely {@code boolean}, {@code byte}, |
|
884 * {@code char}, {@code short}, {@code int}, |
|
885 * {@code long}, {@code float}, and {@code double}. |
|
886 * |
|
887 * <p> These objects may only be accessed via the following public static |
|
888 * final variables, and are the only {@code Class} objects for which |
|
889 * this method returns {@code true}. |
|
890 * |
|
891 * @return true if and only if this class represents a primitive type |
|
892 * |
|
893 * @see java.lang.Boolean#TYPE |
|
894 * @see java.lang.Character#TYPE |
|
895 * @see java.lang.Byte#TYPE |
|
896 * @see java.lang.Short#TYPE |
|
897 * @see java.lang.Integer#TYPE |
|
898 * @see java.lang.Long#TYPE |
|
899 * @see java.lang.Float#TYPE |
|
900 * @see java.lang.Double#TYPE |
|
901 * @see java.lang.Void#TYPE |
|
902 * @since JDK1.1 |
|
903 */ |
|
904 public native boolean isPrimitive(); |
|
905 |
|
906 /** |
|
907 * Returns true if this {@code Class} object represents an annotation |
|
908 * type. Note that if this method returns true, {@link #isInterface()} |
|
909 * would also return true, as all annotation types are also interfaces. |
|
910 * |
|
911 * @return {@code true} if this class object represents an annotation |
|
912 * type; {@code false} otherwise |
|
913 * @since 1.5 |
|
914 */ |
|
915 public boolean isAnnotation() { |
|
916 return (getModifiers() & ANNOTATION) !!= 0; |
|
917 } |
|
918 |
|
919 /** |
|
920 * Returns {@code true} if this class is a synthetic class; |
|
921 * returns {@code false} otherwise. |
|
922 * @return {@code true} if and only if this class is a synthetic class as |
|
923 * defined by the Java Language Specification. |
|
924 * @since 1.5 |
|
925 */ |
|
926 public boolean isSynthetic() { |
|
927 return (getModifiers() & SYNTHETIC) !!= 0; |
|
928 } |
|
929 |
|
930 /** |
|
931 * Returns the name of the entity (class, interface, array class, |
|
932 * primitive type, or void) represented by this {@code Class} object, |
|
933 * as a {@code String}. |
|
934 * |
|
935 * <p> If this class object represents a reference type that is not an |
|
936 * array type then the binary name of the class is returned, as specified |
|
937 * by the Java Language Specification, Second Edition. |
|
938 * |
|
939 * <p> If this class object represents a primitive type or void, then the |
|
940 * name returned is a {@code String} equal to the Java language |
|
941 * keyword corresponding to the primitive type or void. |
|
942 * |
|
943 * <p> If this class object represents a class of arrays, then the internal |
|
944 * form of the name consists of the name of the element type preceded by |
|
945 * one or more ''{@code [}'' characters representing the depth of the array |
|
946 * nesting. The encoding of element type names is as follows: |
|
947 * |
|
948 * <blockquote><table summary="Element types and encodings"> |
|
949 * <tr><th> Element Type <th> <th> Encoding |
|
950 * <tr><td> boolean <td> <td align=center> Z |
|
951 * <tr><td> byte <td> <td align=center> B |
|
952 * <tr><td> char <td> <td align=center> C |
|
953 * <tr><td> class or interface |
|
954 * <td> <td align=center> L<i>classname</i>; |
|
955 * <tr><td> double <td> <td align=center> D |
|
956 * <tr><td> float <td> <td align=center> F |
|
957 * <tr><td> int <td> <td align=center> I |
|
958 * <tr><td> long <td> <td align=center> J |
|
959 * <tr><td> short <td> <td align=center> S |
|
960 * </table></blockquote> |
|
961 * |
|
962 * <p> The class or interface name <i>classname</i> is the binary name of |
|
963 * the class specified above. |
|
964 * |
|
965 * <p> Examples: |
|
966 * <blockquote><pre> |
|
967 * String.class.getName() |
|
968 * returns "java.lang.String" |
|
969 * byte.class.getName() |
|
970 * returns "byte" |
|
971 * (new Object[3]).getClass().getName() |
|
972 * returns "[Ljava.lang.Object;" |
|
973 * (new int[3][4][5][6][7][8][9]).getClass().getName() |
|
974 * returns "[[[[[[[I" |
|
975 * </pre></blockquote> |
|
976 * |
|
977 * @return the name of the class or interface |
|
978 * represented by this object. |
|
979 */ |
|
980 public String getName() { |
|
981 if (name == null) |
|
982 name = getName0(); |
|
983 return name; |
|
984 } |
|
985 |
|
986 // cache the name to reduce the number of calls into the VM |
|
987 private transient String name; |
|
988 private native String getName0(); |
|
989 |
|
990 /** |
|
991 * Returns the class loader for the class. Some implementations may use |
|
992 * null to represent the bootstrap class loader. This method will return |
|
993 * null in such implementations if this class was loaded by the bootstrap |
|
994 * class loader. |
|
995 * |
|
996 * <p> If a security manager is present, and the caller''s class loader is |
|
997 * not null and the caller''s class loader is not the same as or an ancestor of |
|
998 * the class loader for the class whose class loader is requested, then |
|
999 * this method calls the security manager''s {@code checkPermission} |
|
1000 * method with a {@code RuntimePermission("getClassLoader")} |
|
1001 * permission to ensure it''s ok to access the class loader for the class. |
|
1002 * |
|
1003 * <p>If this object |
|
1004 * represents a primitive type or void, null is returned. |
|
1005 * |
|
1006 * @return the class loader that loaded the class or interface |
|
1007 * represented by this object. |
|
1008 * @throws SecurityException |
|
1009 * if a security manager exists and its |
|
1010 * {@code checkPermission} method denies |
|
1011 * access to the class loader for the class. |
|
1012 * @see java.lang.ClassLoader |
|
1013 * @see SecurityManager#checkPermission |
|
1014 * @see java.lang.RuntimePermission |
|
1015 */ |
|
1016 public ClassLoader getClassLoader() { |
|
1017 ClassLoader cl = getClassLoader0(); |
|
1018 if (cl == null) |
|
1019 return null; |
|
1020 SecurityManager sm = System.getSecurityManager(); |
|
1021 if (sm !!= null) { |
|
1022 ClassLoader ccl = ClassLoader.getCallerClassLoader(); |
|
1023 if (ccl !!= null && ccl !!= cl && !!cl.isAncestor(ccl)) { |
|
1024 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); |
|
1025 } |
|
1026 } |
|
1027 return cl; |
|
1028 } |
|
1029 |
|
1030 // Package-private to allow ClassLoader access |
|
1031 native ClassLoader getClassLoader0(); |
|
1032 |
|
1033 |
|
1034 /** |
|
1035 * Returns an array of {@code TypeVariable} objects that represent the |
|
1036 * type variables declared by the generic declaration represented by this |
|
1037 * {@code GenericDeclaration} object, in declaration order. Returns an |
|
1038 * array of length 0 if the underlying generic declaration declares no type |
|
1039 * variables. |
|
1040 * |
|
1041 * @return an array of {@code TypeVariable} objects that represent |
|
1042 * the type variables declared by this generic declaration |
|
1043 * @throws GenericSignatureFormatError if the generic |
|
1044 * signature of this generic declaration does not conform to |
|
1045 * the format specified in the Java Virtual Machine Specification, |
|
1046 * 3rd edition |
|
1047 * @since 1.5 |
|
1048 */ |
|
1049 public TypeVariable<Class<T>>[] getTypeParameters() { |
|
1050 if (getGenericSignature() !!= null) |
|
1051 return (TypeVariable<Class<T>>[])getGenericInfo().getTypeParameters(); |
|
1052 else |
|
1053 return (TypeVariable<Class<T>>[])new TypeVariable[0]; |
|
1054 } |
|
1055 |
|
1056 |
|
1057 /** |
|
1058 * Returns the {@code Class} representing the superclass of the entity |
|
1059 * (class, interface, primitive type or void) represented by this |
|
1060 * {@code Class}. If this {@code Class} represents either the |
|
1061 * {@code Object} class, an interface, a primitive type, or void, then |
|
1062 * null is returned. If this object represents an array class then the |
|
1063 * {@code Class} object representing the {@code Object} class is |
|
1064 * returned. |
|
1065 * |
|
1066 * @return the superclass of the class represented by this object. |
|
1067 */ |
|
1068 public native Class<? super T> getSuperclass(); |
|
1069 |
|
1070 |
|
1071 /** |
|
1072 * Returns the {@code Type} representing the direct superclass of |
|
1073 * the entity (class, interface, primitive type or void) represented by |
|
1074 * this {@code Class}. |
|
1075 * |
|
1076 * <p>If the superclass is a parameterized type, the {@code Type} |
|
1077 * object returned must accurately reflect the actual type |
|
1078 * parameters used in the source code. The parameterized type |
|
1079 * representing the superclass is created if it had not been |
|
1080 * created before. See the declaration of {@link |
|
1081 * java.lang.reflect.ParameterizedType ParameterizedType} for the |
|
1082 * semantics of the creation process for parameterized types. If |
|
1083 * this {@code Class} represents either the {@code Object} |
|
1084 * class, an interface, a primitive type, or void, then null is |
|
1085 * returned. If this object represents an array class then the |
|
1086 * {@code Class} object representing the {@code Object} class is |
|
1087 * returned. |
|
1088 * |
|
1089 * @throws GenericSignatureFormatError if the generic |
|
1090 * class signature does not conform to the format specified in the |
|
1091 * Java Virtual Machine Specification, 3rd edition |
|
1092 * @throws TypeNotPresentException if the generic superclass |
|
1093 * refers to a non-existent type declaration |
|
1094 * @throws MalformedParameterizedTypeException if the |
|
1095 * generic superclass refers to a parameterized type that cannot be |
|
1096 * instantiated for any reason |
|
1097 * @return the superclass of the class represented by this object |
|
1098 * @since 1.5 |
|
1099 */ |
|
1100 public Type getGenericSuperclass() { |
|
1101 if (getGenericSignature() !!= null) { |
|
1102 // Historical irregularity: |
|
1103 // Generic signature marks interfaces with superclass = Object |
|
1104 // but this API returns null for interfaces |
|
1105 if (isInterface()) |
|
1106 return null; |
|
1107 return getGenericInfo().getSuperclass(); |
|
1108 } else |
|
1109 return getSuperclass(); |
|
1110 } |
|
1111 |
|
1112 /** |
|
1113 * Gets the package for this class. The class loader of this class is used |
|
1114 * to find the package. If the class was loaded by the bootstrap class |
|
1115 * loader the set of packages loaded from CLASSPATH is searched to find the |
|
1116 * package of the class. Null is returned if no package object was created |
|
1117 * by the class loader of this class. |
|
1118 * |
|
1119 * <p> Packages have attributes for versions and specifications only if the |
|
1120 * information was defined in the manifests that accompany the classes, and |
|
1121 * if the class loader created the package instance with the attributes |
|
1122 * from the manifest. |
|
1123 * |
|
1124 * @return the package of the class, or null if no package |
|
1125 * information is available from the archive or codebase. |
|
1126 */ |
|
1127 public Package getPackage() { |
|
1128 return Package.getPackage(this); |
|
1129 } |
|
1130 |
|
1131 |
|
1132 /** |
|
1133 * Determines the interfaces implemented by the class or interface |
|
1134 * represented by this object. |
|
1135 * |
|
1136 * <p> If this object represents a class, the return value is an array |
|
1137 * containing objects representing all interfaces implemented by the |
|
1138 * class. The order of the interface objects in the array corresponds to |
|
1139 * the order of the interface names in the {@code implements} clause |
|
1140 * of the declaration of the class represented by this object. For |
|
1141 * example, given the declaration: |
|
1142 * <blockquote> |
|
1143 * {@code class Shimmer implements FloorWax, DessertTopping { ... }} |
|
1144 * </blockquote> |
|
1145 * suppose the value of {@code s} is an instance of |
|
1146 * {@code Shimmer}; the value of the expression: |
|
1147 * <blockquote> |
|
1148 * {@code s.getClass().getInterfaces()[0]} |
|
1149 * </blockquote> |
|
1150 * is the {@code Class} object that represents interface |
|
1151 * {@code FloorWax}; and the value of: |
|
1152 * <blockquote> |
|
1153 * {@code s.getClass().getInterfaces()[1]} |
|
1154 * </blockquote> |
|
1155 * is the {@code Class} object that represents interface |
|
1156 * {@code DessertTopping}. |
|
1157 * |
|
1158 * <p> If this object represents an interface, the array contains objects |
|
1159 * representing all interfaces extended by the interface. The order of the |
|
1160 * interface objects in the array corresponds to the order of the interface |
|
1161 * names in the {@code extends} clause of the declaration of the |
|
1162 * interface represented by this object. |
|
1163 * |
|
1164 * <p> If this object represents a class or interface that implements no |
|
1165 * interfaces, the method returns an array of length 0. |
|
1166 * |
|
1167 * <p> If this object represents a primitive type or void, the method |
|
1168 * returns an array of length 0. |
|
1169 * |
|
1170 * @return an array of interfaces implemented by this class. |
|
1171 */ |
|
1172 public native Class<?>[] getInterfaces(); |
|
1173 |
|
1174 /** |
|
1175 * Returns the {@code Type}s representing the interfaces |
|
1176 * directly implemented by the class or interface represented by |
|
1177 * this object. |
|
1178 * |
|
1179 * <p>If a superinterface is a parameterized type, the |
|
1180 * {@code Type} object returned for it must accurately reflect |
|
1181 * the actual type parameters used in the source code. The |
|
1182 * parameterized type representing each superinterface is created |
|
1183 * if it had not been created before. See the declaration of |
|
1184 * {@link java.lang.reflect.ParameterizedType ParameterizedType} |
|
1185 * for the semantics of the creation process for parameterized |
|
1186 * types. |
|
1187 * |
|
1188 * <p> If this object represents a class, the return value is an |
|
1189 * array containing objects representing all interfaces |
|
1190 * implemented by the class. The order of the interface objects in |
|
1191 * the array corresponds to the order of the interface names in |
|
1192 * the {@code implements} clause of the declaration of the class |
|
1193 * represented by this object. In the case of an array class, the |
|
1194 * interfaces {@code Cloneable} and {@code Serializable} are |
|
1195 * returned in that order. |
|
1196 * |
|
1197 * <p>If this object represents an interface, the array contains |
|
1198 * objects representing all interfaces directly extended by the |
|
1199 * interface. The order of the interface objects in the array |
|
1200 * corresponds to the order of the interface names in the |
|
1201 * {@code extends} clause of the declaration of the interface |
|
1202 * represented by this object. |
|
1203 * |
|
1204 * <p>If this object represents a class or interface that |
|
1205 * implements no interfaces, the method returns an array of length |
|
1206 * 0. |
|
1207 * |
|
1208 * <p>If this object represents a primitive type or void, the |
|
1209 * method returns an array of length 0. |
|
1210 * |
|
1211 * @throws GenericSignatureFormatError |
|
1212 * if the generic class signature does not conform to the format |
|
1213 * specified in the Java Virtual Machine Specification, 3rd edition |
|
1214 * @throws TypeNotPresentException if any of the generic |
|
1215 * superinterfaces refers to a non-existent type declaration |
|
1216 * @throws MalformedParameterizedTypeException if any of the |
|
1217 * generic superinterfaces refer to a parameterized type that cannot |
|
1218 * be instantiated for any reason |
|
1219 * @return an array of interfaces implemented by this class |
|
1220 * @since 1.5 |
|
1221 */ |
|
1222 public Type[] getGenericInterfaces() { |
|
1223 if (getGenericSignature() !!= null) |
|
1224 return getGenericInfo().getSuperInterfaces(); |
|
1225 else |
|
1226 return getInterfaces(); |
|
1227 } |
|
1228 |
|
1229 |
|
1230 /** |
|
1231 * Returns the {@code Class} representing the component type of an |
|
1232 * array. If this class does not represent an array class this method |
|
1233 * returns null. |
|
1234 * |
|
1235 * @return the {@code Class} representing the component type of this |
|
1236 * class if this class is an array |
|
1237 * @see java.lang.reflect.Array |
|
1238 * @since JDK1.1 |
|
1239 */ |
|
1240 public native Class<?> getComponentType(); |
|
1241 |
|
1242 |
|
1243 /** |
|
1244 * Returns the Java language modifiers for this class or interface, encoded |
|
1245 * in an integer. The modifiers consist of the Java Virtual Machine''s |
|
1246 * constants for {@code public}, {@code protected}, |
|
1247 * {@code private}, {@code final}, {@code static}, |
|
1248 * {@code abstract} and {@code interface}; they should be decoded |
|
1249 * using the methods of class {@code Modifier}. |
|
1250 * |
|
1251 * <p> If the underlying class is an array class, then its |
|
1252 * {@code public}, {@code private} and {@code protected} |
|
1253 * modifiers are the same as those of its component type. If this |
|
1254 * {@code Class} represents a primitive type or void, its |
|
1255 * {@code public} modifier is always {@code true}, and its |
|
1256 * {@code protected} and {@code private} modifiers are always |
|
1257 * {@code false}. If this object represents an array class, a |
|
1258 * primitive type or void, then its {@code final} modifier is always |
|
1259 * {@code true} and its interface modifier is always |
|
1260 * {@code false}. The values of its other modifiers are not determined |
|
1261 * by this specification. |
|
1262 * |
|
1263 * <p> The modifier encodings are defined in <em>The Java Virtual Machine |
|
1264 * Specification</em>, table 4.1. |
|
1265 * |
|
1266 * @return the {@code int} representing the modifiers for this class |
|
1267 * @see java.lang.reflect.Modifier |
|
1268 * @since JDK1.1 |
|
1269 */ |
|
1270 public native int getModifiers(); |
|
1271 |
|
1272 |
|
1273 /** |
|
1274 * Gets the signers of this class. |
|
1275 * |
|
1276 * @return the signers of this class, or null if there are no signers. In |
|
1277 * particular, this method returns null if this object represents |
|
1278 * a primitive type or void. |
|
1279 * @since JDK1.1 |
|
1280 */ |
|
1281 public native Object[] getSigners(); |
|
1282 |
|
1283 |
|
1284 /** |
|
1285 * Set the signers of this class. |
|
1286 */ |
|
1287 native void setSigners(Object[] signers); |
|
1288 |
|
1289 |
|
1290 /** |
|
1291 * If this {@code Class} object represents a local or anonymous |
|
1292 * class within a method, returns a {@link |
|
1293 * java.lang.reflect.Method Method} object representing the |
|
1294 * immediately enclosing method of the underlying class. Returns |
|
1295 * {@code null} otherwise. |
|
1296 * |
|
1297 * In particular, this method returns {@code null} if the underlying |
|
1298 * class is a local or anonymous class immediately enclosed by a type |
|
1299 * declaration, instance initializer or static initializer. |
|
1300 * |
|
1301 * @return the immediately enclosing method of the underlying class, if |
|
1302 * that class is a local or anonymous class; otherwise {@code null}. |
|
1303 * @since 1.5 |
|
1304 */ |
|
1305 public Method getEnclosingMethod() { |
|
1306 EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo(); |
|
1307 |
|
1308 if (enclosingInfo == null) |
|
1309 return null; |
|
1310 else { |
|
1311 if (!!enclosingInfo.isMethod()) |
|
1312 return null; |
|
1313 |
|
1314 MethodRepository typeInfo = MethodRepository.make(enclosingInfo.getDescriptor(), |
|
1315 getFactory()); |
|
1316 Class returnType = toClass(typeInfo.getReturnType()); |
|
1317 Type [] parameterTypes = typeInfo.getParameterTypes(); |
|
1318 Class<?>[] parameterClasses = new Class<?>[parameterTypes.length]; |
|
1319 |
|
1320 // Convert Types to Classes; returned types *should* |
|
1321 // be class objects since the methodDescriptor''s used |
|
1322 // don''t have generics information |
|
1323 for(int i = 0; i < parameterClasses.length; i++) |
|
1324 parameterClasses[i] = toClass(parameterTypes[i]); |
|
1325 |
|
1326 /* |
|
1327 * Loop over all declared methods; match method name, |
|
1328 * number of and type of parameters, *and* return |
|
1329 * type. Matching return type is also necessary |
|
1330 * because of covariant returns, etc. |
|
1331 */ |
|
1332 for(Method m: enclosingInfo.getEnclosingClass().getDeclaredMethods()) { |
|
1333 if (m.getName().equals(enclosingInfo.getName()) ) { |
|
1334 Class<?>[] candidateParamClasses = m.getParameterTypes(); |
|
1335 if (candidateParamClasses.length == parameterClasses.length) { |
|
1336 boolean matches = true; |
|
1337 for(int i = 0; i < candidateParamClasses.length; i++) { |
|
1338 if (!!candidateParamClasses[i].equals(parameterClasses[i])) { |
|
1339 matches = false; |
|
1340 break; |
|
1341 } |
|
1342 } |
|
1343 |
|
1344 if (matches) { // finally, check return type |
|
1345 if (m.getReturnType().equals(returnType) ) |
|
1346 return m; |
|
1347 } |
|
1348 } |
|
1349 } |
|
1350 } |
|
1351 |
|
1352 throw new InternalError("Enclosing method not found"); |
|
1353 } |
|
1354 } |
|
1355 |
|
1356 private native Object[] getEnclosingMethod0(); |
|
1357 |
|
1358 private EnclosingMethodInfo getEnclosingMethodInfo() { |
|
1359 Object[] enclosingInfo = getEnclosingMethod0(); |
|
1360 if (enclosingInfo == null) |
|
1361 return null; |
|
1362 else { |
|
1363 return new EnclosingMethodInfo(enclosingInfo); |
|
1364 } |
|
1365 } |
|
1366 |
|
1367 private final static class EnclosingMethodInfo { |
|
1368 private Class<?> enclosingClass; |
|
1369 private String name; |
|
1370 private String descriptor; |
|
1371 |
|
1372 private EnclosingMethodInfo(Object[] enclosingInfo) { |
|
1373 if (enclosingInfo.length !!= 3) |
|
1374 throw new InternalError("Malformed enclosing method information"); |
|
1375 try { |
|
1376 // The array is expected to have three elements: |
|
1377 |
|
1378 // the immediately enclosing class |
|
1379 enclosingClass = (Class<?>) enclosingInfo[0]; |
|
1380 assert(enclosingClass !!= null); |
|
1381 |
|
1382 // the immediately enclosing method or constructor''s |
|
1383 // name (can be null). |
|
1384 name = (String) enclosingInfo[1]; |
|
1385 |
|
1386 // the immediately enclosing method or constructor''s |
|
1387 // descriptor (null iff name is). |
|
1388 descriptor = (String) enclosingInfo[2]; |
|
1389 assert((name !!= null && descriptor !!= null) || name == descriptor); |
|
1390 } catch (ClassCastException cce) { |
|
1391 throw new InternalError("Invalid type in enclosing method information"); |
|
1392 } |
|
1393 } |
|
1394 |
|
1395 boolean isPartial() { |
|
1396 return enclosingClass == null || name == null || descriptor == null; |
|
1397 } |
|
1398 |
|
1399 boolean isConstructor() { return !!isPartial() && "<init>".equals(name); } |
|
1400 |
|
1401 boolean isMethod() { return !!isPartial() && !!isConstructor() && !!"<clinit>".equals(name); } |
|
1402 |
|
1403 Class<?> getEnclosingClass() { return enclosingClass; } |
|
1404 |
|
1405 String getName() { return name; } |
|
1406 |
|
1407 String getDescriptor() { return descriptor; } |
|
1408 |
|
1409 } |
|
1410 |
|
1411 private static Class toClass(Type o) { |
|
1412 if (o instanceof GenericArrayType) |
|
1413 return Array.newInstance(toClass(((GenericArrayType)o).getGenericComponentType()), |
|
1414 0) |
|
1415 .getClass(); |
|
1416 return (Class)o; |
|
1417 } |
|
1418 |
|
1419 /** |
|
1420 * If this {@code Class} object represents a local or anonymous |
|
1421 * class within a constructor, returns a {@link |
|
1422 * java.lang.reflect.Constructor Constructor} object representing |
|
1423 * the immediately enclosing constructor of the underlying |
|
1424 * class. Returns {@code null} otherwise. In particular, this |
|
1425 * method returns {@code null} if the underlying class is a local |
|
1426 * or anonymous class immediately enclosed by a type declaration, |
|
1427 * instance initializer or static initializer. |
|
1428 * |
|
1429 * @return the immediately enclosing constructor of the underlying class, if |
|
1430 * that class is a local or anonymous class; otherwise {@code null}. |
|
1431 * @since 1.5 |
|
1432 */ |
|
1433 public Constructor<?> getEnclosingConstructor() { |
|
1434 EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo(); |
|
1435 |
|
1436 if (enclosingInfo == null) |
|
1437 return null; |
|
1438 else { |
|
1439 if (!!enclosingInfo.isConstructor()) |
|
1440 return null; |
|
1441 |
|
1442 ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(), |
|
1443 getFactory()); |
|
1444 Type [] parameterTypes = typeInfo.getParameterTypes(); |
|
1445 Class<?>[] parameterClasses = new Class<?>[parameterTypes.length]; |
|
1446 |
|
1447 // Convert Types to Classes; returned types *should* |
|
1448 // be class objects since the methodDescriptor''s used |
|
1449 // don''t have generics information |
|
1450 for(int i = 0; i < parameterClasses.length; i++) |
|
1451 parameterClasses[i] = toClass(parameterTypes[i]); |
|
1452 |
|
1453 /* |
|
1454 * Loop over all declared constructors; match number |
|
1455 * of and type of parameters. |
|
1456 */ |
|
1457 for(Constructor c: enclosingInfo.getEnclosingClass().getDeclaredConstructors()) { |
|
1458 Class<?>[] candidateParamClasses = c.getParameterTypes(); |
|
1459 if (candidateParamClasses.length == parameterClasses.length) { |
|
1460 boolean matches = true; |
|
1461 for(int i = 0; i < candidateParamClasses.length; i++) { |
|
1462 if (!!candidateParamClasses[i].equals(parameterClasses[i])) { |
|
1463 matches = false; |
|
1464 break; |
|
1465 } |
|
1466 } |
|
1467 |
|
1468 if (matches) |
|
1469 return c; |
|
1470 } |
|
1471 } |
|
1472 |
|
1473 throw new InternalError("Enclosing constructor not found"); |
|
1474 } |
|
1475 } |
|
1476 |
|
1477 |
|
1478 /** |
|
1479 * If the class or interface represented by this {@code Class} object |
|
1480 * is a member of another class, returns the {@code Class} object |
|
1481 * representing the class in which it was declared. This method returns |
|
1482 * null if this class or interface is not a member of any other class. If |
|
1483 * this {@code Class} object represents an array class, a primitive |
|
1484 * type, or void,then this method returns null. |
|
1485 * |
|
1486 * @return the declaring class for this class |
|
1487 * @since JDK1.1 |
|
1488 */ |
|
1489 public native Class<?> getDeclaringClass(); |
|
1490 |
|
1491 |
|
1492 /** |
|
1493 * Returns the immediately enclosing class of the underlying |
|
1494 * class. If the underlying class is a top level class this |
|
1495 * method returns {@code null}. |
|
1496 * @return the immediately enclosing class of the underlying class |
|
1497 * @since 1.5 |
|
1498 */ |
|
1499 public Class<?> getEnclosingClass() { |
|
1500 // There are five kinds of classes (or interfaces): |
|
1501 // a) Top level classes |
|
1502 // b) Nested classes (static member classes) |
|
1503 // c) Inner classes (non-static member classes) |
|
1504 // d) Local classes (named classes declared within a method) |
|
1505 // e) Anonymous classes |
|
1506 |
|
1507 |
|
1508 // JVM Spec 4.8.6: A class must have an EnclosingMethod |
|
1509 // attribute if and only if it is a local class or an |
|
1510 // anonymous class. |
|
1511 EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo(); |
|
1512 |
|
1513 if (enclosingInfo == null) { |
|
1514 // This is a top level or a nested class or an inner class (a, b, or c) |
|
1515 return getDeclaringClass(); |
|
1516 } else { |
|
1517 Class<?> enclosingClass = enclosingInfo.getEnclosingClass(); |
|
1518 // This is a local class or an anonymous class (d or e) |
|
1519 if (enclosingClass == this || enclosingClass == null) |
|
1520 throw new InternalError("Malformed enclosing method information"); |
|
1521 else |
|
1522 return enclosingClass; |
|
1523 } |
|
1524 } |
|
1525 |
|
1526 /** |
|
1527 * Returns the simple name of the underlying class as given in the |
|
1528 * source code. Returns an empty string if the underlying class is |
|
1529 * anonymous. |
|
1530 * |
|
1531 * <p>The simple name of an array is the simple name of the |
|
1532 * component type with "[]" appended. In particular the simple |
|
1533 * name of an array whose component type is anonymous is "[]". |
|
1534 * |
|
1535 * @return the simple name of the underlying class |
|
1536 * @since 1.5 |
|
1537 */ |
|
1538 public String getSimpleName() { |
|
1539 if (isArray()) |
|
1540 return getComponentType().getSimpleName()+"[]"; |
|
1541 |
|
1542 String simpleName = getSimpleBinaryName(); |
|
1543 if (simpleName == null) { // top level class |
|
1544 simpleName = getName(); |
|
1545 return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name |
|
1546 } |
|
1547 // According to JLS3 "Binary Compatibility" (13.1) the binary |
|
1548 // name of non-package classes (not top level) is the binary |
|
1549 // name of the immediately enclosing class followed by a ''$'' followed by: |
|
1550 // (for nested and inner classes): the simple name. |
|
1551 // (for local classes): 1 or more digits followed by the simple name. |
|
1552 // (for anonymous classes): 1 or more digits. |
|
1553 |
|
1554 // Since getSimpleBinaryName() will strip the binary name of |
|
1555 // the immediatly enclosing class, we are now looking at a |
|
1556 // string that matches the regular expression "\$[0-9]*" |
|
1557 // followed by a simple name (considering the simple of an |
|
1558 // anonymous class to be the empty string). |
|
1559 |
|
1560 // Remove leading "\$[0-9]*" from the name |
|
1561 int length = simpleName.length(); |
|
1562 if (length < 1 || simpleName.charAt(0) !!= ''$'') |
|
1563 throw new InternalError("Malformed class name"); |
|
1564 int index = 1; |
|
1565 while (index < length && isAsciiDigit(simpleName.charAt(index))) |
|
1566 index++; |
|
1567 // Eventually, this is the empty string iff this is an anonymous class |
|
1568 return simpleName.substring(index); |
|
1569 } |
|
1570 |
|
1571 /** |
|
1572 * Character.isDigit answers {@code true} to some non-ascii |
|
1573 * digits. This one does not. |
|
1574 */ |
|
1575 private static boolean isAsciiDigit(char c) { |
|
1576 return ''0'' <= c && c <= ''9''; |
|
1577 } |
|
1578 |
|
1579 /** |
|
1580 * Returns the canonical name of the underlying class as |
|
1581 * defined by the Java Language Specification. Returns null if |
|
1582 * the underlying class does not have a canonical name (i.e., if |
|
1583 * it is a local or anonymous class or an array whose component |
|
1584 * type does not have a canonical name). |
|
1585 * @return the canonical name of the underlying class if it exists, and |
|
1586 * {@code null} otherwise. |
|
1587 * @since 1.5 |
|
1588 */ |
|
1589 public String getCanonicalName() { |
|
1590 if (isArray()) { |
|
1591 String canonicalName = getComponentType().getCanonicalName(); |
|
1592 if (canonicalName !!= null) |
|
1593 return canonicalName + "[]"; |
|
1594 else |
|
1595 return null; |
|
1596 } |
|
1597 if (isLocalOrAnonymousClass()) |
|
1598 return null; |
|
1599 Class<?> enclosingClass = getEnclosingClass(); |
|
1600 if (enclosingClass == null) { // top level class |
|
1601 return getName(); |
|
1602 } else { |
|
1603 String enclosingName = enclosingClass.getCanonicalName(); |
|
1604 if (enclosingName == null) |
|
1605 return null; |
|
1606 return enclosingName + "." + getSimpleName(); |
|
1607 } |
|
1608 } |
|
1609 |
|
1610 /** |
|
1611 * Returns {@code true} if and only if the underlying class |
|
1612 * is an anonymous class. |
|
1613 * |
|
1614 * @return {@code true} if and only if this class is an anonymous class. |
|
1615 * @since 1.5 |
|
1616 */ |
|
1617 public boolean isAnonymousClass() { |
|
1618 return "".equals(getSimpleName()); |
|
1619 } |
|
1620 |
|
1621 /** |
|
1622 * Returns {@code true} if and only if the underlying class |
|
1623 * is a local class. |
|
1624 * |
|
1625 * @return {@code true} if and only if this class is a local class. |
|
1626 * @since 1.5 |
|
1627 */ |
|
1628 public boolean isLocalClass() { |
|
1629 return isLocalOrAnonymousClass() && !!isAnonymousClass(); |
|
1630 } |
|
1631 |
|
1632 /** |
|
1633 * Returns {@code true} if and only if the underlying class |
|
1634 * is a member class. |
|
1635 * |
|
1636 * @return {@code true} if and only if this class is a member class. |
|
1637 * @since 1.5 |
|
1638 */ |
|
1639 public boolean isMemberClass() { |
|
1640 return getSimpleBinaryName() !!= null && !!isLocalOrAnonymousClass(); |
|
1641 } |
|
1642 |
|
1643 /** |
|
1644 * Returns the "simple binary name" of the underlying class, i.e., |
|
1645 * the binary name without the leading enclosing class name. |
|
1646 * Returns {@code null} if the underlying class is a top level |
|
1647 * class. |
|
1648 */ |
|
1649 private String getSimpleBinaryName() { |
|
1650 Class<?> enclosingClass = getEnclosingClass(); |
|
1651 if (enclosingClass == null) // top level class |
|
1652 return null; |
|
1653 // Otherwise, strip the enclosing class'' name |
|
1654 try { |
|
1655 return getName().substring(enclosingClass.getName().length()); |
|
1656 } catch (IndexOutOfBoundsException ex) { |
|
1657 throw new InternalError("Malformed class name"); |
|
1658 } |
|
1659 } |
|
1660 |
|
1661 /** |
|
1662 * Returns {@code true} if this is a local class or an anonymous |
|
1663 * class. Returns {@code false} otherwise. |
|
1664 */ |
|
1665 private boolean isLocalOrAnonymousClass() { |
|
1666 // JVM Spec 4.8.6: A class must have an EnclosingMethod |
|
1667 // attribute if and only if it is a local class or an |
|
1668 // anonymous class. |
|
1669 return getEnclosingMethodInfo() !!= null; |
|
1670 } |
|
1671 |
|
1672 /** |
|
1673 * Returns an array containing {@code Class} objects representing all |
|
1674 * the public classes and interfaces that are members of the class |
|
1675 * represented by this {@code Class} object. This includes public |
|
1676 * class and interface members inherited from superclasses and public class |
|
1677 * and interface members declared by the class. This method returns an |
|
1678 * array of length 0 if this {@code Class} object has no public member |
|
1679 * classes or interfaces. This method also returns an array of length 0 if |
|
1680 * this {@code Class} object represents a primitive type, an array |
|
1681 * class, or void. |
|
1682 * |
|
1683 * @return the array of {@code Class} objects representing the public |
|
1684 * members of this class |
|
1685 * @exception SecurityException |
|
1686 * If a security manager, <i>s</i>, is present and any of the |
|
1687 * following conditions is met: |
|
1688 * |
|
1689 * <ul> |
|
1690 * |
|
1691 * <li> invocation of |
|
1692 * {@link SecurityManager#checkMemberAccess |
|
1693 * s.checkMemberAccess(this, Member.PUBLIC)} method |
|
1694 * denies access to the classes within this class |
|
1695 * |
|
1696 * <li> the caller''s class loader is not the same as or an |
|
1697 * ancestor of the class loader for the current class and |
|
1698 * invocation of {@link SecurityManager#checkPackageAccess |
|
1699 * s.checkPackageAccess()} denies access to the package |
|
1700 * of this class |
|
1701 * |
|
1702 * </ul> |
|
1703 * |
|
1704 * @since JDK1.1 |
|
1705 */ |
|
1706 public Class<?>[] getClasses() { |
|
1707 // be very careful not to change the stack depth of this |
|
1708 // checkMemberAccess call for security reasons |
|
1709 // see java.lang.SecurityManager.checkMemberAccess |
|
1710 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); |
|
1711 |
|
1712 // Privileged so this implementation can look at DECLARED classes, |
|
1713 // something the caller might not have privilege to do. The code here |
|
1714 // is allowed to look at DECLARED classes because (1) it does not hand |
|
1715 // out anything other than public members and (2) public member access |
|
1716 // has already been ok''d by the SecurityManager. |
|
1717 |
|
1718 Class[] result = (Class[]) java.security.AccessController.doPrivileged |
|
1719 (new java.security.PrivilegedAction() { |
|
1720 public Object run() { |
|
1721 java.util.List<Class> list = new java.util.ArrayList(); |
|
1722 Class currentClass = Class.this; |
|
1723 while (currentClass !!= null) { |
|
1724 Class[] members = currentClass.getDeclaredClasses(); |
|
1725 for (int i = 0; i < members.length; i++) { |
|
1726 if (Modifier.isPublic(members[i].getModifiers())) { |
|
1727 list.add(members[i]); |
|
1728 } |
|
1729 } |
|
1730 currentClass = currentClass.getSuperclass(); |
|
1731 } |
|
1732 Class[] empty = {}; |
|
1733 return list.toArray(empty); |
|
1734 } |
|
1735 }); |
|
1736 |
|
1737 return result; |
|
1738 } |
|
1739 |
|
1740 |
|
1741 /** |
|
1742 * Returns an array containing {@code Field} objects reflecting all |
|
1743 * the accessible public fields of the class or interface represented by |
|
1744 * this {@code Class} object. The elements in the array returned are |
|
1745 * not sorted and are not in any particular order. This method returns an |
|
1746 * array of length 0 if the class or interface has no accessible public |
|
1747 * fields, or if it represents an array class, a primitive type, or void. |
|
1748 * |
|
1749 * <p> Specifically, if this {@code Class} object represents a class, |
|
1750 * this method returns the public fields of this class and of all its |
|
1751 * superclasses. If this {@code Class} object represents an |
|
1752 * interface, this method returns the fields of this interface and of all |
|
1753 * its superinterfaces. |
|
1754 * |
|
1755 * <p> The implicit length field for array class is not reflected by this |
|
1756 * method. User code should use the methods of class {@code Array} to |
|
1757 * manipulate arrays. |
|
1758 * |
|
1759 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. |
|
1760 * |
|
1761 * @return the array of {@code Field} objects representing the |
|
1762 * public fields |
|
1763 * @exception SecurityException |
|
1764 * If a security manager, <i>s</i>, is present and any of the |
|
1765 * following conditions is met: |
|
1766 * |
|
1767 * <ul> |
|
1768 * |
|
1769 * <li> invocation of |
|
1770 * {@link SecurityManager#checkMemberAccess |
|
1771 * s.checkMemberAccess(this, Member.PUBLIC)} denies |
|
1772 * access to the fields within this class |
|
1773 * |
|
1774 * <li> the caller''s class loader is not the same as or an |
|
1775 * ancestor of the class loader for the current class and |
|
1776 * invocation of {@link SecurityManager#checkPackageAccess |
|
1777 * s.checkPackageAccess()} denies access to the package |
|
1778 * of this class |
|
1779 * |
|
1780 * </ul> |
|
1781 * |
|
1782 * @since JDK1.1 |
|
1783 */ |
|
1784 public Field[] getFields() throws SecurityException { |
|
1785 // be very careful not to change the stack depth of this |
|
1786 // checkMemberAccess call for security reasons |
|
1787 // see java.lang.SecurityManager.checkMemberAccess |
|
1788 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); |
|
1789 return copyFields(privateGetPublicFields(null)); |
|
1790 } |
|
1791 |
|
1792 |
|
1793 /** |
|
1794 * Returns an array containing {@code Method} objects reflecting all |
|
1795 * the public <em>member</em> methods of the class or interface represented |
|
1796 * by this {@code Class} object, including those declared by the class |
|
1797 * or interface and those inherited from superclasses and |
|
1798 * superinterfaces. Array classes return all the (public) member methods |
|
1799 * inherited from the {@code Object} class. The elements in the array |
|
1800 * returned are not sorted and are not in any particular order. This |
|
1801 * method returns an array of length 0 if this {@code Class} object |
|
1802 * represents a class or interface that has no public member methods, or if |
|
1803 * this {@code Class} object represents a primitive type or void. |
|
1804 * |
|
1805 * <p> The class initialization method {@code <clinit>} is not |
|
1806 * included in the returned array. If the class declares multiple public |
|
1807 * member methods with the same parameter types, they are all included in |
|
1808 * the returned array. |
|
1809 * |
|
1810 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. |
|
1811 * |
|
1812 * @return the array of {@code Method} objects representing the |
|
1813 * public methods of this class |
|
1814 * @exception SecurityException |
|
1815 * If a security manager, <i>s</i>, is present and any of the |
|
1816 * following conditions is met: |
|
1817 * |
|
1818 * <ul> |
|
1819 * |
|
1820 * <li> invocation of |
|
1821 * {@link SecurityManager#checkMemberAccess |
|
1822 * s.checkMemberAccess(this, Member.PUBLIC)} denies |
|
1823 * access to the methods within this class |
|
1824 * |
|
1825 * <li> the caller''s class loader is not the same as or an |
|
1826 * ancestor of the class loader for the current class and |
|
1827 * invocation of {@link SecurityManager#checkPackageAccess |
|
1828 * s.checkPackageAccess()} denies access to the package |
|
1829 * of this class |
|
1830 * |
|
1831 * </ul> |
|
1832 * |
|
1833 * @since JDK1.1 |
|
1834 */ |
|
1835 public Method[] getMethods() throws SecurityException { |
|
1836 // be very careful not to change the stack depth of this |
|
1837 // checkMemberAccess call for security reasons |
|
1838 // see java.lang.SecurityManager.checkMemberAccess |
|
1839 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); |
|
1840 return copyMethods(privateGetPublicMethods()); |
|
1841 } |
|
1842 |
|
1843 |
|
1844 /** |
|
1845 * Returns an array containing {@code Constructor} objects reflecting |
|
1846 * all the public constructors of the class represented by this |
|
1847 * {@code Class} object. An array of length 0 is returned if the |
|
1848 * class has no public constructors, or if the class is an array class, or |
|
1849 * if the class reflects a primitive type or void. |
|
1850 * |
|
1851 * Note that while this method returns an array of {@code |
|
1852 * Constructor<T>} objects (that is an array of constructors from |
|
1853 * this class), the return type of this method is {@code |
|
1854 * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as |
|
1855 * might be expected. This less informative return type is |
|
1856 * necessary since after being returned from this method, the |
|
1857 * array could be modified to hold {@code Constructor} objects for |
|
1858 * different classes, which would violate the type guarantees of |
|
1859 * {@code Constructor<T>[]}. |
|
1860 * |
|
1861 * @return the array of {@code Constructor} objects representing the |
|
1862 * public constructors of this class |
|
1863 * @exception SecurityException |
|
1864 * If a security manager, <i>s</i>, is present and any of the |
|
1865 * following conditions is met: |
|
1866 * |
|
1867 * <ul> |
|
1868 * |
|
1869 * <li> invocation of |
|
1870 * {@link SecurityManager#checkMemberAccess |
|
1871 * s.checkMemberAccess(this, Member.PUBLIC)} denies |
|
1872 * access to the constructors within this class |
|
1873 * |
|
1874 * <li> the caller''s class loader is not the same as or an |
|
1875 * ancestor of the class loader for the current class and |
|
1876 * invocation of {@link SecurityManager#checkPackageAccess |
|
1877 * s.checkPackageAccess()} denies access to the package |
|
1878 * of this class |
|
1879 * |
|
1880 * </ul> |
|
1881 * |
|
1882 * @since JDK1.1 |
|
1883 */ |
|
1884 public Constructor<?>[] getConstructors() throws SecurityException { |
|
1885 // be very careful not to change the stack depth of this |
|
1886 // checkMemberAccess call for security reasons |
|
1887 // see java.lang.SecurityManager.checkMemberAccess |
|
1888 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); |
|
1889 return copyConstructors(privateGetDeclaredConstructors(true)); |
|
1890 } |
|
1891 |
|
1892 |
|
1893 /** |
|
1894 * Returns a {@code Field} object that reflects the specified public |
|
1895 * member field of the class or interface represented by this |
|
1896 * {@code Class} object. The {@code name} parameter is a |
|
1897 * {@code String} specifying the simple name of the desired field. |
|
1898 * |
|
1899 * <p> The field to be reflected is determined by the algorithm that |
|
1900 * follows. Let C be the class represented by this object: |
|
1901 * <OL> |
|
1902 * <LI> If C declares a public field with the name specified, that is the |
|
1903 * field to be reflected.</LI> |
|
1904 * <LI> If no field was found in step 1 above, this algorithm is applied |
|
1905 * recursively to each direct superinterface of C. The direct |
|
1906 * superinterfaces are searched in the order they were declared.</LI> |
|
1907 * <LI> If no field was found in steps 1 and 2 above, and C has a |
|
1908 * superclass S, then this algorithm is invoked recursively upon S. |
|
1909 * If C has no superclass, then a {@code NoSuchFieldException} |
|
1910 * is thrown.</LI> |
|
1911 * </OL> |
|
1912 * |
|
1913 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. |
|
1914 * |
|
1915 * @param name the field name |
|
1916 * @return the {@code Field} object of this class specified by |
|
1917 * {@code name} |
|
1918 * @exception NoSuchFieldException if a field with the specified name is |
|
1919 * not found. |
|
1920 * @exception NullPointerException if {@code name} is {@code null} |
|
1921 * @exception SecurityException |
|
1922 * If a security manager, <i>s</i>, is present and any of the |
|
1923 * following conditions is met: |
|
1924 * |
|
1925 * <ul> |
|
1926 * |
|
1927 * <li> invocation of |
|
1928 * {@link SecurityManager#checkMemberAccess |
|
1929 * s.checkMemberAccess(this, Member.PUBLIC)} denies |
|
1930 * access to the field |
|
1931 * |
|
1932 * <li> the caller''s class loader is not the same as or an |
|
1933 * ancestor of the class loader for the current class and |
|
1934 * invocation of {@link SecurityManager#checkPackageAccess |
|
1935 * s.checkPackageAccess()} denies access to the package |
|
1936 * of this class |
|
1937 * |
|
1938 * </ul> |
|
1939 * |
|
1940 * @since JDK1.1 |
|
1941 */ |
|
1942 public Field getField(String name) |
|
1943 throws NoSuchFieldException, SecurityException { |
|
1944 // be very careful not to change the stack depth of this |
|
1945 // checkMemberAccess call for security reasons |
|
1946 // see java.lang.SecurityManager.checkMemberAccess |
|
1947 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); |
|
1948 Field field = getField0(name); |
|
1949 if (field == null) { |
|
1950 throw new NoSuchFieldException(name); |
|
1951 } |
|
1952 return field; |
|
1953 } |
|
1954 |
|
1955 |
|
1956 /** |
|
1957 * Returns a {@code Method} object that reflects the specified public |
|
1958 * member method of the class or interface represented by this |
|
1959 * {@code Class} object. The {@code name} parameter is a |
|
1960 * {@code String} specifying the simple name of the desired method. The |
|
1961 * {@code parameterTypes} parameter is an array of {@code Class} |
|
1962 * objects that identify the method''s formal parameter types, in declared |
|
1963 * order. If {@code parameterTypes} is {@code null}, it is |
|
1964 * treated as if it were an empty array. |
|
1965 * |
|
1966 * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a |
|
1967 * {@code NoSuchMethodException} is raised. Otherwise, the method to |
|
1968 * be reflected is determined by the algorithm that follows. Let C be the |
|
1969 * class represented by this object: |
|
1970 * <OL> |
|
1971 * <LI> C is searched for any <I>matching methods</I>. If no matching |
|
1972 * method is found, the algorithm of step 1 is invoked recursively on |
|
1973 * the superclass of C.</LI> |
|
1974 * <LI> If no method was found in step 1 above, the superinterfaces of C |
|
1975 * are searched for a matching method. If any such method is found, it |
|
1976 * is reflected.</LI> |
|
1977 * </OL> |
|
1978 * |
|
1979 * To find a matching method in a class C: If C declares exactly one |
|
1980 * public method with the specified name and exactly the same formal |
|
1981 * parameter types, that is the method reflected. If more than one such |
|
1982 * method is found in C, and one of these methods has a return type that is |
|
1983 * more specific than any of the others, that method is reflected; |
|
1984 * otherwise one of the methods is chosen arbitrarily. |
|
1985 * |
|
1986 * <p>Note that there may be more than one matching method in a |
|
1987 * class because while the Java language forbids a class to |
|
1988 * declare multiple methods with the same signature but different |
|
1989 * return types, the Java virtual machine does not. This |
|
1990 * increased flexibility in the virtual machine can be used to |
|
1991 * implement various language features. For example, covariant |
|
1992 * returns can be implemented with {@linkplain |
|
1993 * java.lang.reflect.Method#isBridge bridge methods}; the bridge |
|
1994 * method and the method being overridden would have the same |
|
1995 * signature but different return types. |
|
1996 * |
|
1997 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4. |
|
1998 * |
|
1999 * @param name the name of the method |
|
2000 * @param parameterTypes the list of parameters |
|
2001 * @return the {@code Method} object that matches the specified |
|
2002 * {@code name} and {@code parameterTypes} |
|
2003 * @exception NoSuchMethodException if a matching method is not found |
|
2004 * or if the name is "<init>"or "<clinit>". |
|
2005 * @exception NullPointerException if {@code name} is {@code null} |
|
2006 * @exception SecurityException |
|
2007 * If a security manager, <i>s</i>, is present and any of the |
|
2008 * following conditions is met: |
|
2009 * |
|
2010 * <ul> |
|
2011 * |
|
2012 * <li> invocation of |
|
2013 * {@link SecurityManager#checkMemberAccess |
|
2014 * s.checkMemberAccess(this, Member.PUBLIC)} denies |
|
2015 * access to the method |
|
2016 * |
|
2017 * <li> the caller''s class loader is not the same as or an |
|
2018 * ancestor of the class loader for the current class and |
|
2019 * invocation of {@link SecurityManager#checkPackageAccess |
|
2020 * s.checkPackageAccess()} denies access to the package |
|
2021 * of this class |
|
2022 * |
|
2023 * </ul> |
|
2024 * |
|
2025 * @since JDK1.1 |
|
2026 */ |
|
2027 public Method getMethod(String name, Class<?>... parameterTypes) |
|
2028 throws NoSuchMethodException, SecurityException { |
|
2029 // be very careful not to change the stack depth of this |
|
2030 // checkMemberAccess call for security reasons |
|
2031 // see java.lang.SecurityManager.checkMemberAccess |
|
2032 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); |
|
2033 Method method = getMethod0(name, parameterTypes); |
|
2034 if (method == null) { |
|
2035 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); |
|
2036 } |
|
2037 return method; |
|
2038 } |
|
2039 |
|
2040 |
|
2041 /** |
|
2042 * Returns a {@code Constructor} object that reflects the specified |
|
2043 * public constructor of the class represented by this {@code Class} |
|
2044 * object. The {@code parameterTypes} parameter is an array of |
|
2045 * {@code Class} objects that identify the constructor''s formal |
|
2046 * parameter types, in declared order. |
|
2047 * |
|
2048 * If this {@code Class} object represents an inner class |
|
2049 * declared in a non-static context, the formal parameter types |
|
2050 * include the explicit enclosing instance as the first parameter. |
|
2051 * |
|
2052 * <p> The constructor to reflect is the public constructor of the class |
|
2053 * represented by this {@code Class} object whose formal parameter |
|
2054 * types match those specified by {@code parameterTypes}. |
|
2055 * |
|
2056 * @param parameterTypes the parameter array |
|
2057 * @return the {@code Constructor} object of the public constructor that |
|
2058 * matches the specified {@code parameterTypes} |
|
2059 * @exception NoSuchMethodException if a matching method is not found. |
|
2060 * @exception SecurityException |
|
2061 * If a security manager, <i>s</i>, is present and any of the |
|
2062 * following conditions is met: |
|
2063 * |
|
2064 * <ul> |
|
2065 * |
|
2066 * <li> invocation of |
|
2067 * {@link SecurityManager#checkMemberAccess |
|
2068 * s.checkMemberAccess(this, Member.PUBLIC)} denies |
|
2069 * access to the constructor |
|
2070 * |
|
2071 * <li> the caller''s class loader is not the same as or an |
|
2072 * ancestor of the class loader for the current class and |
|
2073 * invocation of {@link SecurityManager#checkPackageAccess |
|
2074 * s.checkPackageAccess()} denies access to the package |
|
2075 * of this class |
|
2076 * |
|
2077 * </ul> |
|
2078 * |
|
2079 * @since JDK1.1 |
|
2080 */ |
|
2081 public Constructor<T> getConstructor(Class<?>... parameterTypes) |
|
2082 throws NoSuchMethodException, SecurityException { |
|
2083 // be very careful not to change the stack depth of this |
|
2084 // checkMemberAccess call for security reasons |
|
2085 // see java.lang.SecurityManager.checkMemberAccess |
|
2086 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); |
|
2087 return getConstructor0(parameterTypes, Member.PUBLIC); |
|
2088 } |
|
2089 |
|
2090 |
|
2091 /** |
|
2092 * Returns an array of {@code Class} objects reflecting all the |
|
2093 * classes and interfaces declared as members of the class represented by |
|
2094 * this {@code Class} object. This includes public, protected, default |
|
2095 * (package) access, and private classes and interfaces declared by the |
|
2096 * class, but excludes inherited classes and interfaces. This method |
|
2097 * returns an array of length 0 if the class declares no classes or |
|
2098 * interfaces as members, or if this {@code Class} object represents a |
|
2099 * primitive type, an array class, or void. |
|
2100 * |
|
2101 * @return the array of {@code Class} objects representing all the |
|
2102 * declared members of this class |
|
2103 * @exception SecurityException |
|
2104 * If a security manager, <i>s</i>, is present and any of the |
|
2105 * following conditions is met: |
|
2106 * |
|
2107 * <ul> |
|
2108 * |
|
2109 * <li> invocation of |
|
2110 * {@link SecurityManager#checkMemberAccess |
|
2111 * s.checkMemberAccess(this, Member.DECLARED)} denies |
|
2112 * access to the declared classes within this class |
|
2113 * |
|
2114 * <li> the caller''s class loader is not the same as or an |
|
2115 * ancestor of the class loader for the current class and |
|
2116 * invocation of {@link SecurityManager#checkPackageAccess |
|
2117 * s.checkPackageAccess()} denies access to the package |
|
2118 * of this class |
|
2119 * |
|
2120 * </ul> |
|
2121 * |
|
2122 * @since JDK1.1 |
|
2123 */ |
|
2124 public Class<?>[] getDeclaredClasses() throws SecurityException { |
|
2125 // be very careful not to change the stack depth of this |
|
2126 // checkMemberAccess call for security reasons |
|
2127 // see java.lang.SecurityManager.checkMemberAccess |
|
2128 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); |
|
2129 return getDeclaredClasses0(); |
|
2130 } |
|
2131 |
|
2132 |
|
2133 /** |
|
2134 * Returns an array of {@code Field} objects reflecting all the fields |
|
2135 * declared by the class or interface represented by this |
|
2136 * {@code Class} object. This includes public, protected, default |
|
2137 * (package) access, and private fields, but excludes inherited fields. |
|
2138 * The elements in the array returned are not sorted and are not in any |
|
2139 * particular order. This method returns an array of length 0 if the class |
|
2140 * or interface declares no fields, or if this {@code Class} object |
|
2141 * represents a primitive type, an array class, or void. |
|
2142 * |
|
2143 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3. |
|
2144 * |
|
2145 * @return the array of {@code Field} objects representing all the |
|
2146 * declared fields of this class |
|
2147 * @exception SecurityException |
|
2148 * If a security manager, <i>s</i>, is present and any of the |
|
2149 * following conditions is met: |
|
2150 * |
|
2151 * <ul> |
|
2152 * |
|
2153 * <li> invocation of |
|
2154 * {@link SecurityManager#checkMemberAccess |
|
2155 * s.checkMemberAccess(this, Member.DECLARED)} denies |
|
2156 * access to the declared fields within this class |
|
2157 * |
|
2158 * <li> the caller''s class loader is not the same as or an |
|
2159 * ancestor of the class loader for the current class and |
|
2160 * invocation of {@link SecurityManager#checkPackageAccess |
|
2161 * s.checkPackageAccess()} denies access to the package |
|
2162 * of this class |
|
2163 * |
|
2164 * </ul> |
|
2165 * |
|
2166 * @since JDK1.1 |
|
2167 */ |
|
2168 public Field[] getDeclaredFields() throws SecurityException { |
|
2169 // be very careful not to change the stack depth of this |
|
2170 // checkMemberAccess call for security reasons |
|
2171 // see java.lang.SecurityManager.checkMemberAccess |
|
2172 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); |
|
2173 return copyFields(privateGetDeclaredFields(false)); |
|
2174 } |
|
2175 |
|
2176 |
|
2177 /** |
|
2178 * Returns an array of {@code Method} objects reflecting all the |
|
2179 * methods declared by the class or interface represented by this |
|
2180 * {@code Class} object. This includes public, protected, default |
|
2181 * (package) access, and private methods, but excludes inherited methods. |
|
2182 * The elements in the array returned are not sorted and are not in any |
|
2183 * particular order. This method returns an array of length 0 if the class |
|
2184 * or interface declares no methods, or if this {@code Class} object |
|
2185 * represents a primitive type, an array class, or void. The class |
|
2186 * initialization method {@code <clinit>} is not included in the |
|
2187 * returned array. If the class declares multiple public member methods |
|
2188 * with the same parameter types, they are all included in the returned |
|
2189 * array. |
|
2190 * |
|
2191 * <p> See <em>The Java Language Specification</em>, section 8.2. |
|
2192 * |
|
2193 * @return the array of {@code Method} objects representing all the |
|
2194 * declared methods of this class |
|
2195 * @exception SecurityException |
|
2196 * If a security manager, <i>s</i>, is present and any of the |
|
2197 * following conditions is met: |
|
2198 * |
|
2199 * <ul> |
|
2200 * |
|
2201 * <li> invocation of |
|
2202 * {@link SecurityManager#checkMemberAccess |
|
2203 * s.checkMemberAccess(this, Member.DECLARED)} denies |
|
2204 * access to the declared methods within this class |
|
2205 * |
|
2206 * <li> the caller''s class loader is not the same as or an |
|
2207 * ancestor of the class loader for the current class and |
|
2208 * invocation of {@link SecurityManager#checkPackageAccess |
|
2209 * s.checkPackageAccess()} denies access to the package |
|
2210 * of this class |
|
2211 * |
|
2212 * </ul> |
|
2213 * |
|
2214 * @since JDK1.1 |
|
2215 */ |
|
2216 public Method[] getDeclaredMethods() throws SecurityException { |
|
2217 // be very careful not to change the stack depth of this |
|
2218 // checkMemberAccess call for security reasons |
|
2219 // see java.lang.SecurityManager.checkMemberAccess |
|
2220 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); |
|
2221 return copyMethods(privateGetDeclaredMethods(false)); |
|
2222 } |
|
2223 |
|
2224 |
|
2225 /** |
|
2226 * Returns an array of {@code Constructor} objects reflecting all the |
|
2227 * constructors declared by the class represented by this |
|
2228 * {@code Class} object. These are public, protected, default |
|
2229 * (package) access, and private constructors. The elements in the array |
|
2230 * returned are not sorted and are not in any particular order. If the |
|
2231 * class has a default constructor, it is included in the returned array. |
|
2232 * This method returns an array of length 0 if this {@code Class} |
|
2233 * object represents an interface, a primitive type, an array class, or |
|
2234 * void. |
|
2235 * |
|
2236 * <p> See <em>The Java Language Specification</em>, section 8.2. |
|
2237 * |
|
2238 * @return the array of {@code Constructor} objects representing all the |
|
2239 * declared constructors of this class |
|
2240 * @exception SecurityException |
|
2241 * If a security manager, <i>s</i>, is present and any of the |
|
2242 * following conditions is met: |
|
2243 * |
|
2244 * <ul> |
|
2245 * |
|
2246 * <li> invocation of |
|
2247 * {@link SecurityManager#checkMemberAccess |
|
2248 * s.checkMemberAccess(this, Member.DECLARED)} denies |
|
2249 * access to the declared constructors within this class |
|
2250 * |
|
2251 * <li> the caller''s class loader is not the same as or an |
|
2252 * ancestor of the class loader for the current class and |
|
2253 * invocation of {@link SecurityManager#checkPackageAccess |
|
2254 * s.checkPackageAccess()} denies access to the package |
|
2255 * of this class |
|
2256 * |
|
2257 * </ul> |
|
2258 * |
|
2259 * @since JDK1.1 |
|
2260 */ |
|
2261 public Constructor<?>[] getDeclaredConstructors() throws SecurityException { |
|
2262 // be very careful not to change the stack depth of this |
|
2263 // checkMemberAccess call for security reasons |
|
2264 // see java.lang.SecurityManager.checkMemberAccess |
|
2265 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); |
|
2266 return copyConstructors(privateGetDeclaredConstructors(false)); |
|
2267 } |
|
2268 |
|
2269 |
|
2270 /** |
|
2271 * Returns a {@code Field} object that reflects the specified declared |
|
2272 * field of the class or interface represented by this {@code Class} |
|
2273 * object. The {@code name} parameter is a {@code String} that |
|
2274 * specifies the simple name of the desired field. Note that this method |
|
2275 * will not reflect the {@code length} field of an array class. |
|
2276 * |
|
2277 * @param name the name of the field |
|
2278 * @return the {@code Field} object for the specified field in this |
|
2279 * class |
|
2280 * @exception NoSuchFieldException if a field with the specified name is |
|
2281 * not found. |
|
2282 * @exception NullPointerException if {@code name} is {@code null} |
|
2283 * @exception SecurityException |
|
2284 * If a security manager, <i>s</i>, is present and any of the |
|
2285 * following conditions is met: |
|
2286 * |
|
2287 * <ul> |
|
2288 * |
|
2289 * <li> invocation of |
|
2290 * {@link SecurityManager#checkMemberAccess |
|
2291 * s.checkMemberAccess(this, Member.DECLARED)} denies |
|
2292 * access to the declared field |
|
2293 * |
|
2294 * <li> the caller''s class loader is not the same as or an |
|
2295 * ancestor of the class loader for the current class and |
|
2296 * invocation of {@link SecurityManager#checkPackageAccess |
|
2297 * s.checkPackageAccess()} denies access to the package |
|
2298 * of this class |
|
2299 * |
|
2300 * </ul> |
|
2301 * |
|
2302 * @since JDK1.1 |
|
2303 */ |
|
2304 public Field getDeclaredField(String name) |
|
2305 throws NoSuchFieldException, SecurityException { |
|
2306 // be very careful not to change the stack depth of this |
|
2307 // checkMemberAccess call for security reasons |
|
2308 // see java.lang.SecurityManager.checkMemberAccess |
|
2309 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); |
|
2310 Field field = searchFields(privateGetDeclaredFields(false), name); |
|
2311 if (field == null) { |
|
2312 throw new NoSuchFieldException(name); |
|
2313 } |
|
2314 return field; |
|
2315 } |
|
2316 |
|
2317 |
|
2318 /** |
|
2319 * Returns a {@code Method} object that reflects the specified |
|
2320 * declared method of the class or interface represented by this |
|
2321 * {@code Class} object. The {@code name} parameter is a |
|
2322 * {@code String} that specifies the simple name of the desired |
|
2323 * method, and the {@code parameterTypes} parameter is an array of |
|
2324 * {@code Class} objects that identify the method''s formal parameter |
|
2325 * types, in declared order. If more than one method with the same |
|
2326 * parameter types is declared in a class, and one of these methods has a |
|
2327 * return type that is more specific than any of the others, that method is |
|
2328 * returned; otherwise one of the methods is chosen arbitrarily. If the |
|
2329 * name is "<init>"or "<clinit>" a {@code NoSuchMethodException} |
|
2330 * is raised. |
|
2331 * |
|
2332 * @param name the name of the method |
|
2333 * @param parameterTypes the parameter array |
|
2334 * @return the {@code Method} object for the method of this class |
|
2335 * matching the specified name and parameters |
|
2336 * @exception NoSuchMethodException if a matching method is not found. |
|
2337 * @exception NullPointerException if {@code name} is {@code null} |
|
2338 * @exception SecurityException |
|
2339 * If a security manager, <i>s</i>, is present and any of the |
|
2340 * following conditions is met: |
|
2341 * |
|
2342 * <ul> |
|
2343 * |
|
2344 * <li> invocation of |
|
2345 * {@link SecurityManager#checkMemberAccess |
|
2346 * s.checkMemberAccess(this, Member.DECLARED)} denies |
|
2347 * access to the declared method |
|
2348 * |
|
2349 * <li> the caller''s class loader is not the same as or an |
|
2350 * ancestor of the class loader for the current class and |
|
2351 * invocation of {@link SecurityManager#checkPackageAccess |
|
2352 * s.checkPackageAccess()} denies access to the package |
|
2353 * of this class |
|
2354 * |
|
2355 * </ul> |
|
2356 * |
|
2357 * @since JDK1.1 |
|
2358 */ |
|
2359 public Method getDeclaredMethod(String name, Class<?>... parameterTypes) |
|
2360 throws NoSuchMethodException, SecurityException { |
|
2361 // be very careful not to change the stack depth of this |
|
2362 // checkMemberAccess call for security reasons |
|
2363 // see java.lang.SecurityManager.checkMemberAccess |
|
2364 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); |
|
2365 Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes); |
|
2366 if (method == null) { |
|
2367 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); |
|
2368 } |
|
2369 return method; |
|
2370 } |
|
2371 |
|
2372 |
|
2373 /** |
|
2374 * Returns a {@code Constructor} object that reflects the specified |
|
2375 * constructor of the class or interface represented by this |
|
2376 * {@code Class} object. The {@code parameterTypes} parameter is |
|
2377 * an array of {@code Class} objects that identify the constructor''s |
|
2378 * formal parameter types, in declared order. |
|
2379 * |
|
2380 * If this {@code Class} object represents an inner class |
|
2381 * declared in a non-static context, the formal parameter types |
|
2382 * include the explicit enclosing instance as the first parameter. |
|
2383 * |
|
2384 * @param parameterTypes the parameter array |
|
2385 * @return The {@code Constructor} object for the constructor with the |
|
2386 * specified parameter list |
|
2387 * @exception NoSuchMethodException if a matching method is not found. |
|
2388 * @exception SecurityException |
|
2389 * If a security manager, <i>s</i>, is present and any of the |
|
2390 * following conditions is met: |
|
2391 * |
|
2392 * <ul> |
|
2393 * |
|
2394 * <li> invocation of |
|
2395 * {@link SecurityManager#checkMemberAccess |
|
2396 * s.checkMemberAccess(this, Member.DECLARED)} denies |
|
2397 * access to the declared constructor |
|
2398 * |
|
2399 * <li> the caller''s class loader is not the same as or an |
|
2400 * ancestor of the class loader for the current class and |
|
2401 * invocation of {@link SecurityManager#checkPackageAccess |
|
2402 * s.checkPackageAccess()} denies access to the package |
|
2403 * of this class |
|
2404 * |
|
2405 * </ul> |
|
2406 * |
|
2407 * @since JDK1.1 |
|
2408 */ |
|
2409 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) |
|
2410 throws NoSuchMethodException, SecurityException { |
|
2411 // be very careful not to change the stack depth of this |
|
2412 // checkMemberAccess call for security reasons |
|
2413 // see java.lang.SecurityManager.checkMemberAccess |
|
2414 checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); |
|
2415 return getConstructor0(parameterTypes, Member.DECLARED); |
|
2416 } |
|
2417 |
|
2418 /** |
|
2419 * Finds a resource with a given name. The rules for searching resources |
|
2420 * associated with a given class are implemented by the defining |
|
2421 * {@linkplain ClassLoader class loader} of the class. This method |
|
2422 * delegates to this object''s class loader. If this object was loaded by |
|
2423 * the bootstrap class loader, the method delegates to {@link |
|
2424 * ClassLoader#getSystemResourceAsStream}. |
|
2425 * |
|
2426 * <p> Before delegation, an absolute resource name is constructed from the |
|
2427 * given resource name using this algorithm: |
|
2428 * |
|
2429 * <ul> |
|
2430 * |
|
2431 * <li> If the {@code name} begins with a {@code ''/''} |
|
2432 * (<tt>''\u002f''</tt>), then the absolute name of the resource is the |
|
2433 * portion of the {@code name} following the {@code ''/''}. |
|
2434 * |
|
2435 * <li> Otherwise, the absolute name is of the following form: |
|
2436 * |
|
2437 * <blockquote> |
|
2438 * {@code modified_package_name/name} |
|
2439 * </blockquote> |
|
2440 * |
|
2441 * <p> Where the {@code modified_package_name} is the package name of this |
|
2442 * object with {@code ''/''} substituted for {@code ''.''} |
|
2443 * (<tt>''\u002e''</tt>). |
|
2444 * |
|
2445 * </ul> |
|
2446 * |
|
2447 * @param name name of the desired resource |
|
2448 * @return A {@link java.io.InputStream} object or {@code null} if |
|
2449 * no resource with this name is found |
|
2450 * @throws NullPointerException If {@code name} is {@code null} |
|
2451 * @since JDK1.1 |
|
2452 */ |
|
2453 public InputStream getResourceAsStream(String name) { |
|
2454 name = resolveName(name); |
|
2455 ClassLoader cl = getClassLoader0(); |
|
2456 if (cl==null) { |
|
2457 // A system class. |
|
2458 return ClassLoader.getSystemResourceAsStream(name); |
|
2459 } |
|
2460 return cl.getResourceAsStream(name); |
|
2461 } |
|
2462 |
|
2463 /** |
|
2464 * Finds a resource with a given name. The rules for searching resources |
|
2465 * associated with a given class are implemented by the defining |
|
2466 * {@linkplain ClassLoader class loader} of the class. This method |
|
2467 * delegates to this object''s class loader. If this object was loaded by |
|
2468 * the bootstrap class loader, the method delegates to {@link |
|
2469 * ClassLoader#getSystemResource}. |
|
2470 * |
|
2471 * <p> Before delegation, an absolute resource name is constructed from the |
|
2472 * given resource name using this algorithm: |
|
2473 * |
|
2474 * <ul> |
|
2475 * |
|
2476 * <li> If the {@code name} begins with a {@code ''/''} |
|
2477 * (<tt>''\u002f''</tt>), then the absolute name of the resource is the |
|
2478 * portion of the {@code name} following the {@code ''/''}. |
|
2479 * |
|
2480 * <li> Otherwise, the absolute name is of the following form: |
|
2481 * |
|
2482 * <blockquote> |
|
2483 * {@code modified_package_name/name} |
|
2484 * </blockquote> |
|
2485 * |
|
2486 * <p> Where the {@code modified_package_name} is the package name of this |
|
2487 * object with {@code ''/''} substituted for {@code ''.''} |
|
2488 * (<tt>''\u002e''</tt>). |
|
2489 * |
|
2490 * </ul> |
|
2491 * |
|
2492 * @param name name of the desired resource |
|
2493 * @return A {@link java.net.URL} object or {@code null} if no |
|
2494 * resource with this name is found |
|
2495 * @since JDK1.1 |
|
2496 */ |
|
2497 public java.net.URL getResource(String name) { |
|
2498 name = resolveName(name); |
|
2499 ClassLoader cl = getClassLoader0(); |
|
2500 if (cl==null) { |
|
2501 // A system class. |
|
2502 return ClassLoader.getSystemResource(name); |
|
2503 } |
|
2504 return cl.getResource(name); |
|
2505 } |
|
2506 |
|
2507 |
|
2508 |
|
2509 /** protection domain returned when the internal domain is null */ |
|
2510 private static java.security.ProtectionDomain allPermDomain; |
|
2511 |
|
2512 |
|
2513 /** |
|
2514 * Returns the {@code ProtectionDomain} of this class. If there is a |
|
2515 * security manager installed, this method first calls the security |
|
2516 * manager''s {@code checkPermission} method with a |
|
2517 * {@code RuntimePermission("getProtectionDomain")} permission to |
|
2518 * ensure it''s ok to get the |
|
2519 * {@code ProtectionDomain}. |
|
2520 * |
|
2521 * @return the ProtectionDomain of this class |
|
2522 * |
|
2523 * @throws SecurityException |
|
2524 * if a security manager exists and its |
|
2525 * {@code checkPermission} method doesn''t allow |
|
2526 * getting the ProtectionDomain. |
|
2527 * |
|
2528 * @see java.security.ProtectionDomain |
|
2529 * @see SecurityManager#checkPermission |
|
2530 * @see java.lang.RuntimePermission |
|
2531 * @since 1.2 |
|
2532 */ |
|
2533 public java.security.ProtectionDomain getProtectionDomain() { |
|
2534 SecurityManager sm = System.getSecurityManager(); |
|
2535 if (sm !!= null) { |
|
2536 sm.checkPermission(SecurityConstants.GET_PD_PERMISSION); |
|
2537 } |
|
2538 java.security.ProtectionDomain pd = getProtectionDomain0(); |
|
2539 if (pd == null) { |
|
2540 if (allPermDomain == null) { |
|
2541 java.security.Permissions perms = |
|
2542 new java.security.Permissions(); |
|
2543 perms.add(SecurityConstants.ALL_PERMISSION); |
|
2544 allPermDomain = |
|
2545 new java.security.ProtectionDomain(null, perms); |
|
2546 } |
|
2547 pd = allPermDomain; |
|
2548 } |
|
2549 return pd; |
|
2550 } |
|
2551 |
|
2552 |
|
2553 /** |
|
2554 * Returns the ProtectionDomain of this class. |
|
2555 */ |
|
2556 private native java.security.ProtectionDomain getProtectionDomain0(); |
|
2557 |
|
2558 |
|
2559 /** |
|
2560 * Set the ProtectionDomain for this class. Called by |
|
2561 * ClassLoader.defineClass. |
|
2562 */ |
|
2563 native void setProtectionDomain0(java.security.ProtectionDomain pd); |
|
2564 |
|
2565 |
|
2566 /* |
|
2567 * Return the Virtual Machine''s Class object for the named |
|
2568 * primitive type. |
|
2569 */ |
|
2570 static native Class getPrimitiveClass(String name); |
|
2571 |
|
2572 |
|
2573 /* |
|
2574 * Check if client is allowed to access members. If access is denied, |
|
2575 * throw a SecurityException. |
|
2576 * |
|
2577 * Be very careful not to change the stack depth of this checkMemberAccess |
|
2578 * call for security reasons. |
|
2579 * See java.lang.SecurityManager.checkMemberAccess. |
|
2580 * |
|
2581 * <p> Default policy: allow all clients access with normal Java access |
|
2582 * control. |
|
2583 */ |
|
2584 private void checkMemberAccess(int which, ClassLoader ccl) { |
|
2585 SecurityManager s = System.getSecurityManager(); |
|
2586 if (s !!= null) { |
|
2587 s.checkMemberAccess(this, which); |
|
2588 ClassLoader cl = getClassLoader0(); |
|
2589 if ((ccl !!= null) && (ccl !!= cl) && |
|
2590 ((cl == null) || !!cl.isAncestor(ccl))) { |
|
2591 String name = this.getName(); |
|
2592 int i = name.lastIndexOf(''.''); |
|
2593 if (i !!= -1) { |
|
2594 s.checkPackageAccess(name.substring(0, i)); |
|
2595 } |
|
2596 } |
|
2597 } |
|
2598 } |
|
2599 |
|
2600 /** |
|
2601 * Add a package name prefix if the name is not absolute Remove leading "/" |
|
2602 * if name is absolute |
|
2603 */ |
|
2604 private String resolveName(String name) { |
|
2605 if (name == null) { |
|
2606 return name; |
|
2607 } |
|
2608 if (!!name.startsWith("/")) { |
|
2609 Class c = this; |
|
2610 while (c.isArray()) { |
|
2611 c = c.getComponentType(); |
|
2612 } |
|
2613 String baseName = c.getName(); |
|
2614 int index = baseName.lastIndexOf(''.''); |
|
2615 if (index !!= -1) { |
|
2616 name = baseName.substring(0, index).replace(''.'', ''/'') |
|
2617 +"/"+name; |
|
2618 } |
|
2619 } else { |
|
2620 name = name.substring(1); |
|
2621 } |
|
2622 return name; |
|
2623 } |
|
2624 |
|
2625 /** |
|
2626 * Reflection support. |
|
2627 */ |
|
2628 |
|
2629 // Caches for certain reflective results |
|
2630 private static boolean useCaches = true; |
|
2631 private volatile transient SoftReference declaredFields; |
|
2632 private volatile transient SoftReference publicFields; |
|
2633 private volatile transient SoftReference declaredMethods; |
|
2634 private volatile transient SoftReference publicMethods; |
|
2635 private volatile transient SoftReference declaredConstructors; |
|
2636 private volatile transient SoftReference publicConstructors; |
|
2637 // Intermediate results for getFields and getMethods |
|
2638 private volatile transient SoftReference declaredPublicFields; |
|
2639 private volatile transient SoftReference declaredPublicMethods; |
|
2640 |
|
2641 // Incremented by the VM on each call to JVM TI RedefineClasses() |
|
2642 // that redefines this class or a superclass. |
|
2643 private volatile transient int classRedefinedCount = 0; |
|
2644 |
|
2645 // Value of classRedefinedCount when we last cleared the cached values |
|
2646 // that are sensitive to class redefinition. |
|
2647 private volatile transient int lastRedefinedCount = 0; |
|
2648 |
|
2649 // Clears cached values that might possibly have been obsoleted by |
|
2650 // a class redefinition. |
|
2651 private void clearCachesOnClassRedefinition() { |
|
2652 if (lastRedefinedCount !!= classRedefinedCount) { |
|
2653 declaredFields = publicFields = declaredPublicFields = null; |
|
2654 declaredMethods = publicMethods = declaredPublicMethods = null; |
|
2655 declaredConstructors = publicConstructors = null; |
|
2656 annotations = declaredAnnotations = null; |
|
2657 |
|
2658 // Use of "volatile" (and synchronization by caller in the case |
|
2659 // of annotations) ensures that no thread sees the update to |
|
2660 // lastRedefinedCount before seeing the caches cleared. |
|
2661 // We do not guard against brief windows during which multiple |
|
2662 // threads might redundantly work to fill an empty cache. |
|
2663 lastRedefinedCount = classRedefinedCount; |
|
2664 } |
|
2665 } |
|
2666 |
|
2667 // Generic signature handling |
|
2668 private native String getGenericSignature(); |
|
2669 |
|
2670 // Generic info repository; lazily initialized |
|
2671 private transient ClassRepository genericInfo; |
|
2672 |
|
2673 // accessor for factory |
|
2674 private GenericsFactory getFactory() { |
|
2675 // create scope and factory |
|
2676 return CoreReflectionFactory.make(this, ClassScope.make(this)); |
|
2677 } |
|
2678 |
|
2679 // accessor for generic info repository |
|
2680 private ClassRepository getGenericInfo() { |
|
2681 // lazily initialize repository if necessary |
|
2682 if (genericInfo == null) { |
|
2683 // create and cache generic info repository |
|
2684 genericInfo = ClassRepository.make(getGenericSignature(), |
|
2685 getFactory()); |
|
2686 } |
|
2687 return genericInfo; //return cached repository |
|
2688 } |
|
2689 |
|
2690 // Annotations handling |
|
2691 private native byte[] getRawAnnotations(); |
|
2692 |
|
2693 native ConstantPool getConstantPool(); |
|
2694 |
|
2695 // |
|
2696 // |
|
2697 // java.lang.reflect.Field handling |
|
2698 // |
|
2699 // |
|
2700 |
|
2701 // Returns an array of "root" fields. These Field objects must NOT |
|
2702 // be propagated to the outside world, but must instead be copied |
|
2703 // via ReflectionFactory.copyField. |
|
2704 private Field[] privateGetDeclaredFields(boolean publicOnly) { |
|
2705 checkInitted(); |
|
2706 Field[] res = null; |
|
2707 if (useCaches) { |
|
2708 clearCachesOnClassRedefinition(); |
|
2709 if (publicOnly) { |
|
2710 if (declaredPublicFields !!= null) { |
|
2711 res = (Field[]) declaredPublicFields.get(); |
|
2712 } |
|
2713 } else { |
|
2714 if (declaredFields !!= null) { |
|
2715 res = (Field[]) declaredFields.get(); |
|
2716 } |
|
2717 } |
|
2718 if (res !!= null) return res; |
|
2719 } |
|
2720 // No cached value available; request value from VM |
|
2721 res = Reflection.filterFields(this, getDeclaredFields0(publicOnly)); |
|
2722 if (useCaches) { |
|
2723 if (publicOnly) { |
|
2724 declaredPublicFields = new SoftReference(res); |
|
2725 } else { |
|
2726 declaredFields = new SoftReference(res); |
|
2727 } |
|
2728 } |
|
2729 return res; |
|
2730 } |
|
2731 |
|
2732 // Returns an array of "root" fields. These Field objects must NOT |
|
2733 // be propagated to the outside world, but must instead be copied |
|
2734 // via ReflectionFactory.copyField. |
|
2735 private Field[] privateGetPublicFields(Set traversedInterfaces) { |
|
2736 checkInitted(); |
|
2737 Field[] res = null; |
|
2738 if (useCaches) { |
|
2739 clearCachesOnClassRedefinition(); |
|
2740 if (publicFields !!= null) { |
|
2741 res = (Field[]) publicFields.get(); |
|
2742 } |
|
2743 if (res !!= null) return res; |
|
2744 } |
|
2745 |
|
2746 // No cached value available; compute value recursively. |
|
2747 // Traverse in correct order for getField(). |
|
2748 List fields = new ArrayList(); |
|
2749 if (traversedInterfaces == null) { |
|
2750 traversedInterfaces = new HashSet(); |
|
2751 } |
|
2752 |
|
2753 // Local fields |
|
2754 Field[] tmp = privateGetDeclaredFields(true); |
|
2755 addAll(fields, tmp); |
|
2756 |
|
2757 // Direct superinterfaces, recursively |
|
2758 Class[] interfaces = getInterfaces(); |
|
2759 for (int i = 0; i < interfaces.length; i++) { |
|
2760 Class c = interfaces[i]; |
|
2761 if (!!traversedInterfaces.contains(c)) { |
|
2762 traversedInterfaces.add(c); |
|
2763 addAll(fields, c.privateGetPublicFields(traversedInterfaces)); |
|
2764 } |
|
2765 } |
|
2766 |
|
2767 // Direct superclass, recursively |
|
2768 if (!!isInterface()) { |
|
2769 Class c = getSuperclass(); |
|
2770 if (c !!= null) { |
|
2771 addAll(fields, c.privateGetPublicFields(traversedInterfaces)); |
|
2772 } |
|
2773 } |
|
2774 |
|
2775 res = new Field[fields.size()]; |
|
2776 fields.toArray(res); |
|
2777 if (useCaches) { |
|
2778 publicFields = new SoftReference(res); |
|
2779 } |
|
2780 return res; |
|
2781 } |
|
2782 |
|
2783 private static void addAll(Collection c, Field[] o) { |
|
2784 for (int i = 0; i < o.length; i++) { |
|
2785 c.add(o[i]); |
|
2786 } |
|
2787 } |
|
2788 |
|
2789 |
|
2790 // |
|
2791 // |
|
2792 // java.lang.reflect.Constructor handling |
|
2793 // |
|
2794 // |
|
2795 |
|
2796 // Returns an array of "root" constructors. These Constructor |
|
2797 // objects must NOT be propagated to the outside world, but must |
|
2798 // instead be copied via ReflectionFactory.copyConstructor. |
|
2799 private Constructor[] privateGetDeclaredConstructors(boolean publicOnly) { |
|
2800 checkInitted(); |
|
2801 Constructor[] res = null; |
|
2802 if (useCaches) { |
|
2803 clearCachesOnClassRedefinition(); |
|
2804 if (publicOnly) { |
|
2805 if (publicConstructors !!= null) { |
|
2806 res = (Constructor[]) publicConstructors.get(); |
|
2807 } |
|
2808 } else { |
|
2809 if (declaredConstructors !!= null) { |
|
2810 res = (Constructor[]) declaredConstructors.get(); |
|
2811 } |
|
2812 } |
|
2813 if (res !!= null) return res; |
|
2814 } |
|
2815 // No cached value available; request value from VM |
|
2816 if (isInterface()) { |
|
2817 res = new Constructor[0]; |
|
2818 } else { |
|
2819 res = getDeclaredConstructors0(publicOnly); |
|
2820 } |
|
2821 if (useCaches) { |
|
2822 if (publicOnly) { |
|
2823 publicConstructors = new SoftReference(res); |
|
2824 } else { |
|
2825 declaredConstructors = new SoftReference(res); |
|
2826 } |
|
2827 } |
|
2828 return res; |
|
2829 } |
|
2830 |
|
2831 // |
|
2832 // |
|
2833 // java.lang.reflect.Method handling |
|
2834 // |
|
2835 // |
|
2836 |
|
2837 // Returns an array of "root" methods. These Method objects must NOT |
|
2838 // be propagated to the outside world, but must instead be copied |
|
2839 // via ReflectionFactory.copyMethod. |
|
2840 private Method[] privateGetDeclaredMethods(boolean publicOnly) { |
|
2841 checkInitted(); |
|
2842 Method[] res = null; |
|
2843 if (useCaches) { |
|
2844 clearCachesOnClassRedefinition(); |
|
2845 if (publicOnly) { |
|
2846 if (declaredPublicMethods !!= null) { |
|
2847 res = (Method[]) declaredPublicMethods.get(); |
|
2848 } |
|
2849 } else { |
|
2850 if (declaredMethods !!= null) { |
|
2851 res = (Method[]) declaredMethods.get(); |
|
2852 } |
|
2853 } |
|
2854 if (res !!= null) return res; |
|
2855 } |
|
2856 // No cached value available; request value from VM |
|
2857 res = Reflection.filterMethods(this, getDeclaredMethods0(publicOnly)); |
|
2858 if (useCaches) { |
|
2859 if (publicOnly) { |
|
2860 declaredPublicMethods = new SoftReference(res); |
|
2861 } else { |
|
2862 declaredMethods = new SoftReference(res); |
|
2863 } |
|
2864 } |
|
2865 return res; |
|
2866 } |
|
2867 |
|
2868 static class MethodArray { |
|
2869 private Method[] methods; |
|
2870 private int length; |
|
2871 |
|
2872 MethodArray() { |
|
2873 methods = new Method[20]; |
|
2874 length = 0; |
|
2875 } |
|
2876 |
|
2877 void add(Method m) { |
|
2878 if (length == methods.length) { |
|
2879 methods = Arrays.copyOf(methods, 2 * methods.length); |
|
2880 } |
|
2881 methods[length++] = m; |
|
2882 } |
|
2883 |
|
2884 void addAll(Method[] ma) { |
|
2885 for (int i = 0; i < ma.length; i++) { |
|
2886 add(ma[i]); |
|
2887 } |
|
2888 } |
|
2889 |
|
2890 void addAll(MethodArray ma) { |
|
2891 for (int i = 0; i < ma.length(); i++) { |
|
2892 add(ma.get(i)); |
|
2893 } |
|
2894 } |
|
2895 |
|
2896 void addIfNotPresent(Method newMethod) { |
|
2897 for (int i = 0; i < length; i++) { |
|
2898 Method m = methods[i]; |
|
2899 if (m == newMethod || (m !!= null && m.equals(newMethod))) { |
|
2900 return; |
|
2901 } |
|
2902 } |
|
2903 add(newMethod); |
|
2904 } |
|
2905 |
|
2906 void addAllIfNotPresent(MethodArray newMethods) { |
|
2907 for (int i = 0; i < newMethods.length(); i++) { |
|
2908 Method m = newMethods.get(i); |
|
2909 if (m !!= null) { |
|
2910 addIfNotPresent(m); |
|
2911 } |
|
2912 } |
|
2913 } |
|
2914 |
|
2915 int length() { |
|
2916 return length; |
|
2917 } |
|
2918 |
|
2919 Method get(int i) { |
|
2920 return methods[i]; |
|
2921 } |
|
2922 |
|
2923 void removeByNameAndSignature(Method toRemove) { |
|
2924 for (int i = 0; i < length; i++) { |
|
2925 Method m = methods[i]; |
|
2926 if (m !!= null && |
|
2927 m.getReturnType() == toRemove.getReturnType() && |
|
2928 m.getName() == toRemove.getName() && |
|
2929 arrayContentsEq(m.getParameterTypes(), |
|
2930 toRemove.getParameterTypes())) { |
|
2931 methods[i] = null; |
|
2932 } |
|
2933 } |
|
2934 } |
|
2935 |
|
2936 void compactAndTrim() { |
|
2937 int newPos = 0; |
|
2938 // Get rid of null slots |
|
2939 for (int pos = 0; pos < length; pos++) { |
|
2940 Method m = methods[pos]; |
|
2941 if (m !!= null) { |
|
2942 if (pos !!= newPos) { |
|
2943 methods[newPos] = m; |
|
2944 } |
|
2945 newPos++; |
|
2946 } |
|
2947 } |
|
2948 if (newPos !!= methods.length) { |
|
2949 methods = Arrays.copyOf(methods, newPos); |
|
2950 } |
|
2951 } |
|
2952 |
|
2953 Method[] getArray() { |
|
2954 return methods; |
|
2955 } |
|
2956 } |
|
2957 |
|
2958 |
|
2959 // Returns an array of "root" methods. These Method objects must NOT |
|
2960 // be propagated to the outside world, but must instead be copied |
|
2961 // via ReflectionFactory.copyMethod. |
|
2962 private Method[] privateGetPublicMethods() { |
|
2963 checkInitted(); |
|
2964 Method[] res = null; |
|
2965 if (useCaches) { |
|
2966 clearCachesOnClassRedefinition(); |
|
2967 if (publicMethods !!= null) { |
|
2968 res = (Method[]) publicMethods.get(); |
|
2969 } |
|
2970 if (res !!= null) return res; |
|
2971 } |
|
2972 |
|
2973 // No cached value available; compute value recursively. |
|
2974 // Start by fetching public declared methods |
|
2975 MethodArray methods = new MethodArray(); |
|
2976 { |
|
2977 Method[] tmp = privateGetDeclaredMethods(true); |
|
2978 methods.addAll(tmp); |
|
2979 } |
|
2980 // Now recur over superclass and direct superinterfaces. |
|
2981 // Go over superinterfaces first so we can more easily filter |
|
2982 // out concrete implementations inherited from superclasses at |
|
2983 // the end. |
|
2984 MethodArray inheritedMethods = new MethodArray(); |
|
2985 Class[] interfaces = getInterfaces(); |
|
2986 for (int i = 0; i < interfaces.length; i++) { |
|
2987 inheritedMethods.addAll(interfaces[i].privateGetPublicMethods()); |
|
2988 } |
|
2989 if (!!isInterface()) { |
|
2990 Class c = getSuperclass(); |
|
2991 if (c !!= null) { |
|
2992 MethodArray supers = new MethodArray(); |
|
2993 supers.addAll(c.privateGetPublicMethods()); |
|
2994 // Filter out concrete implementations of any |
|
2995 // interface methods |
|
2996 for (int i = 0; i < supers.length(); i++) { |
|
2997 Method m = supers.get(i); |
|
2998 if (m !!= null && !!Modifier.isAbstract(m.getModifiers())) { |
|
2999 inheritedMethods.removeByNameAndSignature(m); |
|
3000 } |
|
3001 } |
|
3002 // Insert superclass''s inherited methods before |
|
3003 // superinterfaces'' to satisfy getMethod''s search |
|
3004 // order |
|
3005 supers.addAll(inheritedMethods); |
|
3006 inheritedMethods = supers; |
|
3007 } |
|
3008 } |
|
3009 // Filter out all local methods from inherited ones |
|
3010 for (int i = 0; i < methods.length(); i++) { |
|
3011 Method m = methods.get(i); |
|
3012 inheritedMethods.removeByNameAndSignature(m); |
|
3013 } |
|
3014 methods.addAllIfNotPresent(inheritedMethods); |
|
3015 methods.compactAndTrim(); |
|
3016 res = methods.getArray(); |
|
3017 if (useCaches) { |
|
3018 publicMethods = new SoftReference(res); |
|
3019 } |
|
3020 return res; |
|
3021 } |
|
3022 |
|
3023 |
|
3024 // |
|
3025 // Helpers for fetchers of one field, method, or constructor |
|
3026 // |
|
3027 |
|
3028 private Field searchFields(Field[] fields, String name) { |
|
3029 String internedName = name.intern(); |
|
3030 for (int i = 0; i < fields.length; i++) { |
|
3031 if (fields[i].getName() == internedName) { |
|
3032 return getReflectionFactory().copyField(fields[i]); |
|
3033 } |
|
3034 } |
|
3035 return null; |
|
3036 } |
|
3037 |
|
3038 private Field getField0(String name) throws NoSuchFieldException { |
|
3039 // Note: the intent is that the search algorithm this routine |
|
3040 // uses be equivalent to the ordering imposed by |
|
3041 // privateGetPublicFields(). It fetches only the declared |
|
3042 // public fields for each class, however, to reduce the number |
|
3043 // of Field objects which have to be created for the common |
|
3044 // case where the field being requested is declared in the |
|
3045 // class which is being queried. |
|
3046 Field res = null; |
|
3047 // Search declared public fields |
|
3048 if ((res = searchFields(privateGetDeclaredFields(true), name)) !!= null) { |
|
3049 return res; |
|
3050 } |
|
3051 // Direct superinterfaces, recursively |
|
3052 Class[] interfaces = getInterfaces(); |
|
3053 for (int i = 0; i < interfaces.length; i++) { |
|
3054 Class c = interfaces[i]; |
|
3055 if ((res = c.getField0(name)) !!= null) { |
|
3056 return res; |
|
3057 } |
|
3058 } |
|
3059 // Direct superclass, recursively |
|
3060 if (!!isInterface()) { |
|
3061 Class c = getSuperclass(); |
|
3062 if (c !!= null) { |
|
3063 if ((res = c.getField0(name)) !!= null) { |
|
3064 return res; |
|
3065 } |
|
3066 } |
|
3067 } |
|
3068 return null; |
|
3069 } |
|
3070 |
|
3071 private static Method searchMethods(Method[] methods, |
|
3072 String name, |
|
3073 Class[] parameterTypes) |
|
3074 { |
|
3075 Method res = null; |
|
3076 String internedName = name.intern(); |
|
3077 for (int i = 0; i < methods.length; i++) { |
|
3078 Method m = methods[i]; |
|
3079 if (m.getName() == internedName |
|
3080 && arrayContentsEq(parameterTypes, m.getParameterTypes()) |
|
3081 && (res == null |
|
3082 || res.getReturnType().isAssignableFrom(m.getReturnType()))) |
|
3083 res = m; |
|
3084 } |
|
3085 |
|
3086 return (res == null ? res : getReflectionFactory().copyMethod(res)); |
|
3087 } |
|
3088 |
|
3089 |
|
3090 private Method getMethod0(String name, Class[] parameterTypes) { |
|
3091 // Note: the intent is that the search algorithm this routine |
|
3092 // uses be equivalent to the ordering imposed by |
|
3093 // privateGetPublicMethods(). It fetches only the declared |
|
3094 // public methods for each class, however, to reduce the |
|
3095 // number of Method objects which have to be created for the |
|
3096 // common case where the method being requested is declared in |
|
3097 // the class which is being queried. |
|
3098 Method res = null; |
|
3099 // Search declared public methods |
|
3100 if ((res = searchMethods(privateGetDeclaredMethods(true), |
|
3101 name, |
|
3102 parameterTypes)) !!= null) { |
|
3103 return res; |
|
3104 } |
|
3105 // Search superclass''s methods |
|
3106 if (!!isInterface()) { |
|
3107 Class c = getSuperclass(); |
|
3108 if (c !!= null) { |
|
3109 if ((res = c.getMethod0(name, parameterTypes)) !!= null) { |
|
3110 return res; |
|
3111 } |
|
3112 } |
|
3113 } |
|
3114 // Search superinterfaces'' methods |
|
3115 Class[] interfaces = getInterfaces(); |
|
3116 for (int i = 0; i < interfaces.length; i++) { |
|
3117 Class c = interfaces[i]; |
|
3118 if ((res = c.getMethod0(name, parameterTypes)) !!= null) { |
|
3119 return res; |
|
3120 } |
|
3121 } |
|
3122 // Not found |
|
3123 return null; |
|
3124 } |
|
3125 |
|
3126 private Constructor<T> getConstructor0(Class[] parameterTypes, |
|
3127 int which) throws NoSuchMethodException |
|
3128 { |
|
3129 Constructor[] constructors = privateGetDeclaredConstructors((which == Member.PUBLIC)); |
|
3130 for (int i = 0; i < constructors.length; i++) { |
|
3131 if (arrayContentsEq(parameterTypes, |
|
3132 constructors[i].getParameterTypes())) { |
|
3133 return getReflectionFactory().copyConstructor(constructors[i]); |
|
3134 } |
|
3135 } |
|
3136 throw new NoSuchMethodException(getName() + ".<init>" + argumentTypesToString(parameterTypes)); |
|
3137 } |
|
3138 |
|
3139 // |
|
3140 // Other helpers and base implementation |
|
3141 // |
|
3142 |
|
3143 private static boolean arrayContentsEq(Object[] a1, Object[] a2) { |
|
3144 if (a1 == null) { |
|
3145 return a2 == null || a2.length == 0; |
|
3146 } |
|
3147 |
|
3148 if (a2 == null) { |
|
3149 return a1.length == 0; |
|
3150 } |
|
3151 |
|
3152 if (a1.length !!= a2.length) { |
|
3153 return false; |
|
3154 } |
|
3155 |
|
3156 for (int i = 0; i < a1.length; i++) { |
|
3157 if (a1[i] !!= a2[i]) { |
|
3158 return false; |
|
3159 } |
|
3160 } |
|
3161 |
|
3162 return true; |
|
3163 } |
|
3164 |
|
3165 private static Field[] copyFields(Field[] arg) { |
|
3166 Field[] out = new Field[arg.length]; |
|
3167 ReflectionFactory fact = getReflectionFactory(); |
|
3168 for (int i = 0; i < arg.length; i++) { |
|
3169 out[i] = fact.copyField(arg[i]); |
|
3170 } |
|
3171 return out; |
|
3172 } |
|
3173 |
|
3174 private static Method[] copyMethods(Method[] arg) { |
|
3175 Method[] out = new Method[arg.length]; |
|
3176 ReflectionFactory fact = getReflectionFactory(); |
|
3177 for (int i = 0; i < arg.length; i++) { |
|
3178 out[i] = fact.copyMethod(arg[i]); |
|
3179 } |
|
3180 return out; |
|
3181 } |
|
3182 |
|
3183 private static Constructor[] copyConstructors(Constructor[] arg) { |
|
3184 Constructor[] out = new Constructor[arg.length]; |
|
3185 ReflectionFactory fact = getReflectionFactory(); |
|
3186 for (int i = 0; i < arg.length; i++) { |
|
3187 out[i] = fact.copyConstructor(arg[i]); |
|
3188 } |
|
3189 return out; |
|
3190 } |
|
3191 |
|
3192 private native Field[] getDeclaredFields0(boolean publicOnly); |
|
3193 private native Method[] getDeclaredMethods0(boolean publicOnly); |
|
3194 private native Constructor[] getDeclaredConstructors0(boolean publicOnly); |
|
3195 private native Class[] getDeclaredClasses0(); |
|
3196 |
|
3197 private static String argumentTypesToString(Class[] argTypes) { |
|
3198 StringBuilder buf = new StringBuilder(); |
|
3199 buf.append("("); |
|
3200 if (argTypes !!= null) { |
|
3201 for (int i = 0; i < argTypes.length; i++) { |
|
3202 if (i > 0) { |
|
3203 buf.append(", "); |
|
3204 } |
|
3205 Class c = argTypes[i]; |
|
3206 buf.append((c == null) ? "null" : c.getName()); |
|
3207 } |
|
3208 } |
|
3209 buf.append(")"); |
|
3210 return buf.toString(); |
|
3211 } |
|
3212 |
|
3213 /** use serialVersionUID from JDK 1.1 for interoperability */ |
|
3214 private static final long serialVersionUID = 3206093459760846163L; |
|
3215 |
|
3216 |
|
3217 /** |
|
3218 * Class Class is special cased within the Serialization Stream Protocol. |
|
3219 * |
|
3220 * A Class instance is written initially into an ObjectOutputStream in the |
|
3221 * following format: |
|
3222 * <pre> |
|
3223 * {@code TC_CLASS} ClassDescriptor |
|
3224 * A ClassDescriptor is a special cased serialization of |
|
3225 * a {@code java.io.ObjectStreamClass} instance. |
|
3226 * </pre> |
|
3227 * A new handle is generated for the initial time the class descriptor |
|
3228 * is written into the stream. Future references to the class descriptor |
|
3229 * are written as references to the initial class descriptor instance. |
|
3230 * |
|
3231 * @see java.io.ObjectStreamClass |
|
3232 */ |
|
3233 private static final ObjectStreamField[] serialPersistentFields = |
|
3234 new ObjectStreamField[0]; |
|
3235 |
|
3236 |
|
3237 /** |
|
3238 * Returns the assertion status that would be assigned to this |
|
3239 * class if it were to be initialized at the time this method is invoked. |
|
3240 * If this class has had its assertion status set, the most recent |
|
3241 * setting will be returned; otherwise, if any package default assertion |
|
3242 * status pertains to this class, the most recent setting for the most |
|
3243 * specific pertinent package default assertion status is returned; |
|
3244 * otherwise, if this class is not a system class (i.e., it has a |
|
3245 * class loader) its class loader''s default assertion status is returned; |
|
3246 * otherwise, the system class default assertion status is returned. |
|
3247 * <p> |
|
3248 * Few programmers will have any need for this method; it is provided |
|
3249 * for the benefit of the JRE itself. (It allows a class to determine at |
|
3250 * the time that it is initialized whether assertions should be enabled.) |
|
3251 * Note that this method is not guaranteed to return the actual |
|
3252 * assertion status that was (or will be) associated with the specified |
|
3253 * class when it was (or will be) initialized. |
|
3254 * |
|
3255 * @return the desired assertion status of the specified class. |
|
3256 * @see java.lang.ClassLoader#setClassAssertionStatus |
|
3257 * @see java.lang.ClassLoader#setPackageAssertionStatus |
|
3258 * @see java.lang.ClassLoader#setDefaultAssertionStatus |
|
3259 * @since 1.4 |
|
3260 */ |
|
3261 public boolean desiredAssertionStatus() { |
|
3262 ClassLoader loader = getClassLoader(); |
|
3263 // If the loader is null this is a system class, so ask the VM |
|
3264 if (loader == null) |
|
3265 return desiredAssertionStatus0(this); |
|
3266 |
|
3267 synchronized(loader) { |
|
3268 // If the classloader has been initialized with |
|
3269 // the assertion directives, ask it. Otherwise, |
|
3270 // ask the VM. |
|
3271 return (loader.classAssertionStatus == null ? |
|
3272 desiredAssertionStatus0(this) : |
|
3273 loader.desiredAssertionStatus(getName())); |
|
3274 } |
|
3275 } |
|
3276 |
|
3277 // Retrieves the desired assertion status of this class from the VM |
|
3278 private static native boolean desiredAssertionStatus0(Class clazz); |
|
3279 |
|
3280 /** |
|
3281 * Returns true if and only if this class was declared as an enum in the |
|
3282 * source code. |
|
3283 * |
|
3284 * @return true if and only if this class was declared as an enum in the |
|
3285 * source code |
|
3286 * @since 1.5 |
|
3287 */ |
|
3288 public boolean isEnum() { |
|
3289 // An enum must both directly extend java.lang.Enum and have |
|
3290 // the ENUM bit set; classes for specialized enum constants |
|
3291 // don''t do the former. |
|
3292 return (this.getModifiers() & ENUM) !!= 0 && |
|
3293 this.getSuperclass() == java.lang.Enum.class; |
|
3294 } |
|
3295 |
|
3296 // Fetches the factory for reflective objects |
|
3297 private static ReflectionFactory getReflectionFactory() { |
|
3298 if (reflectionFactory == null) { |
|
3299 reflectionFactory = (ReflectionFactory) |
|
3300 java.security.AccessController.doPrivileged |
|
3301 (new sun.reflect.ReflectionFactory.GetReflectionFactoryAction()); |
|
3302 } |
|
3303 return reflectionFactory; |
|
3304 } |
|
3305 private static ReflectionFactory reflectionFactory; |
|
3306 |
|
3307 // To be able to query system properties as soon as they''re available |
|
3308 private static boolean initted = false; |
|
3309 private static void checkInitted() { |
|
3310 if (initted) return; |
|
3311 AccessController.doPrivileged(new PrivilegedAction() { |
|
3312 public Object run() { |
|
3313 // Tests to ensure the system properties table is fully |
|
3314 // initialized. This is needed because reflection code is |
|
3315 // called very early in the initialization process (before |
|
3316 // command-line arguments have been parsed and therefore |
|
3317 // these user-settable properties installed.) We assume that |
|
3318 // if System.out is non-null then the System class has been |
|
3319 // fully initialized and that the bulk of the startup code |
|
3320 // has been run. |
|
3321 |
|
3322 if (System.out == null) { |
|
3323 // java.lang.System not yet fully initialized |
|
3324 return null; |
|
3325 } |
|
3326 |
|
3327 String val = |
|
3328 System.getProperty("sun.reflect.noCaches"); |
|
3329 if (val !!= null && val.equals("true")) { |
|
3330 useCaches = false; |
|
3331 } |
|
3332 |
|
3333 initted = true; |
|
3334 return null; |
|
3335 } |
|
3336 }); |
|
3337 } |
|
3338 |
|
3339 /** |
|
3340 * Returns the elements of this enum class or null if this |
|
3341 * Class object does not represent an enum type. |
|
3342 * |
|
3343 * @return an array containing the values comprising the enum class |
|
3344 * represented by this Class object in the order they''re |
|
3345 * declared, or null if this Class object does not |
|
3346 * represent an enum type |
|
3347 * @since 1.5 |
|
3348 */ |
|
3349 public T[] getEnumConstants() { |
|
3350 T[] values = getEnumConstantsShared(); |
|
3351 return (values !!= null) ? values.clone() : null; |
|
3352 } |
|
3353 |
|
3354 /** |
|
3355 * Returns the elements of this enum class or null if this |
|
3356 * Class object does not represent an enum type; |
|
3357 * identical to getEnumConstantsShared except that |
|
3358 * the result is uncloned, cached, and shared by all callers. |
|
3359 */ |
|
3360 T[] getEnumConstantsShared() { |
|
3361 if (enumConstants == null) { |
|
3362 if (!!isEnum()) return null; |
|
3363 try { |
|
3364 final Method values = getMethod("values"); |
|
3365 java.security.AccessController.doPrivileged |
|
3366 (new java.security.PrivilegedAction() { |
|
3367 public Object run() { |
|
3368 values.setAccessible(true); |
|
3369 return null; |
|
3370 } |
|
3371 }); |
|
3372 enumConstants = (T[])values.invoke(null); |
|
3373 } |
|
3374 // These can happen when users concoct enum-like classes |
|
3375 // that don''t comply with the enum spec. |
|
3376 catch (InvocationTargetException ex) { return null; } |
|
3377 catch (NoSuchMethodException ex) { return null; } |
|
3378 catch (IllegalAccessException ex) { return null; } |
|
3379 } |
|
3380 return enumConstants; |
|
3381 } |
|
3382 private volatile transient T[] enumConstants = null; |
|
3383 |
|
3384 /** |
|
3385 * Returns a map from simple name to enum constant. This package-private |
|
3386 * method is used internally by Enum to implement |
|
3387 * public static <T extends Enum<T>> T valueOf(Class<T>, String) |
|
3388 * efficiently. Note that the map is returned by this method is |
|
3389 * created lazily on first use. Typically it won''t ever get created. |
|
3390 */ |
|
3391 Map<String, T> enumConstantDirectory() { |
|
3392 if (enumConstantDirectory == null) { |
|
3393 T[] universe = getEnumConstantsShared(); |
|
3394 if (universe == null) |
|
3395 throw new IllegalArgumentException( |
|
3396 getName() + " is not an enum type"); |
|
3397 Map<String, T> m = new HashMap<String, T>(2 * universe.length); |
|
3398 for (T constant : universe) |
|
3399 m.put(((Enum)constant).name(), constant); |
|
3400 enumConstantDirectory = m; |
|
3401 } |
|
3402 return enumConstantDirectory; |
|
3403 } |
|
3404 private volatile transient Map<String, T> enumConstantDirectory = null; |
|
3405 |
|
3406 /** |
|
3407 * Casts an object to the class or interface represented |
|
3408 * by this {@code Class} object. |
|
3409 * |
|
3410 * @param obj the object to be cast |
|
3411 * @return the object after casting, or null if obj is null |
|
3412 * |
|
3413 * @throws ClassCastException if the object is not |
|
3414 * null and is not assignable to the type T. |
|
3415 * |
|
3416 * @since 1.5 |
|
3417 */ |
|
3418 public T cast(Object obj) { |
|
3419 if (obj !!= null && !!isInstance(obj)) |
|
3420 throw new ClassCastException(cannotCastMsg(obj)); |
|
3421 return (T) obj; |
|
3422 } |
|
3423 |
|
3424 private String cannotCastMsg(Object obj) { |
|
3425 return "Cannot cast " + obj.getClass().getName() + " to " + getName(); |
|
3426 } |
|
3427 |
|
3428 /** |
|
3429 * Casts this {@code Class} object to represent a subclass of the class |
|
3430 * represented by the specified class object. Checks that that the cast |
|
3431 * is valid, and throws a {@code ClassCastException} if it is not. If |
|
3432 * this method succeeds, it always returns a reference to this class object. |
|
3433 * |
|
3434 * <p>This method is useful when a client needs to "narrow" the type of |
|
3435 * a {@code Class} object to pass it to an API that restricts the |
|
3436 * {@code Class} objects that it is willing to accept. A cast would |
|
3437 * generate a compile-time warning, as the correctness of the cast |
|
3438 * could not be checked at runtime (because generic types are implemented |
|
3439 * by erasure). |
|
3440 * |
|
3441 * @return this {@code Class} object, cast to represent a subclass of |
|
3442 * the specified class object. |
|
3443 * @throws ClassCastException if this {@code Class} object does not |
|
3444 * represent a subclass of the specified class (here "subclass" includes |
|
3445 * the class itself). |
|
3446 * @since 1.5 |
|
3447 */ |
|
3448 public <U> Class<? extends U> asSubclass(Class<U> clazz) { |
|
3449 if (clazz.isAssignableFrom(this)) |
|
3450 return (Class<? extends U>) this; |
|
3451 else |
|
3452 throw new ClassCastException(this.toString()); |
|
3453 } |
|
3454 |
|
3455 /** |
|
3456 * @throws NullPointerException {@inheritDoc} |
|
3457 * @since 1.5 |
|
3458 */ |
|
3459 public <A extends Annotation> A getAnnotation(Class<A> annotationClass) { |
|
3460 if (annotationClass == null) |
|
3461 throw new NullPointerException(); |
|
3462 |
|
3463 initAnnotationsIfNecessary(); |
|
3464 return (A) annotations.get(annotationClass); |
|
3465 } |
|
3466 |
|
3467 /** |
|
3468 * @throws NullPointerException {@inheritDoc} |
|
3469 * @since 1.5 |
|
3470 */ |
|
3471 public boolean isAnnotationPresent( |
|
3472 Class<? extends Annotation> annotationClass) { |
|
3473 if (annotationClass == null) |
|
3474 throw new NullPointerException(); |
|
3475 |
|
3476 return getAnnotation(annotationClass) !!= null; |
|
3477 } |
|
3478 |
|
3479 |
|
3480 private static Annotation[] EMPTY_ANNOTATIONS_ARRAY = new Annotation[0]; |
|
3481 |
|
3482 /** |
|
3483 * @since 1.5 |
|
3484 */ |
|
3485 public Annotation[] getAnnotations() { |
|
3486 initAnnotationsIfNecessary(); |
|
3487 return annotations.values().toArray(EMPTY_ANNOTATIONS_ARRAY); |
|
3488 } |
|
3489 |
|
3490 /** |
|
3491 * @since 1.5 |
|
3492 */ |
|
3493 public Annotation[] getDeclaredAnnotations() { |
|
3494 initAnnotationsIfNecessary(); |
|
3495 return declaredAnnotations.values().toArray(EMPTY_ANNOTATIONS_ARRAY); |
|
3496 } |
|
3497 |
|
3498 // Annotations cache |
|
3499 private transient Map<Class, Annotation> annotations; |
|
3500 private transient Map<Class, Annotation> declaredAnnotations; |
|
3501 |
|
3502 private synchronized void initAnnotationsIfNecessary() { |
|
3503 clearCachesOnClassRedefinition(); |
|
3504 if (annotations !!= null) |
|
3505 return; |
|
3506 declaredAnnotations = AnnotationParser.parseAnnotations( |
|
3507 getRawAnnotations(), getConstantPool(), this); |
|
3508 Class<?> superClass = getSuperclass(); |
|
3509 if (superClass == null) { |
|
3510 annotations = declaredAnnotations; |
|
3511 } else { |
|
3512 annotations = new HashMap<Class, Annotation>(); |
|
3513 superClass.initAnnotationsIfNecessary(); |
|
3514 for (Map.Entry<Class, Annotation> e : superClass.annotations.entrySet()) { |
|
3515 Class annotationClass = e.getKey(); |
|
3516 if (AnnotationType.getInstance(annotationClass).isInherited()) |
|
3517 annotations.put(annotationClass, e.getValue()); |
|
3518 } |
|
3519 annotations.putAll(declaredAnnotations); |
|
3520 } |
|
3521 } |
|
3522 |
|
3523 // Annotation types cache their internal (AnnotationType) form |
|
3524 |
|
3525 private AnnotationType annotationType; |
|
3526 |
|
3527 void setAnnotationType(AnnotationType type) { |
|
3528 annotationType = type; |
|
3529 } |
|
3530 |
|
3531 AnnotationType getAnnotationType() { |
|
3532 return annotationType; |
|
3533 } |
|
3534 }' |
|
3535 ! |
|
3536 |
|
3537 classJavaLangObject |
|
3538 ^ |
|
3539 '/* |
|
3540 * Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved. |
|
3541 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3542 * |
|
3543 * This code is free software; you can redistribute it and/or modify it |
|
3544 * under the terms of the GNU General Public License version 2 only, as |
|
3545 * published by the Free Software Foundation. Oracle designates this |
|
3546 * particular file as subject to the "Classpath" exception as provided |
|
3547 * by Oracle in the LICENSE file that accompanied this code. |
|
3548 * |
|
3549 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
3550 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
3551 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
3552 * version 2 for more details (a copy is included in the LICENSE file that |
|
3553 * accompanied this code). |
|
3554 * |
|
3555 * You should have received a copy of the GNU General Public License version |
|
3556 * 2 along with this work; if not, write to the Free Software Foundation, |
|
3557 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
3558 * |
|
3559 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
3560 * or visit www.oracle.com if you need additional information or have any |
|
3561 * questions. |
|
3562 */ |
|
3563 |
|
3564 package java.lang; |
|
3565 |
|
3566 /** |
|
3567 * Class <code>Object</code> is the root of the class hierarchy. |
|
3568 * Every class has <code>Object</code> as a superclass. All objects, |
|
3569 * including arrays, implement the methods of this class. |
|
3570 * |
|
3571 * @author unascribed |
|
3572 * @see java.lang.Class |
|
3573 * @since JDK1.0 |
|
3574 */ |
|
3575 public class Object { |
|
3576 |
|
3577 private static native void registerNatives(); |
|
3578 static { |
|
3579 registerNatives(); |
|
3580 } |
|
3581 |
|
3582 /** |
|
3583 * Returns the runtime class of this {@code Object}. The returned |
|
3584 * {@code Class} object is the object that is locked by {@code |
|
3585 * static synchronized} methods of the represented class. |
|
3586 * |
|
3587 * <p><b>The actual result type is {@code Class<? extends |X|>} |
|
3588 * where {@code |X|} is the erasure of the static type of the |
|
3589 * expression on which {@code getClass} is called.</b> For |
|
3590 * example, no cast is required in this code fragment:</p> |
|
3591 * |
|
3592 * <p> |
|
3593 * {@code Number n = 0; }<br> |
|
3594 * {@code Class<? extends Number> c = n.getClass(); } |
|
3595 * </p> |
|
3596 * |
|
3597 * @return The {@code Class} object that represents the runtime |
|
3598 * class of this object. |
|
3599 * @see <a href="http://java.sun.com/docs/books/jls/">The Java |
|
3600 * Language Specification, Third Edition (15.8.2 Class |
|
3601 * Literals)</a> |
|
3602 */ |
|
3603 public final native Class<?> getClass(); |
|
3604 |
|
3605 /** |
|
3606 * Returns a hash code value for the object. This method is |
|
3607 * supported for the benefit of hashtables such as those provided by |
|
3608 * <code>java.util.Hashtable</code>. |
|
3609 * <p> |
|
3610 * The general contract of <code>hashCode</code> is: |
|
3611 * <ul> |
|
3612 * <li>Whenever it is invoked on the same object more than once during |
|
3613 * an execution of a Java application, the <tt>hashCode</tt> method |
|
3614 * must consistently return the same integer, provided no information |
|
3615 * used in <tt>equals</tt> comparisons on the object is modified. |
|
3616 * This integer need not remain consistent from one execution of an |
|
3617 * application to another execution of the same application. |
|
3618 * <li>If two objects are equal according to the <tt>equals(Object)</tt> |
|
3619 * method, then calling the <code>hashCode</code> method on each of |
|
3620 * the two objects must produce the same integer result. |
|
3621 * <li>It is <em>not</em> required that if two objects are unequal |
|
3622 * according to the {@link java.lang.Object#equals(java.lang.Object)} |
|
3623 * method, then calling the <tt>hashCode</tt> method on each of the |
|
3624 * two objects must produce distinct integer results. However, the |
|
3625 * programmer should be aware that producing distinct integer results |
|
3626 * for unequal objects may improve the performance of hashtables. |
|
3627 * </ul> |
|
3628 * <p> |
|
3629 * As much as is reasonably practical, the hashCode method defined by |
|
3630 * class <tt>Object</tt> does return distinct integers for distinct |
|
3631 * objects. (This is typically implemented by converting the internal |
|
3632 * address of the object into an integer, but this implementation |
|
3633 * technique is not required by the |
|
3634 * Java<font size="-2"><sup>TM</sup></font> programming language.) |
|
3635 * |
|
3636 * @return a hash code value for this object. |
|
3637 * @see java.lang.Object#equals(java.lang.Object) |
|
3638 * @see java.util.Hashtable |
|
3639 */ |
|
3640 public native int hashCode(); |
|
3641 |
|
3642 /** |
|
3643 * Indicates whether some other object is "equal to" this one. |
|
3644 * <p> |
|
3645 * The <code>equals</code> method implements an equivalence relation |
|
3646 * on non-null object references: |
|
3647 * <ul> |
|
3648 * <li>It is <i>reflexive</i>: for any non-null reference value |
|
3649 * <code>x</code>, <code>x.equals(x)</code> should return |
|
3650 * <code>true</code>. |
|
3651 * <li>It is <i>symmetric</i>: for any non-null reference values |
|
3652 * <code>x</code> and <code>y</code>, <code>x.equals(y)</code> |
|
3653 * should return <code>true</code> if and only if |
|
3654 * <code>y.equals(x)</code> returns <code>true</code>. |
|
3655 * <li>It is <i>transitive</i>: for any non-null reference values |
|
3656 * <code>x</code>, <code>y</code>, and <code>z</code>, if |
|
3657 * <code>x.equals(y)</code> returns <code>true</code> and |
|
3658 * <code>y.equals(z)</code> returns <code>true</code>, then |
|
3659 * <code>x.equals(z)</code> should return <code>true</code>. |
|
3660 * <li>It is <i>consistent</i>: for any non-null reference values |
|
3661 * <code>x</code> and <code>y</code>, multiple invocations of |
|
3662 * <tt>x.equals(y)</tt> consistently return <code>true</code> |
|
3663 * or consistently return <code>false</code>, provided no |
|
3664 * information used in <code>equals</code> comparisons on the |
|
3665 * objects is modified. |
|
3666 * <li>For any non-null reference value <code>x</code>, |
|
3667 * <code>x.equals(null)</code> should return <code>false</code>. |
|
3668 * </ul> |
|
3669 * <p> |
|
3670 * The <tt>equals</tt> method for class <code>Object</code> implements |
|
3671 * the most discriminating possible equivalence relation on objects; |
|
3672 * that is, for any non-null reference values <code>x</code> and |
|
3673 * <code>y</code>, this method returns <code>true</code> if and only |
|
3674 * if <code>x</code> and <code>y</code> refer to the same object |
|
3675 * (<code>x == y</code> has the value <code>true</code>). |
|
3676 * <p> |
|
3677 * Note that it is generally necessary to override the <tt>hashCode</tt> |
|
3678 * method whenever this method is overridden, so as to maintain the |
|
3679 * general contract for the <tt>hashCode</tt> method, which states |
|
3680 * that equal objects must have equal hash codes. |
|
3681 * |
|
3682 * @param obj the reference object with which to compare. |
|
3683 * @return <code>true</code> if this object is the same as the obj |
|
3684 * argument; <code>false</code> otherwise. |
|
3685 * @see #hashCode() |
|
3686 * @see java.util.Hashtable |
|
3687 */ |
|
3688 public boolean equals(Object obj) { |
|
3689 return (this == obj); |
|
3690 } |
|
3691 |
|
3692 /** |
|
3693 * Creates and returns a copy of this object. The precise meaning |
|
3694 * of "copy" may depend on the class of the object. The general |
|
3695 * intent is that, for any object <tt>x</tt>, the expression: |
|
3696 * <blockquote> |
|
3697 * <pre> |
|
3698 * x.clone() !!= x</pre></blockquote> |
|
3699 * will be true, and that the expression: |
|
3700 * <blockquote> |
|
3701 * <pre> |
|
3702 * x.clone().getClass() == x.getClass()</pre></blockquote> |
|
3703 * will be <tt>true</tt>, but these are not absolute requirements. |
|
3704 * While it is typically the case that: |
|
3705 * <blockquote> |
|
3706 * <pre> |
|
3707 * x.clone().equals(x)</pre></blockquote> |
|
3708 * will be <tt>true</tt>, this is not an absolute requirement. |
|
3709 * <p> |
|
3710 * By convention, the returned object should be obtained by calling |
|
3711 * <tt>super.clone</tt>. If a class and all of its superclasses (except |
|
3712 * <tt>Object</tt>) obey this convention, it will be the case that |
|
3713 * <tt>x.clone().getClass() == x.getClass()</tt>. |
|
3714 * <p> |
|
3715 * By convention, the object returned by this method should be independent |
|
3716 * of this object (which is being cloned). To achieve this independence, |
|
3717 * it may be necessary to modify one or more fields of the object returned |
|
3718 * by <tt>super.clone</tt> before returning it. Typically, this means |
|
3719 * copying any mutable objects that comprise the internal "deep structure" |
|
3720 * of the object being cloned and replacing the references to these |
|
3721 * objects with references to the copies. If a class contains only |
|
3722 * primitive fields or references to immutable objects, then it is usually |
|
3723 * the case that no fields in the object returned by <tt>super.clone</tt> |
|
3724 * need to be modified. |
|
3725 * <p> |
|
3726 * The method <tt>clone</tt> for class <tt>Object</tt> performs a |
|
3727 * specific cloning operation. First, if the class of this object does |
|
3728 * not implement the interface <tt>Cloneable</tt>, then a |
|
3729 * <tt>CloneNotSupportedException</tt> is thrown. Note that all arrays |
|
3730 * are considered to implement the interface <tt>Cloneable</tt>. |
|
3731 * Otherwise, this method creates a new instance of the class of this |
|
3732 * object and initializes all its fields with exactly the contents of |
|
3733 * the corresponding fields of this object, as if by assignment; the |
|
3734 * contents of the fields are not themselves cloned. Thus, this method |
|
3735 * performs a "shallow copy" of this object, not a "deep copy" operation. |
|
3736 * <p> |
|
3737 * The class <tt>Object</tt> does not itself implement the interface |
|
3738 * <tt>Cloneable</tt>, so calling the <tt>clone</tt> method on an object |
|
3739 * whose class is <tt>Object</tt> will result in throwing an |
|
3740 * exception at run time. |
|
3741 * |
|
3742 * @return a clone of this instance. |
|
3743 * @exception CloneNotSupportedException if the object''s class does not |
|
3744 * support the <code>Cloneable</code> interface. Subclasses |
|
3745 * that override the <code>clone</code> method can also |
|
3746 * throw this exception to indicate that an instance cannot |
|
3747 * be cloned. |
|
3748 * @see java.lang.Cloneable |
|
3749 */ |
|
3750 protected native Object clone() throws CloneNotSupportedException; |
|
3751 |
|
3752 /** |
|
3753 * Returns a string representation of the object. In general, the |
|
3754 * <code>toString</code> method returns a string that |
|
3755 * "textually represents" this object. The result should |
|
3756 * be a concise but informative representation that is easy for a |
|
3757 * person to read. |
|
3758 * It is recommended that all subclasses override this method. |
|
3759 * <p> |
|
3760 * The <code>toString</code> method for class <code>Object</code> |
|
3761 * returns a string consisting of the name of the class of which the |
|
3762 * object is an instance, the at-sign character `<code>@</code>'', and |
|
3763 * the unsigned hexadecimal representation of the hash code of the |
|
3764 * object. In other words, this method returns a string equal to the |
|
3765 * value of: |
|
3766 * <blockquote> |
|
3767 * <pre> |
|
3768 * getClass().getName() + ''@'' + Integer.toHexString(hashCode()) |
|
3769 * </pre></blockquote> |
|
3770 * |
|
3771 * @return a string representation of the object. |
|
3772 */ |
|
3773 public String toString() { |
|
3774 return getClass().getName() + "@" + Integer.toHexString(hashCode()); |
|
3775 } |
|
3776 |
|
3777 /** |
|
3778 * Wakes up a single thread that is waiting on this object''s |
|
3779 * monitor. If any threads are waiting on this object, one of them |
|
3780 * is chosen to be awakened. The choice is arbitrary and occurs at |
|
3781 * the discretion of the implementation. A thread waits on an object''s |
|
3782 * monitor by calling one of the <code>wait</code> methods. |
|
3783 * <p> |
|
3784 * The awakened thread will not be able to proceed until the current |
|
3785 * thread relinquishes the lock on this object. The awakened thread will |
|
3786 * compete in the usual manner with any other threads that might be |
|
3787 * actively competing to synchronize on this object; for example, the |
|
3788 * awakened thread enjoys no reliable privilege or disadvantage in being |
|
3789 * the next thread to lock this object. |
|
3790 * <p> |
|
3791 * This method should only be called by a thread that is the owner |
|
3792 * of this object''s monitor. A thread becomes the owner of the |
|
3793 * object''s monitor in one of three ways: |
|
3794 * <ul> |
|
3795 * <li>By executing a synchronized instance method of that object. |
|
3796 * <li>By executing the body of a <code>synchronized</code> statement |
|
3797 * that synchronizes on the object. |
|
3798 * <li>For objects of type <code>Class,</code> by executing a |
|
3799 * synchronized static method of that class. |
|
3800 * </ul> |
|
3801 * <p> |
|
3802 * Only one thread at a time can own an object''s monitor. |
|
3803 * |
|
3804 * @exception IllegalMonitorStateException if the current thread is not |
|
3805 * the owner of this object''s monitor. |
|
3806 * @see java.lang.Object#notifyAll() |
|
3807 * @see java.lang.Object#wait() |
|
3808 */ |
|
3809 public final native void notify(); |
|
3810 |
|
3811 /** |
|
3812 * Wakes up all threads that are waiting on this object''s monitor. A |
|
3813 * thread waits on an object''s monitor by calling one of the |
|
3814 * <code>wait</code> methods. |
|
3815 * <p> |
|
3816 * The awakened threads will not be able to proceed until the current |
|
3817 * thread relinquishes the lock on this object. The awakened threads |
|
3818 * will compete in the usual manner with any other threads that might |
|
3819 * be actively competing to synchronize on this object; for example, |
|
3820 * the awakened threads enjoy no reliable privilege or disadvantage in |
|
3821 * being the next thread to lock this object. |
|
3822 * <p> |
|
3823 * This method should only be called by a thread that is the owner |
|
3824 * of this object''s monitor. See the <code>notify</code> method for a |
|
3825 * description of the ways in which a thread can become the owner of |
|
3826 * a monitor. |
|
3827 * |
|
3828 * @exception IllegalMonitorStateException if the current thread is not |
|
3829 * the owner of this object''s monitor. |
|
3830 * @see java.lang.Object#notify() |
|
3831 * @see java.lang.Object#wait() |
|
3832 */ |
|
3833 public final native void notifyAll(); |
|
3834 |
|
3835 /** |
|
3836 * Causes the current thread to wait until either another thread invokes the |
|
3837 * {@link java.lang.Object#notify()} method or the |
|
3838 * {@link java.lang.Object#notifyAll()} method for this object, or a |
|
3839 * specified amount of time has elapsed. |
|
3840 * <p> |
|
3841 * The current thread must own this object''s monitor. |
|
3842 * <p> |
|
3843 * This method causes the current thread (call it <var>T</var>) to |
|
3844 * place itself in the wait set for this object and then to relinquish |
|
3845 * any and all synchronization claims on this object. Thread <var>T</var> |
|
3846 * becomes disabled for thread scheduling purposes and lies dormant |
|
3847 * until one of four things happens: |
|
3848 * <ul> |
|
3849 * <li>Some other thread invokes the <tt>notify</tt> method for this |
|
3850 * object and thread <var>T</var> happens to be arbitrarily chosen as |
|
3851 * the thread to be awakened. |
|
3852 * <li>Some other thread invokes the <tt>notifyAll</tt> method for this |
|
3853 * object. |
|
3854 * <li>Some other thread {@linkplain Thread#interrupt() interrupts} |
|
3855 * thread <var>T</var>. |
|
3856 * <li>The specified amount of real time has elapsed, more or less. If |
|
3857 * <tt>timeout</tt> is zero, however, then real time is not taken into |
|
3858 * consideration and the thread simply waits until notified. |
|
3859 * </ul> |
|
3860 * The thread <var>T</var> is then removed from the wait set for this |
|
3861 * object and re-enabled for thread scheduling. It then competes in the |
|
3862 * usual manner with other threads for the right to synchronize on the |
|
3863 * object; once it has gained control of the object, all its |
|
3864 * synchronization claims on the object are restored to the status quo |
|
3865 * ante - that is, to the situation as of the time that the <tt>wait</tt> |
|
3866 * method was invoked. Thread <var>T</var> then returns from the |
|
3867 * invocation of the <tt>wait</tt> method. Thus, on return from the |
|
3868 * <tt>wait</tt> method, the synchronization state of the object and of |
|
3869 * thread <tt>T</tt> is exactly as it was when the <tt>wait</tt> method |
|
3870 * was invoked. |
|
3871 * <p> |
|
3872 * A thread can also wake up without being notified, interrupted, or |
|
3873 * timing out, a so-called <i>spurious wakeup</i>. While this will rarely |
|
3874 * occur in practice, applications must guard against it by testing for |
|
3875 * the condition that should have caused the thread to be awakened, and |
|
3876 * continuing to wait if the condition is not satisfied. In other words, |
|
3877 * waits should always occur in loops, like this one: |
|
3878 * <pre> |
|
3879 * synchronized (obj) { |
|
3880 * while (<condition does not hold>) |
|
3881 * obj.wait(timeout); |
|
3882 * ... // Perform action appropriate to condition |
|
3883 * } |
|
3884 * </pre> |
|
3885 * (For more information on this topic, see Section 3.2.3 in Doug Lea''s |
|
3886 * "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, |
|
3887 * 2000), or Item 50 in Joshua Bloch''s "Effective Java Programming |
|
3888 * Language Guide" (Addison-Wesley, 2001). |
|
3889 * |
|
3890 * <p>If the current thread is {@linkplain java.lang.Thread#interrupt() |
|
3891 * interrupted} by any thread before or while it is waiting, then an |
|
3892 * <tt>InterruptedException</tt> is thrown. This exception is not |
|
3893 * thrown until the lock status of this object has been restored as |
|
3894 * described above. |
|
3895 * |
|
3896 * <p> |
|
3897 * Note that the <tt>wait</tt> method, as it places the current thread |
|
3898 * into the wait set for this object, unlocks only this object; any |
|
3899 * other objects on which the current thread may be synchronized remain |
|
3900 * locked while the thread waits. |
|
3901 * <p> |
|
3902 * This method should only be called by a thread that is the owner |
|
3903 * of this object''s monitor. See the <code>notify</code> method for a |
|
3904 * description of the ways in which a thread can become the owner of |
|
3905 * a monitor. |
|
3906 * |
|
3907 * @param timeout the maximum time to wait in milliseconds. |
|
3908 * @exception IllegalArgumentException if the value of timeout is |
|
3909 * negative. |
|
3910 * @exception IllegalMonitorStateException if the current thread is not |
|
3911 * the owner of the object''s monitor. |
|
3912 * @exception InterruptedException if any thread interrupted the |
|
3913 * current thread before or while the current thread |
|
3914 * was waiting for a notification. The <i>interrupted |
|
3915 * status</i> of the current thread is cleared when |
|
3916 * this exception is thrown. |
|
3917 * @see java.lang.Object#notify() |
|
3918 * @see java.lang.Object#notifyAll() |
|
3919 */ |
|
3920 public final native void wait(long timeout) throws InterruptedException; |
|
3921 |
|
3922 /** |
|
3923 * Causes the current thread to wait until another thread invokes the |
|
3924 * {@link java.lang.Object#notify()} method or the |
|
3925 * {@link java.lang.Object#notifyAll()} method for this object, or |
|
3926 * some other thread interrupts the current thread, or a certain |
|
3927 * amount of real time has elapsed. |
|
3928 * <p> |
|
3929 * This method is similar to the <code>wait</code> method of one |
|
3930 * argument, but it allows finer control over the amount of time to |
|
3931 * wait for a notification before giving up. The amount of real time, |
|
3932 * measured in nanoseconds, is given by: |
|
3933 * <blockquote> |
|
3934 * <pre> |
|
3935 * 1000000*timeout+nanos</pre></blockquote> |
|
3936 * <p> |
|
3937 * In all other respects, this method does the same thing as the |
|
3938 * method {@link #wait(long)} of one argument. In particular, |
|
3939 * <tt>wait(0, 0)</tt> means the same thing as <tt>wait(0)</tt>. |
|
3940 * <p> |
|
3941 * The current thread must own this object''s monitor. The thread |
|
3942 * releases ownership of this monitor and waits until either of the |
|
3943 * following two conditions has occurred: |
|
3944 * <ul> |
|
3945 * <li>Another thread notifies threads waiting on this object''s monitor |
|
3946 * to wake up either through a call to the <code>notify</code> method |
|
3947 * or the <code>notifyAll</code> method. |
|
3948 * <li>The timeout period, specified by <code>timeout</code> |
|
3949 * milliseconds plus <code>nanos</code> nanoseconds arguments, has |
|
3950 * elapsed. |
|
3951 * </ul> |
|
3952 * <p> |
|
3953 * The thread then waits until it can re-obtain ownership of the |
|
3954 * monitor and resumes execution. |
|
3955 * <p> |
|
3956 * As in the one argument version, interrupts and spurious wakeups are |
|
3957 * possible, and this method should always be used in a loop: |
|
3958 * <pre> |
|
3959 * synchronized (obj) { |
|
3960 * while (<condition does not hold>) |
|
3961 * obj.wait(timeout, nanos); |
|
3962 * ... // Perform action appropriate to condition |
|
3963 * } |
|
3964 * </pre> |
|
3965 * This method should only be called by a thread that is the owner |
|
3966 * of this object''s monitor. See the <code>notify</code> method for a |
|
3967 * description of the ways in which a thread can become the owner of |
|
3968 * a monitor. |
|
3969 * |
|
3970 * @param timeout the maximum time to wait in milliseconds. |
|
3971 * @param nanos additional time, in nanoseconds range |
|
3972 * 0-999999. |
|
3973 * @exception IllegalArgumentException if the value of timeout is |
|
3974 * negative or the value of nanos is |
|
3975 * not in the range 0-999999. |
|
3976 * @exception IllegalMonitorStateException if the current thread is not |
|
3977 * the owner of this object''s monitor. |
|
3978 * @exception InterruptedException if any thread interrupted the |
|
3979 * current thread before or while the current thread |
|
3980 * was waiting for a notification. The <i>interrupted |
|
3981 * status</i> of the current thread is cleared when |
|
3982 * this exception is thrown. |
|
3983 */ |
|
3984 public final void wait(long timeout, int nanos) throws InterruptedException { |
|
3985 if (timeout < 0) { |
|
3986 throw new IllegalArgumentException("timeout value is negative"); |
|
3987 } |
|
3988 |
|
3989 if (nanos < 0 || nanos > 999999) { |
|
3990 throw new IllegalArgumentException( |
|
3991 "nanosecond timeout value out of range"); |
|
3992 } |
|
3993 |
|
3994 if (nanos >= 500000 || (nanos !!= 0 && timeout == 0)) { |
|
3995 timeout++; |
|
3996 } |
|
3997 |
|
3998 wait(timeout); |
|
3999 } |
|
4000 |
|
4001 /** |
|
4002 * Causes the current thread to wait until another thread invokes the |
|
4003 * {@link java.lang.Object#notify()} method or the |
|
4004 * {@link java.lang.Object#notifyAll()} method for this object. |
|
4005 * In other words, this method behaves exactly as if it simply |
|
4006 * performs the call <tt>wait(0)</tt>. |
|
4007 * <p> |
|
4008 * The current thread must own this object''s monitor. The thread |
|
4009 * releases ownership of this monitor and waits until another thread |
|
4010 * notifies threads waiting on this object''s monitor to wake up |
|
4011 * either through a call to the <code>notify</code> method or the |
|
4012 * <code>notifyAll</code> method. The thread then waits until it can |
|
4013 * re-obtain ownership of the monitor and resumes execution. |
|
4014 * <p> |
|
4015 * As in the one argument version, interrupts and spurious wakeups are |
|
4016 * possible, and this method should always be used in a loop: |
|
4017 * <pre> |
|
4018 * synchronized (obj) { |
|
4019 * while (<condition does not hold>) |
|
4020 * obj.wait(); |
|
4021 * ... // Perform action appropriate to condition |
|
4022 * } |
|
4023 * </pre> |
|
4024 * This method should only be called by a thread that is the owner |
|
4025 * of this object''s monitor. See the <code>notify</code> method for a |
|
4026 * description of the ways in which a thread can become the owner of |
|
4027 * a monitor. |
|
4028 * |
|
4029 * @exception IllegalMonitorStateException if the current thread is not |
|
4030 * the owner of the object''s monitor. |
|
4031 * @exception InterruptedException if any thread interrupted the |
|
4032 * current thread before or while the current thread |
|
4033 * was waiting for a notification. The <i>interrupted |
|
4034 * status</i> of the current thread is cleared when |
|
4035 * this exception is thrown. |
|
4036 * @see java.lang.Object#notify() |
|
4037 * @see java.lang.Object#notifyAll() |
|
4038 */ |
|
4039 public final void wait() throws InterruptedException { |
|
4040 wait(0); |
|
4041 } |
|
4042 |
|
4043 /** |
|
4044 * Called by the garbage collector on an object when garbage collection |
|
4045 * determines that there are no more references to the object. |
|
4046 * A subclass overrides the <code>finalize</code> method to dispose of |
|
4047 * system resources or to perform other cleanup. |
|
4048 * <p> |
|
4049 * The general contract of <tt>finalize</tt> is that it is invoked |
|
4050 * if and when the Java<font size="-2"><sup>TM</sup></font> virtual |
|
4051 * machine has determined that there is no longer any |
|
4052 * means by which this object can be accessed by any thread that has |
|
4053 * not yet died, except as a result of an action taken by the |
|
4054 * finalization of some other object or class which is ready to be |
|
4055 * finalized. The <tt>finalize</tt> method may take any action, including |
|
4056 * making this object available again to other threads; the usual purpose |
|
4057 * of <tt>finalize</tt>, however, is to perform cleanup actions before |
|
4058 * the object is irrevocably discarded. For example, the finalize method |
|
4059 * for an object that represents an input/output connection might perform |
|
4060 * explicit I/O transactions to break the connection before the object is |
|
4061 * permanently discarded. |
|
4062 * <p> |
|
4063 * The <tt>finalize</tt> method of class <tt>Object</tt> performs no |
|
4064 * special action; it simply returns normally. Subclasses of |
|
4065 * <tt>Object</tt> may override this definition. |
|
4066 * <p> |
|
4067 * The Java programming language does not guarantee which thread will |
|
4068 * invoke the <tt>finalize</tt> method for any given object. It is |
|
4069 * guaranteed, however, that the thread that invokes finalize will not |
|
4070 * be holding any user-visible synchronization locks when finalize is |
|
4071 * invoked. If an uncaught exception is thrown by the finalize method, |
|
4072 * the exception is ignored and finalization of that object terminates. |
|
4073 * <p> |
|
4074 * After the <tt>finalize</tt> method has been invoked for an object, no |
|
4075 * further action is taken until the Java virtual machine has again |
|
4076 * determined that there is no longer any means by which this object can |
|
4077 * be accessed by any thread that has not yet died, including possible |
|
4078 * actions by other objects or classes which are ready to be finalized, |
|
4079 * at which point the object may be discarded. |
|
4080 * <p> |
|
4081 * The <tt>finalize</tt> method is never invoked more than once by a Java |
|
4082 * virtual machine for any given object. |
|
4083 * <p> |
|
4084 * Any exception thrown by the <code>finalize</code> method causes |
|
4085 * the finalization of this object to be halted, but is otherwise |
|
4086 * ignored. |
|
4087 * |
|
4088 * @throws Throwable the <code>Exception</code> raised by this method |
|
4089 */ |
|
4090 protected void finalize() throws Throwable { } |
|
4091 }' |
|
4092 ! |
|
4093 |
|
4094 file1 |
|
4095 ^ |
|
4096 'package org.test; |
|
4097 |
|
4098 import java.util.*; |
|
4099 |
|
4100 public class Foo |
|
4101 { |
|
4102 // I am only empty class |
|
4103 }' |
|
4104 ! |
|
4105 |
|
4106 file2 |
|
4107 ^ |
|
4108 ' |
|
4109 public class Foo |
|
4110 { |
|
4111 public void methodA() {} |
|
4112 |
|
4113 public Bar methodB(Some argument, and another) {} |
|
4114 }' |
|
4115 ! |
|
4116 |
|
4117 file3 |
|
4118 ^ |
|
4119 'package org.test; |
|
4120 |
|
4121 import java.util.*; |
|
4122 |
|
4123 public class Foo<Bar> extends Zorg |
|
4124 { |
|
4125 // some comment |
|
4126 |
|
4127 public void methodA() { |
|
4128 System.out.println(); |
|
4129 } |
|
4130 |
|
4131 private InnerClass {} |
|
4132 |
|
4133 /** |
|
4134 * |
|
4135 */ |
|
4136 public Bar methodB(Some argument, and another) |
|
4137 { |
|
4138 return new Bar(); |
|
4139 } |
|
4140 }' |
|
4141 ! |
|
4142 |
|
4143 file4 |
|
4144 ^ |
|
4145 'package java.lang; |
|
4146 |
|
4147 import java.lang.reflect.Array; |
|
4148 /** |
|
4149 * Instances of the class {@code Class} represent classes and |
|
4150 * interfaces in a running Java application. |
|
4151 * <p> The following example uses a {@code Class} object to print the |
|
4152 * class name of an object: |
|
4153 * |
|
4154 * <p> <blockquote><pre> |
|
4155 * void printClassName(Object obj) { |
|
4156 * System.out.println("The class of " + obj + |
|
4157 * " is " + obj.getClass().getName()); |
|
4158 * } |
|
4159 * </pre></blockquote> |
|
4160 * |
|
4161 * <p> It is also possible to get the {@code Class} object for a named |
|
4162 * type (or for void) using a class literal |
|
4163 * (JLS Section <A HREF="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#251530">15.8.2</A>). |
|
4164 * For example: |
|
4165 * |
|
4166 * <p> <blockquote> |
|
4167 * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());} |
|
4168 * </blockquote> |
|
4169 * |
|
4170 * @param <T> the type of the class modeled by this {@code Class} |
|
4171 * object. For example, the type of {@code String.class} is {@code |
|
4172 * Class<String>}. Use {@code Class<?>} if the class being modeled is |
|
4173 * unknown. |
|
4174 * |
|
4175 * @author unascribed |
|
4176 * @see java.lang.ClassLoader#defineClass(byte[], int, int) |
|
4177 * @since JDK1.0 |
|
4178 */ |
|
4179 public final |
|
4180 class Class<T> implements java.io.Serializable, |
|
4181 java.lang.reflect.GenericDeclaration, |
|
4182 java.lang.reflect.Type, |
|
4183 java.lang.reflect.AnnotatedElement { |
|
4184 private static final int ANNOTATION= 0x00002000; |
|
4185 private static final int ENUM = 0x00004000; |
|
4186 private static final int SYNTHETIC = 0x00001000; |
|
4187 |
|
4188 private static native void registerNatives(); |
|
4189 static { |
|
4190 registerNatives(); |
|
4191 } |
|
4192 |
|
4193 /* |
|
4194 * Constructor. Only the Java Virtual Machine creates Class |
|
4195 * objects. |
|
4196 */ |
|
4197 private Class() {} |
|
4198 |
|
4199 |
|
4200 /** |
|
4201 * Converts the object to a string. The string representation is the |
|
4202 * string "class" or "interface", followed by a space, and then by the |
|
4203 * fully qualified name of the class in the format returned by |
|
4204 * {@code getName}. If this {@code Class} object represents a |
|
4205 * primitive type, this method returns the name of the primitive type. If |
|
4206 * this {@code Class} object represents void this method returns |
|
4207 * "void". |
|
4208 * |
|
4209 * @return a string representation of this class object. |
|
4210 */ |
|
4211 public String toString() { |
|
4212 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) |
|
4213 + getName(); |
|
4214 } |
|
4215 }' |
|
4216 ! |
|
4217 |
|
4218 file5 |
|
4219 ^ |
|
4220 'package java.lang; |
|
4221 |
|
4222 import java.lang.reflect.Array; |
|
4223 |
|
4224 /** |
|
4225 * This is a class that has a comment!! |
|
4226 * public class Bar { public void bar() { } } } |
|
4227 * |
|
4228 * @author unascribed |
|
4229 * @since JDK1.0 |
|
4230 */ |
|
4231 public final class Foo implements java.io.Serializable { |
|
4232 /* |
|
4233 * Constructor. Only the Java Virtual Machine creates Class |
|
4234 * objects. |
|
4235 */ |
|
4236 private Foo() {} |
|
4237 |
|
4238 |
|
4239 /** |
|
4240 * Converts the object to a string. The string representation is the |
|
4241 * string "class" or "interface", followed by a space, and then by the |
|
4242 * fully qualified name of the class in the format returned by |
|
4243 * {@code getName}. If this {@code Class} object represents a |
|
4244 * primitive type, this method returns the name of the primitive type. If |
|
4245 * this {@code Class} object represents void this method returns |
|
4246 * "void". |
|
4247 * |
|
4248 * @return a string representation of this class object. |
|
4249 */ |
|
4250 public String toString() { |
|
4251 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) |
|
4252 + getName(); |
|
4253 } |
|
4254 }' |
|
4255 ! |
|
4256 |
|
4257 file6 |
|
4258 ^ |
|
4259 '/* |
|
4260 * Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved. |
|
4261 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4262 * |
|
4263 * This code is free software; you can redistribute it and/or modify it |
|
4264 * under the terms of the GNU General Public License version 2 only, as |
|
4265 * published by the Free Software Foundation. Oracle designates this |
|
4266 * particular file as subject to the "Classpath" exception as provided |
|
4267 * by Oracle in the LICENSE file that accompanied this code. |
|
4268 * |
|
4269 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
4270 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
4271 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
4272 * version 2 for more details (a copy is included in the LICENSE file that |
|
4273 * accompanied this code). |
|
4274 * |
|
4275 * You should have received a copy of the GNU General Public License version |
|
4276 * 2 along with this work; if not, write to the Free Software Foundation, |
|
4277 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
4278 * |
|
4279 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
4280 * or visit www.oracle.com if you need additional information or have any |
|
4281 * questions. |
|
4282 */ |
|
4283 |
|
4284 package java.lang; |
|
4285 |
|
4286 import java.lang.reflect.Array; |
|
4287 import java.lang.reflect.GenericArrayType; |
|
4288 import java.lang.reflect.Member; |
|
4289 import java.lang.reflect.Field; |
|
4290 import java.lang.reflect.Method; |
|
4291 import java.lang.reflect.Constructor; |
|
4292 import java.lang.reflect.GenericDeclaration; |
|
4293 import java.lang.reflect.Modifier; |
|
4294 import java.lang.reflect.Type; |
|
4295 import java.lang.reflect.TypeVariable; |
|
4296 import java.lang.reflect.InvocationTargetException; |
|
4297 import java.lang.ref.SoftReference; |
|
4298 import java.io.InputStream; |
|
4299 import java.io.ObjectStreamField; |
|
4300 import java.security.AccessController; |
|
4301 import java.security.PrivilegedAction; |
|
4302 import java.util.ArrayList; |
|
4303 import java.util.Arrays; |
|
4304 import java.util.Collection; |
|
4305 import java.util.HashSet; |
|
4306 import java.util.Iterator; |
|
4307 import java.util.List; |
|
4308 import java.util.LinkedList; |
|
4309 import java.util.LinkedHashSet; |
|
4310 import java.util.Set; |
|
4311 import java.util.Map; |
|
4312 import java.util.HashMap; |
|
4313 import sun.misc.Unsafe; |
|
4314 import sun.reflect.ConstantPool; |
|
4315 import sun.reflect.Reflection; |
|
4316 import sun.reflect.ReflectionFactory; |
|
4317 import sun.reflect.SignatureIterator; |
|
4318 import sun.reflect.generics.factory.CoreReflectionFactory; |
|
4319 import sun.reflect.generics.factory.GenericsFactory; |
|
4320 import sun.reflect.generics.repository.ClassRepository; |
|
4321 import sun.reflect.generics.repository.MethodRepository; |
|
4322 import sun.reflect.generics.repository.ConstructorRepository; |
|
4323 import sun.reflect.generics.scope.ClassScope; |
|
4324 import sun.security.util.SecurityConstants; |
|
4325 import java.lang.annotation.Annotation; |
|
4326 import sun.reflect.annotation.*; |
|
4327 |
|
4328 /** |
|
4329 * Instances of the class {@code Class} represent classes and |
|
4330 * interfaces in a running Java application. An enum is a kind of |
|
4331 * class and an annotation is a kind of interface. Every array also |
|
4332 * belongs to a class that is reflected as a {@code Class} object |
|
4333 * that is shared by all arrays with the same element type and number |
|
4334 * of dimensions. The primitive Java types ({@code boolean}, |
|
4335 * {@code byte}, {@code char}, {@code short}, |
|
4336 * {@code int}, {@code long}, {@code float}, and |
|
4337 * {@code double}), and the keyword {@code void} are also |
|
4338 * represented as {@code Class} objects. |
|
4339 * |
|
4340 * <p> {@code Class} has no public constructor. Instead {@code Class} |
|
4341 * objects are constructed automatically by the Java Virtual Machine as classes |
|
4342 * are loaded and by calls to the {@code defineClass} method in the class |
|
4343 * loader. |
|
4344 * |
|
4345 * <p> The following example uses a {@code Class} object to print the |
|
4346 * class name of an object: |
|
4347 * |
|
4348 * <p> <blockquote><pre> |
|
4349 * void printClassName(Object obj) { |
|
4350 * System.out.println("The class of " + obj + |
|
4351 * " is " + obj.getClass().getName()); |
|
4352 * } |
|
4353 * </pre></blockquote> |
|
4354 * |
|
4355 * <p> It is also possible to get the {@code Class} object for a named |
|
4356 * type (or for void) using a class literal |
|
4357 * (JLS Section <A HREF="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#251530">15.8.2</A>). |
|
4358 * For example: |
|
4359 * |
|
4360 * <p> <blockquote> |
|
4361 * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());} |
|
4362 * </blockquote> |
|
4363 * |
|
4364 * @param <T> the type of the class modeled by this {@code Class} |
|
4365 * object. For example, the type of {@code String.class} is {@code |
|
4366 * Class<String>}. Use {@code Class<?>} if the class being modeled is |
|
4367 * unknown. |
|
4368 * |
|
4369 * @author unascribed |
|
4370 * @see java.lang.ClassLoader#defineClass(byte[], int, int) |
|
4371 * @since JDK1.0 |
|
4372 */ |
|
4373 public final |
|
4374 class Class<T> implements java.io.Serializable, |
|
4375 java.lang.reflect.GenericDeclaration, |
|
4376 java.lang.reflect.Type, |
|
4377 java.lang.reflect.AnnotatedElement { |
|
4378 private static final int ANNOTATION= 0x00002000; |
|
4379 private static final int ENUM = 0x00004000; |
|
4380 private static final int SYNTHETIC = 0x00001000; |
|
4381 |
|
4382 private static native void registerNatives(); |
|
4383 static { |
|
4384 registerNatives(); |
|
4385 } |
|
4386 |
|
4387 /* |
|
4388 * Constructor. Only the Java Virtual Machine creates Class |
|
4389 * objects. |
|
4390 */ |
|
4391 private Class() {} |
|
4392 |
|
4393 |
|
4394 /** |
|
4395 * Converts the object to a string. The string representation is the |
|
4396 * string "class" or "interface", followed by a space, and then by the |
|
4397 * fully qualified name of the class in the format returned by |
|
4398 * {@code getName}. If this {@code Class} object represents a |
|
4399 * primitive type, this method returns the name of the primitive type. If |
|
4400 * this {@code Class} object represents void this method returns |
|
4401 * "void". |
|
4402 * |
|
4403 * @return a string representation of this class object. |
|
4404 */ |
|
4405 public String toString() { |
|
4406 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) |
|
4407 + getName(); |
|
4408 } |
|
4409 |
|
4410 |
|
4411 /** |
|
4412 * Returns the {@code Class} object associated with the class or |
|
4413 * interface with the given string name. Invoking this method is |
|
4414 * equivalent to: |
|
4415 * |
|
4416 * <blockquote> |
|
4417 * {@code Class.forName(className, true, currentLoader)} |
|
4418 * </blockquote> |
|
4419 * |
|
4420 * where {@code currentLoader} denotes the defining class loader of |
|
4421 * the current class. |
|
4422 * |
|
4423 * <p> For example, the following code fragment returns the |
|
4424 * runtime {@code Class} descriptor for the class named |
|
4425 * {@code java.lang.Thread}: |
|
4426 * |
|
4427 * <blockquote> |
|
4428 * {@code Class t = Class.forName("java.lang.Thread")} |
|
4429 * </blockquote> |
|
4430 * <p> |
|
4431 * A call to {@code forName("X")} causes the class named |
|
4432 * {@code X} to be initialized. |
|
4433 * |
|
4434 * @param className the fully qualified name of the desired class. |
|
4435 * @return the {@code Class} object for the class with the |
|
4436 * specified name. |
|
4437 * @exception LinkageError if the linkage fails |
|
4438 * @exception ExceptionInInitializerError if the initialization provoked |
|
4439 * by this method fails |
|
4440 * @exception ClassNotFoundException if the class cannot be located |
|
4441 */ |
|
4442 public static Class<?> forName(String className) |
|
4443 throws ClassNotFoundException { |
|
4444 return forName0(className, true, ClassLoader.getCallerClassLoader()); |
|
4445 } |
|
4446 |
|
4447 |
|
4448 /** |
|
4449 * Returns the {@code Class} object associated with the class or |
|
4450 * interface with the given string name, using the given class loader. |
|
4451 * Given the fully qualified name for a class or interface (in the same |
|
4452 * format returned by {@code getName}) this method attempts to |
|
4453 * locate, load, and link the class or interface. The specified class |
|
4454 * loader is used to load the class or interface. If the parameter |
|
4455 * {@code loader} is null, the class is loaded through the bootstrap |
|
4456 * class loader. The class is initialized only if the |
|
4457 * {@code initialize} parameter is {@code true} and if it has |
|
4458 * not been initialized earlier. |
|
4459 * |
|
4460 * <p> If {@code name} denotes a primitive type or void, an attempt |
|
4461 * will be made to locate a user-defined class in the unnamed package whose |
|
4462 * name is {@code name}. Therefore, this method cannot be used to |
|
4463 * obtain any of the {@code Class} objects representing primitive |
|
4464 * types or void. |
|
4465 * |
|
4466 * <p> If {@code name} denotes an array class, the component type of |
|
4467 * the array class is loaded but not initialized. |
|
4468 * |
|
4469 * <p> For example, in an instance method the expression: |
|
4470 * |
|
4471 * <blockquote> |
|
4472 * {@code Class.forName("Foo")} |
|
4473 * </blockquote> |
|
4474 * |
|
4475 * is equivalent to: |
|
4476 * |
|
4477 * <blockquote> |
|
4478 * {@code Class.forName("Foo", true, this.getClass().getClassLoader())} |
|
4479 * </blockquote> |
|
4480 * |
|
4481 * Note that this method throws errors related to loading, linking or |
|
4482 * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The |
|
4483 * Java Language Specification</em>. |
|
4484 * Note that this method does not check whether the requested class |
|
4485 * is accessible to its caller. |
|
4486 * |
|
4487 * <p> If the {@code loader} is {@code null}, and a security |
|
4488 * manager is present, and the caller''s class loader is not null, then this |
|
4489 * method calls the security manager''s {@code checkPermission} method |
|
4490 * with a {@code RuntimePermission("getClassLoader")} permission to |
|
4491 * ensure it''s ok to access the bootstrap class loader. |
|
4492 * |
|
4493 * @param name fully qualified name of the desired class |
|
4494 * @param initialize whether the class must be initialized |
|
4495 * @param loader class loader from which the class must be loaded |
|
4496 * @return class object representing the desired class |
|
4497 * |
|
4498 * @exception LinkageError if the linkage fails |
|
4499 * @exception ExceptionInInitializerError if the initialization provoked |
|
4500 * by this method fails |
|
4501 * @exception ClassNotFoundException if the class cannot be located by |
|
4502 * the specified class loader |
|
4503 * |
|
4504 * @see java.lang.Class#forName(String) |
|
4505 * @see java.lang.ClassLoader |
|
4506 * @since 1.2 |
|
4507 */ |
|
4508 public static Class<?> forName(String name, boolean initialize, |
|
4509 ClassLoader loader) |
|
4510 throws ClassNotFoundException |
|
4511 { |
|
4512 if (loader == null) { |
|
4513 SecurityManager sm = System.getSecurityManager(); |
|
4514 if (sm !!= null) { |
|
4515 ClassLoader ccl = ClassLoader.getCallerClassLoader(); |
|
4516 if (ccl !!= null) { |
|
4517 sm.checkPermission( |
|
4518 SecurityConstants.GET_CLASSLOADER_PERMISSION); |
|
4519 } |
|
4520 } |
|
4521 } |
|
4522 return forName0(name, initialize, loader); |
|
4523 } |
|
4524 |
|
4525 /** Called after security checks have been made. */ |
|
4526 private static native Class forName0(String name, boolean initialize, |
|
4527 ClassLoader loader) |
|
4528 throws ClassNotFoundException; |
|
4529 |
|
4530 /** |
|
4531 * Creates a new instance of the class represented by this {@code Class} |
|
4532 * object. The class is instantiated as if by a {@code new} |
|
4533 * expression with an empty argument list. The class is initialized if it |
|
4534 * has not already been initialized. |
|
4535 * |
|
4536 * <p>Note that this method propagates any exception thrown by the |
|
4537 * nullary constructor, including a checked exception. Use of |
|
4538 * this method effectively bypasses the compile-time exception |
|
4539 * checking that would otherwise be performed by the compiler. |
|
4540 * The {@link |
|
4541 * java.lang.reflect.Constructor#newInstance(java.lang.Object...) |
|
4542 * Constructor.newInstance} method avoids this problem by wrapping |
|
4543 * any exception thrown by the constructor in a (checked) {@link |
|
4544 * java.lang.reflect.InvocationTargetException}. |
|
4545 * |
|
4546 * @return a newly allocated instance of the class represented by this |
|
4547 * object. |
|
4548 * @exception IllegalAccessException if the class or its nullary |
|
4549 * constructor is not accessible. |
|
4550 * @exception InstantiationException |
|
4551 * if this {@code Class} represents an abstract class, |
|
4552 * an interface, an array class, a primitive type, or void; |
|
4553 * or if the class has no nullary constructor; |
|
4554 * or if the instantiation fails for some other reason. |
|
4555 * @exception ExceptionInInitializerError if the initialization |
|
4556 * provoked by this method fails. |
|
4557 * @exception SecurityException |
|
4558 * If a security manager, <i>s</i>, is present and any of the |
|
4559 * following conditions is met: |
|
4560 * |
|
4561 * <ul> |
|
4562 * |
|
4563 * <li> invocation of |
|
4564 * {@link SecurityManager#checkMemberAccess |
|
4565 * s.checkMemberAccess(this, Member.PUBLIC)} denies |
|
4566 * creation of new instances of this class |
|
4567 * |
|
4568 * <li> the caller''s class loader is not the same as or an |
|
4569 * ancestor of the class loader for the current class and |
|
4570 * invocation of {@link SecurityManager#checkPackageAccess |
|
4571 * s.checkPackageAccess()} denies access to the package |
|
4572 * of this class |
|
4573 * |
|
4574 * </ul> |
|
4575 * |
|
4576 */ |
|
4577 public T newInstance() |
|
4578 throws InstantiationException, IllegalAccessException |
|
4579 { |
|
4580 if (System.getSecurityManager() !!= null) { |
|
4581 checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); |
|
4582 } |
|
4583 return newInstance0(); |
|
4584 } |
|
4585 |
|
4586 private T newInstance0() |
|
4587 throws InstantiationException, IllegalAccessException |
|
4588 { |
|
4589 // NOTE: the following code may not be strictly correct under |
|
4590 // the current Java memory model. |
|
4591 |
|
4592 // Constructor lookup |
|
4593 if (cachedConstructor == null) { |
|
4594 if (this == Class.class) { |
|
4595 throw new IllegalAccessException( |
|
4596 "Can not call newInstance() on the Class for java.lang.Class" |
|
4597 ); |
|
4598 } |
|
4599 try { |
|
4600 Class[] empty = {}; |
|
4601 final Constructor<T> c = getConstructor0(empty, Member.DECLARED); |
|
4602 // Disable accessibility checks on the constructor |
|
4603 // since we have to do the security check here anyway |
|
4604 // (the stack depth is wrong for the Constructor''s |
|
4605 // security check to work) |
|
4606 java.security.AccessController.doPrivileged |
|
4607 (new java.security.PrivilegedAction() { |
|
4608 public Object run() { |
|
4609 c.setAccessible(true); |
|
4610 return null; |
|
4611 } |
|
4612 }); |
|
4613 cachedConstructor = c; |
|
4614 } catch (NoSuchMethodException e) { |
|
4615 throw new InstantiationException(getName()); |
|
4616 } |
|
4617 } |
|
4618 Constructor<T> tmpConstructor = cachedConstructor; |
|
4619 // Security check (same as in java.lang.reflect.Constructor) |
|
4620 int modifiers = tmpConstructor.getModifiers(); |
|
4621 if (!!Reflection.quickCheckMemberAccess(this, modifiers)) { |
|
4622 Class caller = Reflection.getCallerClass(3); |
|
4623 if (newInstanceCallerCache !!= caller) { |
|
4624 Reflection.ensureMemberAccess(caller, this, null, modifiers); |
|
4625 newInstanceCallerCache = caller; |
|
4626 } |
|
4627 } |
|
4628 // Run constructor |
|
4629 try { |
|
4630 return tmpConstructor.newInstance((Object[])null); |
|
4631 } catch (InvocationTargetException e) { |
|
4632 Unsafe.getUnsafe().throwException(e.getTargetException()); |
|
4633 // Not reached |
|
4634 return null; |
|
4635 } |
|
4636 } |
|
4637 private volatile transient Constructor<T> cachedConstructor; |
|
4638 private volatile transient Class newInstanceCallerCache; |
|
4639 |
|
4640 |
|
4641 /** |
|
4642 * Determines if the specified {@code Object} is assignment-compatible |
|
4643 * with the object represented by this {@code Class}. This method is |
|
4644 * the dynamic equivalent of the Java language {@code instanceof} |
|
4645 * operator. The method returns {@code true} if the specified |
|
4646 * {@code Object} argument is non-null and can be cast to the |
|
4647 * reference type represented by this {@code Class} object without |
|
4648 * raising a {@code ClassCastException.} It returns {@code false} |
|
4649 * otherwise. |
|
4650 * |
|
4651 * <p> Specifically, if this {@code Class} object represents a |
|
4652 * declared class, this method returns {@code true} if the specified |
|
4653 * {@code Object} argument is an instance of the represented class (or |
|
4654 * of any of its subclasses); it returns {@code false} otherwise. If |
|
4655 * this {@code Class} object represents an array class, this method |
|
4656 * returns {@code true} if the specified {@code Object} argument |
|
4657 * can be converted to an object of the array class by an identity |
|
4658 * conversion or by a widening reference conversion; it returns |
|
4659 * {@code false} otherwise. If this {@code Class} object |
|
4660 * represents an interface, this method returns {@code true} if the |
|
4661 * class or any superclass of the specified {@code Object} argument |
|
4662 * implements this interface; it returns {@code false} otherwise. If |
|
4663 * this {@code Class} object represents a primitive type, this method |
|
4664 * returns {@code false}. |
|
4665 * |
|
4666 * @param obj the object to check |
|
4667 * @return true if {@code obj} is an instance of this class |
|
4668 * |
|
4669 * @since JDK1.1 |
|
4670 */ |
|
4671 public native boolean isInstance(Object obj); |
|
4672 |
|
4673 |
|
4674 /** |
|
4675 * Determines if the class or interface represented by this |
|
4676 * {@code Class} object is either the same as, or is a superclass or |
|
4677 * superinterface of, the class or interface represented by the specified |
|
4678 * {@code Class} parameter. It returns {@code true} if so; |
|
4679 * otherwise it returns {@code false}. If this {@code Class} |
|
4680 * object represents a primitive type, this method returns |
|
4681 * {@code true} if the specified {@code Class} parameter is |
|
4682 * exactly this {@code Class} object; otherwise it returns |
|
4683 * {@code false}. |
|
4684 * |
|
4685 * <p> Specifically, this method tests whether the type represented by the |
|
4686 * specified {@code Class} parameter can be converted to the type |
|
4687 * represented by this {@code Class} object via an identity conversion |
|
4688 * or via a widening reference conversion. See <em>The Java Language |
|
4689 * Specification</em>, sections 5.1.1 and 5.1.4 , for details. |
|
4690 * |
|
4691 * @param cls the {@code Class} object to be checked |
|
4692 * @return the {@code boolean} value indicating whether objects of the |
|
4693 * type {@code cls} can be assigned to objects of this class |
|
4694 * @exception NullPointerException if the specified Class parameter is |
|
4695 * null. |
|
4696 * @since JDK1.1 |
|
4697 */ |
|
4698 public native boolean isAssignableFrom(Class<?> cls); |
|
4699 |
|
4700 |
|
4701 /** |
|
4702 * Determines if the specified {@code Class} object represents an |
|
4703 * interface type. |
|
4704 * |
|
4705 * @return {@code true} if this object represents an interface; |
|
4706 * {@code false} otherwise. |
|
4707 */ |
|
4708 public native boolean isInterface(); |
|
4709 |
|
4710 |
|
4711 /** |
|
4712 * Determines if this {@code Class} object represents an array class. |
|
4713 * |
|
4714 * @return {@code true} if this object represents an array class; |
|
4715 * {@code false} otherwise. |
|
4716 * @since JDK1.1 |
|
4717 */ |
|
4718 public native boolean isArray(); |
|
4719 |
|
4720 |
|
4721 /** |
|
4722 * Determines if the specified {@code Class} object represents a |
|
4723 * primitive type. |
|
4724 * |
|
4725 * <p> There are nine predefined {@code Class} objects to represent |
|
4726 * the eight primitive types and void. These are created by the Java |
|
4727 * Virtual Machine, and have the same names as the primitive types that |
|
4728 * they represent, namely {@code boolean}, {@code byte}, |
|
4729 * {@code char}, {@code short}, {@code int}, |
|
4730 * {@code long}, {@code float}, and {@code double}. |
|
4731 * |
|
4732 * <p> These objects may only be accessed via the following public static |
|
4733 * final variables, and are the only {@code Class} objects for which |
|
4734 * this method returns {@code true}. |
|
4735 * |
|
4736 * @return true if and only if this class represents a primitive type |
|
4737 * |
|
4738 * @see java.lang.Boolean#TYPE |
|
4739 * @see java.lang.Character#TYPE |
|
4740 * @see java.lang.Byte#TYPE |
|
4741 * @see java.lang.Short#TYPE |
|
4742 * @see java.lang.Integer#TYPE |
|
4743 * @see java.lang.Long#TYPE |
|
4744 * @see java.lang.Float#TYPE |
|
4745 * @see java.lang.Double#TYPE |
|
4746 * @see java.lang.Void#TYPE |
|
4747 * @since JDK1.1 |
|
4748 */ |
|
4749 public native boolean isPrimitive(); |
|
4750 |
|
4751 /** |
|
4752 * Returns true if this {@code Class} object represents an annotation |
|
4753 * type. Note that if this method returns true, {@link #isInterface()} |
|
4754 * would also return true, as all annotation types are also interfaces. |
|
4755 * |
|
4756 * @return {@code true} if this class object represents an annotation |
|
4757 * type; {@code false} otherwise |
|
4758 * @since 1.5 |
|
4759 */ |
|
4760 public boolean isAnnotation() { |
|
4761 return (getModifiers() & ANNOTATION) !!= 0; |
|
4762 } |
|
4763 |
|
4764 /** |
|
4765 * Returns {@code true} if this class is a synthetic class; |
|
4766 * returns {@code false} otherwise. |
|
4767 * @return {@code true} if and only if this class is a synthetic class as |
|
4768 * defined by the Java Language Specification. |
|
4769 * @since 1.5 |
|
4770 */ |
|
4771 public boolean isSynthetic() { |
|
4772 return (getModifiers() & SYNTHETIC) !!= 0; |
|
4773 } |
|
4774 |
|
4775 /** |
|
4776 * Returns the name of the entity (class, interface, array class, |
|
4777 * primitive type, or void) represented by this {@code Class} object, |
|
4778 * as a {@code String}. |
|
4779 * |
|
4780 * <p> If this class object represents a reference type that is not an |
|
4781 * array type then the binary name of the class is returned, as specified |
|
4782 * by the Java Language Specification, Second Edition. |
|
4783 * |
|
4784 * <p> If this class object represents a primitive type or void, then the |
|
4785 * name returned is a {@code String} equal to the Java language |
|
4786 * keyword corresponding to the primitive type or void. |
|
4787 * |
|
4788 * <p> If this class object represents a class of arrays, then the internal |
|
4789 * form of the name consists of the name of the element type preceded by |
|
4790 * one or more ''{@code [}'' characters representing the depth of the array |
|
4791 * nesting. The encoding of element type names is as follows: |
|
4792 * |
|
4793 * <blockquote><table summary="Element types and encodings"> |
|
4794 * <tr><th> Element Type <th> <th> Encoding |
|
4795 * <tr><td> boolean <td> <td align=center> Z |
|
4796 * <tr><td> byte <td> <td align=center> B |
|
4797 * <tr><td> char <td> <td align=center> C |
|
4798 * <tr><td> class or interface |
|
4799 * <td> <td align=center> L<i>classname</i>; |
|
4800 * <tr><td> double <td> <td align=center> D |
|
4801 * <tr><td> float <td> <td align=center> F |
|
4802 * <tr><td> int <td> <td align=center> I |
|
4803 * <tr><td> long <td> <td align=center> J |
|
4804 * <tr><td> short <td> <td align=center> S |
|
4805 * </table></blockquote> |
|
4806 * |
|
4807 * <p> The class or interface name <i>classname</i> is the binary name of |
|
4808 * the class specified above. |
|
4809 * |
|
4810 * <p> Examples: |
|
4811 * <blockquote><pre> |
|
4812 * String.class.getName() |
|
4813 * returns "java.lang.String" |
|
4814 * byte.class.getName() |
|
4815 * returns "byte" |
|
4816 * (new Object[3]).getClass().getName() |
|
4817 * returns "[Ljava.lang.Object;" |
|
4818 * (new int[3][4][5][6][7][8][9]).getClass().getName() |
|
4819 * returns "[[[[[[[I" |
|
4820 * </pre></blockquote> |
|
4821 * |
|
4822 * @return the name of the class or interface |
|
4823 * represented by this object. |
|
4824 */ |
|
4825 public String getName() { |
|
4826 if (name == null) |
|
4827 name = getName0(); |
|
4828 return name; |
|
4829 } |
|
4830 |
|
4831 // cache the name to reduce the number of calls into the VM |
|
4832 private transient String name; |
|
4833 private native String getName0(); |
|
4834 |
|
4835 /** |
|
4836 * Returns the class loader for the class. Some implementations may use |
|
4837 * null to represent the bootstrap class loader. This method will return |
|
4838 * null in such implementations if this class was loaded by the bootstrap |
|
4839 * class loader. |
|
4840 * |
|
4841 * <p> If a security manager is present, and the caller''s class loader is |
|
4842 * not null and the caller''s class loader is not the same as or an ancestor of |
|
4843 * the class loader for the class whose class loader is requested, then |
|
4844 * this method calls the security manager''s {@code checkPermission} |
|
4845 * method with a {@code RuntimePermission("getClassLoader")} |
|
4846 * permission to ensure it''s ok to access the class loader for the class. |
|
4847 * |
|
4848 * <p>If this object |
|
4849 * represents a primitive type or void, null is returned. |
|
4850 * |
|
4851 * @return the class loader that loaded the class or interface |
|
4852 * represented by this object. |
|
4853 * @throws SecurityException |
|
4854 * if a security manager exists and its |
|
4855 * {@code checkPermission} method denies |
|
4856 * access to the class loader for the class. |
|
4857 * @see java.lang.ClassLoader |
|
4858 * @see SecurityManager#checkPermission |
|
4859 * @see java.lang.RuntimePermission |
|
4860 */ |
|
4861 public ClassLoader getClassLoader() { |
|
4862 ClassLoader cl = getClassLoader0(); |
|
4863 if (cl == null) |
|
4864 return null; |
|
4865 SecurityManager sm = System.getSecurityManager(); |
|
4866 if (sm !!= null) { |
|
4867 ClassLoader ccl = ClassLoader.getCallerClassLoader(); |
|
4868 if (ccl !!= null && ccl !!= cl && !!cl.isAncestor(ccl)) { |
|
4869 sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); |
|
4870 } |
|
4871 } |
|
4872 return cl; |
|
4873 } |
|
4874 |
|
4875 // Package-private to allow ClassLoader access |
|
4876 native ClassLoader getClassLoader0(); |
|
4877 |
|
4878 |
|
4879 /** |
|
4880 * Returns an array of {@code TypeVariable} objects that represent the |
|
4881 * type variables declared by the generic declaration represented by this |
|
4882 * {@code GenericDeclaration} object, in declaration order. Returns an |
|
4883 * array of length 0 if the underlying generic declaration declares no type |
|
4884 * variables. |
|
4885 * |
|
4886 * @return an array of {@code TypeVariable} objects that represent |
|
4887 * the type variables declared by this generic declaration |
|
4888 * @throws GenericSignatureFormatError if the generic |
|
4889 * signature of this generic declaration does not conform to |
|
4890 * the format specified in the Java Virtual Machine Specification, |
|
4891 * 3rd edition |
|
4892 * @since 1.5 |
|
4893 */ |
|
4894 public TypeVariable<Class<T>>[] getTypeParameters() { |
|
4895 if (getGenericSignature() !!= null) |
|
4896 return (TypeVariable<Class<T>>[])getGenericInfo().getTypeParameters(); |
|
4897 else |
|
4898 return (TypeVariable<Class<T>>[])new TypeVariable[0]; |
|
4899 } |
|
4900 |
|
4901 |
|
4902 /** |
|
4903 * Returns the {@code Class} representing the superclass of the entity |
|
4904 * (class, interface, primitive type or void) represented by this |
|
4905 * {@code Class}. If this {@code Class} represents either the |
|
4906 * {@code Object} class, an interface, a primitive type, or void, then |
|
4907 * null is returned. If this object represents an array class then the |
|
4908 * {@code Class} object representing the {@code Object} class is |
|
4909 * returned. |
|
4910 * |
|
4911 * @return the superclass of the class represented by this object. |
|
4912 */ |
|
4913 public native Class<? super T> getSuperclass(); |
|
4914 |
|
4915 |
|
4916 /** |
|
4917 * Returns the {@code Type} representing the direct superclass of |
|
4918 * the entity (class, interface, primitive type or void) represented by |
|
4919 * this {@code Class}. |
|
4920 * |
|
4921 * <p>If the superclass is a parameterized type, the {@code Type} |
|
4922 * object returned must accurately reflect the actual type |
|
4923 * parameters used in the source code. The parameterized type |
|
4924 * representing the superclass is created if it had not been |
|
4925 * created before. See the declaration of {@link |
|
4926 * java.lang.reflect.ParameterizedType ParameterizedType} for the |
|
4927 * semantics of the creation process for parameterized types. If |
|
4928 * this {@code Class} represents either the {@code Object} |
|
4929 * class, an interface, a primitive type, or void, then null is |
|
4930 * returned. If this object represents an array class then the |
|
4931 * {@code Class} object representing the {@code Object} class is |
|
4932 * returned. |
|
4933 * |
|
4934 * @throws GenericSignatureFormatError if the generic |
|
4935 * class signature does not conform to the format specified in the |
|
4936 * Java Virtual Machine Specification, 3rd edition |
|
4937 * @throws TypeNotPresentException if the generic superclass |
|
4938 * refers to a non-existent type declaration |
|
4939 * @throws MalformedParameterizedTypeException if the |
|
4940 * generic superclass refers to a parameterized type that cannot be |
|
4941 * instantiated for any reason |
|
4942 * @return the superclass of the class represented by this object |
|
4943 * @since 1.5 |
|
4944 */ |
|
4945 public Type getGenericSuperclass() { |
|
4946 if (getGenericSignature() !!= null) { |
|
4947 // Historical irregularity: |
|
4948 // Generic signature marks interfaces with superclass = Object |
|
4949 // but this API returns null for interfaces |
|
4950 if (isInterface()) |
|
4951 return null; |
|
4952 return getGenericInfo().getSuperclass(); |
|
4953 } else |
|
4954 return getSuperclass(); |
|
4955 } |
|
4956 |
|
4957 /** |
|
4958 * Gets the package for this class. The class loader of this class is used |
|
4959 * to find the package. If the class was loaded by the bootstrap class |
|
4960 * loader the set of packages loaded from CLASSPATH is searched to find the |
|
4961 * package of the class. Null is returned if no package object was created |
|
4962 * by the class loader of this class. |
|
4963 * |
|
4964 * <p> Packages have attributes for versions and specifications only if the |
|
4965 * information was defined in the manifests that accompany the classes, and |
|
4966 * if the class loader created the package instance with the attributes |
|
4967 * from the manifest. |
|
4968 * |
|
4969 * @return the package of the class, or null if no package |
|
4970 * information is available from the archive or codebase. |
|
4971 */ |
|
4972 public Package getPackage() { |
|
4973 return Package.getPackage(this); |
|
4974 } |
|
4975 |
|
4976 |
|
4977 /** |
|
4978 * Determines the interfaces implemented by the class or interface |
|
4979 * represented by this object. |
|
4980 * |
|
4981 * <p> If this object represents a class, the return value is an array |
|
4982 * containing objects representing all interfaces implemented by the |
|
4983 * class. The order of the interface objects in the array corresponds to |
|
4984 * the order of the interface names in the {@code implements} clause |
|
4985 * of the declaration of the class represented by this object. For |
|
4986 * example, given the declaration: |
|
4987 * <blockquote> |
|
4988 * {@code class Shimmer implements FloorWax, DessertTopping { ... }} |
|
4989 * </blockquote> |
|
4990 * suppose the value of {@code s} is an instance of |
|
4991 * {@code Shimmer}; the value of the expression: |
|
4992 * <blockquote> |
|
4993 * {@code s.getClass().getInterfaces()[0]} |
|
4994 * </blockquote> |
|
4995 * is the {@code Class} object that represents interface |
|
4996 * {@code FloorWax}; and the value of: |
|
4997 * <blockquote> |
|
4998 * {@code s.getClass().getInterfaces()[1]} |
|
4999 * </blockquote> |
|
5000 * is the {@code Class} object that represents interface |
|
5001 * {@code DessertTopping}. |
|
5002 * |
|
5003 * <p> If this object represents an interface, the array contains objects |
|
5004 * representing all interfaces extended by the interface. The order of the |
|
5005 * interface objects in the array corresponds to the order of the interface |
|
5006 * names in the {@code extends} clause of the declaration of the |
|
5007 * interface represented by this object. |
|
5008 * |
|
5009 * <p> If this object represents a class or interface that implements no |
|
5010 * interfaces, the method returns an array of length 0. |
|
5011 * |
|
5012 * <p> If this object represents a primitive type or void, the method |
|
5013 * returns an array of length 0. |
|
5014 * |
|
5015 * @return an array of interfaces implemented by this class. |
|
5016 */ |
|
5017 public native Class<?>[] getInterfaces(); |
|
5018 |
|
5019 /** |
|
5020 * Returns the {@code Type}s representing the interfaces |
|
5021 * directly implemented by the class or interface represented by |
|
5022 * this object. |
|
5023 * |
|
5024 * <p>If a superinterface is a parameterized type, the |
|
5025 * {@code Type} object returned for it must accurately reflect |
|
5026 * the actual type parameters used in the source code. The |
|
5027 * parameterized type representing each superinterface is created |
|
5028 * if it had not been created before. See the declaration of |
|
5029 * {@link java.lang.reflect.ParameterizedType ParameterizedType} |
|
5030 * for the semantics of the creation process for parameterized |
|
5031 * types. |
|
5032 * |
|
5033 * <p> If this object represents a class, the return value is an |
|
5034 * array containing objects representing all interfaces |
|
5035 * implemented by the class. The order of the interface objects in |
|
5036 * the array corresponds to the order of the interface names in |
|
5037 * the {@code implements} clause of the declaration of the class |
|
5038 * represented by this object. In the case of an array class, the |
|
5039 * interfaces {@code Cloneable} and {@code Serializable} are |
|
5040 * returned in that order. |
|
5041 * |
|
5042 * <p>If this object represents an interface, the array contains |
|
5043 * objects representing all interfaces directly extended by the |
|
5044 * interface. The order of the interface objects in the array |
|
5045 * corresponds to the order of the interface names in the |
|
5046 * {@code extends} clause of the declaration of the interface |
|
5047 * represented by this object. |
|
5048 * |
|
5049 * <p>If this object represents a class or interface that |
|
5050 * implements no interfaces, the method returns an array of length |
|
5051 * 0. |
|
5052 * |
|
5053 * <p>If this object represents a primitive type or void, the |
|
5054 * method returns an array of length 0. |
|
5055 * |
|
5056 * @throws GenericSignatureFormatError |
|
5057 * if the generic class signature does not conform to the format |
|
5058 * specified in the Java Virtual Machine Specification, 3rd edition |
|
5059 * @throws TypeNotPresentException if any of the generic |
|
5060 * superinterfaces refers to a non-existent type declaration |
|
5061 * @throws MalformedParameterizedTypeException if any of the |
|
5062 * generic superinterfaces refer to a parameterized type that cannot |
|
5063 * be instantiated for any reason |
|
5064 * @return an array of interfaces implemented by this class |
|
5065 * @since 1.5 |
|
5066 */ |
|
5067 public Type[] getGenericInterfaces() { |
|
5068 if (getGenericSignature() !!= null) |
|
5069 return getGenericInfo().getSuperInterfaces(); |
|
5070 else |
|
5071 return getInterfaces(); |
|
5072 } |
|
5073 |
|
5074 /** |
|
5075 * Returns the {@code Class} representing the component type of an |
|
5076 * array. If this class does not represent an array class this method |
|
5077 * returns null. |
|
5078 * |
|
5079 * @return the {@code Class} representing the component type of this |
|
5080 * class if this class is an array |
|
5081 * @see java.lang.reflect.Array |
|
5082 * @since JDK1.1 |
|
5083 */ |
|
5084 public native Class<?> getComponentType(); |
|
5085 |
|
5086 |
|
5087 /** |
|
5088 * Returns the Java language modifiers for this class or interface, encoded |
|
5089 * in an integer. The modifiers consist of the Java Virtual Machine''s |
|
5090 * constants for {@code public}, {@code protected}, |
|
5091 * {@code private}, {@code final}, {@code static}, |
|
5092 * {@code abstract} and {@code interface}; they should be decoded |
|
5093 * using the methods of class {@code Modifier}. |
|
5094 * |
|
5095 * <p> If the underlying class is an array class, then its |
|
5096 * {@code public}, {@code private} and {@code protected} |
|
5097 * modifiers are the same as those of its component type. If this |
|
5098 * {@code Class} represents a primitive type or void, its |
|
5099 * {@code public} modifier is always {@code true}, and its |
|
5100 * {@code protected} and {@code private} modifiers are always |
|
5101 * {@code false}. If this object represents an array class, a |
|
5102 * primitive type or void, then its {@code final} modifier is always |
|
5103 * {@code true} and its interface modifier is always |
|
5104 * {@code false}. The values of its other modifiers are not determined |
|
5105 * by this specification. |
|
5106 * |
|
5107 * <p> The modifier encodings are defined in <em>The Java Virtual Machine |
|
5108 * Specification</em>, table 4.1. |
|
5109 * |
|
5110 * @return the {@code int} representing the modifiers for this class |
|
5111 * @see java.lang.reflect.Modifier |
|
5112 * @since JDK1.1 |
|
5113 */ |
|
5114 public native int getModifiers(); |
|
5115 |
|
5116 |
|
5117 /** |
|
5118 * Gets the signers of this class. |
|
5119 * |
|
5120 * @return the signers of this class, or null if there are no signers. In |
|
5121 * particular, this method returns null if this object represents |
|
5122 * a primitive type or void. |
|
5123 * @since JDK1.1 |
|
5124 */ |
|
5125 public native Object[] getSigners(); |
|
5126 |
|
5127 |
|
5128 /** |
|
5129 * Set the signers of this class. |
|
5130 */ |
|
5131 native void setSigners(Object[] signers); |
|
5132 |
|
5133 |
|
5134 /** |
|
5135 * If this {@code Class} object represents a local or anonymous |
|
5136 * class within a method, returns a {@link |
|
5137 * java.lang.reflect.Method Method} object representing the |
|
5138 * immediately enclosing method of the underlying class. Returns |
|
5139 * {@code null} otherwise. |
|
5140 * |
|
5141 * In particular, this method returns {@code null} if the underlying |
|
5142 * class is a local or anonymous class immediately enclosed by a type |
|
5143 * declaration, instance initializer or static initializer. |
|
5144 * |
|
5145 * @return the immediately enclosing method of the underlying class, if |
|
5146 * that class is a local or anonymous class; otherwise {@code null}. |
|
5147 * @since 1.5 |
|
5148 */ |
|
5149 public Method getEnclosingMethod() { |
|
5150 EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo(); |
|
5151 |
|
5152 if (enclosingInfo == null) |
|
5153 return null; |
|
5154 else { |
|
5155 if (!!enclosingInfo.isMethod()) |
|
5156 return null; |
|
5157 |
|
5158 MethodRepository typeInfo = MethodRepository.make(enclosingInfo.getDescriptor(), |
|
5159 getFactory()); |
|
5160 Class returnType = toClass(typeInfo.getReturnType()); |
|
5161 Type [] parameterTypes = typeInfo.getParameterTypes(); |
|
5162 Class<?>[] parameterClasses = new Class<?>[parameterTypes.length]; |
|
5163 |
|
5164 // Convert Types to Classes; returned types *should* |
|
5165 // be class objects since the methodDescriptor''s used |
|
5166 // don''t have generics information |
|
5167 for(int i = 0; i < parameterClasses.length; i++) |
|
5168 parameterClasses[i] = toClass(parameterTypes[i]); |
|
5169 |
|
5170 /* |
|
5171 * Loop over all declared methods; match method name, |
|
5172 * number of and type of parameters, *and* return |
|
5173 * type. Matching return type is also necessary |
|
5174 * because of covariant returns, etc. |
|
5175 */ |
|
5176 for(Method m: enclosingInfo.getEnclosingClass().getDeclaredMethods()) { |
|
5177 if (m.getName().equals(enclosingInfo.getName()) ) { |
|
5178 Class<?>[] candidateParamClasses = m.getParameterTypes(); |
|
5179 if (candidateParamClasses.length == parameterClasses.length) { |
|
5180 boolean matches = true; |
|
5181 for(int i = 0; i < candidateParamClasses.length; i++) { |
|
5182 if (!!candidateParamClasses[i].equals(parameterClasses[i])) { |
|
5183 matches = false; |
|
5184 break; |
|
5185 } |
|
5186 } |
|
5187 |
|
5188 if (matches) { // finally, check return type |
|
5189 if (m.getReturnType().equals(returnType) ) |
|
5190 return m; |
|
5191 } |
|
5192 } |
|
5193 } |
|
5194 } |
|
5195 |
|
5196 throw new InternalError("Enclosing method not found"); |
|
5197 } |
|
5198 } |
|
5199 |
|
5200 |
|
5201 private AnnotationType annotationType; |
|
5202 |
|
5203 void setAnnotationType(AnnotationType type) { |
|
5204 annotationType = type; |
|
5205 } |
|
5206 |
|
5207 AnnotationType getAnnotationType() { |
|
5208 return annotationType; |
|
5209 } |
|
5210 }' |
|
5211 ! |
|
5212 |
|
5213 file7 |
|
5214 ^ |
|
5215 ' |
|
5216 |
|
5217 package java.lang; |
|
5218 |
|
5219 import sun.reflect.annotation.*; |
|
5220 |
|
5221 /** |
|
5222 * @author unascribed |
|
5223 * @see java.lang.ClassLoader#defineClass(byte[], int, int) |
|
5224 * @since JDK1.0 |
|
5225 */ |
|
5226 public final |
|
5227 class Class<T> implements java.io.Serializable, |
|
5228 java.lang.reflect.GenericDeclaration, |
|
5229 java.lang.reflect.Type, |
|
5230 java.lang.reflect.AnnotatedElement { |
|
5231 |
|
5232 private static native void registerNatives(); |
|
5233 |
|
5234 static { |
|
5235 registerNatives(); |
|
5236 } |
|
5237 |
|
5238 /* |
|
5239 * Constructor. Only the Java Virtual Machine creates Class |
|
5240 * objects. |
|
5241 */ |
|
5242 private Class() { } |
|
5243 |
|
5244 |
|
5245 public String toString() { |
|
5246 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) |
|
5247 + getName(); |
|
5248 } |
|
5249 |
|
5250 |
|
5251 |
|
5252 |
|
5253 AnnotationType getAnnotationType() { |
|
5254 return annotationType; |
|
5255 } |
|
5256 }' |
|
5257 ! ! |
|
5258 |
|
5259 !JavaParserTest methodsFor:'tests'! |
|
5260 |
|
5261 test1 |
|
5262 self parse: self file1. |
|
5263 self assert: result first = 'Foo'. |
|
5264 self assert: result second size = 0. |
|
5265 ! |
|
5266 |
|
5267 test2 |
|
5268 self parse: self file2. |
|
5269 |
|
5270 self assert: result first = 'Foo'. |
|
5271 self assert: result second size = 2. |
|
5272 |
|
5273 self assert: result second first = 'methodA'. |
|
5274 self assert: result second second = 'methodB'. |
|
5275 ! |
|
5276 |
|
5277 test3 |
|
5278 self parse: self file3. |
|
5279 |
|
5280 self assert: result first = 'Foo'. |
|
5281 self assert: result second size = 2. |
|
5282 |
|
5283 self assert: result second first = 'methodA'. |
|
5284 self assert: result second second = 'methodB'. |
|
5285 ! |
|
5286 |
|
5287 test4 |
|
5288 self parse: self file4. |
|
5289 |
|
5290 self assert: result first = 'Class'. |
|
5291 self assert: result second size > 2. |
|
5292 |
|
5293 self assert: (result second anySatisfy: [ :e | e = 'Class' ]). |
|
5294 self assert: (result second anySatisfy: [ :e | e = 'toString' ]). |
|
5295 ! |
|
5296 |
|
5297 test5 |
|
5298 self parse: self file5. |
|
5299 |
|
5300 self assert: result first = 'Foo'. |
|
5301 self assert: result second size = 2. |
|
5302 |
|
5303 self assert: result second first = 'Foo'. |
|
5304 self assert: result second second = 'toString'. |
|
5305 ! |
|
5306 |
|
5307 test6 |
|
5308 self parse: self file6. |
|
5309 |
|
5310 self assert: result first = 'Class'. |
|
5311 self assert: result second size > 2. |
|
5312 |
|
5313 self assert: (result second anySatisfy: [ :e | e = 'registerNatives' ]). |
|
5314 self assert: (result second anySatisfy: [ :e | e = 'Class' ]). |
|
5315 self assert: (result second anySatisfy: [ :e | e = 'toString' ]). |
|
5316 self assert: (result second anySatisfy: [ :e | e = 'getGenericInterfaces' ]). |
|
5317 self assert: (result second anySatisfy: [ :e | e = 'getAnnotationType' ]) |
|
5318 ! |
|
5319 |
|
5320 test7 |
|
5321 self parse: self file7. |
|
5322 |
|
5323 self assert: result first = 'Class'. |
|
5324 self assert: result second size > 2. |
|
5325 |
|
5326 self assert: (result second anySatisfy: [ :e | e = 'Class' ]). |
|
5327 self assert: (result second anySatisfy: [ :e | e = 'toString' ]). |
|
5328 self assert: (result second anySatisfy: [ :e | e = 'getAnnotationType' ]) |
|
5329 ! |
|
5330 |
|
5331 testBlock |
|
5332 self parse: '{}' rule: #block. |
|
5333 |
|
5334 self assert: result isPetitFailure not. |
|
5335 ! |
|
5336 |
|
5337 testBlock2 |
|
5338 self parse: '{ }' rule: #block. |
|
5339 |
|
5340 self assert: result isPetitFailure not. |
|
5341 ! |
|
5342 |
|
5343 testBlock3 |
|
5344 self parse: '{ {} }' rule: #block. |
|
5345 |
|
5346 self assert: result isPetitFailure not. |
|
5347 ! |
|
5348 |
|
5349 testClass1 |
|
5350 self parse: 'private final class Foo |
|
5351 { |
|
5352 // I am only empty class |
|
5353 }' |
|
5354 rule: #javaClass. |
|
5355 |
|
5356 self assert: result first = 'Foo'. |
|
5357 ! |
|
5358 |
|
5359 testClass2 |
|
5360 self parse: 'public class Foo<Bar> extends Zorg implements Qwark, Bark |
|
5361 { |
|
5362 // I am only empty class |
|
5363 }' |
|
5364 rule: #javaClass. |
|
5365 |
|
5366 self assert: result first = 'Foo'. |
|
5367 ! |
|
5368 |
|
5369 testClass3 |
|
5370 self parse: 'private class Bar<T> extends Zorg implements Qwark, Bark |
|
5371 { |
|
5372 public static void methodA() { /* is empty */ } |
|
5373 }' |
|
5374 rule: #javaClass. |
|
5375 |
|
5376 self assert: result first = 'Bar'. |
|
5377 self assert: result second first = 'methodA'. |
|
5378 ! |
|
5379 |
|
5380 testClassBody1 |
|
5381 self parse: '{ |
|
5382 // some comment |
|
5383 public void methodA() |
|
5384 { |
|
5385 } |
|
5386 }' |
|
5387 rule: #classBody. |
|
5388 |
|
5389 self assert: result first = 'methodA'. |
|
5390 ! |
|
5391 |
|
5392 testClassBody10 |
|
5393 self parse: '{ |
|
5394 private static final int SKIP_BUFFER_SIZE = 2048; |
|
5395 public int read(byte b[]) throws IOException { |
|
5396 return read(b, 0, b.length); |
|
5397 } |
|
5398 }' |
|
5399 rule: #classBody. |
|
5400 |
|
5401 self assert: result isPetitFailure not. |
|
5402 self assert: result size = 1. |
|
5403 self assert: result first = 'read'. |
|
5404 ! |
|
5405 |
|
5406 testClassBody2 |
|
5407 self parse: '{ |
|
5408 // some comment |
|
5409 public void methodA() { |
|
5410 } |
|
5411 |
|
5412 private Another methodB( ) |
|
5413 { |
|
5414 return new Another(); |
|
5415 } |
|
5416 }' |
|
5417 rule: #classBody. |
|
5418 |
|
5419 self assert: result first = 'methodA'. |
|
5420 self assert: result second = 'methodB'. |
|
5421 ! |
|
5422 |
|
5423 testClassBody3 |
|
5424 self parse: '{ |
|
5425 // some comment |
|
5426 |
|
5427 public void methodA() { |
|
5428 System.out.println(); |
|
5429 } |
|
5430 |
|
5431 /** |
|
5432 * |
|
5433 */ |
|
5434 public Bar methodB(Some argument, and another) |
|
5435 { |
|
5436 return new Bar(); |
|
5437 } |
|
5438 }' |
|
5439 rule: #classBody. |
|
5440 |
|
5441 self assert: result first = 'methodA'. |
|
5442 self assert: result second = 'methodB'. |
|
5443 ! |
|
5444 |
|
5445 testClassBody4 |
|
5446 self parse: '{ |
|
5447 public void methodA() { |
|
5448 System.out.println(); |
|
5449 } |
|
5450 private InnerClass { } |
|
5451 |
|
5452 public Bar methodB(Some argument, and another) |
|
5453 { |
|
5454 return new Bar(); |
|
5455 } |
|
5456 }' |
|
5457 rule: #classBody. |
|
5458 |
|
5459 self assert: result first = 'methodA'. |
|
5460 self assert: result second = 'methodB'. |
|
5461 ! |
|
5462 |
|
5463 testClassBody5 |
|
5464 self parse: '{ |
|
5465 // I am only empty class |
|
5466 }' |
|
5467 rule: #classBody. |
|
5468 |
|
5469 self assert: result size = 0. |
|
5470 ! |
|
5471 |
|
5472 testClassBody6 |
|
5473 self parse: '{ |
|
5474 static { int i; } |
|
5475 }' |
|
5476 rule: #classBody. |
|
5477 |
|
5478 |
|
5479 self assert: result size = 0. |
|
5480 ! |
|
5481 |
|
5482 testClassBody7 |
|
5483 self parse: '{ |
|
5484 static { int i; } |
|
5485 |
|
5486 public String[] getStrings() { } |
|
5487 }' |
|
5488 rule: #classBody. |
|
5489 |
|
5490 |
|
5491 self assert: result size = 1. |
|
5492 ! |
|
5493 |
|
5494 testClassBody8 |
|
5495 self parse: '{ |
|
5496 private static final int ANNOTATION= 0x00002000; |
|
5497 static { int i; } |
|
5498 |
|
5499 public String[] getStrings() { } |
|
5500 }' |
|
5501 rule: #classBody. |
|
5502 |
|
5503 |
|
5504 self assert: result size = 1. |
|
5505 self assert: result first = 'getStrings'. |
|
5506 ! |
|
5507 |
|
5508 testClassBody9 |
|
5509 self parse: '{ |
|
5510 private static final int ANNOTATION= 0x00002000; |
|
5511 |
|
5512 private static native void registerNatives(); |
|
5513 static { |
|
5514 registerNatives(); |
|
5515 } |
|
5516 |
|
5517 public String toString() { |
|
5518 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) |
|
5519 + getName(); |
|
5520 } |
|
5521 }' |
|
5522 rule: #classBody. |
|
5523 |
|
5524 "debugResult inspect." |
|
5525 self assert: result isPetitFailure not. |
|
5526 self assert: result first = 'registerNatives'. |
|
5527 self assert: result second = 'toString'. |
|
5528 ! |
|
5529 |
|
5530 testClassDef1 |
|
5531 self parse: 'public class Foo' rule: #classDef |
|
5532 ! |
|
5533 |
|
5534 testComment |
|
5535 | input res | |
|
5536 input := '/* hello there */'. |
|
5537 res := self parserInstance comment end parse: input. |
|
5538 self assert: res isPetitFailure not. |
|
5539 ! |
|
5540 |
|
5541 testComment02 |
|
5542 | input res | |
|
5543 input := '/** |
|
5544 * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a |
|
5545 * @since JDK1.1 |
|
5546 */'. |
|
5547 res := self parserInstance comment end parse: input. |
|
5548 self assert: res isPetitFailure not. |
|
5549 ! |
|
5550 |
|
5551 testJavaIoInputStream |
|
5552 self parse: self classJavaIoInputStream. |
|
5553 |
|
5554 self assert: result first = 'InputStream'. |
|
5555 |
|
5556 self assert: result second size = 9. |
|
5557 |
|
5558 |
|
5559 self assert: (result second anySatisfy: [ :e | e = 'reset' ]). |
|
5560 self assert: (result second anySatisfy: [ :e | e = 'read' ]). |
|
5561 self assert: (result second anySatisfy: [ :e | e = 'available' ]). |
|
5562 self assert: (result second anySatisfy: [ :e | e = 'markSupported' ]). |
|
5563 ! |
|
5564 |
|
5565 testJavaLangClass |
|
5566 self parse: self classJavaLangClass. |
|
5567 |
|
5568 self assert: result first = 'Class'. |
|
5569 |
|
5570 self assert: result second size > 2. |
|
5571 |
|
5572 self assert: (result second anySatisfy: [ :e | e = 'Class' ]). |
|
5573 self assert: (result second anySatisfy: [ :e | e = 'toString' ]). |
|
5574 self assert: (result second anySatisfy: [ :e | e = 'getGenericInterfaces' ]). |
|
5575 self assert: (result second anySatisfy: [ :e | e = 'getAnnotationType' ]) |
|
5576 ! |
|
5577 |
|
5578 testJavaLangObject |
|
5579 self parse: self classJavaLangObject. |
|
5580 |
|
5581 self assert: result first = 'Object'. |
|
5582 |
|
5583 self assert: result second size > 2. |
|
5584 |
|
5585 |
|
5586 self assert: (result second anySatisfy: [ :e | e = 'equals' ]). |
|
5587 self assert: (result second anySatisfy: [ :e | e = 'toString' ]). |
|
5588 self assert: (result second anySatisfy: [ :e | e = 'wait' ]). |
|
5589 self assert: (result second anySatisfy: [ :e | e = 'finalize' ]). |
|
5590 ! |
|
5591 |
|
5592 testMethod1 |
|
5593 self parse: 'void foo() { }' rule: #methodDef. |
|
5594 |
|
5595 self assert: result = 'foo'. |
|
5596 ! |
|
5597 |
|
5598 testMethod2 |
|
5599 self parse: 'Foo m() { /** method body */ }' rule: #methodDef. |
|
5600 |
|
5601 self assert: result = 'm'. |
|
5602 ! |
|
5603 |
|
5604 testMethod3 |
|
5605 self parse: 'Bar methodB(Some argument, and another) |
|
5606 { |
|
5607 return new Bar(); |
|
5608 }' |
|
5609 rule: #methodDef. |
|
5610 |
|
5611 self assert: result = 'methodB'. |
|
5612 ! |
|
5613 |
|
5614 testMethod4 |
|
5615 self parse: 'void finalize() throws Throwable { }' |
|
5616 rule: #methodDef. |
|
5617 |
|
5618 self assert: result = 'finalize'. |
|
5619 ! |
|
5620 |
|
5621 testMethod5 |
|
5622 self parse: 'public void methodA() |
|
5623 { |
|
5624 }' |
|
5625 rule: #methodDef. |
|
5626 |
|
5627 self assert: result = 'methodA'. |
|
5628 ! |
|
5629 |
|
5630 testMethod6 |
|
5631 self fail: 'private InnerClass { } |
|
5632 |
|
5633 public void methodA() |
|
5634 { |
|
5635 }' |
|
5636 rule: #methodDef. |
|
5637 |
|
5638 ! |
|
5639 |
|
5640 testMethod7 |
|
5641 self parse: 'public int read(byte b) throws IOException { |
|
5642 return b; |
|
5643 }' |
|
5644 rule: #methodDef. |
|
5645 |
|
5646 self assert: result isPetitFailure not. |
|
5647 self assert: result = 'read'. |
|
5648 ! |
|
5649 |
|
5650 testSingleLineComment |
|
5651 | input res | |
|
5652 input := '// hello there |
|
5653 '. |
|
5654 res := self parserInstance singleLineComment end parse: input. |
|
5655 self assert: res isPetitFailure not. |
|
5656 ! ! |
|
5657 |
|
5658 !JavaParserTest class methodsFor:'documentation'! |
|
5659 |
|
5660 version_HG |
|
5661 |
|
5662 ^ '$Changeset: <not expanded> $' |
|
5663 ! ! |
|
5664 |