Skip to content

Commit

Permalink
Merge branch 'feature/issue-12-StringUtils'
Browse files Browse the repository at this point in the history
* feature/issue-12-StringUtils:
  issue #12: Hint for converting to StringUtils.isBlank()/StringUtils.isNotBlank()/StringUtils.isEmpty()
  • Loading branch information
markiewb committed May 12, 2014
2 parents d117088 + 538ff48 commit f1da1f8
Show file tree
Hide file tree
Showing 10 changed files with 504 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@
<version>${netbeans.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -220,6 +226,7 @@
&lt;li&gt;"Remove "public abstract" modifiers from method declarations within interfaces" (since 1.1)&lt;/li&gt;
&lt;li&gt;"Change modifiers" (since 1.2)&lt;/li&gt;
&lt;li&gt;"Convert char to string and back" (since 1.2)&lt;/li&gt;
&lt;li&gt;"Convert to StringUtils.isBlank()/StringUtils.isNotBlank()/StringUtils.isEmpty()" (since 1.2)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Example:&lt;/h2&gt;
Expand All @@ -232,6 +239,7 @@
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-additional-hints/issues/24"&gt;New Hint&lt;/a&gt;]: Remove public/abstract/final modifiers from field declarations within interfaces&lt;/li&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-additional-hints/issues/9"&gt;New Hint&lt;/a&gt;]: Convert from char and string and back&lt;/li&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-additional-hints/issues/10"&gt;New Hint&lt;/a&gt;]: Convert number in literal to number and back&lt;/li&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-additional-hints/issues/12"&gt;New Hint&lt;/a&gt;]: Convert to StringUtils.isBlank()/StringUtils.isNotBlank()/StringUtils.isEmpty()&lt;/li&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-additional-hints/issues/29"&gt;Updated Hint&lt;/a&gt;]: "Convert to assertTrue/assertFalse" now supports junit.framework.Assert too&lt;/li&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-additional-hints/issues/20"&gt;Updated Hint&lt;/a&gt;]: "Replace +..." hints can now be configured&lt;/li&gt;
&lt;/ul&gt;
Expand Down
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);
}

}
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);
}

}
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);
}

}
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");

}

}
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");

}

}
Loading

0 comments on commit f1da1f8

Please sign in to comment.