Fix NPE on goto to invalid method reference#9210
Conversation
| if (peer.getKind() == ElementKind.RECORD_COMPONENT) { | ||
| for (RecordComponentElement component : ((TypeElement)peer.getEnclosingElement()).getRecordComponents()) { |
There was a problem hiding this comment.
I think it mostly is OK. And it basically weakly suggests that peer.getEnclosingElement() == enclosing, which should be always true. (I probably would just do enclosing.getEnclosingElement(), though.)
I wonder if it is possible to write a test for this - I can try at some point if needed.
Otherwise - great cleanup, thanks for doing this!
There was a problem hiding this comment.
(I probably would just do enclosing.getEnclosingElement(), though.)
you mean changing
for (RecordComponentElement component : ((TypeElement)peer.getEnclosingElement()).getRecordComponents()) {into
for (RecordComponentElement component : ((TypeElement)enclosing).getRecordComponents()) {good idea,
There was a problem hiding this comment.
added regression test and updated the logic so that it breaks out of both loops instead of just the inner loop.
712cb66 to
ce2ac52
Compare
- check if element is a record before listing components - removed reflective method calls and updated logic - added regression test
ce2ac52 to
74c8117
Compare
mbien
left a comment
There was a problem hiding this comment.
rebased, planning to merge once tests are green.
| Element enclosing = el.getEnclosingElement(); | ||
| if (enclosing.getKind() == ElementKind.RECORD) { |
There was a problem hiding this comment.
btw for future reference: this check basically is the fix for this issue. Without it, javac would throw a NPE a few lines later (see reproducer).
java.lang.NullPointerException: Cannot invoke "com.sun.tools.javac.code.Scope$WriteableScope.getSymbols(com.sun.tools.javac.code.Scope$LookupKind)" because the return value of "com.sun.tools.javac.code.Symbol$TypeSymbol.members()" is null
at com.sun.tools.javac.code.Symbol$TypeSymbol.getEnclosedElements(Symbol.java:879)
at com.sun.tools.javac.code.Symbol$TypeSymbol.getEnclosedElements(Symbol.java:818)
at org.netbeans.modules.editor.java.GoToSupport.resolveContext(GoToSupport.java:481)
reproducer:
fixes #8735