< prev index next >

src/java.base/share/classes/java/lang/invoke/MethodHandles.java

Print this page




2384             if (!putField.isFinal()) {
2385                 // A VarHandle does not support updates to final fields, any
2386                 // such VarHandle to a final field will be read-only and
2387                 // therefore the following write-based accessibility checks are
2388                 // only required for non-final fields
2389                 checkField(putRefKind, refc, putField);
2390                 if (checkSecurity)
2391                     checkSecurityManager(refc, putField);
2392             }
2393 
2394             boolean doRestrict = (MethodHandleNatives.refKindHasReceiver(getRefKind) &&
2395                                   restrictProtectedReceiver(getField));
2396             if (doRestrict) {
2397                 assert !getField.isStatic();
2398                 // receiver type of VarHandle is too wide; narrow to caller
2399                 if (!getField.getDeclaringClass().isAssignableFrom(lookupClass())) {
2400                     throw getField.makeAccessException("caller class must be a subclass below the method", lookupClass());
2401                 }
2402                 refc = lookupClass();
2403             }
2404             return VarHandles.makeFieldHandle(getField, refc, getField.getFieldType(), this.allowedModes == TRUSTED);


2405         }
2406         /** Check access and get the requested constructor. */
2407         private MethodHandle getDirectConstructor(Class<?> refc, MemberName ctor) throws IllegalAccessException {
2408             final boolean checkSecurity = true;
2409             return getDirectConstructorCommon(refc, ctor, checkSecurity);
2410         }
2411         /** Check access and get the requested constructor, eliding security manager checks. */
2412         private MethodHandle getDirectConstructorNoSecurityManager(Class<?> refc, MemberName ctor) throws IllegalAccessException {
2413             final boolean checkSecurity = false;  // not needed for reflection or for linking CONSTANT_MH constants
2414             return getDirectConstructorCommon(refc, ctor, checkSecurity);
2415         }
2416         /** Common code for all constructors; do not call directly except from immediately above. */
2417         private MethodHandle getDirectConstructorCommon(Class<?> refc, MemberName ctor,
2418                                                   boolean checkSecurity) throws IllegalAccessException {
2419             assert(ctor.isConstructor());
2420             checkAccess(REF_newInvokeSpecial, refc, ctor);
2421             // Optionally check with the security manager; this isn't needed for unreflect* calls.
2422             if (checkSecurity)
2423                 checkSecurityManager(refc, ctor);
2424             assert(!MethodHandleNatives.isCallerSensitive(ctor));  // maybeBindCaller not relevant here




2384             if (!putField.isFinal()) {
2385                 // A VarHandle does not support updates to final fields, any
2386                 // such VarHandle to a final field will be read-only and
2387                 // therefore the following write-based accessibility checks are
2388                 // only required for non-final fields
2389                 checkField(putRefKind, refc, putField);
2390                 if (checkSecurity)
2391                     checkSecurityManager(refc, putField);
2392             }
2393 
2394             boolean doRestrict = (MethodHandleNatives.refKindHasReceiver(getRefKind) &&
2395                                   restrictProtectedReceiver(getField));
2396             if (doRestrict) {
2397                 assert !getField.isStatic();
2398                 // receiver type of VarHandle is too wide; narrow to caller
2399                 if (!getField.getDeclaringClass().isAssignableFrom(lookupClass())) {
2400                     throw getField.makeAccessException("caller class must be a subclass below the method", lookupClass());
2401                 }
2402                 refc = lookupClass();
2403             }
2404             // don't allow writing on value type
2405             boolean isWriteAllowedOnFinalFields = this.allowedModes == TRUSTED && !putField.isValue();
2406             return VarHandles.makeFieldHandle(getField, refc, getField.getFieldType(), isWriteAllowedOnFinalFields);
2407         }
2408         /** Check access and get the requested constructor. */
2409         private MethodHandle getDirectConstructor(Class<?> refc, MemberName ctor) throws IllegalAccessException {
2410             final boolean checkSecurity = true;
2411             return getDirectConstructorCommon(refc, ctor, checkSecurity);
2412         }
2413         /** Check access and get the requested constructor, eliding security manager checks. */
2414         private MethodHandle getDirectConstructorNoSecurityManager(Class<?> refc, MemberName ctor) throws IllegalAccessException {
2415             final boolean checkSecurity = false;  // not needed for reflection or for linking CONSTANT_MH constants
2416             return getDirectConstructorCommon(refc, ctor, checkSecurity);
2417         }
2418         /** Common code for all constructors; do not call directly except from immediately above. */
2419         private MethodHandle getDirectConstructorCommon(Class<?> refc, MemberName ctor,
2420                                                   boolean checkSecurity) throws IllegalAccessException {
2421             assert(ctor.isConstructor());
2422             checkAccess(REF_newInvokeSpecial, refc, ctor);
2423             // Optionally check with the security manager; this isn't needed for unreflect* calls.
2424             if (checkSecurity)
2425                 checkSecurityManager(refc, ctor);
2426             assert(!MethodHandleNatives.isCallerSensitive(ctor));  // maybeBindCaller not relevant here


< prev index next >