< prev index next >

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

Print this page
rev 52786 : 8210031: implementation for JVM Constants API


   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.annotation.Annotation;


  29 import java.lang.module.ModuleReader;
  30 import java.lang.ref.SoftReference;
  31 import java.io.IOException;
  32 import java.io.InputStream;
  33 import java.io.ObjectStreamField;
  34 import java.lang.reflect.AnnotatedElement;
  35 import java.lang.reflect.AnnotatedType;
  36 import java.lang.reflect.Array;
  37 import java.lang.reflect.Constructor;
  38 import java.lang.reflect.Executable;
  39 import java.lang.reflect.Field;
  40 import java.lang.reflect.GenericArrayType;
  41 import java.lang.reflect.GenericDeclaration;
  42 import java.lang.reflect.InvocationTargetException;
  43 import java.lang.reflect.Member;
  44 import java.lang.reflect.Method;
  45 import java.lang.reflect.Modifier;
  46 import java.lang.reflect.Proxy;
  47 import java.lang.reflect.Type;
  48 import java.lang.reflect.TypeVariable;

  49 import java.net.URL;
  50 import java.security.AccessController;
  51 import java.security.PrivilegedAction;
  52 import java.util.ArrayList;
  53 import java.util.Arrays;
  54 import java.util.Collection;
  55 import java.util.HashMap;
  56 import java.util.LinkedHashMap;
  57 import java.util.LinkedHashSet;
  58 import java.util.List;
  59 import java.util.Map;
  60 import java.util.Objects;

  61 import java.util.StringJoiner;
  62 import java.util.stream.Stream;
  63 import java.util.stream.Collectors;
  64 
  65 import jdk.internal.HotSpotIntrinsicCandidate;
  66 import jdk.internal.loader.BootLoader;
  67 import jdk.internal.loader.BuiltinClassLoader;
  68 import jdk.internal.misc.Unsafe;
  69 import jdk.internal.module.Resources;
  70 import jdk.internal.reflect.CallerSensitive;
  71 import jdk.internal.reflect.ConstantPool;
  72 import jdk.internal.reflect.Reflection;
  73 import jdk.internal.reflect.ReflectionFactory;
  74 import jdk.internal.vm.annotation.ForceInline;

  75 import sun.reflect.generics.factory.CoreReflectionFactory;
  76 import sun.reflect.generics.factory.GenericsFactory;
  77 import sun.reflect.generics.repository.ClassRepository;
  78 import sun.reflect.generics.repository.MethodRepository;
  79 import sun.reflect.generics.repository.ConstructorRepository;
  80 import sun.reflect.generics.scope.ClassScope;
  81 import sun.security.util.SecurityConstants;
  82 import sun.reflect.annotation.*;
  83 import sun.reflect.misc.ReflectUtil;
  84 
  85 /**
  86  * Instances of the class {@code Class} represent classes and interfaces
  87  * in a running Java application. An enum type is a kind of class and an
  88  * annotation type is a kind of interface. Every array also
  89  * belongs to a class that is reflected as a {@code Class} object
  90  * that is shared by all arrays with the same element type and number
  91  * of dimensions.  The primitive Java types ({@code boolean},
  92  * {@code byte}, {@code char}, {@code short},
  93  * {@code int}, {@code long}, {@code float}, and
  94  * {@code double}), and the keyword {@code void} are also


 137  * type (or for void) using a class literal.  See Section 15.8.2 of
 138  * <cite>The Java&trade; Language Specification</cite>.
 139  * For example:
 140  *
 141  * <blockquote>
 142  *     {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
 143  * </blockquote>
 144  *
 145  * @param <T> the type of the class modeled by this {@code Class}
 146  * object.  For example, the type of {@code String.class} is {@code
 147  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 148  * unknown.
 149  *
 150  * @author  unascribed
 151  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 152  * @since   1.0
 153  */
 154 public final class Class<T> implements java.io.Serializable,
 155                               GenericDeclaration,
 156                               Type,
 157                               AnnotatedElement {


 158     private static final int ANNOTATION= 0x00002000;
 159     private static final int ENUM      = 0x00004000;
 160     private static final int SYNTHETIC = 0x00001000;
 161 
 162     private static native void registerNatives();
 163     static {
 164         registerNatives();
 165     }
 166 
 167     /*
 168      * Private constructor. Only the Java Virtual Machine creates Class objects.
 169      * This constructor is not used and prevents the default constructor being
 170      * generated.
 171      */
 172     private Class(ClassLoader loader, Class<?> arrayComponentType) {
 173         // Initialize final field for classLoader.  The initialization value of non-null
 174         // prevents future JIT optimizations from assuming this final field is null.
 175         classLoader = loader;
 176         componentType = arrayComponentType;
 177     }


4009      */
4010     @CallerSensitive
4011     public Class<?>[] getNestMembers() {
4012         if (isPrimitive() || isArray()) {
4013             return new Class<?>[] { this };
4014         }
4015         Class<?>[] members = getNestMembers0();
4016         // Can't actually enable this due to bootstrapping issues
4017         // assert(members.length != 1 || members[0] == this); // expected invariant from VM
4018 
4019         if (members.length > 1) {
4020             // If we return anything other than the current class we need
4021             // a security check
4022             SecurityManager sm = System.getSecurityManager();
4023             if (sm != null) {
4024                 checkPackageAccess(sm,
4025                                    ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
4026             }
4027         }
4028         return members;






























































4029     }
4030 }


   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.annotation.Annotation;
  29 import java.lang.constant.ClassDesc;
  30 import java.lang.invoke.TypeDescriptor;
  31 import java.lang.module.ModuleReader;
  32 import java.lang.ref.SoftReference;
  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.io.ObjectStreamField;
  36 import java.lang.reflect.AnnotatedElement;
  37 import java.lang.reflect.AnnotatedType;
  38 import java.lang.reflect.Array;
  39 import java.lang.reflect.Constructor;
  40 import java.lang.reflect.Executable;
  41 import java.lang.reflect.Field;
  42 import java.lang.reflect.GenericArrayType;
  43 import java.lang.reflect.GenericDeclaration;
  44 import java.lang.reflect.InvocationTargetException;
  45 import java.lang.reflect.Member;
  46 import java.lang.reflect.Method;
  47 import java.lang.reflect.Modifier;
  48 import java.lang.reflect.Proxy;
  49 import java.lang.reflect.Type;
  50 import java.lang.reflect.TypeVariable;
  51 import java.lang.constant.Constable;
  52 import java.net.URL;
  53 import java.security.AccessController;
  54 import java.security.PrivilegedAction;
  55 import java.util.ArrayList;
  56 import java.util.Arrays;
  57 import java.util.Collection;
  58 import java.util.HashMap;
  59 import java.util.LinkedHashMap;
  60 import java.util.LinkedHashSet;
  61 import java.util.List;
  62 import java.util.Map;
  63 import java.util.Objects;
  64 import java.util.Optional;
  65 import java.util.StringJoiner;
  66 import java.util.stream.Stream;
  67 import java.util.stream.Collectors;
  68 
  69 import jdk.internal.HotSpotIntrinsicCandidate;
  70 import jdk.internal.loader.BootLoader;
  71 import jdk.internal.loader.BuiltinClassLoader;
  72 import jdk.internal.misc.Unsafe;
  73 import jdk.internal.module.Resources;
  74 import jdk.internal.reflect.CallerSensitive;
  75 import jdk.internal.reflect.ConstantPool;
  76 import jdk.internal.reflect.Reflection;
  77 import jdk.internal.reflect.ReflectionFactory;
  78 import jdk.internal.vm.annotation.ForceInline;
  79 import sun.invoke.util.Wrapper;
  80 import sun.reflect.generics.factory.CoreReflectionFactory;
  81 import sun.reflect.generics.factory.GenericsFactory;
  82 import sun.reflect.generics.repository.ClassRepository;
  83 import sun.reflect.generics.repository.MethodRepository;
  84 import sun.reflect.generics.repository.ConstructorRepository;
  85 import sun.reflect.generics.scope.ClassScope;
  86 import sun.security.util.SecurityConstants;
  87 import sun.reflect.annotation.*;
  88 import sun.reflect.misc.ReflectUtil;
  89 
  90 /**
  91  * Instances of the class {@code Class} represent classes and interfaces
  92  * in a running Java application. An enum type is a kind of class and an
  93  * annotation type is a kind of interface. Every array also
  94  * belongs to a class that is reflected as a {@code Class} object
  95  * that is shared by all arrays with the same element type and number
  96  * of dimensions.  The primitive Java types ({@code boolean},
  97  * {@code byte}, {@code char}, {@code short},
  98  * {@code int}, {@code long}, {@code float}, and
  99  * {@code double}), and the keyword {@code void} are also


 142  * type (or for void) using a class literal.  See Section 15.8.2 of
 143  * <cite>The Java&trade; Language Specification</cite>.
 144  * For example:
 145  *
 146  * <blockquote>
 147  *     {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
 148  * </blockquote>
 149  *
 150  * @param <T> the type of the class modeled by this {@code Class}
 151  * object.  For example, the type of {@code String.class} is {@code
 152  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
 153  * unknown.
 154  *
 155  * @author  unascribed
 156  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
 157  * @since   1.0
 158  */
 159 public final class Class<T> implements java.io.Serializable,
 160                               GenericDeclaration,
 161                               Type,
 162                               AnnotatedElement,
 163                                        TypeDescriptor.OfField<Class<?>>,
 164                               Constable {
 165     private static final int ANNOTATION= 0x00002000;
 166     private static final int ENUM      = 0x00004000;
 167     private static final int SYNTHETIC = 0x00001000;
 168 
 169     private static native void registerNatives();
 170     static {
 171         registerNatives();
 172     }
 173 
 174     /*
 175      * Private constructor. Only the Java Virtual Machine creates Class objects.
 176      * This constructor is not used and prevents the default constructor being
 177      * generated.
 178      */
 179     private Class(ClassLoader loader, Class<?> arrayComponentType) {
 180         // Initialize final field for classLoader.  The initialization value of non-null
 181         // prevents future JIT optimizations from assuming this final field is null.
 182         classLoader = loader;
 183         componentType = arrayComponentType;
 184     }


4016      */
4017     @CallerSensitive
4018     public Class<?>[] getNestMembers() {
4019         if (isPrimitive() || isArray()) {
4020             return new Class<?>[] { this };
4021         }
4022         Class<?>[] members = getNestMembers0();
4023         // Can't actually enable this due to bootstrapping issues
4024         // assert(members.length != 1 || members[0] == this); // expected invariant from VM
4025 
4026         if (members.length > 1) {
4027             // If we return anything other than the current class we need
4028             // a security check
4029             SecurityManager sm = System.getSecurityManager();
4030             if (sm != null) {
4031                 checkPackageAccess(sm,
4032                                    ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
4033             }
4034         }
4035         return members;
4036     }
4037 
4038     /**
4039      * Produce the type descriptor string for this class.
4040      * <p>
4041      * Note that this is not a strict inverse of {@link #forName};
4042      * distinct classes which share a common name but have different class loaders
4043      * will have identical descriptor strings.
4044      *
4045      * @return the type descriptor representation
4046      * @jvms 4.3.2 Field Descriptors
4047      * @since 12
4048      */
4049     @Override
4050     public String descriptorString() {
4051         if (isPrimitive())
4052             return Wrapper.forPrimitiveType(this).basicTypeString();
4053         else if (isArray()) {
4054             return "[" + componentType.descriptorString();
4055         }
4056         else {
4057             return "L" + getName().replace('.', '/') + ";";
4058         }
4059     }
4060 
4061     /**
4062      * Returns the component type of this {@code Class}, if it describes
4063      * an array type, or {@code null} otherwise.  Equivalent to
4064      * {@link Class#getComponentType()}.
4065      *
4066      * @return a {@code Class} describing the component type, or {@code null}
4067      * if this {@code Class} does not describe an array type
4068      * @since 12
4069      */
4070     @Override
4071     public Class<?> componentType() {
4072         return isArray() ? componentType : null;
4073     }
4074 
4075     /**
4076      * Create a {@code Class} for an array type whose component type
4077      * is described by this {@linkplain Class}.
4078      *
4079      * @return a {@code Class} describing the array type
4080      * @since 12
4081      */
4082     @Override
4083     public Class<?> arrayType() {
4084         return Array.newInstance(this, 0).getClass();
4085     }
4086 
4087     /**
4088      * Return a nominal descriptor for this instance, if one can be
4089      * constructed, or an empty {@link Optional} if one cannot be.
4090      *
4091      * @return An {@link Optional} containing the resulting nominal descriptor,
4092      * or an empty {@link Optional} if one cannot be constructed.
4093      * @since 12
4094      */
4095     @Override
4096     public Optional<ClassDesc> describeConstable() {
4097         return Optional.of(ClassDesc.ofDescriptor(descriptorString()));
4098     }
4099 }
< prev index next >