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

jlahoda 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 b26b9c2  Added Collection/Map.size() > 0 case to the isEmpty() 
refactoring.
b26b9c2 is described below

commit b26b9c213bdb4bda1012431ac54fb4f62b52b9de
Author: Michael Bien <[email protected]>
AuthorDate: Sun Sep 12 13:03:59 2021 +0200

    Added Collection/Map.size() > 0 case to the isEmpty() refactoring.
---
 .../modules/java/hints/perf/Bundle.properties      | 18 ++---
 .../modules/java/hints/perf/SizeEqualsZero.java    | 35 +++++++---
 .../modules/java/hints/perf/Bundle_test.properties |  6 +-
 .../java/hints/perf/SizeEqualsZeroTest.java        | 77 +++++++++++++++++++++-
 4 files changed, 116 insertions(+), 20 deletions(-)

diff --git 
a/java/java.hints/src/org/netbeans/modules/java/hints/perf/Bundle.properties 
b/java/java.hints/src/org/netbeans/modules/java/hints/perf/Bundle.properties
index f5ca735..bbcd6fc 100644
--- a/java/java.hints/src/org/netbeans/modules/java/hints/perf/Bundle.properties
+++ b/java/java.hints/src/org/netbeans/modules/java/hints/perf/Bundle.properties
@@ -23,14 +23,16 @@ FIX_manual-array-copy-coll=Replace with 
Collection.addAll(Arrays.asList(...))
 ERR_manual-array-copy=Manual array copy
 FIX_manual-array-copy=Replace with System.arraycopy
 
-DN_org.netbeans.modules.java.hints.perf.SizeEqualsZero=Usage of .size() == 0
-DESC_org.netbeans.modules.java.hints.perf.SizeEqualsZero=Use .isEmpty() or 
!.isEmpty() instead of .size() == 0 or .size() != 0 where possible.
-ERR_SizeEqualsZero=Usage of .size() == 0 can be replaced with .isEmpty()
-ERR_SizeEqualsZeroNeg=Usage of .size() != 0 can be replaced with !.isEmpty()
-FIX_UseIsEmpty=Use .isEmpty() instead of .size() == 0
-FIX_UseIsEmptyNeg=Use .isEmpty() instead of .size() != 0
-LBL_org.netbeans.modules.java.hints.perf.SizeEqualsZero.CHECK_NOT_EQUALS=Also 
check for .size() != 0
-TP_org.netbeans.modules.java.hints.perf.SizeEqualsZero.CHECK_NOT_EQUALS=Should 
this hint also report occurrences of ".size() != 0"?
+DN_org.netbeans.modules.java.hints.perf.SizeEqualsZero=Usage of 
[Collection|Map].size() == 0
+DESC_org.netbeans.modules.java.hints.perf.SizeEqualsZero=Use 
[Collection|Map].isEmpty() instead of .size() == 0 and !.isEmpty() instead of 
.size() != 0 or .size() > 0 where possible.
+ERR_SizeEqualsZero={0}.size() == 0 can be replaced with {0}.isEmpty()
+ERR_SizeEqualsZeroNeg={0}.size() != 0 can be replaced with !{0}.isEmpty()
+ERR_SizeGreaterZeroNeg={0}.size() > 0 can be replaced with !{0}.isEmpty()
+FIX_SizeEqualsZero=Change to {0}.isEmpty()
+FIX_SizeEqualsZeroNeg=Change to !{0}.isEmpty()
+FIX_SizeGreaterZeroNeg=Change to !{0}.isEmpty()
+LBL_org.netbeans.modules.java.hints.perf.SizeEqualsZero.CHECK_NOT_EQUALS=Also 
check for .size() != 0 and .size() > 0
+TP_org.netbeans.modules.java.hints.perf.SizeEqualsZero.CHECK_NOT_EQUALS=Should 
this hint also report occurrences of ".size() != 0" and ".size() > 0"?
 
 DN_org.netbeans.modules.java.hints.perf.Tiny.stringConstructor=String 
constructor
 DESC_org.netbeans.modules.java.hints.perf.Tiny.stringConstructor=Use of 
java.lang.String constructor is usually useless.
diff --git 
a/java/java.hints/src/org/netbeans/modules/java/hints/perf/SizeEqualsZero.java 
b/java/java.hints/src/org/netbeans/modules/java/hints/perf/SizeEqualsZero.java
index f9e28aa..24ffba2 100644
--- 
a/java/java.hints/src/org/netbeans/modules/java/hints/perf/SizeEqualsZero.java
+++ 
b/java/java.hints/src/org/netbeans/modules/java/hints/perf/SizeEqualsZero.java
@@ -62,7 +62,7 @@ public class SizeEqualsZero {
             constraints = @ConstraintVariableType(type = "java.util.Map", 
variable = "$subj")),
     })
     public static ErrorDescription sizeEqualsZero(HintContext ctx) {
-        return sizeEqualsZeroHint(ctx, false);
+        return sizeEqualsZeroHint(ctx, "SizeEqualsZero", "$subj.isEmpty()"); 
// NOI18N
     }
 
     @TriggerPatterns({
@@ -79,10 +79,27 @@ public class SizeEqualsZero {
         if (!ctx.getPreferences().getBoolean(CHECK_NOT_EQUALS, 
CHECK_NOT_EQUALS_DEFAULT)) {
             return null;
         }
-        return sizeEqualsZeroHint(ctx, true);
+        return sizeEqualsZeroHint(ctx, "SizeEqualsZeroNeg", 
"!$subj.isEmpty()"); // NOI18N
     }
 
-    public static ErrorDescription sizeEqualsZeroHint(HintContext ctx, boolean 
not) {
+    @TriggerPatterns({
+        @TriggerPattern(value="$subj.size() > 0", 
+            constraints = @ConstraintVariableType(type = 
"java.util.Collection", variable = "$subj")),
+        @TriggerPattern(value="$subj.size() > 0", 
+            constraints = @ConstraintVariableType(type = "java.util.Map", 
variable = "$subj")),
+        @TriggerPattern(value="0 < $subj.size()",
+            constraints = @ConstraintVariableType(type = 
"java.util.Collection", variable = "$subj")),
+        @TriggerPattern(value="0 < $subj.size()",
+            constraints = @ConstraintVariableType(type = "java.util.Map", 
variable = "$subj")),
+    })
+    public static ErrorDescription sizeGreaterZero(HintContext ctx) {
+        if (!ctx.getPreferences().getBoolean(CHECK_NOT_EQUALS, 
CHECK_NOT_EQUALS_DEFAULT)) {
+            return null;
+        }
+        return sizeEqualsZeroHint(ctx, "SizeGreaterZeroNeg", 
"!$subj.isEmpty()"); // NOI18N
+    }
+
+    public static ErrorDescription sizeEqualsZeroHint(HintContext ctx, String 
keyPostfix, String to) {
         TreePath subj = ctx.getVariables().get("$subj");
         if (subj == null) {
             // assume implicit this
@@ -122,11 +139,13 @@ public class SizeEqualsZero {
                 return null;
             }
         }
-
-        String fixDisplayName = NbBundle.getMessage(SizeEqualsZero.class, not 
? "FIX_UseIsEmptyNeg" : "FIX_UseIsEmpty");
-        Fix f = JavaFixUtilities.rewriteFix(ctx, fixDisplayName, 
ctx.getPath(), not ? "!$subj.isEmpty()" : "$subj.isEmpty()");
-        String displayName = NbBundle.getMessage(SizeEqualsZero.class, not ? 
"ERR_SizeEqualsZeroNeg" : "ERR_SizeEqualsZero");
-        return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), 
displayName, f);
+        
+        String subjName = subj.getLeaf().toString();
+        String fixDisplayName = NbBundle.getMessage(SizeEqualsZero.class, 
"FIX_" + keyPostfix, subjName); // NOI18N
+        String errDisplayName = NbBundle.getMessage(SizeEqualsZero.class, 
"ERR_" + keyPostfix, subjName); // NOI18N
+        
+        Fix fix = JavaFixUtilities.rewriteFix(ctx, fixDisplayName, 
ctx.getPath(), to);
+        return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), 
errDisplayName, fix);
     }
 
 }
diff --git 
a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/perf/Bundle_test.properties
 
b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/perf/Bundle_test.properties
index 51acaa5..ab07327 100644
--- 
a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/perf/Bundle_test.properties
+++ 
b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/perf/Bundle_test.properties
@@ -23,8 +23,10 @@ FIX_manual-array-copy=FIX_manual-array-copy
 ERR_StringConstructor=new String(...)
 ERR_SizeEqualsZero=.size() == 0
 ERR_SizeEqualsZeroNeg=.size() != 0
-FIX_UseIsEmpty=.size() == 0 => .isEmpty()
-FIX_UseIsEmptyNeg=.size() != 0 => !.isEmpty()
+ERR_SizeGreaterZeroNeg=.size() > 0
+FIX_SizeEqualsZero=.size() == 0 => .isEmpty()
+FIX_SizeEqualsZeroNeg=.size() != 0 => !.isEmpty()
+FIX_SizeGreaterZeroNeg=.size() > 0 => !.isEmpty()
 ERR_StringConstructor=new String(...)
 FIX_StringConstructor=Remove new String(...)
 ERR_StringEqualsEmpty=$string.equals("")
diff --git 
a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/perf/SizeEqualsZeroTest.java
 
b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/perf/SizeEqualsZeroTest.java
index 939d5b1..5c3a149 100644
--- 
a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/perf/SizeEqualsZeroTest.java
+++ 
b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/perf/SizeEqualsZeroTest.java
@@ -30,7 +30,7 @@ import 
org.netbeans.modules.java.hints.test.api.HintTest.HintOutput;
 public class SizeEqualsZeroTest {
 
     @Test
-    public void testSimple1() throws Exception {
+    public void testEqualsZero() throws Exception {
         final HintOutput output = HintTest.create()
                 .input("test/Test.java",
                        "package test;\n" +
@@ -67,7 +67,7 @@ public class SizeEqualsZeroTest {
     }
 
     @Test
-    public void testSimple2() throws Exception {
+    public void testNotEqualsZero() throws Exception {
         final HintOutput output = HintTest.create()
                 .input("package test;\n" +
                        "import java.util.List;" +
@@ -104,6 +104,43 @@ public class SizeEqualsZeroTest {
     }
 
     @Test
+    public void testGreaterZero() throws Exception {
+        final HintOutput output = HintTest.create()
+                .input("package test;\n" +
+                       "import java.util.List;" +
+                       "public class Test {\n" +
+                       "     private void test(List l) {\n" +
+                       "         boolean b = l.size() > 0;\n" +
+                       "         boolean b2 = 0 < l.size();\n" +
+                       "     }\n" +
+                       "}\n")
+                .preference(SizeEqualsZero.CHECK_NOT_EQUALS, true)
+                .run(SizeEqualsZero.class);
+        output.findWarning("3:21-3:33:verifier:.size() > 0")
+                .applyFix()
+                .assertCompilable()
+                .assertOutput("package test;\n" +
+                        "import java.util.List;" +
+                        "public class Test {\n" +
+                        "     private void test(List l) {\n" +
+                        "         boolean b = !l.isEmpty();\n" +
+                        "         boolean b2 = 0 < l.size();\n" +
+                        "     }\n" +
+                        "}\n");
+        output.findWarning("4:22-4:34:verifier:.size() > 0")
+                .applyFix()
+                .assertCompilable()
+                .assertOutput("package test;\n" +
+                        "import java.util.List;" +
+                        "public class Test {\n" +
+                        "     private void test(List l) {\n" +
+                        "         boolean b = !l.isEmpty();\n" +
+                        "         boolean b2 = !l.isEmpty();\n" +
+                        "     }\n" +
+                        "}\n");
+    }
+
+    @Test
     public void testCollection() throws Exception {
         final HintOutput output = HintTest.create()
                 .input("test/Test.java",
@@ -113,6 +150,7 @@ public class SizeEqualsZeroTest {
                        "     private void test() {\n" +
                        "         boolean b = size() == 0;\n" +
                        "         boolean b2 = 0 != size();\n" +
+                       "         boolean b3 = 0 < size();\n" +
                        "     }\n" +
                        "}\n")
                 .run(SizeEqualsZero.class);
@@ -125,6 +163,7 @@ public class SizeEqualsZeroTest {
                        "     private void test() {\n" +
                        "         boolean b = isEmpty();\n" +
                        "         boolean b2 = 0 != size();\n" +
+                       "         boolean b3 = 0 < size();\n" +
                        "     }\n" +
                        "}\n");
         output.findWarning("4:22-4:33:verifier:.size() != 0")
@@ -136,6 +175,19 @@ public class SizeEqualsZeroTest {
                        "     private void test() {\n" +
                        "         boolean b = isEmpty();\n" +
                        "         boolean b2 = !isEmpty();\n" +
+                       "         boolean b3 = 0 < size();\n" +
+                       "     }\n" +
+                       "}\n");
+        output.findWarning("5:22-5:32:verifier:.size() > 0")
+                .applyFix()
+                .assertCompilable()
+                .assertOutput("package test;\n" +
+                       "import java.util.ArrayList;" +
+                       "public class Test extends ArrayList {\n" +
+                       "     private void test() {\n" +
+                       "         boolean b = isEmpty();\n" +
+                       "         boolean b2 = !isEmpty();\n" +
+                       "         boolean b3 = !isEmpty();\n" +
                        "     }\n" +
                        "}\n");
     }
@@ -150,6 +202,7 @@ public class SizeEqualsZeroTest {
                        "     private void test() {\n" +
                        "         boolean b = size() == 0;\n" +
                        "         boolean b2 = 0 != size();\n" +
+                       "         boolean b3 = size() > 0;\n" +
                        "     }\n" +
                        "}\n")
                 .run(SizeEqualsZero.class);
@@ -162,6 +215,7 @@ public class SizeEqualsZeroTest {
                        "     private void test() {\n" +
                        "         boolean b = isEmpty();\n" +
                        "         boolean b2 = 0 != size();\n" +
+                       "         boolean b3 = size() > 0;\n" +
                        "     }\n" +
                        "}\n");
         output.findWarning("4:22-4:33:verifier:.size() != 0")
@@ -173,6 +227,19 @@ public class SizeEqualsZeroTest {
                        "     private void test() {\n" +
                        "         boolean b = isEmpty();\n" +
                        "         boolean b2 = !isEmpty();\n" +
+                       "         boolean b3 = size() > 0;\n" +
+                       "     }\n" +
+                       "}\n");
+        output.findWarning("5:22-5:32:verifier:.size() > 0")
+                .applyFix()
+                .assertCompilable()
+                .assertOutput("package test;\n" +
+                       "import java.util.HashMap;" +
+                       "public class Test extends HashMap {\n" +
+                       "     private void test() {\n" +
+                       "         boolean b = isEmpty();\n" +
+                       "         boolean b2 = !isEmpty();\n" +
+                       "         boolean b3 = !isEmpty();\n" +
                        "     }\n" +
                        "}\n");
     }
@@ -193,6 +260,11 @@ public class SizeEqualsZeroTest {
                        "     public boolean isEmpty() {\n" +
                        "         return !(0 != size());\n" +
                        "     }\n" +
+                       "}\n" +
+                       "class EntirelyDifferentTest extends HashMap {\n" +
+                       "     public boolean isEmpty() {\n" +
+                       "         return !(0 < size());\n" +
+                       "     }\n" +
                        "}\n")
                 .run(SizeEqualsZero.class)
                 .assertWarnings();
@@ -207,6 +279,7 @@ public class SizeEqualsZeroTest {
                        "     private void test(List l) {\n" +
                        "         boolean b = l.size() != 0;\n" +
                        "         boolean b2 = 0 != l.size();\n" +
+                       "         boolean b3 = 0 < l.size();\n" +
                        "     }\n" +
                        "}\n")
                 .preference(SizeEqualsZero.CHECK_NOT_EQUALS, false)

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