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

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 

  28 import java.lang.reflect.Array;
  29 import java.lang.reflect.GenericArrayType;
  30 import java.lang.reflect.Member;
  31 import java.lang.reflect.Field;
  32 import java.lang.reflect.Executable;
  33 import java.lang.reflect.Method;
  34 import java.lang.reflect.Constructor;
  35 import java.lang.reflect.Modifier;
  36 import java.lang.reflect.Type;
  37 import java.lang.reflect.TypeVariable;
  38 import java.lang.reflect.InvocationTargetException;
  39 import java.lang.reflect.AnnotatedType;
  40 import java.lang.ref.SoftReference;
  41 import java.io.InputStream;
  42 import java.io.ObjectStreamField;
  43 import java.security.AccessController;
  44 import java.security.PrivilegedAction;
  45 import java.util.ArrayList;
  46 import java.util.Arrays;
  47 import java.util.Collection;


3064      * @since 1.5
3065      */
3066     @SuppressWarnings("unchecked")
3067     public <U> Class<? extends U> asSubclass(Class<U> clazz) {
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     @Override
3091     public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
3092         Objects.requireNonNull(annotationClass);
3093 
3094         initAnnotationsIfNecessary();
3095         return AnnotationSupport.getMultipleAnnotations(annotations, annotationClass);
3096     }
3097 
3098     /**
3099      * @since 1.5
3100      */
3101     public Annotation[] getAnnotations() {
3102         initAnnotationsIfNecessary();
3103         return AnnotationParser.toArray(annotations);




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.reflect.AnnotatedElement;
  29 import java.lang.reflect.Array;
  30 import java.lang.reflect.GenericArrayType;
  31 import java.lang.reflect.Member;
  32 import java.lang.reflect.Field;
  33 import java.lang.reflect.Executable;
  34 import java.lang.reflect.Method;
  35 import java.lang.reflect.Constructor;
  36 import java.lang.reflect.Modifier;
  37 import java.lang.reflect.Type;
  38 import java.lang.reflect.TypeVariable;
  39 import java.lang.reflect.InvocationTargetException;
  40 import java.lang.reflect.AnnotatedType;
  41 import java.lang.ref.SoftReference;
  42 import java.io.InputStream;
  43 import java.io.ObjectStreamField;
  44 import java.security.AccessController;
  45 import java.security.PrivilegedAction;
  46 import java.util.ArrayList;
  47 import java.util.Arrays;
  48 import java.util.Collection;


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