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

Print this page




  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;
  49 import java.util.HashSet;
  50 import java.util.List;


  98  *
  99  * <p> It is also possible to get the {@code Class} object for a named
 100  * type (or for void) using a class literal.  See Section 15.8.2 of
 101  * <cite>The Java&trade; Language Specification</cite>.
 102  * For example:
 103  *
 104  * <p> <blockquote>
 105  *     {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
 106  * </blockquote>
 107  *
 108  * @param <T> the type of the class modeled by this {@code Class}
 109  * object.  For example, the type of {@code String.class} is {@code
 110  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 111  * unknown.
 112  *
 113  * @author  unascribed
 114  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 115  * @since   JDK1.0
 116  */
 117 public final class Class<T> implements java.io.Serializable,
 118                               java.lang.reflect.GenericDeclaration,
 119                               java.lang.reflect.Type,
 120                               java.lang.reflect.AnnotatedElement {
 121     private static final int ANNOTATION= 0x00002000;
 122     private static final int ENUM      = 0x00004000;
 123     private static final int SYNTHETIC = 0x00001000;
 124 
 125     private static native void registerNatives();
 126     static {
 127         registerNatives();
 128     }
 129 
 130     /*
 131      * Constructor. Only the Java Virtual Machine creates Class
 132      * objects.
 133      */
 134     private Class() {}
 135 
 136 
 137     /**
 138      * Converts the object to a string. The string representation is the
 139      * string "class" or "interface", followed by a space, and then by the
 140      * fully qualified name of the class in the format returned by


3165 
3166     /**
3167      * @throws NullPointerException {@inheritDoc}
3168      * @since 1.5
3169      */
3170     @SuppressWarnings("unchecked")
3171     public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
3172         Objects.requireNonNull(annotationClass);
3173 
3174         initAnnotationsIfNecessary();
3175         return (A) annotations.get(annotationClass);
3176     }
3177 
3178     /**
3179      * {@inheritDoc}
3180      * @throws NullPointerException {@inheritDoc}
3181      * @since 1.5
3182      */
3183     @Override
3184     public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
3185         return AnnotatedElement.super.isAnnotationPresent(annotationClass);
3186     }
3187 
3188     /**
3189      * @throws NullPointerException {@inheritDoc}
3190      * @since 1.8
3191      */
3192     @Override
3193     public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
3194         Objects.requireNonNull(annotationClass);
3195 
3196         initAnnotationsIfNecessary();
3197         return AnnotationSupport.getMultipleAnnotations(annotations, annotationClass);
3198     }
3199 
3200     /**
3201      * @since 1.5
3202      */
3203     public Annotation[] getAnnotations() {
3204         initAnnotationsIfNecessary();
3205         return AnnotationParser.toArray(annotations);




  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.GenericDeclaration;
  32 import java.lang.reflect.Member;
  33 import java.lang.reflect.Field;
  34 import java.lang.reflect.Executable;
  35 import java.lang.reflect.Method;
  36 import java.lang.reflect.Constructor;
  37 import java.lang.reflect.Modifier;
  38 import java.lang.reflect.Type;
  39 import java.lang.reflect.TypeVariable;
  40 import java.lang.reflect.InvocationTargetException;
  41 import java.lang.reflect.AnnotatedType;
  42 import java.lang.ref.SoftReference;
  43 import java.io.InputStream;
  44 import java.io.ObjectStreamField;
  45 import java.security.AccessController;
  46 import java.security.PrivilegedAction;
  47 import java.util.ArrayList;
  48 import java.util.Arrays;
  49 import java.util.Collection;
  50 import java.util.HashSet;
  51 import java.util.List;


  99  *
 100  * <p> It is also possible to get the {@code Class} object for a named
 101  * type (or for void) using a class literal.  See Section 15.8.2 of
 102  * <cite>The Java&trade; Language Specification</cite>.
 103  * For example:
 104  *
 105  * <p> <blockquote>
 106  *     {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
 107  * </blockquote>
 108  *
 109  * @param <T> the type of the class modeled by this {@code Class}
 110  * object.  For example, the type of {@code String.class} is {@code
 111  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 112  * unknown.
 113  *
 114  * @author  unascribed
 115  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 116  * @since   JDK1.0
 117  */
 118 public final class Class<T> implements java.io.Serializable,
 119                               GenericDeclaration,
 120                               Type,
 121                               AnnotatedElement {
 122     private static final int ANNOTATION= 0x00002000;
 123     private static final int ENUM      = 0x00004000;
 124     private static final int SYNTHETIC = 0x00001000;
 125 
 126     private static native void registerNatives();
 127     static {
 128         registerNatives();
 129     }
 130 
 131     /*
 132      * Constructor. Only the Java Virtual Machine creates Class
 133      * objects.
 134      */
 135     private Class() {}
 136 
 137 
 138     /**
 139      * Converts the object to a string. The string representation is the
 140      * string "class" or "interface", followed by a space, and then by the
 141      * fully qualified name of the class in the format returned by


3166 
3167     /**
3168      * @throws NullPointerException {@inheritDoc}
3169      * @since 1.5
3170      */
3171     @SuppressWarnings("unchecked")
3172     public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
3173         Objects.requireNonNull(annotationClass);
3174 
3175         initAnnotationsIfNecessary();
3176         return (A) annotations.get(annotationClass);
3177     }
3178 
3179     /**
3180      * {@inheritDoc}
3181      * @throws NullPointerException {@inheritDoc}
3182      * @since 1.5
3183      */
3184     @Override
3185     public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
3186         return GenericDeclaration.super.isAnnotationPresent(annotationClass);
3187     }
3188 
3189     /**
3190      * @throws NullPointerException {@inheritDoc}
3191      * @since 1.8
3192      */
3193     @Override
3194     public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
3195         Objects.requireNonNull(annotationClass);
3196 
3197         initAnnotationsIfNecessary();
3198         return AnnotationSupport.getMultipleAnnotations(annotations, annotationClass);
3199     }
3200 
3201     /**
3202      * @since 1.5
3203      */
3204     public Annotation[] getAnnotations() {
3205         initAnnotationsIfNecessary();
3206         return AnnotationParser.toArray(annotations);