< prev index next >

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

Print this page




 152      *     Module#isExported(String,Module) exports} to at least the module
 153      *     containing {@code C}. </li>
 154      *
 155      *     <li> The member is {@code protected} {@code static}, {@code D} is
 156      *     {@code public} in a package that the module containing {@code D}
 157      *     exports to at least the module containing {@code C}, and {@code C}
 158      *     is a subclass of {@code D}. </li>
 159      *
 160      *     <li> {@code D} is in a package that the module containing {@code D}
 161      *     {@link Module#isOpen(String,Module) opens} to at least the module
 162      *     containing {@code C}.
 163      *     All packages in unnamed and open modules are open to all modules and
 164      *     so this method always succeeds when {@code D} is in an unnamed or
 165      *     open module. </li>
 166      * </ul>
 167      *
 168      * <p> This method cannot be used to enable access to private members,
 169      * members with default (package) access, protected instance members, or
 170      * protected constructors when the declaring class is in a different module
 171      * to the caller and the package containing the declaring class is not open
 172      * to the caller's module. </p>

 173      *
 174      * <p> If there is a security manager, its
 175      * {@code checkPermission} method is first called with a
 176      * {@code ReflectPermission("suppressAccessChecks")} permission.
 177      *
 178      * @param flag the new value for the {@code accessible} flag
 179      * @throws InaccessibleObjectException if access cannot be enabled
 180      * @throws SecurityException if the request is denied by the security manager
 181      * @see #trySetAccessible
 182      * @see java.lang.invoke.MethodHandles#privateLookupIn
 183      * @revised 9
 184      * @spec JPMS
 185      */
 186     @CallerSensitive   // overrides in Method/Field/Constructor are @CS
 187     public void setAccessible(boolean flag) {
 188         AccessibleObject.checkPermission();
 189         setAccessible0(flag);
 190     }
 191 
 192     /**


 272 
 273 
 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)) {




 152      *     Module#isExported(String,Module) exports} to at least the module
 153      *     containing {@code C}. </li>
 154      *
 155      *     <li> The member is {@code protected} {@code static}, {@code D} is
 156      *     {@code public} in a package that the module containing {@code D}
 157      *     exports to at least the module containing {@code C}, and {@code C}
 158      *     is a subclass of {@code D}. </li>
 159      *
 160      *     <li> {@code D} is in a package that the module containing {@code D}
 161      *     {@link Module#isOpen(String,Module) opens} to at least the module
 162      *     containing {@code C}.
 163      *     All packages in unnamed and open modules are open to all modules and
 164      *     so this method always succeeds when {@code D} is in an unnamed or
 165      *     open module. </li>
 166      * </ul>
 167      *
 168      * <p> This method cannot be used to enable access to private members,
 169      * members with default (package) access, protected instance members, or
 170      * protected constructors when the declaring class is in a different module
 171      * to the caller and the package containing the declaring class is not open
 172      * to the caller's module or it is a {@linkplain Class#isHiddenClass()
 173      * hidden class}.</p>
 174      *
 175      * <p> If there is a security manager, its
 176      * {@code checkPermission} method is first called with a
 177      * {@code ReflectPermission("suppressAccessChecks")} permission.
 178      *
 179      * @param flag the new value for the {@code accessible} flag
 180      * @throws InaccessibleObjectException if access cannot be enabled
 181      * @throws SecurityException if the request is denied by the security manager
 182      * @see #trySetAccessible
 183      * @see java.lang.invoke.MethodHandles#privateLookupIn
 184      * @revised 9
 185      * @spec JPMS
 186      */
 187     @CallerSensitive   // overrides in Method/Field/Constructor are @CS
 188     public void setAccessible(boolean flag) {
 189         AccessibleObject.checkPermission();
 190         setAccessible0(flag);
 191     }
 192 
 193     /**


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


< prev index next >