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 the returned class is not the current class, and
3860 * if a security manager, <i>s</i>, is present and the caller's
3861 * class loader is not the same as or an ancestor of the class
3862 * loader for the returned class and invocation of {@link
3863 * SecurityManager#checkPackageAccess s.checkPackageAccess()}
3864 * denies access to the package of the returned class
3865 * @since 11
3866 */
3867 @CallerSensitive
3868 public Class<?> getNestHost() {
3869 if (isPrimitive() || isArray()) {
3870 return this;
3871 }
3872 Class<?> host;
3873 try {
3874 host = getNestHost0();
3875 } catch (LinkageError e) {
3876 // if we couldn't load our nest-host then we
3877 // act as-if we have no nest-host
3878 return this;
3879 }
3880 // if null then nest membership validation failed, so we
3881 // act as-if we have no nest-host
3882 if (host == null || host == this) {
3883 return this;
3884 }
3885 // returning a different class requires a security check
3886 SecurityManager sm = System.getSecurityManager();
3887 if (sm != null) {
3888 checkPackageAccess(sm,
3889 ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
3890 }
3891 return host;
3892 }
3893
3894 /**
3895 * Determines if the given {@code Class} is a nestmate of the
3896 * object represented by this {@code Class}. Two classes are nestmates
3897 * if they have the same {@linkplain #getNestHost nest host}.
3898 *
3899 * @param c the class to check
3900 * @return {@code true} if this class and {@code c} are valid members of the same
3901 * nest; and {@code false} otherwise.
3902 *
3903 * @since 11
3904 */
3905 public boolean isNestmateOf(Class<?> c) {
3906 if (this == c) {
3907 return true;
3908 }
3909 if (isPrimitive() || isArray() ||
3910 c.isPrimitive() || c.isArray()) {
3911 return false;
3912 }
3913 try {
3914 return getNestHost0() == c.getNestHost0();
3915 } catch (LinkageError e) {
3916 return false;
3917 }
3918 }
3919
3920 private native Class<?>[] getNestMembers0();
3921
3922 /**
3923 * Returns an array containing {@code Class} objects representing all the
3924 * classes and interfaces that are declared in the
3925 * {@linkplain #getNestHost() nest host} of this class, as being members
3926 * of its nest. The nest host will always be the zeroth element.
3927 *
3928 * <p>Each listed nest member must be validated by checking its own
3929 * declared nest host. Any exceptions that occur as part of this process
3930 * will be thrown.
3931 *
3932 * <p>The list of nest members in the classfile is permitted to
3933 * contain duplicates, or to explicitly include the nest host. It is not
3934 * required that an implementation of this method removes these duplicates.
3935 *
3936 * @implNote This implementation does not remove duplicate nest members if they
3937 * are present.
3938 *
3939 * @return an array of all classes and interfaces in the same nest as
3940 * this class
3941 *
3942 * @throws LinkageError
3943 * If there is any problem loading or validating a nest member or
3944 * its nest host
3945 * @throws SecurityException
3946 * If any returned class is not the current class, and
3947 * if a security manager, <i>s</i>, is present and the caller's
3948 * class loader is not the same as or an ancestor of the class
3949 * loader for that returned class and invocation of {@link
3950 * SecurityManager#checkPackageAccess s.checkPackageAccess()}
3951 * denies access to the package of that returned class
3952 *
3953 * @since 11
3954 */
3955 @CallerSensitive
3956 public Class<?>[] getNestMembers() {
3957 if (isPrimitive() || isArray()) {
3958 return new Class<?>[] { this };
3959 }
3960 Class<?>[] members = getNestMembers0();
3961 // Can't actually enable this due to bootstrapping issues
3962 // assert(members.length != 1 || members[0] == this); // expected invariant from VM
3963
3964 if (members.length > 1) {
3965 // If we return anything other than the current class we need
3966 // a security check
3967 SecurityManager sm = System.getSecurityManager();
3968 if (sm != null) {
3969 checkPackageAccess(sm,
3970 ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
3971 }
3972 }
3973 return members;
3974 }
3975 }
|