jlahoda commented on a change in pull request #1071: [NETBEANS-1728] fix for
Exception encountered when break returns value
URL:
https://github.com/apache/incubator-netbeans/pull/1071#discussion_r249652777
##########
File path:
java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/MarkOccurrencesHighlighterBase.java
##########
@@ -499,42 +501,61 @@ protected final synchronized void resume() {
private List<int[]> detectBreakOrContinueTarget(CompilationInfo info,
Document document, TreePath breakOrContinue, int caretPosition) {
List<int[]> result = new ArrayList<int[]>();
- StatementTree target =
info.getTreeUtilities().getBreakContinueTarget(breakOrContinue);
+ Tree target =
info.getTreeUtilities().getBreakContinueTargetTree(breakOrContinue);
if (target == null)
return null;
-
+
TokenSequence<JavaTokenId> ts =
info.getTokenHierarchy().tokenSequence(JavaTokenId.language());
ts.move((int)
info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(),
target));
if (ts.moveNext()) {
result.add(new int[] {ts.offset(), ts.offset() +
ts.token().length()});
}
-
- StatementTree statement = target.getKind() == Kind.LABELED_STATEMENT ?
((LabeledStatementTree) target).getStatement() : target;
+ StatementTree targetStatementTree = null;
+ ExpressionTree targetExpressionTree = null;
+ if (target instanceof StatementTree) {
+ targetStatementTree = target.getKind() == Kind.LABELED_STATEMENT ?
((LabeledStatementTree) target).getStatement() : (StatementTree) target;
+ } else if (target instanceof ExpressionTree) {
+ targetExpressionTree = (ExpressionTree) target;
+ }
Tree block = null;
-
- switch (statement.getKind()) {
- case SWITCH:
- block = statement;
- break;
- case WHILE_LOOP:
- if (((WhileLoopTree) statement).getStatement().getKind() ==
Kind.BLOCK)
- block = ((WhileLoopTree) statement).getStatement();
- break;
- case FOR_LOOP:
- if (((ForLoopTree) statement).getStatement().getKind() ==
Kind.BLOCK)
- block = ((ForLoopTree) statement).getStatement();
- break;
- case ENHANCED_FOR_LOOP:
- if (((EnhancedForLoopTree) statement).getStatement().getKind()
== Kind.BLOCK)
- block = ((EnhancedForLoopTree) statement).getStatement();
- break;
- case DO_WHILE_LOOP:
- if (((DoWhileLoopTree) statement).getStatement().getKind() ==
Kind.BLOCK)
- block = ((DoWhileLoopTree) statement).getStatement();
- break;
+
+ if (targetStatementTree != null) {
+ switch (targetStatementTree.getKind()) {
+ case SWITCH:
+ block = targetStatementTree;
+ break;
+ case WHILE_LOOP:
+ if (((WhileLoopTree)
targetStatementTree).getStatement().getKind() == Kind.BLOCK)
+ block = ((WhileLoopTree)
targetStatementTree).getStatement();
+ break;
+ case FOR_LOOP:
+ if (((ForLoopTree)
targetStatementTree).getStatement().getKind() == Kind.BLOCK)
+ block = ((ForLoopTree)
targetStatementTree).getStatement();
+ break;
+ case ENHANCED_FOR_LOOP:
+ if (((EnhancedForLoopTree)
targetStatementTree).getStatement().getKind() == Kind.BLOCK)
+ block = ((EnhancedForLoopTree)
targetStatementTree).getStatement();
+ break;
+ case DO_WHILE_LOOP:
+ if (((DoWhileLoopTree)
targetStatementTree).getStatement().getKind() == Kind.BLOCK)
+ block = ((DoWhileLoopTree)
targetStatementTree).getStatement();
+ break;
+ }
+ // No need to check for version of JDK, as targetExpressionTree can
only be non-null in case of JDK-12 or higher
+ } else if (targetExpressionTree != null) {
+ try {
Review comment:
Two comments:
-I guess I'd suggest to use TreeShims instead of directly using reflection.
That would allow us to encapsulate the reflection on one place. It is not in
java.editor.base yet, but simply pick an important class and a package in
java.editor.base and add these into:
java/java.source.base/src/org/netbeans/modules/java/source/TreeShimsCopier.java
The TreeShims are not exactly pretty, but allow to keep the reflection stuff
somewhat concentrated on one place. At least until/unless we find a better way
to handle new JDK versions.
-but: what is the point here? Wouldn't body = targetExpressionTree work?
After all, "body" is only used to find the closing '}'.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists