1 /*
2 * Copyright (c) 2001, 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
126 return false;
127 }
128
129 boolean gotIsSameClassPackage = false;
130 boolean isSameClassPackage = false;
131
132 if (!Modifier.isPublic(getClassAccessFlags(memberClass))) {
133 isSameClassPackage = isSameClassPackage(currentClass, memberClass);
134 gotIsSameClassPackage = true;
135 if (!isSameClassPackage) {
136 return false;
137 }
138 }
139
140 // At this point we know that currentClass can access memberClass.
141
142 if (Modifier.isPublic(modifiers)) {
143 return true;
144 }
145
146 boolean successSoFar = false;
147
148 if (Modifier.isProtected(modifiers)) {
149 // See if currentClass is a subclass of memberClass
150 if (isSubclassOf(currentClass, memberClass)) {
151 successSoFar = true;
152 }
153 }
154
155 if (!successSoFar && !Modifier.isPrivate(modifiers)) {
156 if (!gotIsSameClassPackage) {
157 isSameClassPackage = isSameClassPackage(currentClass,
158 memberClass);
159 gotIsSameClassPackage = true;
160 }
161
162 if (isSameClassPackage) {
163 successSoFar = true;
164 }
165 }
334 memberSuffix = " (in " + m2 + ")";
335
336 String memberPackageName = memberClass.getPackageName();
337
338 String msg = currentClass + currentSuffix + " cannot access ";
339 if (m2.isExported(memberPackageName, m1)) {
340
341 // module access okay so include the modifiers in the message
342 msg += "a member of " + memberClass + memberSuffix +
343 " with modifiers \"" + Modifier.toString(modifiers) + "\"";
344
345 } else {
346 // module access failed
347 msg += memberClass + memberSuffix+ " because "
348 + m2 + " does not export " + memberPackageName;
349 if (m2.isNamed()) msg += " to " + m1;
350 }
351
352 return new IllegalAccessException(msg);
353 }
354 }
|
1 /*
2 * Copyright (c) 2001, 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
126 return false;
127 }
128
129 boolean gotIsSameClassPackage = false;
130 boolean isSameClassPackage = false;
131
132 if (!Modifier.isPublic(getClassAccessFlags(memberClass))) {
133 isSameClassPackage = isSameClassPackage(currentClass, memberClass);
134 gotIsSameClassPackage = true;
135 if (!isSameClassPackage) {
136 return false;
137 }
138 }
139
140 // At this point we know that currentClass can access memberClass.
141
142 if (Modifier.isPublic(modifiers)) {
143 return true;
144 }
145
146 // Check for nestmate access if member is private
147 if (Modifier.isPrivate(modifiers)) {
148 // Note: targetClass may be outside the nest, but that is okay
149 // as long as memberClass is in the nest.
150 if (areNestMates(currentClass, memberClass)) {
151 return true;
152 }
153 }
154
155 boolean successSoFar = false;
156
157 if (Modifier.isProtected(modifiers)) {
158 // See if currentClass is a subclass of memberClass
159 if (isSubclassOf(currentClass, memberClass)) {
160 successSoFar = true;
161 }
162 }
163
164 if (!successSoFar && !Modifier.isPrivate(modifiers)) {
165 if (!gotIsSameClassPackage) {
166 isSameClassPackage = isSameClassPackage(currentClass,
167 memberClass);
168 gotIsSameClassPackage = true;
169 }
170
171 if (isSameClassPackage) {
172 successSoFar = true;
173 }
174 }
343 memberSuffix = " (in " + m2 + ")";
344
345 String memberPackageName = memberClass.getPackageName();
346
347 String msg = currentClass + currentSuffix + " cannot access ";
348 if (m2.isExported(memberPackageName, m1)) {
349
350 // module access okay so include the modifiers in the message
351 msg += "a member of " + memberClass + memberSuffix +
352 " with modifiers \"" + Modifier.toString(modifiers) + "\"";
353
354 } else {
355 // module access failed
356 msg += memberClass + memberSuffix+ " because "
357 + m2 + " does not export " + memberPackageName;
358 if (m2.isNamed()) msg += " to " + m1;
359 }
360
361 return new IllegalAccessException(msg);
362 }
363
364 /**
365 * Returns true if {@code currentClass} and {@code memberClass}
366 * are nestmates - that is, if they have the same nesthost as
367 * determined by the VM.
368 */
369 public static native boolean areNestMates(Class<?> currentClass,
370 Class<?> memberClass);
371 }
|