< prev index next >

src/java.base/share/classes/java/lang/reflect/AccessibleObject.java

Print this page




 274    /**
 275     * If the given AccessibleObject is a {@code Constructor}, {@code Method}
 276     * or {@code Field} then checks that its declaring class is in a package
 277     * that can be accessed by the given caller of setAccessible.
 278     */
 279     void checkCanSetAccessible(Class<?> caller) {
 280         // do nothing, needs to be overridden by Constructor, Method, Field
 281     }
 282 
 283     final void checkCanSetAccessible(Class<?> caller, Class<?> declaringClass) {
 284         checkCanSetAccessible(caller, declaringClass, true);
 285     }
 286 
 287     private boolean checkCanSetAccessible(Class<?> caller,
 288                                           Class<?> declaringClass,
 289                                           boolean throwExceptionIfDenied) {
 290         if (caller == MethodHandle.class) {
 291             throw new IllegalCallerException();   // should not happen
 292         }
 293 
 294         Module callerModule = caller.getModule();
 295         Module declaringModule = declaringClass.getModule();
 296 
 297         if (callerModule == declaringModule) return true;
 298         if (callerModule == Object.class.getModule()) return true;
 299         if (!declaringModule.isNamed()) return true;
 300 
 301         String pn = declaringClass.getPackageName();
 302         int modifiers;
 303         if (this instanceof Executable) {
 304             modifiers = ((Executable) this).getModifiers();
 305         } else {
 306             modifiers = ((Field) this).getModifiers();
 307         }
 308 




















 309         // class is public and package is exported to caller
 310         boolean isClassPublic = Modifier.isPublic(declaringClass.getModifiers());

 311         if (isClassPublic && declaringModule.isExported(pn, callerModule)) {
 312             // member is public
 313             if (Modifier.isPublic(modifiers)) {
 314                 logIfExportedForIllegalAccess(caller, declaringClass);
 315                 return true;
 316             }
 317 
 318             // member is protected-static
 319             if (Modifier.isProtected(modifiers)
 320                 && Modifier.isStatic(modifiers)
 321                 && isSubclassOf(caller, declaringClass)) {
 322                 logIfExportedForIllegalAccess(caller, declaringClass);
 323                 return true;
 324             }
 325         }
 326 
 327         // package is open to caller
 328         if (declaringModule.isOpen(pn, callerModule)) {
 329             logIfOpenedForIllegalAccess(caller, declaringClass);
 330             return true;




 274    /**
 275     * If the given AccessibleObject is a {@code Constructor}, {@code Method}
 276     * or {@code Field} then checks that its declaring class is in a package
 277     * that can be accessed by the given caller of setAccessible.
 278     */
 279     void checkCanSetAccessible(Class<?> caller) {
 280         // do nothing, needs to be overridden by Constructor, Method, Field
 281     }
 282 
 283     final void checkCanSetAccessible(Class<?> caller, Class<?> declaringClass) {
 284         checkCanSetAccessible(caller, declaringClass, true);
 285     }
 286 
 287     private boolean checkCanSetAccessible(Class<?> caller,
 288                                           Class<?> declaringClass,
 289                                           boolean throwExceptionIfDenied) {
 290         if (caller == MethodHandle.class) {
 291             throw new IllegalCallerException();   // should not happen
 292         }
 293 








 294         int modifiers;
 295         if (this instanceof Executable) {
 296             modifiers = ((Executable) this).getModifiers();
 297         } else {
 298             modifiers = ((Field) this).getModifiers();
 299         }
 300 
 301         // Do not allow suppression of access check for inline class's field
 302         if (declaringClass.isInlineClass() &&
 303                 this instanceof Field
 304                 && Modifier.isFinal(modifiers)) {
 305             if (throwExceptionIfDenied) {
 306                 String msg = "Unable to make field accessible of inline class "
 307                                 + declaringClass.getName();
 308                 throw new InaccessibleObjectException(msg);
 309             } else {
 310                 return false;
 311             }
 312         }
 313 
 314         Module callerModule = caller.getModule();
 315         Module declaringModule = declaringClass.getModule();
 316 
 317         if (callerModule == declaringModule) return true;
 318         if (callerModule == Object.class.getModule()) return true;
 319         if (!declaringModule.isNamed()) return true;
 320 
 321         // class is public and package is exported to caller
 322         boolean isClassPublic = Modifier.isPublic(declaringClass.getModifiers());
 323         String pn = declaringClass.getPackageName();
 324         if (isClassPublic && declaringModule.isExported(pn, callerModule)) {
 325             // member is public
 326             if (Modifier.isPublic(modifiers)) {
 327                 logIfExportedForIllegalAccess(caller, declaringClass);
 328                 return true;
 329             }
 330 
 331             // member is protected-static
 332             if (Modifier.isProtected(modifiers)
 333                 && Modifier.isStatic(modifiers)
 334                 && isSubclassOf(caller, declaringClass)) {
 335                 logIfExportedForIllegalAccess(caller, declaringClass);
 336                 return true;
 337             }
 338         }
 339 
 340         // package is open to caller
 341         if (declaringModule.isOpen(pn, callerModule)) {
 342             logIfOpenedForIllegalAccess(caller, declaringClass);
 343             return true;


< prev index next >