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