< prev index next >

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

Print this page
rev 50076 : [mq]: jep181-rev2


3837      * <p> If this {@code Class} object represents a class or interface whose
3838      * declaration does not explicitly indicate any annotated superinterfaces,
3839      * the return value is an array of length 0.
3840      *
3841      * <p> If this {@code Class} object represents either the {@code Object}
3842      * class, an array type, a primitive type, or void, the return value is an
3843      * array of length 0.
3844      *
3845      * @return an array representing the superinterfaces
3846      * @since 1.8
3847      */
3848     public AnnotatedType[] getAnnotatedInterfaces() {
3849          return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
3850     }
3851 
3852     private native Class<?> getNestHost0();
3853 
3854     /**
3855      * Returns the nest host of the object represented by this {@code Class}.
3856      *
3857      * <p>If there is any error accessing the nest host, or the nest host is
3858      * in any way invalid, then {@code this} is returned.
3859      *
3860      * <p>A <em>nest</em> is a set of classes and interfaces (nestmates) that
3861      * form an access control context in which each nestmate has access to the
3862      * private members of the other nestmates.
3863      * The <em>nest host</em> is the class or interface designated to hold the list of
3864      * classes and interfaces that make up the nest, and to which each of the
3865      * other nestmates refer.

3866      *
3867      * <p>A class or interface that is not explicitly a member of a nest,

3868      * is a member of the nest consisting only of itself, and is the
3869      * nest host. Every class and interface is a member of exactly one nest.
3870      *
3871      * @apiNote The source language compiler is responsible for deciding which classes
3872      * and interfaces are nestmates, by generating the appropriate attributes
3873      * (JVMS 4.7.28 and 4.7.29) in the class file format (JVMS 4).
3874      * For example, the {@code javac} compiler
3875      * places a top-level class or interface into a nest with all of its direct,
3876      * and indirect, {@linkplain #getDeclaredClasses() nested classes and interfaces}
3877      * (JLS 8).
3878      * The top-level {@linkplain #getEnclosingClass() enclosing class or interface}
3879      * is designated as the nest host.
3880      *
3881      * @return the nest host of this class, or {@code this} if we cannot
3882      * obtain a valid nest host
3883      * @throws SecurityException
3884      *         If the returned class is not the current class, and
3885      *         if a security manager, <i>s</i>, is present and the caller's
3886      *         class loader is not the same as or an ancestor of the class
3887      *         loader for the returned class and invocation of {@link
3888      *         SecurityManager#checkPackageAccess s.checkPackageAccess()}
3889      *         denies access to the package of the returned class
3890      * @since 11
3891      * @jvms 4.7.28 and 4.7.29 NestHost and NestMembers attributes
3892      */
3893     @CallerSensitive
3894     public Class<?> getNestHost() {
3895         if (isPrimitive() || isArray()) {
3896             return this;
3897         }
3898         Class<?> host;
3899         try {
3900             host = getNestHost0();
3901         } catch (LinkageError e) {
3902             // if we couldn't load our nest-host then we
3903             // act as-if we have no nest-host
3904             return this;
3905         }
3906         // if null then nest membership validation failed, so we
3907         // act as-if we have no nest-host
3908         if (host == null || host == this) {
3909             return this;
3910         }
3911         // returning a different class requires a security check
3912         SecurityManager sm = System.getSecurityManager();
3913         if (sm != null) {
3914             checkPackageAccess(sm,
3915                                ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
3916         }
3917         return host;
3918     }
3919 
3920     /**
3921      * Determines if the given {@code Class} is a nestmate of the
3922      * object represented by this {@code Class}. Two classes are nestmates
3923      * if they have the same {@linkplain #getNestHost nest host}.
3924      *
3925      * @param  c the class to check
3926      * @return {@code true} if this class and {@code c} are valid members of the same
3927      * nest; and {@code false} otherwise.
3928      *
3929      * @since 11
3930      */
3931     public boolean isNestmateOf(Class<?> c) {
3932         if (this == c) {
3933             return true;
3934         }
3935         if (isPrimitive() || isArray() ||
3936             c.isPrimitive() || c.isArray()) {
3937             return false;
3938         }
3939         try {
3940             return getNestHost0() == c.getNestHost0();
3941         } catch (LinkageError e) {
3942             return false;
3943         }




3837      * <p> If this {@code Class} object represents a class or interface whose
3838      * declaration does not explicitly indicate any annotated superinterfaces,
3839      * the return value is an array of length 0.
3840      *
3841      * <p> If this {@code Class} object represents either the {@code Object}
3842      * class, an array type, a primitive type, or void, the return value is an
3843      * array of length 0.
3844      *
3845      * @return an array representing the superinterfaces
3846      * @since 1.8
3847      */
3848     public AnnotatedType[] getAnnotatedInterfaces() {
3849          return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
3850     }
3851 
3852     private native Class<?> getNestHost0();
3853 
3854     /**
3855      * Returns the nest host of the object represented by this {@code Class}.
3856      *
3857      * <p>If there is any {@linkplain LinkageError linkage error} accessing the nest host,
3858      * or the nest host is in any way invalid, then {@code this} is returned.
3859      *
3860      * <p>A <em>nest</em> is a set of classes and interfaces (nestmates) that
3861      * form an access control context in which each nestmate has access to the
3862      * private members of the other nestmates.
3863      * The <em>nest host</em> is the class or interface designated to hold the list of
3864      * classes and interfaces that make up the nest, and to which each of the
3865      * other nestmates refer.
3866      * All nestmates are implicitly defined in the same runtime package.
3867      *
3868      * <p>A class or interface that is not explicitly a member of a nest
3869      * (such as a primitive or array class),
3870      * is a member of the nest consisting only of itself, and is the
3871      * nest host. Every class and interface is a member of exactly one nest.
3872      *
3873      * @apiNote The source language compiler is responsible for deciding which classes
3874      * and interfaces are nestmates, by generating the appropriate attributes
3875      * (JVMS 4.7.28 and 4.7.29) in the class file format (JVMS 4).
3876      * For example, the {@code javac} compiler
3877      * places a top-level class or interface into a nest with all of its direct,
3878      * and indirect, {@linkplain #getDeclaredClasses() nested classes and interfaces}
3879      * (JLS 8).
3880      * The top-level {@linkplain #getEnclosingClass() enclosing class or interface}
3881      * is designated as the nest host.
3882      *
3883      * @return the nest host of this class, or {@code this} if a valid nest host
3884      * cannot be obtained
3885      * @throws SecurityException
3886      *         If the returned class is not the current class, and
3887      *         if a security manager, <i>s</i>, is present and the caller's
3888      *         class loader is not the same as or an ancestor of the class
3889      *         loader for the returned class and invocation of {@link
3890      *         SecurityManager#checkPackageAccess s.checkPackageAccess()}
3891      *         denies access to the package of the returned class
3892      * @since 11
3893      * @jvms 4.7.28 and 4.7.29 NestHost and NestMembers attributes
3894      */
3895     @CallerSensitive
3896     public Class<?> getNestHost() {
3897         if (isPrimitive() || isArray()) {
3898             return this;
3899         }
3900         Class<?> host;
3901         try {
3902             host = getNestHost0();
3903         } catch (LinkageError e) {
3904             // if we couldn't load our nest-host then we
3905             // act as-if we have no nest-host
3906             return this;
3907         }
3908         // if null then nest membership validation failed, so we
3909         // act as-if we have no nest-host
3910         if (host == null || host == this) {
3911             return this;
3912         }
3913         // returning a different class requires a security check
3914         SecurityManager sm = System.getSecurityManager();
3915         if (sm != null) {
3916             checkPackageAccess(sm,
3917                                ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
3918         }
3919         return host;
3920     }
3921 
3922     /**
3923      * Determines if the given {@code Class} is a nestmate of the
3924      * object represented by this {@code Class}. Two classes are nestmates
3925      * if they have the same {@linkplain #getNestHost() nest host}.
3926      *
3927      * @param  c the class to check
3928      * @return {@code true} if this class and {@code c} are valid members of the same
3929      * nest; and {@code false} otherwise.
3930      *
3931      * @since 11
3932      */
3933     public boolean isNestmateOf(Class<?> c) {
3934         if (this == c) {
3935             return true;
3936         }
3937         if (isPrimitive() || isArray() ||
3938             c.isPrimitive() || c.isArray()) {
3939             return false;
3940         }
3941         try {
3942             return getNestHost0() == c.getNestHost0();
3943         } catch (LinkageError e) {
3944             return false;
3945         }


< prev index next >