1 /*
2 * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
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 }
|
1 /*
2 * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
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 * @throws SecurityException
3859 * If a security manager, <i>s</i>, is present and the caller's
3860 * class loader is not the same as or an ancestor of the class
3861 * loader for the current class and invocation of {@link
3862 * SecurityManager#checkPackageAccess s.checkPackageAccess()}
3863 * denies access to the package of the current class
3864 * @since 11
3865 */
3866 @CallerSensitive
3867 public Class<?> getNestHost() {
3868 if (isPrimitive() || isArray()) {
3869 return this;
3870 }
3871 Class<?> host;
3872 try {
3873 host = getNestHost0();
3874 } catch (LinkageError e) {
3875 // if we couldn't load our nest-host then we
3876 // act as-if we have no nest-host
3877 return this;
3878 }
3879 // if null then nest membership validation failed, so we
3880 // act as-if we have no nest-host
3881 if (host == null || host == this) {
3882 return this;
3883 }
3884 // returning a different class requires a security check
3885 SecurityManager sm = System.getSecurityManager();
3886 if (sm != null) {
3887 checkPackageAccess(sm,
3888 ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
3889 }
3890 return host;
3891 }
3892
3893 /**
3894 * Determines if the given {@code Class} is a nestmate of the
3895 * object represented by this {@code Class}. Two classes are nestmates
3896 * if they have the same {@linkplain #getNestHost nest host}.
3897 *
3898 * @param c the class to check
3899 * @return {@code true} if this class and {@code c} are valid members of the same
3900 * nest; and {@code false} otherwise.
3901 *
3902 * @since 11
3903 */
3904 public boolean isNestmateOf(Class<?> c) {
3905 if (this == c) {
3906 return true;
3907 }
3908 if (isPrimitive() || isArray() ||
3909 c.isPrimitive() || c.isArray()) {
3910 return false;
3911 }
3912 try {
3913 return getNestHost0() == c.getNestHost0();
3914 } catch (LinkageError e) {
3915 return false;
3916 }
3917 }
3918
3919 private native Class<?>[] getNestMembers0();
3920
3921 /**
3922 * Returns an array containing {@code Class} objects representing all the
3923 * classes and interfaces that are declared in the
3924 * {@linkplain #getNestHost() nest host} of this class, as being members
3925 * of its nest. The nest host will always be the zeroeth element.
3926 *
3927 * <p>Each listed nest member must be validated by checking its own
3928 * declared nest host. Any exceptions that occur as part of this process
3929 * will be thrown.
3930 *
3931 * <p>The list of nest members in the classfile is permitted to
3932 * contain duplicates, or to explicitly include the nest host. It is not
3933 * required that an implementation of this method removes these duplicates.
3934 *
3935 * @implNote This implementation does not remove duplicate nest members if they
3936 * are present.
3937 *
3938 * @return an array of all classes and interfaces in the same nest as
3939 * this class
3940 *
3941 * @throws LinkageError
3942 * If there is any problem loading or validating a nest member or
3943 * its nest host
3944 * @throws SecurityException
3945 * If a security manager, <i>s</i>, is present and the caller's
3946 * class loader is not the same as or an ancestor of the class
3947 * loader for the current class and invocation of {@link
3948 * SecurityManager#checkPackageAccess s.checkPackageAccess()}
3949 * denies access to the package of the current class
3950 *
3951 * @since 11
3952 */
3953 @CallerSensitive
3954 public Class<?>[] getNestMembers() {
3955 if (isPrimitive() || isArray()) {
3956 return new Class<?>[] { this };
3957 }
3958 Class<?>[] members = getNestMembers0();
3959 if (members.length > 1) {
3960 // If we return anything other than the current class we need
3961 // a security check
3962 SecurityManager sm = System.getSecurityManager();
3963 if (sm != null) {
3964 checkPackageAccess(sm,
3965 ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
3966 }
3967 }
3968 return members;
3969 }
3970 }
|