This is an automated email from the ASF dual-hosted git repository.

arusinha pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new dfa1107  [NETBEANS-2349] Convert switch typecast case to switch 
expression (#1237)
dfa1107 is described below

commit dfa11078c6edcc82d999d797fc1c9648a7d114ce
Author: Vikas Prabhakar <[email protected]>
AuthorDate: Sun Sep 15 18:00:26 2019 +0530

    [NETBEANS-2349] Convert switch typecast case to switch expression (#1237)
---
 .../modules/java/hints/errors/Utilities.java       | 59 +++++++++-----
 .../java/hints/jdk/ConvertSwitchToRuleSwitch.java  |  6 +-
 .../hints/jdk/ConvertSwitchToRuleSwitchTest.java   | 94 ++++++++++++++++++++++
 3 files changed, 135 insertions(+), 24 deletions(-)

diff --git 
a/java/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java 
b/java/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java
index b436b55..9b8f8f9 100644
--- a/java/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java
+++ b/java/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java
@@ -122,7 +122,6 @@ import org.openide.NotifyDescriptor;
 import org.openide.filesystems.FileObject;
 import org.openide.text.NbDocument;
 import org.openide.util.Exceptions;
-
 import static com.sun.source.tree.Tree.Kind.*;
 import com.sun.source.tree.UnaryTree;
 import com.sun.source.util.TreePathScanner;
@@ -137,7 +136,6 @@ import com.sun.tools.javac.tree.JCTree;
 import com.sun.tools.javac.util.JCDiagnostic;
 import com.sun.tools.javac.util.Log;
 import java.net.URI;
-import java.util.concurrent.Callable;
 import javax.lang.model.element.NestingKind;
 import javax.lang.model.type.ErrorType;
 import javax.lang.model.type.UnionType;
@@ -161,6 +159,7 @@ import org.openide.util.Pair;
 public class Utilities {
     public  static final String JAVA_MIME_TYPE = "text/x-java";
     private static final String DEFAULT_NAME = "name";
+    enum SWITCH_TYPE { TRADITIONAL_SWITCH, RULE_SWITCH, SWITCH_EXPRESSION }
 
     public Utilities() {
     }
@@ -3167,18 +3166,20 @@ public class Utilities {
         return true;
     }
 
-    public static void performRewriteRuleSwitch(JavaFix.TransformationContext 
ctx, TreePath tp, Tree st, boolean isExpression) {
+    public static void performRewriteRuleSwitch(JavaFix.TransformationContext 
ctx, TreePath tp, Tree st, boolean isSwitchExpression) {
         WorkingCopy wc = ctx.getWorkingCopy();
         TreeMaker make = wc.getTreeMaker();
         List<CaseTree> newCases = new ArrayList<>();
+        SWITCH_TYPE switchType = SWITCH_TYPE.TRADITIONAL_SWITCH;
+        Tree typeCastTree = null;
         List<? extends CaseTree> cases;
         Set<VariableElement> variablesDeclaredInOtherCases = new HashSet<>();
         List<ExpressionTree> patterns = new ArrayList<>();
-        Tree variable = null;
-        boolean isReturnExpression = false;
-        boolean switchExpressionFlag = 
st.getKind().toString().equals(TreeShims.SWITCH_EXPRESSION);
-        if (switchExpressionFlag) {
+        Tree leftVariable = null;
+        boolean ruleSwitchFlag = 
st.getKind().toString().equals(TreeShims.SWITCH_EXPRESSION);
+        if (ruleSwitchFlag) {
             cases = TreeShims.getCases(st);
+            switchType = SWITCH_TYPE.RULE_SWITCH;
         } else {
             cases = ((SwitchTree) st).getCases();
         }
@@ -3197,7 +3198,7 @@ public class Utilities {
                     continue;
                 }
                 //last case, no break
-            } else if (!switchExpressionFlag && 
statements.get(statements.size() - 1).getKind() == Tree.Kind.BREAK
+            } else if (!ruleSwitchFlag && statements.get(statements.size() - 
1).getKind() == Tree.Kind.BREAK
                     && 
ctx.getWorkingCopy().getTreeUtilities().getBreakContinueTarget(new TreePath(new 
TreePath(tp, ct), statements.get(statements.size() - 1))) == st) {
                 statements.remove(statements.size() - 1);
             } else {
@@ -3259,15 +3260,19 @@ public class Utilities {
                     body = statements.get(0);
                 }
             }
-            if (isExpression) {
+            if (isSwitchExpression) {
+                switchType = SWITCH_TYPE.SWITCH_EXPRESSION;
                 if (statements.get(0).getKind() == Tree.Kind.RETURN) {
                     body = ((JCTree.JCReturn) 
statements.get(0)).getExpression();
-                    isReturnExpression = true;
                 } else {
                     JCTree.JCExpressionStatement jceTree = 
(JCTree.JCExpressionStatement) statements.get(0);
                     body = ((JCTree.JCAssign) jceTree.expr).rhs;
-                    variable = ((JCTree.JCAssign) jceTree.expr).lhs;
+                    leftVariable = ((JCTree.JCAssign) jceTree.expr).lhs;
                 }
+                if (body.getKind() == Tree.Kind.TYPE_CAST) {
+                        typeCastTree = ((JCTree.JCTypeCast)body).getType();
+                        body = ((JCTree.JCTypeCast)body).getExpression();
+                    }
                 newCases.add(make.Case(patterns, 
make.ExpressionStatement((ExpressionTree) body)));
             } else {
                 newCases.add(make.Case(patterns, body));
@@ -3280,16 +3285,28 @@ public class Utilities {
                 }
             }
         }
-        if (isReturnExpression) {
-            ExpressionTree et = (ExpressionTree) 
make.SwitchExpression(TreeShims.getExpressions(st).get(0), newCases);
-            wc.rewrite(st, make.Return(et));
-        } else if (isExpression) {
-            ExpressionTree et = (ExpressionTree) 
make.SwitchExpression(TreeShims.getExpressions(st).get(0), newCases);
-            wc.rewrite(st, make.ExpressionStatement((ExpressionTree) 
make.Assignment((ExpressionTree) variable, et)));
-        } else if (switchExpressionFlag) {
-            wc.rewrite(st, 
make.SwitchExpression(TreeShims.getExpressions(st).get(0), newCases));
-        } else {
-            wc.rewrite((SwitchTree) st, make.Switch(((SwitchTree) 
st).getExpression(), newCases));
+        ExpressionTree et = null;
+        switch (switchType) {
+            case SWITCH_EXPRESSION:
+                et = (ExpressionTree) 
make.SwitchExpression(TreeShims.getExpressions(st).get(0), newCases);
+                if (typeCastTree != null) {
+                    et = make.Parenthesized(et);
+                    et = make.TypeCast(typeCastTree, et);
+                }
+                if (leftVariable != null) {
+                    wc.rewrite(st, make.ExpressionStatement((ExpressionTree) 
make.Assignment((ExpressionTree) leftVariable, et)));
+                } else {
+                    wc.rewrite(st, make.Return(et));
+                }
+                break;
+            case RULE_SWITCH:
+                wc.rewrite(st, 
make.SwitchExpression(TreeShims.getExpressions(st).get(0), newCases));
+                break;
+            case TRADITIONAL_SWITCH:
+                wc.rewrite((SwitchTree) st, make.Switch(((SwitchTree) 
st).getExpression(), newCases));
+                break;
+            default:
+                break;
         }
     }
 
diff --git 
a/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertSwitchToRuleSwitch.java
 
b/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertSwitchToRuleSwitch.java
index 1c54a05..aa681f6 100644
--- 
a/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertSwitchToRuleSwitch.java
+++ 
b/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertSwitchToRuleSwitch.java
@@ -39,13 +39,13 @@ import org.openide.util.NbBundle.Messages;
 @Messages({
     "DN_org.netbeans.modules.java.hints.jdk.ConvertSwitchToRuleSwitch=Convert 
switch to rule switch",
     
"DESC_org.netbeans.modules.java.hints.jdk.ConvertSwitchToRuleSwitch=Converts 
switch to rule switch",
-    
"DN_org.netbeans.modules.java.hints.jdk.ConvertSwitchStatementToSwitchExpression=Convert
 switch to switch expression",
-    
"DESC_org.netbeans.modules.java.hints.jdk.ConvertSwitchStatementToSwitchExpression=Converts
 switch to switch expression",
+    
"DN_org.netbeans.modules.java.hints.jdk.ConvertSwitchStatementToSwitchExpression=Convert
 to switch expression",
+    
"DESC_org.netbeans.modules.java.hints.jdk.ConvertSwitchStatementToSwitchExpression=Converts
 to switch expression",
 })
 public class ConvertSwitchToRuleSwitch {
     
     @TriggerTreeKind(Tree.Kind.SWITCH)
-    @Messages({"ERR_ConvertSwitchToRuleSwitch=Convert switch to rule switch", 
"ERR_ConvertSwitchToSwitchExpression=Convert switch to switch expression"})
+    @Messages({"ERR_ConvertSwitchToRuleSwitch=Convert switch to rule switch", 
"ERR_ConvertSwitchToSwitchExpression=Convert to switch expression"})
     public static ErrorDescription switch2RuleSwitch(HintContext ctx) {
         if 
(!CompilerOptionsQuery.getOptions(ctx.getInfo().getFileObject()).getArguments().contains("--enable-preview"))
             return null;
diff --git 
a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertSwitchToRuleSwitchTest.java
 
b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertSwitchToRuleSwitchTest.java
index 754a8e7..08e171d 100644
--- 
a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertSwitchToRuleSwitchTest.java
+++ 
b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertSwitchToRuleSwitchTest.java
@@ -655,6 +655,100 @@ public class ConvertSwitchToRuleSwitchTest extends 
NbTestCase {
                               "}\n");
     }
 
+    public void testSwitch2SwitchExpressionTypeCast() throws Exception {
+        HintTest.create()
+                .input("package test;" +
+                       "public class Test {\n" +
+                       "     private void test(int p, Object o1, Object o2) 
{\n" +
+                       "         String result;\n" +
+                       "         switch (p) {\n" +
+                       "             case 1: result =  (String)o1; break;\n" +
+                       "             default: result = (String)o2; break;\n" +
+                       "         }\n" +
+                       "     }\n" +
+                       "}\n")
+                .sourceLevel(SourceVersion.latest().name())
+                .options("--enable-preview")
+                .run(ConvertSwitchToRuleSwitch.class)
+                .findWarning("3:9-3:15:verifier:" + 
Bundle.ERR_ConvertSwitchToSwitchExpression())
+                .applyFix()
+                .assertCompilable()
+                .assertOutput("package test;" +
+                              "public class Test {\n" +
+                              "     private void test(int p, Object o1, Object 
o2) {\n" +
+                              "         String result;\n" +
+                              "         result = (String) (switch (p) {\n" +
+                              "             case 1 -> o1;\n" +
+                              "             default -> o2;\n" +
+                              "         });\n" +
+                              "     }\n" +
+                              "}\n");
+    }
+
+    public void testSwitch2SwitchExpressionTypeCastReturn() throws Exception {
+        HintTest.create()
+                .input("package test;" +
+                       "public class Test {\n" +
+                       "     private String test(int p, Object o1, Object o2) 
{\n" +
+                       "         switch (p) {\n" +
+                       "             case 1: return (String)o1;\n" +
+                       "             default: return (String)o2;\n" +
+                       "         }\n" +
+                       "     }\n" +
+                       "}\n")
+                .sourceLevel(SourceVersion.latest().name())
+                .options("--enable-preview")
+                .run(ConvertSwitchToRuleSwitch.class)
+                .findWarning("2:9-2:15:verifier:" + 
Bundle.ERR_ConvertSwitchToSwitchExpression())
+                .applyFix()
+                .assertCompilable()
+                .assertOutput("package test;" +
+                              "public class Test {\n" +
+                              "     private String test(int p, Object o1, 
Object o2) {\n" +
+                              "         return (String) (switch (p) {\n" +
+                              "             case 1 -> o1;\n" +
+                              "             default -> o2;\n" +
+                              "         });\n" +
+                              "     }\n" +
+                              "}\n");
+    }
+
+    public void testSwitch2SwitchExpressionNestedSwitchExpression() throws 
Exception {
+        HintTest.create()
+                .input("package test;" +
+                       "public class Test {\n" +
+                       "     private void test(int p, int q, Object o1, Object 
o2) {\n" +
+                       "         String result;\n" +
+                       "         switch (p) {\n" +
+                       "             case 1: result =  (String)(switch(q){ \n" 
+
+                       "                        case 2 -> o2; \n" +
+                       "                        default -> o1;\n" +
+                       "                        }); break; \n" +
+                       "             default: result = (String)o2; break;\n" +
+                       "         }\n" +
+                       "     }\n" +
+                       "}\n")
+                .sourceLevel(SourceVersion.latest().name())
+                .options("--enable-preview")
+                .run(ConvertSwitchToRuleSwitch.class)
+                .findWarning("3:9-3:15:verifier:" + 
Bundle.ERR_ConvertSwitchToSwitchExpression())
+                .applyFix()
+                .assertCompilable()
+                .assertOutput("package test;" +
+                              "public class Test {\n" +
+                              "     private void test(int p, int q, Object o1, 
Object o2) {\n" +
+                              "         String result;\n" +
+                              "         result = (String) (switch (p) {\n" +
+                              "             case 1 -> switch(q){ \n" +
+                              "                        case 2 -> o2; \n" +
+                              "                        default -> o1;\n" +
+                              "                        };\n" +
+                              "             default -> o2;\n" +
+                              "         });\n" +
+                              "     }\n" +
+                              "}\n");
+    }
+
     public void testSwitch2SwitchExpressionNestedOuterSwitchStatement() throws 
Exception {
         HintTest.create()
                 .input("package test;" +


---------------------------------------------------------------------
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

Reply via email to