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 2021/09/14 11:01:06 UTC

[GitHub] [netbeans] lahodaj commented on a change in pull request #3156: [NETBEANS-5799]: Pattern Matching for Switch hints (preview)

lahodaj commented on a change in pull request #3156:
URL: https://github.com/apache/netbeans/pull/3156#discussion_r708152965



##########
File path: java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOf.java
##########
@@ -0,0 +1,347 @@
+package org.netbeans.modules.java.hints.jdk;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import com.sun.source.tree.BlockTree;
+import com.sun.source.tree.CaseTree;
+import com.sun.source.tree.ExpressionStatementTree;
+import com.sun.source.tree.IdentifierTree;
+import com.sun.source.tree.IfTree;
+import com.sun.source.tree.InstanceOfTree;
+import com.sun.source.tree.ParenthesizedTree;
+import com.sun.source.tree.StatementTree;
+import com.sun.source.tree.SwitchTree;
+import com.sun.source.tree.ThrowTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.tree.VariableTree;
+import com.sun.source.util.TreePath;
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+import javax.lang.model.element.Modifier;
+import javax.lang.model.element.Name;
+import javax.lang.model.type.TypeMirror;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.TreeMaker;
+import org.netbeans.api.java.source.WorkingCopy;
+import org.netbeans.spi.editor.hints.ErrorDescription;
+import org.netbeans.spi.editor.hints.Fix;
+import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
+import org.netbeans.spi.java.hints.Hint;
+import org.netbeans.spi.java.hints.HintContext;
+import org.netbeans.spi.java.hints.JavaFix;
+import org.netbeans.spi.java.hints.MatcherUtilities;
+import org.netbeans.spi.java.hints.TriggerPattern;
+import org.netbeans.spi.java.hints.TriggerPatterns;
+import org.netbeans.spi.java.hints.TriggerTreeKind;
+import org.openide.util.Exceptions;
+import org.openide.util.NbBundle;
+
+/**
+ *
+ * @author sandeemi
+ */
+@NbBundle.Messages({
+    "DN_ConvertToSwitchPatternInstanceOf=Convert to switch pattern matching <pattern>",
+    "DESC_ConvertToSwitchPatternInstanceOf=Convert to switch pattern matching <pattern>",
+    "ERR_ConvertToSwitchPatternInstanceOf=switch pattern matching <pattern> can be used here",
+    "FIX_ConvertToSwitchPatternInstanceOf=Use switch pattern matching <pattern>"
+})
+@Hint(displayName="#DN_ConvertToSwitchPatternInstanceOf", description="#DESC_ConvertToSwitchPatternInstanceOf", category="rules15",
+        minSourceVersion = "17")
+public class ConvertToSwitchPatternInstanceOf {
+
+    @TriggerPatterns({
+        @TriggerPattern(value = "if ($expr0 instanceof $typeI0) { $statements0$;} else if ($expr1 instanceof $typeI1) { $statements1$;} else $else$;")
+    })
+    public static ErrorDescription trivial(HintContext ctx) {
+        TreePath parent = ctx.getPath().getParentPath();
+        if (parent.getLeaf().getKind() == Tree.Kind.IF) {
+            return null;
+        }
+
+        Tree ifPath = ctx.getPath().getLeaf();
+        Name expr0 = ((IdentifierTree) ctx.getVariables().get("$expr0").getLeaf()).getName();

Review comment:
       What if the tree is not an `IdentifierTree`? E.g.:
   if (o.a instanceof String) {} else if (o.a instanceof Number) else {}
   
   Will the hint fail with a ClassCastException? (I am not saying it should rewrite that - that is up to a discussion, but it should not crash with a CCE.)

##########
File path: java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOfTest.java
##########
@@ -0,0 +1,351 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.java.hints.jdk;
+
+import org.netbeans.junit.NbTestCase;
+import org.netbeans.modules.java.hints.test.api.HintTest;
+import javax.lang.model.SourceVersion;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author sandeemi
+ */
+public class ConvertToSwitchPatternInstanceOfTest extends NbTestCase {

Review comment:
       There should also be tests testing the cases where the hint is not offered. (Like `if (o instanceof String) {} else if (p instanceof Number) {} else {}`, and probably quite a few others.

##########
File path: java/java.source.base/src/org/netbeans/api/java/source/TreeMaker.java
##########
@@ -267,6 +267,30 @@ public CaseTree Case(List<? extends ExpressionTree> patterns, Tree body) {
         return delegate.Case(patterns, body);
     }
     
+    /**
+     * Creates a new CaseTree for a rule case (case &lt;constants&gt; -> &lt;body&gt;).
+     *
+     * @param patterns the labels for this case statement.
+     * @param body the case's body
+     * @see com.sun.source.tree.CaseTree
+     * @since 2.39

Review comment:
       The `@since` should be updated, module's spec version incremented; and apichanges.xml filled.

##########
File path: java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToSwitchPatternInstanceOf.java
##########
@@ -0,0 +1,347 @@
+package org.netbeans.modules.java.hints.jdk;
+
+/*

Review comment:
       Nit: it is preferred to put the license header in front of the package clause.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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