fix: FieldMatchers NPE with primitive class literals - Issue #5589#5775
Open
lukman48 wants to merge 1 commit intogoogle:masterfrom
Open
fix: FieldMatchers NPE with primitive class literals - Issue #5589#5775lukman48 wants to merge 1 commit intogoogle:masterfrom
lukman48 wants to merge 1 commit intogoogle:masterfrom
Conversation
FieldMatchers.instanceField() and staticField() methods throw a NullPointerException when encountering primitive class literals like 'long.class' or 'int.class'. Root Cause: When VisitorState.getSymbolFromString() is called with a primitive type name, it returns null (primitives don't have compile-time symbols). The code then attempted to use this null symbol in comparisons without null-checking first. The Fix: Added null-check for classSymbol before using it in owner comparison: - Extract classSymbol from supplier: Symbol classSymbol = symbol.get(state) - Check for null before comparing: classSymbol != null && ... - This gracefully returns false for primitive class literals (no fields exist) Behavior: - Before: NPE when primitive class literal encountered - After: Gracefully returns false (no match) for primitives This matches the expected behavior since primitives have no fields to match against. Fixes google#5589 Signed-off-by: lukman48 <lukman_uki@yahoo.co.id>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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
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.
Fixes Issue #5589: FieldMatchers throws null pointer exception on primitive class literals
Problem
FieldMatchers.instanceField()andstaticField()methods throw aNullPointerExceptionwhen aMemberSelectTreeMatcherchecker encounters primitive class literals such aslong.class,int.class,float.class, etc.Root Cause
When
VisitorState.getSymbolFromString(className)is called with a primitive type name, it returnsnullbecause primitive types don't have compile-time Symbol objects. The current code then attempts to use this null symbol in an equality comparison (sym.owner == symbol.get(state)) without null-checking, causing aNullPointerException.Example Crash
Compiling any code with
long.class(or another primitive) triggers:Solution
Added null-check for the resolved class symbol before using it in comparison:
Behavior
false(no match) - correct since primitives have no fieldsTesting Checklist
Why This Fix
Primitive types in Java are not objects and have no Symbol representation. When code encounters a primitive class literal, the symbol lookup correctly returns null. The fix recognizes this as a valid but non-matching case (primitives have no fields to match) rather than crashing with an NPE.