3762 * by erasure). 3763 * 3764 * @param <U> the type to cast this {@code Class} object to 3765 * @param clazz the class of the type to cast this {@code Class} object to 3766 * @return this {@code Class} object, cast to represent a subclass of 3767 * the specified class object. 3768 * @throws ClassCastException if this {@code Class} object does not 3769 * represent a subclass of the specified class (here "subclass" includes 3770 * the class itself). 3771 * @since 1.5 3772 */ 3773 @SuppressWarnings("unchecked") 3774 public <U> Class<? extends U> asSubclass(Class<U> clazz) { 3775 if (clazz.isAssignableFrom(this)) 3776 return (Class<? extends U>) this; 3777 else 3778 throw new ClassCastException(this.toString()); 3779 } 3780 3781 /** 3782 * @throws NullPointerException {@inheritDoc} 3783 * @since 1.5 3784 */ 3785 @SuppressWarnings("unchecked") 3786 public <A extends Annotation> A getAnnotation(Class<A> annotationClass) { 3787 Objects.requireNonNull(annotationClass); 3788 3789 return (A) annotationData().annotations.get(annotationClass); 3790 } 3791 3792 /** 3793 * {@inheritDoc} 3794 * @throws NullPointerException {@inheritDoc} 3795 * @since 1.5 3796 */ 3797 @Override 3798 public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { 3799 return GenericDeclaration.super.isAnnotationPresent(annotationClass); 3800 } 3801 3802 /** 3803 * @throws NullPointerException {@inheritDoc} 3804 * @since 1.8 3805 */ 3806 @Override 3807 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) { 3808 Objects.requireNonNull(annotationClass); 3809 3810 AnnotationData annotationData = annotationData(); 3811 return AnnotationSupport.getAssociatedAnnotations(annotationData.declaredAnnotations, 3812 this, 3813 annotationClass); 3814 } 3815 3816 /** 3817 * @since 1.5 3818 */ 3819 public Annotation[] getAnnotations() { 3820 return AnnotationParser.toArray(annotationData().annotations); 3821 } 3822 3823 /** 3824 * @throws NullPointerException {@inheritDoc} 3825 * @since 1.8 3826 */ 3827 @Override 3828 @SuppressWarnings("unchecked") 3829 public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) { 3830 Objects.requireNonNull(annotationClass); 3831 3832 return (A) annotationData().declaredAnnotations.get(annotationClass); 3833 } 3834 3835 /** 3836 * @throws NullPointerException {@inheritDoc} 3837 * @since 1.8 3838 */ 3839 @Override 3840 public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationClass) { 3841 Objects.requireNonNull(annotationClass); 3842 3843 return AnnotationSupport.getDirectlyAndIndirectlyPresent(annotationData().declaredAnnotations, 3844 annotationClass); 3845 } 3846 3847 /** 3848 * @since 1.5 3849 */ 3850 public Annotation[] getDeclaredAnnotations() { 3851 return AnnotationParser.toArray(annotationData().declaredAnnotations); 3852 } 3853 3854 // annotation data that might get invalidated when JVM TI RedefineClasses() is called 3855 private static class AnnotationData { 3856 final Map<Class<? extends Annotation>, Annotation> annotations; 3857 final Map<Class<? extends Annotation>, Annotation> declaredAnnotations; 3858 3859 // Value of classRedefinedCount when we created this AnnotationData instance 3860 final int redefinedCount; 3861 3862 AnnotationData(Map<Class<? extends Annotation>, Annotation> annotations, 3863 Map<Class<? extends Annotation>, Annotation> declaredAnnotations, 3864 int redefinedCount) { 3865 this.annotations = annotations; 3866 this.declaredAnnotations = declaredAnnotations; 3867 this.redefinedCount = redefinedCount; 3868 } 3869 } | 3762 * by erasure). 3763 * 3764 * @param <U> the type to cast this {@code Class} object to 3765 * @param clazz the class of the type to cast this {@code Class} object to 3766 * @return this {@code Class} object, cast to represent a subclass of 3767 * the specified class object. 3768 * @throws ClassCastException if this {@code Class} object does not 3769 * represent a subclass of the specified class (here "subclass" includes 3770 * the class itself). 3771 * @since 1.5 3772 */ 3773 @SuppressWarnings("unchecked") 3774 public <U> Class<? extends U> asSubclass(Class<U> clazz) { 3775 if (clazz.isAssignableFrom(this)) 3776 return (Class<? extends U>) this; 3777 else 3778 throw new ClassCastException(this.toString()); 3779 } 3780 3781 /** 3782 * {@inheritDoc} 3783 * <p>Note that any annotation returned by this method is a 3784 * declaration annotation. 3785 * 3786 * @throws NullPointerException {@inheritDoc} 3787 * @since 1.5 3788 */ 3789 @Override 3790 @SuppressWarnings("unchecked") 3791 public <A extends Annotation> A getAnnotation(Class<A> annotationClass) { 3792 Objects.requireNonNull(annotationClass); 3793 3794 return (A) annotationData().annotations.get(annotationClass); 3795 } 3796 3797 /** 3798 * {@inheritDoc} 3799 * @throws NullPointerException {@inheritDoc} 3800 * @since 1.5 3801 */ 3802 @Override 3803 public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { 3804 return GenericDeclaration.super.isAnnotationPresent(annotationClass); 3805 } 3806 3807 /** 3808 * {@inheritDoc} 3809 * <p>Note that any annotations returned by this method are 3810 * declaration annotations. 3811 * 3812 * @throws NullPointerException {@inheritDoc} 3813 * @since 1.8 3814 */ 3815 @Override 3816 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) { 3817 Objects.requireNonNull(annotationClass); 3818 3819 AnnotationData annotationData = annotationData(); 3820 return AnnotationSupport.getAssociatedAnnotations(annotationData.declaredAnnotations, 3821 this, 3822 annotationClass); 3823 } 3824 3825 /** 3826 * {@inheritDoc} 3827 * <p>Note that any annotations returned by this method are 3828 * declaration annotations. 3829 * 3830 * @since 1.5 3831 */ 3832 @Override 3833 public Annotation[] getAnnotations() { 3834 return AnnotationParser.toArray(annotationData().annotations); 3835 } 3836 3837 /** 3838 * {@inheritDoc} 3839 * <p>Note that any annotation returned by this method is a 3840 * declaration annotation. 3841 * 3842 * @throws NullPointerException {@inheritDoc} 3843 * @since 1.8 3844 */ 3845 @Override 3846 @SuppressWarnings("unchecked") 3847 public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) { 3848 Objects.requireNonNull(annotationClass); 3849 3850 return (A) annotationData().declaredAnnotations.get(annotationClass); 3851 } 3852 3853 /** 3854 * {@inheritDoc} 3855 * <p>Note that any annotations returned by this method are 3856 * declaration annotations. 3857 * 3858 * @throws NullPointerException {@inheritDoc} 3859 * @since 1.8 3860 */ 3861 @Override 3862 public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationClass) { 3863 Objects.requireNonNull(annotationClass); 3864 3865 return AnnotationSupport.getDirectlyAndIndirectlyPresent(annotationData().declaredAnnotations, 3866 annotationClass); 3867 } 3868 3869 /** 3870 * {@inheritDoc} 3871 * <p>Note that any annotations returned by this method are 3872 * declaration annotations. 3873 * 3874 * @since 1.5 3875 */ 3876 @Override 3877 public Annotation[] getDeclaredAnnotations() { 3878 return AnnotationParser.toArray(annotationData().declaredAnnotations); 3879 } 3880 3881 // annotation data that might get invalidated when JVM TI RedefineClasses() is called 3882 private static class AnnotationData { 3883 final Map<Class<? extends Annotation>, Annotation> annotations; 3884 final Map<Class<? extends Annotation>, Annotation> declaredAnnotations; 3885 3886 // Value of classRedefinedCount when we created this AnnotationData instance 3887 final int redefinedCount; 3888 3889 AnnotationData(Map<Class<? extends Annotation>, Annotation> annotations, 3890 Map<Class<? extends Annotation>, Annotation> declaredAnnotations, 3891 int redefinedCount) { 3892 this.annotations = annotations; 3893 this.declaredAnnotations = declaredAnnotations; 3894 this.redefinedCount = redefinedCount; 3895 } 3896 } |