< prev index next >

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

Print this page




3831      * value is an array containing objects representing the uses of interface
3832      * types to specify interfaces directly extended by the interface. The
3833      * order of the objects in the array corresponds to the order of the
3834      * interface types used in the 'extends' clause of the declaration of this
3835      * {@code Class} object.
3836      *
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 }


3831      * value is an array containing objects representing the uses of interface
3832      * types to specify interfaces directly extended by the interface. The
3833      * order of the objects in the array corresponds to the order of the
3834      * interface types used in the 'extends' clause of the declaration of this
3835      * {@code Class} object.
3836      *
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         }
3944     }
3945 
3946     private native Class<?>[] getNestMembers0();
3947 
3948     /**
3949      * Returns an array containing {@code Class} objects representing all the
3950      * classes and interfaces that are declared in the
3951      * {@linkplain #getNestHost() nest host} of this class, as being members
3952      * of its nest. The nest host will always be the zeroth element.
3953      *
3954      * <p>Each listed nest member must be validated by checking its own
3955      * declared nest host. Any exceptions that occur as part of this process
3956      * will be thrown.
3957      *
3958      * <p>The list of nest members is permitted to contain duplicates, or to
3959      * explicitly include the nest host, as specified in the Java Virtual Machine
3960      * Specification. It is not required that an implementation of this method
3961      * removes these duplicates.
3962      *
3963      * @implNote This implementation does not remove duplicate nest members if they
3964      * are present.
3965      *
3966      * @return an array of all classes and interfaces in the same nest as
3967      * this class
3968      *
3969      * @throws LinkageError
3970      *         If there is any problem loading or validating a nest member or
3971      *         its nest host
3972      * @throws SecurityException
3973      *         If any returned class is not the current class, and
3974      *         if a security manager, <i>s</i>, is present and the caller's
3975      *         class loader is not the same as or an ancestor of the class
3976      *         loader for that returned class and invocation of {@link
3977      *         SecurityManager#checkPackageAccess s.checkPackageAccess()}
3978      *         denies access to the package of that returned class
3979      *
3980      * @since 11
3981      * @jvms 4.7.29 NestMembers attribute
3982      */
3983     @CallerSensitive
3984     public Class<?>[] getNestMembers() {
3985         if (isPrimitive() || isArray()) {
3986             return new Class<?>[] { this };
3987         }
3988         Class<?>[] members = getNestMembers0();
3989         // Can't actually enable this due to bootstrapping issues
3990         // assert(members.length != 1 || members[0] == this); // expected invariant from VM
3991 
3992         if (members.length > 1) {
3993             // If we return anything other than the current class we need
3994             // a security check
3995             SecurityManager sm = System.getSecurityManager();
3996             if (sm != null) {
3997                 checkPackageAccess(sm,
3998                                    ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
3999             }
4000         }
4001         return members;
4002     }
4003 }
< prev index next >