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