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

Print this page




3068         if (clazz.isAssignableFrom(this))
3069             return (Class<? extends U>) this;
3070         else
3071             throw new ClassCastException(this.toString());
3072     }
3073 
3074     /**
3075      * @throws NullPointerException {@inheritDoc}
3076      * @since 1.5
3077      */
3078     @SuppressWarnings("unchecked")
3079     public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
3080         Objects.requireNonNull(annotationClass);
3081 
3082         initAnnotationsIfNecessary();
3083         return (A) annotations.get(annotationClass);
3084     }
3085 
3086     /**
3087      * @throws NullPointerException {@inheritDoc}
3088      * @since 1.5
3089      */
3090     public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
3091         Objects.requireNonNull(annotationClass);
3092 
3093         return getAnnotation(annotationClass) != null;
3094     }
3095 
3096     /**
3097      * @throws NullPointerException {@inheritDoc}
3098      * @since 1.8
3099      */
3100     public <A extends Annotation> A[] getAnnotations(Class<A> annotationClass) {
3101         Objects.requireNonNull(annotationClass);
3102 
3103         initAnnotationsIfNecessary();
3104         return AnnotationSupport.getMultipleAnnotations(annotations, annotationClass);
3105     }
3106 
3107     /**
3108      * @since 1.5
3109      */
3110     public Annotation[] getAnnotations() {
3111         initAnnotationsIfNecessary();
3112         return AnnotationParser.toArray(annotations);
3113     }
3114 
3115     /**
3116      * @throws NullPointerException {@inheritDoc}
3117      * @since 1.8




3068         if (clazz.isAssignableFrom(this))
3069             return (Class<? extends U>) this;
3070         else
3071             throw new ClassCastException(this.toString());
3072     }
3073 
3074     /**
3075      * @throws NullPointerException {@inheritDoc}
3076      * @since 1.5
3077      */
3078     @SuppressWarnings("unchecked")
3079     public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
3080         Objects.requireNonNull(annotationClass);
3081 
3082         initAnnotationsIfNecessary();
3083         return (A) annotations.get(annotationClass);
3084     }
3085 
3086     /**
3087      * @throws NullPointerException {@inheritDoc}










3088      * @since 1.8
3089      */
3090     public <A extends Annotation> A[] getAnnotations(Class<A> annotationClass) {
3091         Objects.requireNonNull(annotationClass);
3092 
3093         initAnnotationsIfNecessary();
3094         return AnnotationSupport.getMultipleAnnotations(annotations, annotationClass);
3095     }
3096 
3097     /**
3098      * @since 1.5
3099      */
3100     public Annotation[] getAnnotations() {
3101         initAnnotationsIfNecessary();
3102         return AnnotationParser.toArray(annotations);
3103     }
3104 
3105     /**
3106      * @throws NullPointerException {@inheritDoc}
3107      * @since 1.8