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
105 }
106 }
107
108 /**
109 * Verify access to a member and return {@code true} if it is granted.
110 *
111 * @param currentClass the class performing the access
112 * @param memberClass the declaring class of the member being accessed
113 * @param targetClass the class of target object if accessing instance
114 * field or method;
115 * or the declaring class if accessing constructor;
116 * or null if accessing static field or method
117 * @param modifiers the member's access modifiers
118 * @return {@code true} if access to member is granted
119 */
120 public static boolean verifyMemberAccess(Class<?> currentClass,
121 Class<?> memberClass,
122 Class<?> targetClass,
123 int modifiers)
124 {
125 if (currentClass == memberClass) {
126 // Always succeeds
127 return true;
128 }
129
130 if (!verifyModuleAccess(currentClass.getModule(), memberClass)) {
131 return false;
132 }
133
134 boolean gotIsSameClassPackage = false;
135 boolean isSameClassPackage = false;
136
137 if (!Modifier.isPublic(getClassAccessFlags(memberClass))) {
138 isSameClassPackage = isSameClassPackage(currentClass, memberClass);
139 gotIsSameClassPackage = true;
140 if (!isSameClassPackage) {
141 return false;
142 }
143 }
144
310 * is defined by either the bootstrap class loader or platform class loader.
311 */
312 public static boolean isCallerSensitive(Method m) {
313 final ClassLoader loader = m.getDeclaringClass().getClassLoader();
314 if (VM.isSystemDomainLoader(loader)) {
315 return m.isAnnotationPresent(CallerSensitive.class);
316 }
317 return false;
318 }
319
320 /**
321 * Returns an IllegalAccessException with an exception message based on
322 * the access that is denied.
323 */
324 public static IllegalAccessException newIllegalAccessException(Class<?> currentClass,
325 Class<?> memberClass,
326 Class<?> targetClass,
327 int modifiers)
328 throws IllegalAccessException
329 {
330 String currentSuffix = "";
331 String memberSuffix = "";
332 Module m1 = currentClass.getModule();
333 if (m1.isNamed())
334 currentSuffix = " (in " + m1 + ")";
335 Module m2 = memberClass.getModule();
336 if (m2.isNamed())
337 memberSuffix = " (in " + m2 + ")";
338
339 String memberPackageName = memberClass.getPackageName();
340
341 String msg = currentClass + currentSuffix + " cannot access ";
342 if (m2.isExported(memberPackageName, m1)) {
343
344 // module access okay so include the modifiers in the message
345 msg += "a member of " + memberClass + memberSuffix +
346 " with modifiers \"" + Modifier.toString(modifiers) + "\"";
347
348 } else {
349 // module access failed
350 msg += memberClass + memberSuffix+ " because "
351 + m2 + " does not export " + memberPackageName;
352 if (m2.isNamed()) msg += " to " + m1;
353 }
354
355 return new IllegalAccessException(msg);
356 }
357
358 /**
359 * Returns true if {@code currentClass} and {@code memberClass}
360 * are nestmates - that is, if they have the same nesthost as
361 * determined by the VM.
362 */
363 public static native boolean areNestMates(Class<?> currentClass,
364 Class<?> memberClass);
365 }
|
1 /*
2 * Copyright (c) 2001, 2019, 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
105 }
106 }
107
108 /**
109 * Verify access to a member and return {@code true} if it is granted.
110 *
111 * @param currentClass the class performing the access
112 * @param memberClass the declaring class of the member being accessed
113 * @param targetClass the class of target object if accessing instance
114 * field or method;
115 * or the declaring class if accessing constructor;
116 * or null if accessing static field or method
117 * @param modifiers the member's access modifiers
118 * @return {@code true} if access to member is granted
119 */
120 public static boolean verifyMemberAccess(Class<?> currentClass,
121 Class<?> memberClass,
122 Class<?> targetClass,
123 int modifiers)
124 {
125 Objects.requireNonNull(currentClass);
126 Objects.requireNonNull(memberClass);
127
128 if (currentClass == memberClass) {
129 // Always succeeds
130 return true;
131 }
132
133 if (!verifyModuleAccess(currentClass.getModule(), memberClass)) {
134 return false;
135 }
136
137 boolean gotIsSameClassPackage = false;
138 boolean isSameClassPackage = false;
139
140 if (!Modifier.isPublic(getClassAccessFlags(memberClass))) {
141 isSameClassPackage = isSameClassPackage(currentClass, memberClass);
142 gotIsSameClassPackage = true;
143 if (!isSameClassPackage) {
144 return false;
145 }
146 }
147
313 * is defined by either the bootstrap class loader or platform class loader.
314 */
315 public static boolean isCallerSensitive(Method m) {
316 final ClassLoader loader = m.getDeclaringClass().getClassLoader();
317 if (VM.isSystemDomainLoader(loader)) {
318 return m.isAnnotationPresent(CallerSensitive.class);
319 }
320 return false;
321 }
322
323 /**
324 * Returns an IllegalAccessException with an exception message based on
325 * the access that is denied.
326 */
327 public static IllegalAccessException newIllegalAccessException(Class<?> currentClass,
328 Class<?> memberClass,
329 Class<?> targetClass,
330 int modifiers)
331 throws IllegalAccessException
332 {
333 if (currentClass == null)
334 return newIllegalAccessException(memberClass, modifiers);
335
336 String currentSuffix = "";
337 String memberSuffix = "";
338 Module m1 = currentClass.getModule();
339 if (m1.isNamed())
340 currentSuffix = " (in " + m1 + ")";
341 Module m2 = memberClass.getModule();
342 if (m2.isNamed())
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 an IllegalAccessException with an exception message where
366 * there is no caller frame.
367 */
368 public static IllegalAccessException newIllegalAccessException(Class<?> memberClass,
369 int modifiers)
370 throws IllegalAccessException
371 {
372 String memberSuffix = "";
373 Module m2 = memberClass.getModule();
374 if (m2.isNamed())
375 memberSuffix = " (in " + m2 + ")";
376
377 String memberPackageName = memberClass.getPackageName();
378
379 String msg = "JNI attached native thread (null caller frame) cannot access ";
380 if (m2.isExported(memberPackageName)) {
381
382 // module access okay so include the modifiers in the message
383 msg += "a member of " + memberClass + memberSuffix +
384 " with modifiers \"" + Modifier.toString(modifiers) + "\"";
385
386 } else {
387 // module access failed
388 msg += memberClass + memberSuffix+ " because "
389 + m2 + " does not export " + memberPackageName;
390 }
391
392 return new IllegalAccessException(msg);
393 }
394
395 /**
396 * Returns true if {@code currentClass} and {@code memberClass}
397 * are nestmates - that is, if they have the same nesthost as
398 * determined by the VM.
399 */
400 public static native boolean areNestMates(Class<?> currentClass,
401 Class<?> memberClass);
402 }
|