Provided by nb-additional-hints plugin
",}) +public class ToBlankFix { + + @Hint(displayName = "#DN_ToCommonsIsBlank", description = "#DESC_ToCommonsIsBlank", category = "suggestions", hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT) + @TriggerPatterns( + { + @TriggerPattern(value = "$v == null || $v.trim().length() == 0", constraints = @ConstraintVariableType(variable = "$v", type = "java.lang.String")), + @TriggerPattern(value = "$v == null || $v.trim().isEmpty()", constraints = @ConstraintVariableType(variable = "$v", type = "java.lang.String")), + @TriggerPattern(value = "$v != null && $v.trim().length() == 0", constraints = @ConstraintVariableType(variable = "$v", type = "java.lang.String")), + @TriggerPattern(value = "$v != null && $v.trim().isEmpty()", constraints = @ConstraintVariableType(variable = "$v", type = "java.lang.String")), + } + ) + public static ErrorDescription hint(HintContext ctx) { + + Fix fix = rewriteFix(ctx, Bundle.ERR_ToCommonsIsBlank(), ctx.getPath(), "org.apache.commons.lang.StringUtils.isBlank($v)"); + return forName(ctx, ctx.getPath(), Bundle.ERR_ToCommonsIsBlank(), fix); + } + +} diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToEmptyFix.java b/src/main/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToEmptyFix.java new file mode 100644 index 0000000..d9a6256 --- /dev/null +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToEmptyFix.java @@ -0,0 +1,36 @@ +package de.markiewb.netbeans.plugins.hints.apache.commons; + +import org.netbeans.spi.editor.hints.ErrorDescription; +import org.netbeans.spi.editor.hints.Fix; +import org.netbeans.spi.editor.hints.Severity; +import org.netbeans.spi.java.hints.ConstraintVariableType; +import static org.netbeans.spi.java.hints.ErrorDescriptionFactory.forName; +import org.netbeans.spi.java.hints.Hint; +import org.netbeans.spi.java.hints.HintContext; +import static org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix; +import org.netbeans.spi.java.hints.TriggerPattern; +import org.netbeans.spi.java.hints.TriggerPatterns; +import org.openide.util.NbBundle.Messages; + +@Messages({ + "ERR_ToCommonsIsEmpty=Convert to StringUtils.isEmpty()", + "DN_ToCommonsIsEmpty=Convert to StringUtils.isEmpty()", + "DESC_ToCommonsIsEmpty=Converts several patterns to use org.apache.commons.lang.StringUtils.isEmpty(). For example $v == null || $v.length() == 0.Provided by nb-additional-hints plugin
",}) +public class ToEmptyFix { + + @Hint(displayName = "#DN_ToCommonsIsEmpty", description = "#DESC_ToCommonsIsEmpty", category = "suggestions", hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT) + @TriggerPatterns( + { + @TriggerPattern(value = "$v == null || $v.length() == 0", constraints = @ConstraintVariableType(variable = "$v", type = "java.lang.String")), + @TriggerPattern(value = "$v == null || $v.isEmpty()", constraints = @ConstraintVariableType(variable = "$v", type = "java.lang.String")), + @TriggerPattern(value = "$v != null && $v.isEmpty()", constraints = @ConstraintVariableType(variable = "$v", type = "java.lang.String")), + @TriggerPattern(value = "$v != null && $v.length() == 0", constraints = @ConstraintVariableType(variable = "$v", type = "java.lang.String")), + } + ) + public static ErrorDescription hint(HintContext ctx) { + + Fix fix = rewriteFix(ctx, Bundle.ERR_ToCommonsIsEmpty(), ctx.getPath(), "org.apache.commons.lang.StringUtils.isEmpty($v)"); + return forName(ctx, ctx.getPath(), Bundle.ERR_ToCommonsIsEmpty(), fix); + } + +} diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToNotBlankFix.java b/src/main/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToNotBlankFix.java new file mode 100644 index 0000000..1f3eea4 --- /dev/null +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToNotBlankFix.java @@ -0,0 +1,35 @@ +package de.markiewb.netbeans.plugins.hints.apache.commons; + +import de.markiewb.netbeans.plugins.hints.common.StringUtils; +import org.netbeans.spi.editor.hints.ErrorDescription; +import org.netbeans.spi.editor.hints.Fix; +import org.netbeans.spi.editor.hints.Severity; +import org.netbeans.spi.java.hints.ConstraintVariableType; +import static org.netbeans.spi.java.hints.ErrorDescriptionFactory.forName; +import org.netbeans.spi.java.hints.Hint; +import org.netbeans.spi.java.hints.HintContext; +import static org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix; +import org.netbeans.spi.java.hints.TriggerPattern; +import org.netbeans.spi.java.hints.TriggerPatterns; +import org.openide.util.NbBundle.Messages; + +@Messages({ + "ERR_ToCommonsIsNotBlank=Convert to StringUtils.isNotBlank()", + "DN_ToCommonsIsNotBlank=Convert to StringUtils.isNotBlank()", + "DESC_ToCommonsIsNotBlank=Converts several patterns to use org.apache.commons.lang.StringUtils.isNotBlank(). For example $v != null && !$v.trim().isEmpty().Provided by nb-additional-hints plugin
",}) +public class ToNotBlankFix { + + @Hint(displayName = "#DN_ToCommonsIsNotBlank", description = "#DESC_ToCommonsIsNotBlank", category = "suggestions", hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT) + @TriggerPatterns( + { + @TriggerPattern(value = "$v != null && $v.trim().length() > 0", constraints = @ConstraintVariableType(variable = "$v", type = "java.lang.String")), + @TriggerPattern(value = "$v != null && !$v.trim().isEmpty()", constraints = @ConstraintVariableType(variable = "$v", type = "java.lang.String")), + } + ) + public static ErrorDescription hint(HintContext ctx) { + + Fix fix = rewriteFix(ctx, Bundle.ERR_ToCommonsIsNotBlank(), ctx.getPath(), "org.apache.commons.lang.StringUtils.isNotBlank($v)"); + return forName(ctx, ctx.getPath(), Bundle.ERR_ToCommonsIsNotBlank(), fix); + } + +} diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToCommonsIsBlankFixTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToCommonsIsBlankFixTest.java new file mode 100644 index 0000000..91318fb --- /dev/null +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToCommonsIsBlankFixTest.java @@ -0,0 +1,129 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package de.markiewb.netbeans.plugins.hints.apache.commons; + +import de.markiewb.netbeans.plugins.hints.tochar.ToStringFix; +import org.apache.commons.lang.StringUtils; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; +import org.netbeans.modules.java.hints.test.api.HintTest; +import org.netbeans.spi.editor.hints.ErrorDescription; +import org.netbeans.spi.java.hints.HintContext; +import org.openide.filesystems.FileUtil; + +/** + * + * @author markiewb + */ +public class ToCommonsIsBlankFixTest { + + @Test + public void testToIsBlank1() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=s == null || s.trim().length() == 0;\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(StringUtils.class.getProtectionDomain().getCodeSource().getLocation())) + .run(ToBlankFix.class) + .findWarning("4:18-4:53:hint:" + Bundle.ERR_ToCommonsIsBlank()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import org.apache.commons.lang.StringUtils;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=StringUtils.isBlank(s);\n" + + " }\n" + + "}\n"); + + } + @Test + public void testToIsBlank2() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=s == null || s.trim().isEmpty();\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(StringUtils.class.getProtectionDomain().getCodeSource().getLocation())) + .run(ToBlankFix.class) + .findWarning("4:18-4:49:hint:" + Bundle.ERR_ToCommonsIsBlank()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import org.apache.commons.lang.StringUtils;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=StringUtils.isBlank(s);\n" + + " }\n" + + "}\n"); + + } + @Test + public void testToIsBlank3() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=s != null && s.trim().length() == 0;\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(StringUtils.class.getProtectionDomain().getCodeSource().getLocation())) + .run(ToBlankFix.class) + .findWarning("4:18-4:53:hint:" + Bundle.ERR_ToCommonsIsBlank()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import org.apache.commons.lang.StringUtils;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=StringUtils.isBlank(s);\n" + + " }\n" + + "}\n"); + + } + + @Test + public void testToIsBlank4() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=s != null && s.trim().isEmpty();\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(StringUtils.class.getProtectionDomain().getCodeSource().getLocation())) + .run(ToBlankFix.class) + .findWarning("4:18-4:49:hint:" + Bundle.ERR_ToCommonsIsBlank()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import org.apache.commons.lang.StringUtils;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=StringUtils.isBlank(s);\n" + + " }\n" + + "}\n"); + + } + +} diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToCommonsIsEmptyFixTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToCommonsIsEmptyFixTest.java new file mode 100644 index 0000000..17aac37 --- /dev/null +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToCommonsIsEmptyFixTest.java @@ -0,0 +1,116 @@ +package de.markiewb.netbeans.plugins.hints.apache.commons; + +import org.apache.commons.lang.StringUtils; +import org.junit.Test; +import org.netbeans.modules.java.hints.test.api.HintTest; +import org.openide.filesystems.FileUtil; + +/** + * + * @author markiewb + */ +public class ToCommonsIsEmptyFixTest { + + @Test + public void testToIsEmpty1() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=s == null || s.length() == 0;\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(StringUtils.class.getProtectionDomain().getCodeSource().getLocation())) + .run(ToEmptyFix.class) + .findWarning("4:18-4:46:hint:" + Bundle.ERR_ToCommonsIsEmpty()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import org.apache.commons.lang.StringUtils;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=StringUtils.isEmpty(s);\n" + + " }\n" + + "}\n"); + + } + @Test + public void testToIsEmpty2() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=s == null || s.isEmpty();\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(StringUtils.class.getProtectionDomain().getCodeSource().getLocation())) + .run(ToEmptyFix.class) + .findWarning("4:18-4:42:hint:" + Bundle.ERR_ToCommonsIsEmpty()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import org.apache.commons.lang.StringUtils;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=StringUtils.isEmpty(s);\n" + + " }\n" + + "}\n"); + + } + @Test + public void testToIsEmpty3() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=s != null && s.isEmpty();\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(StringUtils.class.getProtectionDomain().getCodeSource().getLocation())) + .run(ToEmptyFix.class) + .findWarning("4:18-4:42:hint:" + Bundle.ERR_ToCommonsIsEmpty()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import org.apache.commons.lang.StringUtils;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=StringUtils.isEmpty(s);\n" + + " }\n" + + "}\n"); + + } + + @Test + public void testToIsEmpty4() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=s != null && s.length() == 0;\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(StringUtils.class.getProtectionDomain().getCodeSource().getLocation())) + .run(ToEmptyFix.class) + .findWarning("4:18-4:46:hint:" + Bundle.ERR_ToCommonsIsEmpty()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import org.apache.commons.lang.StringUtils;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=StringUtils.isEmpty(s);\n" + + " }\n" + + "}\n"); + + } + +} diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToCommonsIsNotBlankFixTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToCommonsIsNotBlankFixTest.java new file mode 100644 index 0000000..73ad138 --- /dev/null +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToCommonsIsNotBlankFixTest.java @@ -0,0 +1,65 @@ +package de.markiewb.netbeans.plugins.hints.apache.commons; + +import org.apache.commons.lang.StringUtils; +import org.junit.Test; +import org.netbeans.modules.java.hints.test.api.HintTest; +import org.openide.filesystems.FileUtil; + +/** + * + * @author markiewb + */ +public class ToCommonsIsNotBlankFixTest { + + @Test + public void testToIsNotBlank1() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=s != null && s.trim().length() > 0;\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(StringUtils.class.getProtectionDomain().getCodeSource().getLocation())) + .run(ToNotBlankFix.class) + .findWarning("4:18-4:52:hint:" + Bundle.ERR_ToCommonsIsNotBlank()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import org.apache.commons.lang.StringUtils;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=StringUtils.isNotBlank(s);\n" + + " }\n" + + "}\n"); + + } + + @Test + public void testToIsNotBlank2() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=s != null && !s.trim().isEmpty();\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(StringUtils.class.getProtectionDomain().getCodeSource().getLocation())) + .run(ToNotBlankFix.class) + .findWarning("4:18-4:50:hint:" + Bundle.ERR_ToCommonsIsNotBlank()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import org.apache.commons.lang.StringUtils;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String s=null;\n" + + " boolean a=StringUtils.isNotBlank(s);\n" + + " }\n" + + "}\n"); + + } +} diff --git a/src/test/java/example/ToCommonsBlank.java b/src/test/java/example/ToCommonsBlank.java new file mode 100644 index 0000000..c724da6 --- /dev/null +++ b/src/test/java/example/ToCommonsBlank.java @@ -0,0 +1,23 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package example; + +import org.apache.commons.lang.StringUtils; + +/** + * + * @author markiewb + */ +public class ToCommonsBlank { + + public static void main(String[] args) { + String s = ""; + boolean a = s == null || s.trim().length() == 0; + boolean b = s == null || s.trim().isEmpty(); + boolean c = s != null && s.trim().length() == 0; + boolean d = s != null && s.trim().isEmpty(); + } +} diff --git a/src/test/java/example/ToCommonsEmpty.java b/src/test/java/example/ToCommonsEmpty.java new file mode 100644 index 0000000..8dcc6c7 --- /dev/null +++ b/src/test/java/example/ToCommonsEmpty.java @@ -0,0 +1,34 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package example; + +import org.apache.commons.lang.StringUtils; + +/** + * + * @author markiewb + */ +public class ToCommonsEmpty { + + public static void main(String[] args) { + String s = ""; + + boolean a = s == null || s.length() == 0; + boolean b = s == null || s.isEmpty(); + boolean c = s != null && s.isEmpty(); + boolean d = s != null && s.length() == 0; + } + + public static void main2(String[] args) { + String s = ""; + + boolean a = s == null || s.length() == 0; + boolean b = s == null || s.isEmpty(); + boolean c = s != null && s.isEmpty(); + boolean d = s != null && s.length() == 0; + } + +} diff --git a/src/test/java/example/ToCommonsNotBlank.java b/src/test/java/example/ToCommonsNotBlank.java new file mode 100644 index 0000000..e27c54f --- /dev/null +++ b/src/test/java/example/ToCommonsNotBlank.java @@ -0,0 +1,22 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package example; + +import org.apache.commons.lang.StringUtils; + +/** + * + * @author markiewb + */ +public class ToCommonsNotBlank { + + public static void main(String[] args) { + String s = ""; + + boolean a = s != null && s.trim().length() > 0; + boolean b = s != null && !s.trim().isEmpty(); + } +}