-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/issue-12-StringUtils'
* feature/issue-12-StringUtils: issue #12: Hint for converting to StringUtils.isBlank()/StringUtils.isNotBlank()/StringUtils.isEmpty()
- Loading branch information
Showing
10 changed files
with
504 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToBlankFix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ToCommonsIsBlank=Convert to StringUtils.isBlank()", | ||
"DN_ToCommonsIsBlank=Convert to StringUtils.isBlank()", | ||
"DESC_ToCommonsIsBlank=Converts several patterns to use <tt>org.apache.commons.lang.StringUtils.isBlank()</tt>. For example <tt>$v != null && $v.trim().isEmpty()</tt>.<p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",}) | ||
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); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToEmptyFix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <tt>org.apache.commons.lang.StringUtils.isEmpty()</tt>. For example <tt>$v == null || $v.length() == 0</tt>.<p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",}) | ||
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); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToNotBlankFix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <tt>org.apache.commons.lang.StringUtils.isNotBlank()</tt>. For example <tt>$v != null && !$v.trim().isEmpty()</tt>.<p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>",}) | ||
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); | ||
} | ||
|
||
} |
129 changes: 129 additions & 0 deletions
129
src/test/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToCommonsIsBlankFixTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
|
||
} | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
src/test/java/de/markiewb/netbeans/plugins/hints/apache/commons/ToCommonsIsEmptyFixTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.