You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2019/02/08 05:59:38 UTC

[GitHub] arusinha commented on a change in pull request #1124: [NETBEANS-1969]:Fixed reindentation issues for Switch Rule-Case and S…

arusinha commented on a change in pull request #1124: [NETBEANS-1969]:Fixed reindentation issues for Switch Rule-Case and S…
URL: https://github.com/apache/incubator-netbeans/pull/1124#discussion_r254962742
 
 

 ##########
 File path: java/java.source.base/src/org/netbeans/modules/java/source/TreeShims.java
 ##########
 @@ -50,6 +55,69 @@ public static Tree getBody(CaseTree node) {
         }
     }
 
+    public static List<? extends ExpressionTree> getExpressions(Tree node) {
+        List<? extends ExpressionTree> exprTrees = new ArrayList<>();
+
+        switch (node.getKind().toString()) {
+            case "CASE":
+                exprTrees = getExpressions((CaseTree) node);
+                break;
+            case "SWITCH_EXPRESSION": {
+                try {
+                    Class swExprTreeClass = Class.forName("com.sun.source.tree.SwitchExpressionTree");
+                    Method getExpressions = swExprTreeClass.getDeclaredMethod("getExpression");
+                    exprTrees = Collections.singletonList((ExpressionTree) getExpressions.invoke(node));
+                } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
+                    throw TreeShims.<RuntimeException>throwAny(ex);
+                }
+                break;
+            }
+            default:
+                break;
+        }
+        return exprTrees;
+    }
+
+    public static List<? extends CaseTree> getCases(Tree node) {
+        List<? extends CaseTree> caseTrees = new ArrayList<>();
+
+        switch (node.getKind().toString()) {
+            case "SWITCH":
+                caseTrees = ((SwitchTree) node).getCases();
+                break;
+            case "SWITCH_EXPRESSION": {
+                try {
+                    Class swExprTreeClass = Class.forName("com.sun.source.tree.SwitchExpressionTree");
+                    Method getCases = swExprTreeClass.getDeclaredMethod("getCases");
+                    caseTrees = (List<? extends CaseTree>) getCases.invoke(node);
+                } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
+                    throw TreeShims.<RuntimeException>throwAny(ex);
+                }
+            }
+        }
+        return caseTrees;
+    }
+
+    public static ExpressionTree getValue(BreakTree node) {
+        try {
+            Method getExpression = BreakTree.class.getDeclaredMethod("getValue");
+            return (ExpressionTree) getExpression.invoke(node);
+        } catch (NoSuchMethodException ex) {
+            return null;
+        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
+            throw TreeShims.<RuntimeException>throwAny(ex);
+        }
+    }
+
+    public static List<? extends StatementTree> getStatements(CaseTree node) {
+        List<? extends StatementTree> statements = null;
+        statements = ((CaseTree) node).getStatements();
+        if (statements == null && node instanceof JCTree.JCCase) {
+            statements = ((JCTree.JCCase) node).stats;
 
 Review comment:
   instead of node.stats tried using Treeshims.getBody(), but in below screnario getBody() is returning 5 (JCTree.JCLiteral) whereas we are expecting a statement tree.
   int a = switch(val)
   { case 1 -> break 5;}

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists