-
Notifications
You must be signed in to change notification settings - Fork 665
[epilogue] Use reflection to access non-public superclass fields #7996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SamCarlberg
reviewed
Jul 20, 2025
Comment on lines
262
to
264
" var lookup = MethodHandles.privateLookupIn(" | ||
+ classReference | ||
+ ", \"" | ||
+ fieldName | ||
+ "\", " | ||
+ m_processingEnv.getTypeUtils().erasure(varHandleField.asType()) | ||
+ ".class);"); | ||
+ ", MethodHandles.lookup());"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use a separate private lookup for each type and generate something like this:
// given
@Logged class Superclass {
private double superclassVar;
}
@Logged class Subclass extends Superclass {
private int subclassVar;
}
// generate
var rootLookup = MethodHandles.lookup();
var subclassLookup = MethodHandles.privateLookupIn(Subclass.class, rootLookup);
var superclassLookup = MethodHandles.privateLookupIn(Superclass.class, rootLookup);
$subclassVar = subclassLookup.findVarHandle(Subclass.class, "subclassVar", int.class);
$superclassVar = superclassLookup.findVarHandle(Superclass.class, "superclassVar", double.class);
Then there's no need for runtime reflection
Fields from superclasses /could/ be read directly if they're declared public, or protected or package-private in the same package as the logged class. However, checking package equality is fragile so we only emit direct field accesses for public fields
In case a superclass has a private field with the same name as the logged class, we want to avoid generating the same name for both varhandles
PeterJohnson
approved these changes
Aug 31, 2025
wesleymg
pushed a commit
to wesleymg/allwpilib
that referenced
this pull request
Sep 7, 2025
…libsuite#7996) Co-authored-by: Sam Carlberg <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A sort of patch/continuation to #7993. @SamCarlberg and I overlooked that
VarHandler
does not allow you to violate some Java access permissions, such as accessing aprivate
variable of a superclass or accessing a protected or package-private variable of a superclass if you're not in that package. As a result, some of the superclass-logging features of #7993 didn't work as intended:java.lang.IllegalAccessException
would be raised on startup if a logged class' superclass had a@Logged
private
fieldjava.lang.IllegalAccessException
would be raised on startup if a logged class' superclass had a@Logged
protected
or package-private field and the child class wasn't in the same packageThis PR fixes this by switching to using reflection to handle non-public superclass fields. This lets me revert the change from 7993 that made all non-public fields get accessed with
VarHandler
's - this can go back to being used for private-non-superclass fields only like it was before, which simplifies a lot of loggers and cleans up a lot of the unit test outputs :)I pulled this set of changes into the robot project that helped me catch the issues and all seems to be working well - cross-package superclass fields are logging as intended.