< prev index next >

src/java.base/share/classes/java/lang/Class.java

Print this page




 900      * entity (class, interface, primitive type or void) represented by
 901      * this {@code Class}.  If this {@code Class} represents either the
 902      * {@code Object} class, an interface, a primitive type, or void, then
 903      * null is returned.  If this object represents an array class then the
 904      * {@code Class} object representing the {@code Object} class is
 905      * returned.
 906      *
 907      * @return the direct superclass of the class represented by this object
 908      */
 909     @HotSpotIntrinsicCandidate
 910     public native Class<? super T> getSuperclass();
 911 
 912 
 913     /**
 914      * Returns the {@code Type} representing the direct superclass of
 915      * the entity (class, interface, primitive type or void) represented by
 916      * this {@code Class}.
 917      *
 918      * <p>If the superclass is a parameterized type, the {@code Type}
 919      * object returned must accurately reflect the actual type
 920      * parameters used in the source code. The parameterized type
 921      * representing the superclass is created if it had not been
 922      * created before. See the declaration of {@link
 923      * java.lang.reflect.ParameterizedType ParameterizedType} for the
 924      * semantics of the creation process for parameterized types.  If
 925      * this {@code Class} represents either the {@code Object}
 926      * class, an interface, a primitive type, or void, then null is
 927      * returned.  If this object represents an array class then the
 928      * {@code Class} object representing the {@code Object} class is
 929      * returned.
 930      *
 931      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
 932      *     class signature does not conform to the format specified in
 933      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 934      * @throws TypeNotPresentException if the generic superclass
 935      *     refers to a non-existent type declaration
 936      * @throws java.lang.reflect.MalformedParameterizedTypeException if the
 937      *     generic superclass refers to a parameterized type that cannot be
 938      *     instantiated  for any reason
 939      * @return the direct superclass of the class represented by this object
 940      * @since 1.5


1080         } else {
1081             Class<?>[] interfaces = rd.interfaces;
1082             if (interfaces == null) {
1083                 interfaces = getInterfaces0();
1084                 rd.interfaces = interfaces;
1085             }
1086             // defensively copy if requested
1087             return cloneArray ? interfaces.clone() : interfaces;
1088         }
1089     }
1090 
1091     private native Class<?>[] getInterfaces0();
1092 
1093     /**
1094      * Returns the {@code Type}s representing the interfaces
1095      * directly implemented by the class or interface represented by
1096      * this object.
1097      *
1098      * <p>If a superinterface is a parameterized type, the
1099      * {@code Type} object returned for it must accurately reflect
1100      * the actual type parameters used in the source code. The
1101      * parameterized type representing each superinterface is created
1102      * if it had not been created before. See the declaration of
1103      * {@link java.lang.reflect.ParameterizedType ParameterizedType}
1104      * for the semantics of the creation process for parameterized
1105      * types.
1106      *
1107      * <p>If this object represents a class, the return value is an array
1108      * containing objects representing all interfaces directly implemented by
1109      * the class.  The order of the interface objects in the array corresponds
1110      * to the order of the interface names in the {@code implements} clause of
1111      * the declaration of the class represented by this object.
1112      *
1113      * <p>If this object represents an interface, the array contains objects
1114      * representing all interfaces directly extended by the interface.  The
1115      * order of the interface objects in the array corresponds to the order of
1116      * the interface names in the {@code extends} clause of the declaration of
1117      * the interface represented by this object.
1118      *
1119      * <p>If this object represents a class or interface that implements no
1120      * interfaces, the method returns an array of length 0.


3623      * by erasure).
3624      *
3625      * @param <U> the type to cast this class object to
3626      * @param clazz the class of the type to cast this class object to
3627      * @return this {@code Class} object, cast to represent a subclass of
3628      *    the specified class object.
3629      * @throws ClassCastException if this {@code Class} object does not
3630      *    represent a subclass of the specified class (here "subclass" includes
3631      *    the class itself).
3632      * @since 1.5
3633      */
3634     @SuppressWarnings("unchecked")
3635     public <U> Class<? extends U> asSubclass(Class<U> clazz) {
3636         if (clazz.isAssignableFrom(this))
3637             return (Class<? extends U>) this;
3638         else
3639             throw new ClassCastException(this.toString());
3640     }
3641 
3642     /**


3643      * @throws NullPointerException {@inheritDoc}
3644      * @since 1.5
3645      */

3646     @SuppressWarnings("unchecked")
3647     public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
3648         Objects.requireNonNull(annotationClass);
3649 
3650         return (A) annotationData().annotations.get(annotationClass);
3651     }
3652 
3653     /**
3654      * {@inheritDoc}
3655      * @throws NullPointerException {@inheritDoc}
3656      * @since 1.5
3657      */
3658     @Override
3659     public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
3660         return GenericDeclaration.super.isAnnotationPresent(annotationClass);
3661     }
3662 
3663     /**


3664      * @throws NullPointerException {@inheritDoc}
3665      * @since 1.8
3666      */
3667     @Override
3668     public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
3669         Objects.requireNonNull(annotationClass);
3670 
3671         AnnotationData annotationData = annotationData();
3672         return AnnotationSupport.getAssociatedAnnotations(annotationData.declaredAnnotations,
3673                                                           this,
3674                                                           annotationClass);
3675     }
3676 
3677     /**


3678      * @since 1.5
3679      */

3680     public Annotation[] getAnnotations() {
3681         return AnnotationParser.toArray(annotationData().annotations);
3682     }
3683 
3684     /**


3685      * @throws NullPointerException {@inheritDoc}
3686      * @since 1.8
3687      */
3688     @Override
3689     @SuppressWarnings("unchecked")
3690     public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) {
3691         Objects.requireNonNull(annotationClass);
3692 
3693         return (A) annotationData().declaredAnnotations.get(annotationClass);
3694     }
3695 
3696     /**


3697      * @throws NullPointerException {@inheritDoc}
3698      * @since 1.8
3699      */
3700     @Override
3701     public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationClass) {
3702         Objects.requireNonNull(annotationClass);
3703 
3704         return AnnotationSupport.getDirectlyAndIndirectlyPresent(annotationData().declaredAnnotations,
3705                                                                  annotationClass);
3706     }
3707 
3708     /**


3709      * @since 1.5
3710      */

3711     public Annotation[] getDeclaredAnnotations()  {
3712         return AnnotationParser.toArray(annotationData().declaredAnnotations);
3713     }
3714 
3715     // annotation data that might get invalidated when JVM TI RedefineClasses() is called
3716     private static class AnnotationData {
3717         final Map<Class<? extends Annotation>, Annotation> annotations;
3718         final Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
3719 
3720         // Value of classRedefinedCount when we created this AnnotationData instance
3721         final int redefinedCount;
3722 
3723         AnnotationData(Map<Class<? extends Annotation>, Annotation> annotations,
3724                        Map<Class<? extends Annotation>, Annotation> declaredAnnotations,
3725                        int redefinedCount) {
3726             this.annotations = annotations;
3727             this.declaredAnnotations = declaredAnnotations;
3728             this.redefinedCount = redefinedCount;
3729         }
3730     }




 900      * entity (class, interface, primitive type or void) represented by
 901      * this {@code Class}.  If this {@code Class} represents either the
 902      * {@code Object} class, an interface, a primitive type, or void, then
 903      * null is returned.  If this object represents an array class then the
 904      * {@code Class} object representing the {@code Object} class is
 905      * returned.
 906      *
 907      * @return the direct superclass of the class represented by this object
 908      */
 909     @HotSpotIntrinsicCandidate
 910     public native Class<? super T> getSuperclass();
 911 
 912 
 913     /**
 914      * Returns the {@code Type} representing the direct superclass of
 915      * the entity (class, interface, primitive type or void) represented by
 916      * this {@code Class}.
 917      *
 918      * <p>If the superclass is a parameterized type, the {@code Type}
 919      * object returned must accurately reflect the actual type
 920      * arguments used in the source code. The parameterized type
 921      * representing the superclass is created if it had not been
 922      * created before. See the declaration of {@link
 923      * java.lang.reflect.ParameterizedType ParameterizedType} for the
 924      * semantics of the creation process for parameterized types.  If
 925      * this {@code Class} represents either the {@code Object}
 926      * class, an interface, a primitive type, or void, then null is
 927      * returned.  If this object represents an array class then the
 928      * {@code Class} object representing the {@code Object} class is
 929      * returned.
 930      *
 931      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
 932      *     class signature does not conform to the format specified in
 933      *     <cite>The Java&trade; Virtual Machine Specification</cite>
 934      * @throws TypeNotPresentException if the generic superclass
 935      *     refers to a non-existent type declaration
 936      * @throws java.lang.reflect.MalformedParameterizedTypeException if the
 937      *     generic superclass refers to a parameterized type that cannot be
 938      *     instantiated  for any reason
 939      * @return the direct superclass of the class represented by this object
 940      * @since 1.5


1080         } else {
1081             Class<?>[] interfaces = rd.interfaces;
1082             if (interfaces == null) {
1083                 interfaces = getInterfaces0();
1084                 rd.interfaces = interfaces;
1085             }
1086             // defensively copy if requested
1087             return cloneArray ? interfaces.clone() : interfaces;
1088         }
1089     }
1090 
1091     private native Class<?>[] getInterfaces0();
1092 
1093     /**
1094      * Returns the {@code Type}s representing the interfaces
1095      * directly implemented by the class or interface represented by
1096      * this object.
1097      *
1098      * <p>If a superinterface is a parameterized type, the
1099      * {@code Type} object returned for it must accurately reflect
1100      * the actual type arguments used in the source code. The
1101      * parameterized type representing each superinterface is created
1102      * if it had not been created before. See the declaration of
1103      * {@link java.lang.reflect.ParameterizedType ParameterizedType}
1104      * for the semantics of the creation process for parameterized
1105      * types.
1106      *
1107      * <p>If this object represents a class, the return value is an array
1108      * containing objects representing all interfaces directly implemented by
1109      * the class.  The order of the interface objects in the array corresponds
1110      * to the order of the interface names in the {@code implements} clause of
1111      * the declaration of the class represented by this object.
1112      *
1113      * <p>If this object represents an interface, the array contains objects
1114      * representing all interfaces directly extended by the interface.  The
1115      * order of the interface objects in the array corresponds to the order of
1116      * the interface names in the {@code extends} clause of the declaration of
1117      * the interface represented by this object.
1118      *
1119      * <p>If this object represents a class or interface that implements no
1120      * interfaces, the method returns an array of length 0.


3623      * by erasure).
3624      *
3625      * @param <U> the type to cast this class object to
3626      * @param clazz the class of the type to cast this class object to
3627      * @return this {@code Class} object, cast to represent a subclass of
3628      *    the specified class object.
3629      * @throws ClassCastException if this {@code Class} object does not
3630      *    represent a subclass of the specified class (here "subclass" includes
3631      *    the class itself).
3632      * @since 1.5
3633      */
3634     @SuppressWarnings("unchecked")
3635     public <U> Class<? extends U> asSubclass(Class<U> clazz) {
3636         if (clazz.isAssignableFrom(this))
3637             return (Class<? extends U>) this;
3638         else
3639             throw new ClassCastException(this.toString());
3640     }
3641 
3642     /**
3643      * {@inheritDoc}
3644      * <p>Any annotation returned by this method is a declaration annotation.
3645      * @throws NullPointerException {@inheritDoc}
3646      * @since 1.5
3647      */
3648     @Override
3649     @SuppressWarnings("unchecked")
3650     public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
3651         Objects.requireNonNull(annotationClass);
3652 
3653         return (A) annotationData().annotations.get(annotationClass);
3654     }
3655 
3656     /**
3657      * {@inheritDoc}
3658      * @throws NullPointerException {@inheritDoc}
3659      * @since 1.5
3660      */
3661     @Override
3662     public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
3663         return GenericDeclaration.super.isAnnotationPresent(annotationClass);
3664     }
3665 
3666     /**
3667      * {@inheritDoc}
3668      * <p>Any annotations returned by this method are declaration annotations.
3669      * @throws NullPointerException {@inheritDoc}
3670      * @since 1.8
3671      */
3672     @Override
3673     public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
3674         Objects.requireNonNull(annotationClass);
3675 
3676         AnnotationData annotationData = annotationData();
3677         return AnnotationSupport.getAssociatedAnnotations(annotationData.declaredAnnotations,
3678                                                           this,
3679                                                           annotationClass);
3680     }
3681 
3682     /**
3683      * {@inheritDoc}
3684      * <p>Any annotations returned by this method are declaration annotations.
3685      * @since 1.5
3686      */
3687     @Override
3688     public Annotation[] getAnnotations() {
3689         return AnnotationParser.toArray(annotationData().annotations);
3690     }
3691 
3692     /**
3693      * {@inheritDoc}
3694      * <p>Any annotation returned by this method is a declaration annotation.
3695      * @throws NullPointerException {@inheritDoc}
3696      * @since 1.8
3697      */
3698     @Override
3699     @SuppressWarnings("unchecked")
3700     public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) {
3701         Objects.requireNonNull(annotationClass);
3702 
3703         return (A) annotationData().declaredAnnotations.get(annotationClass);
3704     }
3705 
3706     /**
3707      * {@inheritDoc}
3708      * <p>Any annotations returned by this method are declaration annotations.
3709      * @throws NullPointerException {@inheritDoc}
3710      * @since 1.8
3711      */
3712     @Override
3713     public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationClass) {
3714         Objects.requireNonNull(annotationClass);
3715 
3716         return AnnotationSupport.getDirectlyAndIndirectlyPresent(annotationData().declaredAnnotations,
3717                                                                  annotationClass);
3718     }
3719 
3720     /**
3721      * {@inheritDoc}
3722      * <p>Any annotations returned by this method are declaration annotations.
3723      * @since 1.5
3724      */
3725     @Override
3726     public Annotation[] getDeclaredAnnotations() {
3727         return AnnotationParser.toArray(annotationData().declaredAnnotations);
3728     }
3729 
3730     // annotation data that might get invalidated when JVM TI RedefineClasses() is called
3731     private static class AnnotationData {
3732         final Map<Class<? extends Annotation>, Annotation> annotations;
3733         final Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
3734 
3735         // Value of classRedefinedCount when we created this AnnotationData instance
3736         final int redefinedCount;
3737 
3738         AnnotationData(Map<Class<? extends Annotation>, Annotation> annotations,
3739                        Map<Class<? extends Annotation>, Annotation> declaredAnnotations,
3740                        int redefinedCount) {
3741             this.annotations = annotations;
3742             this.declaredAnnotations = declaredAnnotations;
3743             this.redefinedCount = redefinedCount;
3744         }
3745     }


< prev index next >