80 import sun.security.util.SecurityConstants; |
80 import sun.security.util.SecurityConstants; |
81 import sun.reflect.annotation.*; |
81 import sun.reflect.annotation.*; |
82 import sun.reflect.misc.ReflectUtil; |
82 import sun.reflect.misc.ReflectUtil; |
83 |
83 |
84 /** |
84 /** |
85 * Instances of the class {@code Class} represent classes and |
85 * Instances of the class {@code Class} represent classes and interfaces |
86 * interfaces in a running Java application. An enum is a kind of |
86 * in a running Java application. An enum type is a kind of class and an |
87 * class and an annotation is a kind of interface. Every array also |
87 * annotation type is a kind of interface. Every array also |
88 * belongs to a class that is reflected as a {@code Class} object |
88 * belongs to a class that is reflected as a {@code Class} object |
89 * that is shared by all arrays with the same element type and number |
89 * that is shared by all arrays with the same element type and number |
90 * of dimensions. The primitive Java types ({@code boolean}, |
90 * of dimensions. The primitive Java types ({@code boolean}, |
91 * {@code byte}, {@code char}, {@code short}, |
91 * {@code byte}, {@code char}, {@code short}, |
92 * {@code int}, {@code long}, {@code float}, and |
92 * {@code int}, {@code long}, {@code float}, and |
93 * {@code double}), and the keyword {@code void} are also |
93 * {@code double}), and the keyword {@code void} are also |
94 * represented as {@code Class} objects. |
94 * represented as {@code Class} objects. |
95 * |
95 * |
96 * <p> {@code Class} has no public constructor. Instead {@code Class} |
96 * <p> {@code Class} has no public constructor. Instead a {@code Class} |
97 * objects are constructed automatically by the Java Virtual Machine as classes |
97 * object is constructed automatically by the Java Virtual Machine |
98 * are loaded and by calls to the {@code defineClass} method in the class |
98 * when a class loader invokes one of the |
99 * loader. |
99 * {@link ClassLoader#defineClass(String,byte[], int,int) defineClass} methods |
|
100 * and passes the bytes of a {@code class} file. |
|
101 * |
|
102 * <p> The methods of class {@code Class} expose many characteristics of a |
|
103 * class or interface. Most characteristics are derived from the {@code class} |
|
104 * file that the class loader passed to the Java Virtual Machine. A few |
|
105 * characteristics are determined by the class loading environment at run time, |
|
106 * such as the module returned by {@link #getModule() getModule()}. |
|
107 * |
|
108 * <p> Some methods of class {@code Class} expose whether the declaration of |
|
109 * a class or interface in Java source code was <em>enclosed</em> within |
|
110 * another declaration. Other methods describe how a class or interface |
|
111 * is situated in a <em>nest</em>. A <a id="nest">nest</a> is a set of |
|
112 * classes and interfaces, in the same run-time package, that |
|
113 * allow mutual access to their {@code private} members. |
|
114 * The classes and interfaces are known as <em>nestmates</em>. |
|
115 * One nestmate acts as the |
|
116 * <em>nest host</em>, and enumerates the other nestmates which |
|
117 * belong to the nest; each of them in turn records it as the nest host. |
|
118 * The classes and interfaces which belong to a nest, including its host, are |
|
119 * determined when |
|
120 * {@code class} files are generated, for example, a Java compiler |
|
121 * will typically record a top-level class as the host of a nest where the |
|
122 * other members are the classes and interfaces whose declarations are |
|
123 * enclosed within the top-level class declaration. |
100 * |
124 * |
101 * <p> The following example uses a {@code Class} object to print the |
125 * <p> The following example uses a {@code Class} object to print the |
102 * class name of an object: |
126 * class name of an object: |
103 * |
127 * |
104 * <blockquote><pre> |
128 * <blockquote><pre> |
3846 * @since 1.8 |
3870 * @since 1.8 |
3847 */ |
3871 */ |
3848 public AnnotatedType[] getAnnotatedInterfaces() { |
3872 public AnnotatedType[] getAnnotatedInterfaces() { |
3849 return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this); |
3873 return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this); |
3850 } |
3874 } |
|
3875 |
|
3876 private native Class<?> getNestHost0(); |
|
3877 |
|
3878 /** |
|
3879 * Returns the nest host of the <a href=#nest>nest</a> to which the class |
|
3880 * or interface represented by this {@code Class} object belongs. |
|
3881 * Every class and interface is a member of exactly one nest. |
|
3882 * A class or interface that is not recorded as belonging to a nest |
|
3883 * belongs to the nest consisting only of itself, and is the nest |
|
3884 * host. |
|
3885 * |
|
3886 * <p>Each of the {@code Class} objects representing array types, |
|
3887 * primitive types, and {@code void} returns {@code this} to indicate |
|
3888 * that the represented entity belongs to the nest consisting only of |
|
3889 * itself, and is the nest host. |
|
3890 * |
|
3891 * <p>If there is a {@linkplain LinkageError linkage error} accessing |
|
3892 * the nest host, or if this class or interface is not enumerated as |
|
3893 * a member of the nest by the nest host, then it is considered to belong |
|
3894 * to its own nest and {@code this} is returned as the host. |
|
3895 * |
|
3896 * @apiNote A {@code class} file of version 55.0 or greater may record the |
|
3897 * host of the nest to which it belongs by using the {@code NestHost} |
|
3898 * attribute (JVMS 4.7.28). Alternatively, a {@code class} file of |
|
3899 * version 55.0 or greater may act as a nest host by enumerating the nest's |
|
3900 * other members with the |
|
3901 * {@code NestMembers} attribute (JVMS 4.7.29). |
|
3902 * A {@code class} file of version 54.0 or lower does not use these |
|
3903 * attributes. |
|
3904 * |
|
3905 * @return the nest host of this class or interface |
|
3906 * |
|
3907 * @throws SecurityException |
|
3908 * If the returned class is not the current class, and |
|
3909 * if a security manager, <i>s</i>, is present and the caller's |
|
3910 * class loader is not the same as or an ancestor of the class |
|
3911 * loader for the returned class and invocation of {@link |
|
3912 * SecurityManager#checkPackageAccess s.checkPackageAccess()} |
|
3913 * denies access to the package of the returned class |
|
3914 * @since 11 |
|
3915 * @jvms 4.7.28 and 4.7.29 NestHost and NestMembers attributes |
|
3916 * @jvms 5.4.4 Access Control |
|
3917 */ |
|
3918 @CallerSensitive |
|
3919 public Class<?> getNestHost() { |
|
3920 if (isPrimitive() || isArray()) { |
|
3921 return this; |
|
3922 } |
|
3923 Class<?> host; |
|
3924 try { |
|
3925 host = getNestHost0(); |
|
3926 } catch (LinkageError e) { |
|
3927 // if we couldn't load our nest-host then we |
|
3928 // act as-if we have no nest-host attribute |
|
3929 return this; |
|
3930 } |
|
3931 // if null then nest membership validation failed, so we |
|
3932 // act as-if we have no nest-host attribute |
|
3933 if (host == null || host == this) { |
|
3934 return this; |
|
3935 } |
|
3936 // returning a different class requires a security check |
|
3937 SecurityManager sm = System.getSecurityManager(); |
|
3938 if (sm != null) { |
|
3939 checkPackageAccess(sm, |
|
3940 ClassLoader.getClassLoader(Reflection.getCallerClass()), true); |
|
3941 } |
|
3942 return host; |
|
3943 } |
|
3944 |
|
3945 /** |
|
3946 * Determines if the given {@code Class} is a nestmate of the |
|
3947 * class or interface represented by this {@code Class} object. |
|
3948 * Two classes or interfaces are nestmates |
|
3949 * if they have the same {@linkplain #getNestHost() nest host}. |
|
3950 * |
|
3951 * @param c the class to check |
|
3952 * @return {@code true} if this class and {@code c} are members of |
|
3953 * the same nest; and {@code false} otherwise. |
|
3954 * |
|
3955 * @since 11 |
|
3956 */ |
|
3957 public boolean isNestmateOf(Class<?> c) { |
|
3958 if (this == c) { |
|
3959 return true; |
|
3960 } |
|
3961 if (isPrimitive() || isArray() || |
|
3962 c.isPrimitive() || c.isArray()) { |
|
3963 return false; |
|
3964 } |
|
3965 try { |
|
3966 return getNestHost0() == c.getNestHost0(); |
|
3967 } catch (LinkageError e) { |
|
3968 return false; |
|
3969 } |
|
3970 } |
|
3971 |
|
3972 private native Class<?>[] getNestMembers0(); |
|
3973 |
|
3974 /** |
|
3975 * Returns an array containing {@code Class} objects representing all the |
|
3976 * classes and interfaces that are members of the nest to which the class |
|
3977 * or interface represented by this {@code Class} object belongs. |
|
3978 * The {@linkplain #getNestHost() nest host} of that nest is the zeroth |
|
3979 * element of the array. Subsequent elements represent any classes or |
|
3980 * interfaces that are recorded by the nest host as being members of |
|
3981 * the nest; the order of such elements is unspecified. Duplicates are |
|
3982 * permitted. |
|
3983 * If the nest host of that nest does not enumerate any members, then the |
|
3984 * array has a single element containing {@code this}. |
|
3985 * |
|
3986 * <p>Each of the {@code Class} objects representing array types, |
|
3987 * primitive types, and {@code void} returns an array containing only |
|
3988 * {@code this}. |
|
3989 * |
|
3990 * <p>This method validates that, for each class or interface which is |
|
3991 * recorded as a member of the nest by the nest host, that class or |
|
3992 * interface records itself as a member of that same nest. Any exceptions |
|
3993 * that occur during this validation are rethrown by this method. |
|
3994 * |
|
3995 * @return an array of all classes and interfaces in the same nest as |
|
3996 * this class |
|
3997 * |
|
3998 * @throws LinkageError |
|
3999 * If there is any problem loading or validating a nest member or |
|
4000 * its nest host |
|
4001 * @throws SecurityException |
|
4002 * If any returned class is not the current class, and |
|
4003 * if a security manager, <i>s</i>, is present and the caller's |
|
4004 * class loader is not the same as or an ancestor of the class |
|
4005 * loader for that returned class and invocation of {@link |
|
4006 * SecurityManager#checkPackageAccess s.checkPackageAccess()} |
|
4007 * denies access to the package of that returned class |
|
4008 * |
|
4009 * @since 11 |
|
4010 * @see #getNestHost() |
|
4011 */ |
|
4012 @CallerSensitive |
|
4013 public Class<?>[] getNestMembers() { |
|
4014 if (isPrimitive() || isArray()) { |
|
4015 return new Class<?>[] { this }; |
|
4016 } |
|
4017 Class<?>[] members = getNestMembers0(); |
|
4018 // Can't actually enable this due to bootstrapping issues |
|
4019 // assert(members.length != 1 || members[0] == this); // expected invariant from VM |
|
4020 |
|
4021 if (members.length > 1) { |
|
4022 // If we return anything other than the current class we need |
|
4023 // a security check |
|
4024 SecurityManager sm = System.getSecurityManager(); |
|
4025 if (sm != null) { |
|
4026 checkPackageAccess(sm, |
|
4027 ClassLoader.getClassLoader(Reflection.getCallerClass()), true); |
|
4028 } |
|
4029 } |
|
4030 return members; |
|
4031 } |
3851 } |
4032 } |