< prev index next >

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

Print this page




3813      * <p> If this {@code Class} object represents a class or interface whose
3814      * declaration does not explicitly indicate any annotated superinterfaces,
3815      * the return value is an array of length 0.
3816      *
3817      * <p> If this {@code Class} object represents either the {@code Object}
3818      * class, an array type, a primitive type, or void, the return value is an
3819      * array of length 0.
3820      *
3821      * @return an array representing the superinterfaces
3822      * @since 1.8
3823      */
3824     public AnnotatedType[] getAnnotatedInterfaces() {
3825          return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
3826     }
3827 
3828     private native Class<?> getNestHost0();
3829 
3830     /**
3831      * Returns the nest host of the object represented by this {@code Class}.
3832      *
3833      * <p>If there is any error accessing the nest host, or the nest host is
3834      * in any way invalid, then {@code this} is returned.
3835      *
3836      * @apiNote A nest is a set of classes and interfaces (nestmates) that
3837      * form an access control context in which each nestmate has access to the
3838      * private members of the other nestmates (JVMS 4.7.28).
3839      * Nest membership is declared through special attributes in the binary
3840      * representation of a class or interface (JVMS 4.1).

3841      * The nest host is the class or interface designated to hold the list of
3842      * classes and interfaces that make up the nest, and to which each of the
3843      * other nestmates refer.
3844      * The source language compiler is responsible for deciding which classes
3845      * and interfaces are nestmates. For example, the {@code javac} compiler
3846      * places a top-level class or interface into a nest with all of its direct,
3847      * and indirect, {@linkplain #getDeclaredClasses() nested classes and interfaces}
3848      * (JLS 8).
3849      * The top-level {@linkplain #getEnclosingClass() enclosing class or interface}
3850      * is designated as the nest host.
3851      *
3852      * <p>A class or interface that is not explicitly a member of a nest,
3853      * is a member of the nest consisting only of itself, and is the
3854      * nest host. Every class and interface is a member of exactly one nest.
3855      *
3856      * @return the nest host of this class, or {@code this} if we cannot
3857      * obtain a valid nest host
3858      *








3859      * @since 11

3860      */
3861     public Class<?> getNestHost() {
3862         if (isPrimitive() || isArray()) {
3863             return this;
3864         }
3865         try {
3866             Class<?> host = getNestHost0();
3867             // if null then nest membership validation failed, so we
3868             // act as-if we have no nest-host
3869             if (host == null) {
3870                 host = this;
3871             }
3872             return host;
3873         } catch (LinkageError e) {
3874             // if we couldn't load our nest-host then we
3875             // again act as-if we have no nest-host
3876             return this;
3877         }
3878     }
3879 
3880     /**
3881      * Determines if the given {@code Class} is a nestmate of the
3882      * object represented by this {@code Class}. Two classes are nestmates
3883      * if they have the same {@linkplain #getNestHost nest host}.
3884      *
3885      * @param  c the class to check
3886      * @return {@code true} if this class and {@code c} are valid members of the same
3887      * nest; and {@code false} otherwise.
3888      *
3889      * @since 11
3890      */
3891     public boolean isNestmateOf(Class<?> c) {
3892         // We could use Reflection.areNestmates(this, c) and ignore
3893         // any IllegalAccessError, but prefer to minimize exception
3894         // creation by using getNestHost() directly.
3895         return getNestHost() == c.getNestHost();
3896     }
3897 
3898     private native Class<?>[] getNestMembers0();
3899 
3900     /**
3901      * Returns an array containing {@code Class} objects representing all the
3902      * classes and interfaces that are declared in the
3903      * {@linkplain #getNestHost() nest host} of this class, as being members
3904      * of its nest. The nest host will always be the zeroeth element.
3905      *
3906      * <p>Each listed nest member must be validated by checking its own
3907      * declared nest host. Any exceptions that occur as part of this process
3908      * will be thrown.
3909      *
3910      * <p>The list of nest members in the classfile is permitted to
3911      * contain duplicates, or to explicitly include the nest host. It is not
3912      * required that an implementation of this method removes these duplicates.
3913      *
3914      * @implNote This implementation does not remove duplicate nest members if they
3915      * are present.
3916      *
3917      * @return an array of all classes and interfaces in the same nest as
3918      * this class
3919      *
3920      * @throws LinkageError if there is any problem loading or validating
3921      * a nest member or its nest host







3922      *
3923      * @since 11

3924      */
3925     public Class<?>[] getNestMembers() {
3926         if (isPrimitive() || isArray()) {
3927             return new Class<?>[] { this };
3928         }
3929         return getNestMembers0();
3930     }
3931 }


3813      * <p> If this {@code Class} object represents a class or interface whose
3814      * declaration does not explicitly indicate any annotated superinterfaces,
3815      * the return value is an array of length 0.
3816      *
3817      * <p> If this {@code Class} object represents either the {@code Object}
3818      * class, an array type, a primitive type, or void, the return value is an
3819      * array of length 0.
3820      *
3821      * @return an array representing the superinterfaces
3822      * @since 1.8
3823      */
3824     public AnnotatedType[] getAnnotatedInterfaces() {
3825          return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
3826     }
3827 
3828     private native Class<?> getNestHost0();
3829 
3830     /**
3831      * Returns the nest host of the object represented by this {@code Class}.
3832      *
3833      * If there is any error accessing the nest host, or the nest host is
3834      * in any way invalid, then {@code this} is returned.
3835      *
3836      * <p>
3837      * <a id="nestmates">Nest Membership</a>
3838      * A <em>nest</em> is a set of classes and interfaces
3839      * ({@linkplain #getNestMembers() nestmates}) that
3840      * form an access control context in which each nestmate has access to
3841      * the private members of the other nestmates (JVMS 4.7.28).
3842      * The nest host is the class or interface designated to hold the list of
3843      * classes and interfaces that make up the nest, and to which each of the
3844      * other nestmates refer.
3845      * A {@linkplain #getEnclosingClass() top-level class or interface} is
3846      * typically placed into a nest with all of its direct and indirect
3847      * {@linkplain #getDeclaredClasses() nested classes and interfaces}.
3848      * The top-level class or interface is designated as the nest host.



3849      *
3850      * <p>A class or interface that is not explicitly a member of a nest,
3851      * is a member of the nest consisting only of itself, and is the
3852      * nest host. Every class and interface is a member of exactly one nest.
3853      *
3854      * @return the nest host of this class, or {@code this} if we cannot
3855      * obtain a valid nest host
3856      *
3857      * @throws SecurityException
3858      *         If a security manager, <i>s</i>, is present and
3859      *         the caller's class loader is not the same as or an
3860      *         ancestor of the class loader for the current class and
3861      *         invocation of {@link SecurityManager#checkPackageAccess
3862      *         s.checkPackageAccess()} denies access to the package
3863      *         of this class
3864      *
3865      * @since 11
3866      * @jvms 4.7.28 and 4.7.29 NestHost and NestMembers attributes
3867      */
3868     public Class<?> getNestHost() {
3869         if (isPrimitive() || isArray()) {
3870             return this;
3871         }
3872         try {
3873             Class<?> host = getNestHost0();
3874             // if null then nest membership validation failed, so we
3875             // act as-if we have no nest-host
3876             if (host == null) {
3877                 host = this;
3878             }
3879             return host;
3880         } catch (LinkageError e) {
3881             // if we couldn't load our nest-host then we
3882             // again act as-if we have no nest-host
3883             return this;
3884         }
3885     }
3886 
3887     /**
3888      * Determines if the given {@code Class} is a <a href="#nestmates">nestmate</a>
3889      * of the object represented by this {@code Class}. Two classes are nestmates
3890      * if they have the same {@linkplain #getNestHost nest host}.
3891      *
3892      * @param  c the class to check
3893      * @return {@code true} if this class and {@code c} are valid members of the same
3894      * nest; and {@code false} otherwise.
3895      *
3896      * @since 11
3897      */
3898     public boolean isNestmateOf(Class<?> c) {
3899         // We could use Reflection.areNestmates(this, c) and ignore
3900         // any IllegalAccessError, but prefer to minimize exception
3901         // creation by using getNestHost() directly.
3902         return getNestHost() == c.getNestHost();
3903     }
3904 
3905     private native Class<?>[] getNestMembers0();
3906 
3907     /**
3908      * Returns an array containing {@code Class} objects representing all the
3909      * classes and interfaces that are declared in the
3910      * {@linkplain #getNestHost() nest host} of this class, as being members
3911      * of its nest. The nest host will always be the zeroeth element.
3912      *
3913      * <p>Each listed nest member must be validated by checking its own
3914      * declared nest host. Any exceptions that occur as part of this process
3915      * will be thrown.
3916      *
3917      * <p>The list of nest members in the classfile is permitted to
3918      * contain duplicates, or to explicitly include the nest host. It is not
3919      * required that an implementation of this method removes these duplicates.
3920      *
3921      * @implNote This implementation does not remove duplicate nest members if they
3922      * are present.
3923      *
3924      * @return an array of all classes and interfaces in the same nest as
3925      * this class
3926      *
3927      * @throws LinkageError if there is any problem loading or validating
3928      * a nest member or its nest host
3929      * @throws SecurityException
3930      *         If a security manager, <i>s</i>, is present and
3931      *         the caller's class loader is not the same as or an
3932      *         ancestor of the class loader for the current class and
3933      *         invocation of {@link SecurityManager#checkPackageAccess
3934      *         s.checkPackageAccess()} denies access to the package
3935      *         of this class
3936      *
3937      * @since 11
3938      * @jvms 4.7.29 NestMembers attribute
3939      */
3940     public Class<?>[] getNestMembers() {
3941         if (isPrimitive() || isArray()) {
3942             return new Class<?>[] { this };
3943         }
3944         return getNestMembers0();
3945     }
3946 }
< prev index next >