37 |
|
|
38 |
package java.lang; |
package java.lang; |
39 |
|
|
40 |
|
import java.lang.annotation.Annotation; |
41 |
import java.lang.reflect.Constructor; |
import java.lang.reflect.Constructor; |
42 |
import java.lang.reflect.Field; |
import java.lang.reflect.Field; |
43 |
|
import java.lang.reflect.InvocationTargetException; |
44 |
import java.lang.reflect.Method; |
import java.lang.reflect.Method; |
45 |
|
import java.lang.reflect.Type; |
46 |
|
import java.lang.reflect.TypeVariable; |
47 |
|
|
48 |
/* |
/* |
49 |
* This class is a reference version, mainly for compiling a class library |
* This class is a reference version, mainly for compiling a class library |
156 |
* @param klass the Class object that's calling us |
* @param klass the Class object that's calling us |
157 |
* @return the direct superclass of this class |
* @return the direct superclass of this class |
158 |
*/ |
*/ |
159 |
static native Class getSuperclass(Class klass); |
static native <T> Class<? super T> getSuperclass(Class<T> klass); |
160 |
|
|
161 |
/** |
/** |
162 |
* Get the interfaces this class <EM>directly</EM> implements, in the |
* Get the interfaces this class <EM>directly</EM> implements, in the |
329 |
static native boolean isEnum(Class klass); |
static native boolean isEnum(Class klass); |
330 |
|
|
331 |
/** |
/** |
332 |
* Returns the simple name for this class, as used in the source |
* Returns the simple name for the specified class, as used in the source |
333 |
* code. For normal classes, this is the content returned by |
* code. For normal classes, this is the content returned by |
334 |
* <code>getName()</code> which follows the last ".". Anonymous |
* <code>getName()</code> which follows the last ".". Anonymous |
335 |
* classes have no name, and so the result of calling this method is |
* classes have no name, and so the result of calling this method is |
338 |
* component type of an anonymous class has a simple name of simply |
* component type of an anonymous class has a simple name of simply |
339 |
* "[]". |
* "[]". |
340 |
* |
* |
341 |
|
* @param klass the class whose simple name should be returned. |
342 |
* @return the simple name for this class. |
* @return the simple name for this class. |
343 |
*/ |
*/ |
344 |
static String getSimpleName(Class klass) |
static String getSimpleName(Class<?> klass) |
345 |
{ |
{ |
346 |
if (klass.isArray()) |
if (isArray(klass)) |
347 |
{ |
{ |
348 |
return klass.getComponentType().getSimpleName() + "[]"; |
return getComponentType(klass).getSimpleName() + "[]"; |
349 |
} |
} |
350 |
String fullName = klass.getName(); |
String fullName = getName(klass); |
351 |
return fullName.substring(fullName.lastIndexOf(".") + 1); |
return fullName.substring(fullName.lastIndexOf(".") + 1); |
352 |
} |
} |
353 |
|
|
354 |
|
/** |
355 |
|
* Returns the enumeration constants of this class, or |
356 |
|
* null if this class is not an <code>Enum</code>. |
357 |
|
* |
358 |
|
* @param klass the class whose enumeration constants should be returned. |
359 |
|
* @return an array of <code>Enum</code> constants |
360 |
|
* associated with this class, or null if this |
361 |
|
* class is not an <code>enum</code>. |
362 |
|
* @since 1.5 |
363 |
|
*/ |
364 |
|
static <T> T[] getEnumConstants(Class<T> klass) |
365 |
|
{ |
366 |
|
if (isEnum(klass)) |
367 |
|
{ |
368 |
|
try |
369 |
|
{ |
370 |
|
return (T[]) |
371 |
|
klass.getMethod("values").invoke(null); |
372 |
|
} |
373 |
|
catch (NoSuchMethodException exception) |
374 |
|
{ |
375 |
|
throw new Error("Enum lacks values() method"); |
376 |
|
} |
377 |
|
catch (IllegalAccessException exception) |
378 |
|
{ |
379 |
|
throw new Error("Unable to access Enum class"); |
380 |
|
} |
381 |
|
catch (InvocationTargetException exception) |
382 |
|
{ |
383 |
|
throw new |
384 |
|
RuntimeException("The values method threw an exception", |
385 |
|
exception); |
386 |
|
} |
387 |
|
} |
388 |
|
else |
389 |
|
{ |
390 |
|
return null; |
391 |
|
} |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
|
* Returns all annotations directly defined by the specified class. If |
396 |
|
* there are no annotations associated with this class, then a zero-length |
397 |
|
* array will be returned. The returned array may be modified by the client |
398 |
|
* code, but this will have no effect on the annotation content of this |
399 |
|
* class, and hence no effect on the return value of this method for |
400 |
|
* future callers. |
401 |
|
* |
402 |
|
* @param klass the class whose annotations should be returned. |
403 |
|
* @return the annotations directly defined by the specified class. |
404 |
|
* @since 1.5 |
405 |
|
*/ |
406 |
|
static native Annotation[] getDeclaredAnnotations(Class<?> klass); |
407 |
|
|
408 |
|
/** |
409 |
|
* <p> |
410 |
|
* Returns the canonical name of the specified class, as defined by section |
411 |
|
* 6.7 of the Java language specification. Each package, top-level class, |
412 |
|
* top-level interface and primitive type has a canonical name. A member |
413 |
|
* class has a canonical name, if its parent class has one. Likewise, |
414 |
|
* an array type has a canonical name, if its component type does. |
415 |
|
* Local or anonymous classes do not have canonical names. |
416 |
|
* </p> |
417 |
|
* <p> |
418 |
|
* The canonical name for top-level classes, top-level interfaces and |
419 |
|
* primitive types is always the same as the fully-qualified name. |
420 |
|
* For array types, the canonical name is the canonical name of its |
421 |
|
* component type with `[]' appended. |
422 |
|
* </p> |
423 |
|
* <p> |
424 |
|
* The canonical name of a member class always refers to the place where |
425 |
|
* the class was defined, and is composed of the canonical name of the |
426 |
|
* defining class and the simple name of the member class, joined by `.'. |
427 |
|
* For example, if a <code>Person</code> class has an inner class, |
428 |
|
* <code>M</code>, then both its fully-qualified name and canonical name |
429 |
|
* is <code>Person.M</code>. A subclass, <code>Staff</code>, of |
430 |
|
* <code>Person</code> refers to the same inner class by the fully-qualified |
431 |
|
* name of <code>Staff.M</code>, but its canonical name is still |
432 |
|
* <code>Person.M</code>. |
433 |
|
* </p> |
434 |
|
* <p> |
435 |
|
* Where no canonical name is present, <code>null</code> is returned. |
436 |
|
* </p> |
437 |
|
* |
438 |
|
* @param klass the class whose canonical name should be retrieved. |
439 |
|
* @return the canonical name of the class, or <code>null</code> if the |
440 |
|
* class doesn't have a canonical name. |
441 |
|
* @since 1.5 |
442 |
|
*/ |
443 |
|
static String getCanonicalName(Class<?> klass) |
444 |
|
{ |
445 |
|
if (isArray(klass)) |
446 |
|
{ |
447 |
|
String componentName = getComponentType(klass).getCanonicalName(); |
448 |
|
if (componentName != null) |
449 |
|
return componentName + "[]"; |
450 |
|
} |
451 |
|
if (isMemberClass(klass)) |
452 |
|
{ |
453 |
|
String memberName = getDeclaringClass(klass).getCanonicalName(); |
454 |
|
if (memberName != null) |
455 |
|
return memberName + "." + getSimpleName(klass); |
456 |
|
} |
457 |
|
if (isLocalClass(klass) || isAnonymousClass(klass)) |
458 |
|
return null; |
459 |
|
return getName(klass); |
460 |
|
} |
461 |
|
|
462 |
|
/** |
463 |
|
* Returns the class which immediately encloses the specified class. If |
464 |
|
* the class is a top-level class, this method returns <code>null</code>. |
465 |
|
* |
466 |
|
* @param klass the class whose enclosing class should be returned. |
467 |
|
* @return the immediate enclosing class, or <code>null</code> if this is |
468 |
|
* a top-level class. |
469 |
|
* @since 1.5 |
470 |
|
*/ |
471 |
|
static native Class<?> getEnclosingClass(Class<?> klass); |
472 |
|
|
473 |
|
/** |
474 |
|
* Returns the constructor which immediately encloses the specified class. |
475 |
|
* If the class is a top-level class, or a local or anonymous class |
476 |
|
* immediately enclosed by a type definition, instance initializer |
477 |
|
* or static initializer, then <code>null</code> is returned. |
478 |
|
* |
479 |
|
* @param klass the class whose enclosing constructor should be returned. |
480 |
|
* @return the immediate enclosing constructor if the specified class is |
481 |
|
* declared within a constructor. Otherwise, <code>null</code> |
482 |
|
* is returned. |
483 |
|
* @since 1.5 |
484 |
|
*/ |
485 |
|
static native Constructor<?> getEnclosingConstructor(Class<?> klass); |
486 |
|
|
487 |
|
/** |
488 |
|
* Returns the method which immediately encloses the specified class. If |
489 |
|
* the class is a top-level class, or a local or anonymous class |
490 |
|
* immediately enclosed by a type definition, instance initializer |
491 |
|
* or static initializer, then <code>null</code> is returned. |
492 |
|
* |
493 |
|
* @param klass the class whose enclosing method should be returned. |
494 |
|
* @return the immediate enclosing method if the specified class is |
495 |
|
* declared within a method. Otherwise, <code>null</code> |
496 |
|
* is returned. |
497 |
|
* @since 1.5 |
498 |
|
*/ |
499 |
|
static native Method getEnclosingMethod(Class<?> klass); |
500 |
|
|
501 |
|
/** |
502 |
|
* <p> |
503 |
|
* Returns an array of <code>Type</code> objects which represent the |
504 |
|
* interfaces directly implemented by the specified class or extended by the |
505 |
|
* specified interface. |
506 |
|
* </p> |
507 |
|
* <p> |
508 |
|
* If one of the superinterfaces is a parameterized type, then the |
509 |
|
* object returned for this interface reflects the actual type |
510 |
|
* parameters used in the source code. Type parameters are created |
511 |
|
* using the semantics specified by the <code>ParameterizedType</code> |
512 |
|
* interface, and only if an instance has not already been created. |
513 |
|
* </p> |
514 |
|
* <p> |
515 |
|
* The order of the interfaces in the array matches the order in which |
516 |
|
* the interfaces are declared. For classes which represent an array, |
517 |
|
* an array of two interfaces, <code>Cloneable</code> and |
518 |
|
* <code>Serializable</code>, is always returned, with the objects in |
519 |
|
* that order. A class representing a primitive type or void always |
520 |
|
* returns an array of zero size. |
521 |
|
* </p> |
522 |
|
* |
523 |
|
* @param klass the class whose generic interfaces should be retrieved. |
524 |
|
* @return an array of interfaces implemented or extended by the specified |
525 |
|
* class. |
526 |
|
* @throws GenericSignatureFormatError if the generic signature of one |
527 |
|
* of the interfaces does not comply with that specified by the Java |
528 |
|
* Virtual Machine specification, 3rd edition. |
529 |
|
* @throws TypeNotPresentException if any of the superinterfaces refers |
530 |
|
* to a non-existant type. |
531 |
|
* @throws MalformedParameterizedTypeException if any of the interfaces |
532 |
|
* refer to a parameterized type that can not be instantiated for |
533 |
|
* some reason. |
534 |
|
* @since 1.5 |
535 |
|
* @see java.lang.reflect.ParameterizedType |
536 |
|
*/ |
537 |
|
static native Type[] getGenericInterfaces(Class<?> klass); |
538 |
|
|
539 |
|
/** |
540 |
|
* <p> |
541 |
|
* Returns a <code>Type</code> object representing the direct superclass, |
542 |
|
* whether class, interface, primitive type or void, of the specified class. |
543 |
|
* If the class is an array class, then a class instance representing |
544 |
|
* the <code>Object</code> class is returned. If the class is primitive, |
545 |
|
* an interface, or a representation of either the <code>Object</code> |
546 |
|
* class or void, then <code>null</code> is returned. |
547 |
|
* </p> |
548 |
|
* <p> |
549 |
|
* If the superclass is a parameterized type, then the |
550 |
|
* object returned for this interface reflects the actual type |
551 |
|
* parameters used in the source code. Type parameters are created |
552 |
|
* using the semantics specified by the <code>ParameterizedType</code> |
553 |
|
* interface, and only if an instance has not already been created. |
554 |
|
* </p> |
555 |
|
* |
556 |
|
* @param klass the class whose generic superclass should be obtained. |
557 |
|
* @return the superclass of the specified class. |
558 |
|
* @throws GenericSignatureFormatError if the generic signature of the |
559 |
|
* class does not comply with that specified by the Java |
560 |
|
* Virtual Machine specification, 3rd edition. |
561 |
|
* @throws TypeNotPresentException if the superclass refers |
562 |
|
* to a non-existant type. |
563 |
|
* @throws MalformedParameterizedTypeException if the superclass |
564 |
|
* refers to a parameterized type that can not be instantiated for |
565 |
|
* some reason. |
566 |
|
* @since 1.5 |
567 |
|
* @see java.lang.reflect.ParameterizedType |
568 |
|
*/ |
569 |
|
static native Type getGenericSuperclass(Class<?> klass); |
570 |
|
|
571 |
|
/** |
572 |
|
* Returns an array of <code>TypeVariable</code> objects that represents |
573 |
|
* the type variables declared by the specified class, in declaration order. |
574 |
|
* An array of size zero is returned if the specified class has no type |
575 |
|
* variables. |
576 |
|
* |
577 |
|
* @param klass the class whose type variables should be returned. |
578 |
|
* @return the type variables associated with this class. |
579 |
|
* @throws GenericSignatureFormatError if the generic signature does |
580 |
|
* not conform to the format specified in the Virtual Machine |
581 |
|
* specification, version 3. |
582 |
|
* @since 1.5 |
583 |
|
*/ |
584 |
|
static native <T> TypeVariable<Class<T>>[] getTypeParameters(Class<T> klass); |
585 |
|
|
586 |
|
/** |
587 |
|
* Returns true if the specified class represents an anonymous class. |
588 |
|
* |
589 |
|
* @param klass the klass to test. |
590 |
|
* @return true if the specified class represents an anonymous class. |
591 |
|
* @since 1.5 |
592 |
|
*/ |
593 |
|
static native boolean isAnonymousClass(Class<?> klass); |
594 |
|
|
595 |
|
/** |
596 |
|
* Returns true if the specified class represents an local class. |
597 |
|
* |
598 |
|
* @param klass the klass to test. |
599 |
|
* @return true if the specified class represents an local class. |
600 |
|
* @since 1.5 |
601 |
|
*/ |
602 |
|
static native boolean isLocalClass(Class<?> klass); |
603 |
|
|
604 |
|
/** |
605 |
|
* Returns true if the specified class represents an member class. |
606 |
|
* |
607 |
|
* @param klass the klass to test. |
608 |
|
* @return true if the specified class represents an member class. |
609 |
|
* @since 1.5 |
610 |
|
*/ |
611 |
|
static native boolean isMemberClass(Class<?> klass); |
612 |
|
|
613 |
} // class VMClass |
} // class VMClass |