codeconsole commented on PR #3097:
URL: https://github.com/apache/groovy/pull/3097#issuecomment-6536942328
@blackdrag Simplified in e9c86b5dfc:
```java
if (field.isPublic() || field.isPrivate()) { // nestmates can access a
private field
return isMemberDirectlyAccessible(field.getModifiers(), declaringClass,
accessingClass);
}
// JVMS 5.4.4
return isInSameRuntimePackage(declaringClass, accessingClass)
|| (field.isProtected() && accessingClass.isDerivedFrom(declaringClass));
```
- Public and private fields: the language rule decides; the run-time package
does not matter for them, and a private field is reachable only from nestmates,
which are compiled together.
- Protected and package-private fields: JVMS 5.4.4 allows access from the
run-time package of the declaring class (package name plus defining loader),
and to a protected field from a subclass. `isMemberDirectlyAccessible` also
accepts any class with the same package name, which is the excluded case: a
class compiled earlier by another `GroovyClassLoader` compilation has the same
package name but another loader, and `GETFIELD` of its field fails with
`IllegalAccessError`. For these fields the JVM condition implies
`isMemberDirectlyAccessible` (same run-time package implies same package name),
so it replaces that call instead of being checked after it. Same shape as
`StaticInvocationWriter.isDirectlyAccessible(ConstructorNode)`.
- Both parts are needed: checking the package name instead of
`isInSameRuntimePackage` fails the 5 field tests of
`StaticCompileRuntimePackageTest`; dropping the subclass part fails
`FieldsAndPropertiesStaticCompileTest.testReadFieldFromSuperClass2/3`,
`MethodReferenceTest.testInnerClassProtectedMethodReference` and
`StaticCompileRuntimePackageTest.testProtectedFieldOfSuperclassOfAnotherClassLoaderIsAccessedDirectly`.
The result is the same as before for every field, so no test changed. Also
merged master now that #3022 has landed, so this PR shows only GROOVY-12568.
`MethodReferenceTest.testMethodReferencesInStaticAndDefaultMethodsOfInterface`
fails on master itself since #3056; #3116 fixes it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]