4004 * <p> If this {@code Class} object represents a class or interface whose
4005 * declaration does not explicitly indicate any annotated superinterfaces,
4006 * the return value is an array of length 0.
4007 *
4008 * <p> If this {@code Class} object represents either the {@code Object}
4009 * class, an array type, a primitive type, or void, the return value is an
4010 * array of length 0.
4011 *
4012 * @return an array representing the superinterfaces
4013 * @since 1.8
4014 */
4015 public AnnotatedType[] getAnnotatedInterfaces() {
4016 return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
4017 }
4018
4019 private native Class<?> getNestHost0();
4020
4021 /**
4022 * Returns the nest host of the <a href=#nest>nest</a> to which the class
4023 * or interface represented by this {@code Class} object belongs.
4024 * Every class and interface is a member of exactly one nest.
4025 * A class or interface that is not recorded as belonging to a nest
4026 * belongs to the nest consisting only of itself, and is the nest
4027 * host.
4028 *
4029 * <p>Each of the {@code Class} objects representing array types,
4030 * primitive types, and {@code void} returns {@code this} to indicate
4031 * that the represented entity belongs to the nest consisting only of
4032 * itself, and is the nest host.
4033 *
4034 * <p>If there is a {@linkplain LinkageError linkage error} accessing
4035 * the nest host, or if this class or interface is not enumerated as
4036 * a member of the nest by the nest host, then it is considered to belong
4037 * to its own nest and {@code this} is returned as the host.
4038 *
4039 * <p>If this class is a {@linkplain Class#isHiddenClass() hidden class}
4040 * created by calling
4041 * {@link MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)
4042 * Lookup::defineHiddenClass} with {@link MethodHandles.Lookup.ClassOption#NESTMATE
4043 * NESTMATE} option, then the hidden class is added as a member to
4044 * the nest of a {@linkplain MethodHandles.Lookup#lookupClass() lookup class}
4045 * dynamically and it has the same nest host as the lookup class.
4046 *
4047 * @apiNote A {@code class} file of version 55.0 or greater may record the
4048 * host of the nest to which it belongs by using the {@code NestHost}
4049 * attribute (JVMS 4.7.28). Alternatively, a {@code class} file of
4050 * version 55.0 or greater may act as a nest host by enumerating the nest's
4051 * other members with the
4052 * {@code NestMembers} attribute (JVMS 4.7.29).
4053 * A {@code class} file of version 54.0 or lower does not use these
4054 * attributes.
4055 *
4056 * @return the nest host of this class or interface
4057 *
4058 * @throws SecurityException
4059 * If the returned class is not the current class, and
4060 * if a security manager, <i>s</i>, is present and the caller's
4061 * class loader is not the same as or an ancestor of the class
4062 * loader for the returned class and invocation of {@link
4063 * SecurityManager#checkPackageAccess s.checkPackageAccess()}
4064 * denies access to the package of the returned class
4065 * @since 11
4066 * @jvms 4.7.28 The {@code NestHost} Attribute
4067 * @jvms 4.7.29 The {@code NestMembers} Attribute
4068 * @jvms 5.4.4 Access Control
4069 */
4070 @CallerSensitive
4071 public Class<?> getNestHost() {
4072 if (isPrimitive() || isArray()) {
4073 return this;
4074 }
4075
4076 Class<?> host = this.nest;
4077 if (host == null) {
4078 host = getNestHost0();
4079 // if null then nest membership validation failed, so we
4080 // act as-if we have no nest-host attribute
4081 if (host == null || host == this) {
4082 return this.nest = this;
4083 }
4084 this.nest = host;
4085 }
4086 // returning a different class requires a security check
4087 SecurityManager sm = System.getSecurityManager();
4088 if (sm != null) {
4089 checkPackageAccess(sm,
4090 ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
4091 }
4092 return host;
4093 }
4094
4095 // keep a strong reference to the nest host
4096 private transient Class<?> nest;
4097
4098 /**
4099 * Determines if the given {@code Class} is a nestmate of the
4100 * class or interface represented by this {@code Class} object.
4101 * Two classes or interfaces are nestmates
4102 * if they have the same {@linkplain #getNestHost() nest host}.
4103 *
4104 * @param c the class to check
4105 * @return {@code true} if this class and {@code c} are members of
4106 * the same nest; and {@code false} otherwise.
4107 *
4108 * @since 11
4109 */
4110 public boolean isNestmateOf(Class<?> c) {
4111 if (this == c) {
4112 return true;
4113 }
4114 if (isPrimitive() || isArray() ||
4115 c.isPrimitive() || c.isArray()) {
4116 return false;
4117 }
4118
4119 return getNestHost() == c.getNestHost();
4120 }
4121
4122 private native Class<?>[] getNestMembers0();
4123
4124 /**
4125 * Returns an array containing {@code Class} objects representing all the
4126 * classes and interfaces that are members of the nest to which the class
4127 * or interface represented by this {@code Class} object belongs.
4128 * The {@linkplain #getNestHost() nest host} of that nest is the zeroth
4129 * element of the array. Subsequent elements represent any classes or
4130 * interfaces that are recorded by the nest host as being members of
4131 * the nest; the order of such elements is unspecified. Duplicates are
4132 * permitted.
4133 * If the nest host of that nest does not enumerate any members, then the
4134 * array has a single element containing {@code this}.
4135 *
4136 * <p>Each of the {@code Class} objects representing array types,
4137 * primitive types, and {@code void} returns an array containing only
4138 * {@code this}.
4139 *
4140 * <p>This method validates that, for each class or interface which is
4141 * recorded as a member of the nest by the nest host, that class or
4142 * interface records itself as a member of that same nest. Any exceptions
4143 * that occur during this validation are rethrown by this method.
4144 *
4145 * @apiNote
4146 * This method returns the nest members listed in the {@code NestMembers}
4147 * attribute. The returned array does not include any hidden class that
4148 * were added to the nest of this class via
4149 * {@link MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)
4150 * Lookup::defineHiddenClass}.
4151 *
4152 * @return an array of all classes and interfaces in the same nest as
4153 * this class
4154 *
4155 * @throws LinkageError
4156 * If there is any problem loading or validating a nest member or
4157 * its nest host
4158 * @throws SecurityException
4159 * If any returned class is not the current class, and
4160 * if a security manager, <i>s</i>, is present and the caller's
4161 * class loader is not the same as or an ancestor of the class
4162 * loader for that returned class and invocation of {@link
4163 * SecurityManager#checkPackageAccess s.checkPackageAccess()}
4164 * denies access to the package of that returned class
4165 *
4166 * @since 11
4167 * @see #getNestHost()
4168 */
4169 @CallerSensitive
4170 public Class<?>[] getNestMembers() {
4171 if (isPrimitive() || isArray()) {
4172 return new Class<?>[] { this };
4173 }
4174 Class<?>[] members = getNestMembers0();
4175 // Can't actually enable this due to bootstrapping issues
4176 // assert(members.length != 1 || members[0] == this); // expected invariant from VM
4177
4178 if (members.length > 1) {
4179 // If we return anything other than the current class we need
4180 // a security check
4181 SecurityManager sm = System.getSecurityManager();
4182 if (sm != null) {
4183 checkPackageAccess(sm,
4184 ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
4185 }
4186 }
4187 return members;
|
4004 * <p> If this {@code Class} object represents a class or interface whose
4005 * declaration does not explicitly indicate any annotated superinterfaces,
4006 * the return value is an array of length 0.
4007 *
4008 * <p> If this {@code Class} object represents either the {@code Object}
4009 * class, an array type, a primitive type, or void, the return value is an
4010 * array of length 0.
4011 *
4012 * @return an array representing the superinterfaces
4013 * @since 1.8
4014 */
4015 public AnnotatedType[] getAnnotatedInterfaces() {
4016 return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
4017 }
4018
4019 private native Class<?> getNestHost0();
4020
4021 /**
4022 * Returns the nest host of the <a href=#nest>nest</a> to which the class
4023 * or interface represented by this {@code Class} object belongs.
4024 * Every class and interface belongs to exactly one nest.
4025 *
4026 * If the nest host of this class or interface has previously
4027 * been determined, then this method returns the nest host.
4028 * If the nest host of this class or interface has
4029 * not previously been determined, then this method determines the nest
4030 * host using the algorithm of JVMS 5.4.4, and returns it.
4031 *
4032 * Often, a class or interface belongs to a nest consisting only of itself,
4033 * in which case this method returns {@code this} to indicate that the class
4034 * or interface is the nest host.
4035 *
4036 * <p>If this {@code Class} object represents a primitive type, an array type,
4037 * or {@code void}, then this method returns {@code this},
4038 * indicating that the represented entity belongs to the nest consisting only of
4039 * itself, and is the nest host.
4040 *
4041 * @return the nest host of this class or interface
4042 *
4043 * @throws SecurityException
4044 * If the returned class is not the current class, and
4045 * if a security manager, <i>s</i>, is present and the caller's
4046 * class loader is not the same as or an ancestor of the class
4047 * loader for the returned class and invocation of {@link
4048 * SecurityManager#checkPackageAccess s.checkPackageAccess()}
4049 * denies access to the package of the returned class
4050 * @since 11
4051 * @jvms 4.7.28 The {@code NestHost} Attribute
4052 * @jvms 4.7.29 The {@code NestMembers} Attribute
4053 * @jvms 5.4.4 Access Control
4054 */
4055 @CallerSensitive
4056 public Class<?> getNestHost() {
4057 if (isPrimitive() || isArray()) {
4058 return this;
4059 }
4060
4061 Class<?> host = this.nest;
4062 if (host == null) {
4063 host = getNestHost0();
4064 if (host == this) {
4065 return this.nest = this;
4066 }
4067 }
4068 // returning a different class requires a security check
4069 SecurityManager sm = System.getSecurityManager();
4070 if (sm != null) {
4071 checkPackageAccess(sm,
4072 ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
4073 }
4074 return (this.nest = host);
4075 }
4076
4077 // keep a strong reference to the nest host
4078 private transient Class<?> nest;
4079
4080 /**
4081 * Determines if the given {@code Class} is a nestmate of the
4082 * class or interface represented by this {@code Class} object.
4083 * Two classes or interfaces are nestmates
4084 * if they have the same {@linkplain #getNestHost() nest host}.
4085 *
4086 * @param c the class to check
4087 * @return {@code true} if this class and {@code c} are members of
4088 * the same nest; and {@code false} otherwise.
4089 *
4090 * @since 11
4091 */
4092 public boolean isNestmateOf(Class<?> c) {
4093 if (this == c) {
4094 return true;
4095 }
4096 if (isPrimitive() || isArray() ||
4097 c.isPrimitive() || c.isArray()) {
4098 return false;
4099 }
4100
4101 return getNestHost() == c.getNestHost();
4102 }
4103
4104 private native Class<?>[] getNestMembers0();
4105
4106 /**
4107 * Returns an array containing {@code Class} objects representing all the
4108 * classes and interfaces that are members of the nest to which the class
4109 * or interface represented by this {@code Class} object belongs.
4110 *
4111 * First, this method obtains the {@linkplain #getNestHost() nest host}, {@code H}, of the nest
4112 * to which the class or interface represented by this {@code Class} object belongs.
4113 * The zeroth element of the returned array is {@code H}.
4114 *
4115 * Then, for each class or interface {@code C} which is recorded by {@code H} as being a member
4116 * of its nest, this method attempts to obtain the {@code Class} object for {@code C}
4117 * (using {@linkplain #getClassLoader() the defining class loader} of the current {@code Class} object),
4118 * and then obtains the {@linkplain #getNestHost() nest host} of the nest to which {@code C} belongs.
4119 * The classes and interfaces which are recorded by {@code H} as being members of its nest,
4120 * and for which {@code H} can be determined as their nest host, are indicated by subsequent elements
4121 * of the returned array. The order of such elements is unspecified.
4122 * Duplicates are permitted.
4123 *
4124 * <p>If this {@code Class} object represents a primitive type, an array type,
4125 * or {@code void}, then this method returns a single-element array containing
4126 * {@code this}.
4127 *
4128 * @apiNote
4129 * The returned array includes only the nest members recorded in the {@code NestMembers}
4130 * attribute, and not any hidden classes that were added to the nest via
4131 * {@link MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)
4132 * Lookup::defineHiddenClass}.
4133 *
4134 * @return an array of all classes and interfaces in the same nest as
4135 * this class
4136 *
4137 * @throws SecurityException
4138 * If any returned class is not the current class, and
4139 * if a security manager, <i>s</i>, is present and the caller's
4140 * class loader is not the same as or an ancestor of the class
4141 * loader for that returned class and invocation of {@link
4142 * SecurityManager#checkPackageAccess s.checkPackageAccess()}
4143 * denies access to the package of that returned class
4144 *
4145 * @since 11
4146 * @see #getNestHost()
4147 * @jvms 4.7.28 The {@code NestHost} Attribute
4148 * @jvms 4.7.29 The {@code NestMembers} Attribute
4149 */
4150 @CallerSensitive
4151 public Class<?>[] getNestMembers() {
4152 if (isPrimitive() || isArray()) {
4153 return new Class<?>[] { this };
4154 }
4155 Class<?>[] members = getNestMembers0();
4156 // Can't actually enable this due to bootstrapping issues
4157 // assert(members.length != 1 || members[0] == this); // expected invariant from VM
4158
4159 if (members.length > 1) {
4160 // If we return anything other than the current class we need
4161 // a security check
4162 SecurityManager sm = System.getSecurityManager();
4163 if (sm != null) {
4164 checkPackageAccess(sm,
4165 ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
4166 }
4167 }
4168 return members;
|