From 9b8d9f8c89647f3805394ddd5b4eca476adc9407 Mon Sep 17 00:00:00 2001 From: markiewb Date: Thu, 19 Mar 2015 22:40:35 +0100 Subject: [PATCH] Replace + does not support expressions in exceptions #51 --- pom.xml | 1 + .../hints/replaceplus/ReplacePlusHint.java | 6 +- .../ReplaceWithStringFormatFixTest.java | 333 ++++++++++-------- .../{Example.java => ReplacePlus.java} | 11 +- 4 files changed, 206 insertions(+), 145 deletions(-) rename src/test/java/example/{Example.java => ReplacePlus.java} (89%) diff --git a/pom.xml b/pom.xml index 0c5be1f..78e255d 100644 --- a/pom.xml +++ b/pom.xml @@ -248,6 +248,7 @@ <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/42">Updated Fix</a>]: "Convert to if/else" now supports assignments to new variables</li> <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/43">Updated Fix</a>]: "Invert ternary"/"Convert to ternary" now support conditions without brackets</li> <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/3">Updated Fix</a>]: "Replace +..." is not proposed for erroneous conditions anymore</li> +<li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/51">Updated Fix</a>]: "Replace +..." works for more expressions</li> <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/50">Updated Fix</a>]: Remove false positive detected by "Detect dead instanceof"</li> <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/56">New Fix</a>]: Replace with Optional.isPresent()/Convert return null to return Optional.empty()</li> <li>[Task]: Update requirements JDK7 and NB7.4</li> diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/replaceplus/ReplacePlusHint.java b/src/main/java/de/markiewb/netbeans/plugins/hints/replaceplus/ReplacePlusHint.java index 096eded..bb34543 100644 --- a/src/main/java/de/markiewb/netbeans/plugins/hints/replaceplus/ReplacePlusHint.java +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/replaceplus/ReplacePlusHint.java @@ -206,6 +206,10 @@ public ErrorDescription run(Config config) { *
String foo; foo="B"+42;
*/ final boolean isAssignment = checkParentKind(treePath, 1, Kind.ASSIGNMENT); + /** + *
throw new RuntimeException("B"+42)
+ */ + final boolean isNewClass = checkParentKind(treePath, 1, Kind.NEW_CLASS); //ignore errornous expressions "a"+42+"b"; with no assignment /** @@ -213,7 +217,7 @@ public ErrorDescription run(Config config) { * Detecting erroneous expressions via Kind.ERRONEOUS seems not to * work so we use a whitelist here instead. */ - if (!isMethodInvocation && !isVariableAssignment && !isAssignment) { + if (!isMethodInvocation && !isVariableAssignment && !isAssignment && !isNewClass) { return null; } diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/replaceplus/ReplaceWithStringFormatFixTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/replaceplus/ReplaceWithStringFormatFixTest.java index 8629faa..e97c5c3 100644 --- a/src/test/java/de/markiewb/netbeans/plugins/hints/replaceplus/ReplaceWithStringFormatFixTest.java +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/replaceplus/ReplaceWithStringFormatFixTest.java @@ -58,243 +58,290 @@ */ public class ReplaceWithStringFormatFixTest { + /** + * https://github.com/markiewb/nb-additional-hints/issues/2 + * + * @throws Exception + */ @Test - public void testFixWorkingMixedVARIABLE() throws Exception { + public void testFixWorkingChars() throws Exception { HintTest.create().setCaretMarker('|'). input("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo=\"Output contains \"|+4+\" entries\";\n" - + " }\n" - + "}\n"). + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String name =null, x = null, y = null;\n" + + " String v2 = nam|e + \" (\" + x + \", \" + y + ')';\n" + + " }\n" + + "}\n"). run(ReplacePlusHint.class). - findWarning("3:37-3:37:hint:" + Bundle.DN_ReplacePlus()). + findWarning("4:23-4:23:hint:" + Bundle.DN_ReplacePlus()). applyFix(Bundle.LBL_ReplaceWithStringFormatFix()). assertCompilable(). assertOutput("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo=String.format(\"Output contains %s entries\", 4);\n" - + " }\n" - + "}\n"); + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String name =null, x = null, y = null;\n" + + " String v2 = String.format(\"%s (%s, %s)\", name, x, y);\n" + + " }\n" + + "}\n"); } @Test - public void testFixWorkingMixedASSIGNMENT() throws Exception { + public void testFixWorkingLiteralPostfix() throws Exception { HintTest.create().setCaretMarker('|'). input("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo;\n" - + " foo=\"Output contains \"|+4+\" entries\";\n" - + " }\n" - + "}\n"). + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String foo=4+\"|A\";\n" + + " }\n" + + "}\n"). run(ReplacePlusHint.class). - findWarning("4:30-4:30:hint:" + Bundle.DN_ReplacePlus()). + findWarning("3:22-3:22:hint:" + Bundle.DN_ReplacePlus()). applyFix(Bundle.LBL_ReplaceWithStringFormatFix()). assertCompilable(). assertOutput("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo;\n" - + " foo=String.format(\"Output contains %s entries\", 4);\n" - + " }\n" - + "}\n"); + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String foo=String.format(\"%sA\", 4);\n" + + " }\n" + + "}\n"); } @Test - public void testFixWorkingMixedMETHODINVOCATION() throws Exception { + public void testFixWorkingLiteralPrefix() throws Exception { HintTest.create().setCaretMarker('|'). input("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " System.out.println(\"Output contains \"|+4+\" entries\");\n" - + " }\n" - + "}\n"). + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String foo=\"A\"+|4;\n" + + " }\n" + + "}\n"). run(ReplacePlusHint.class). - findWarning("3:45-3:45:hint:" + Bundle.DN_ReplacePlus()). + findWarning("3:23-3:23:hint:" + Bundle.DN_ReplacePlus()). applyFix(Bundle.LBL_ReplaceWithStringFormatFix()). assertCompilable(). assertOutput("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " System.out.println(String.format(\"Output contains %s entries\", 4));\n" - + " }\n" - + "}\n"); + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String foo=String.format(\"A%s\", 4);\n" + + " }\n" + + "}\n"); } - - /** - * https://github.com/markiewb/nb-additional-hints/issues/2 - * @throws Exception - */ + @Test - public void testFixWorkingChars() throws Exception { + public void testFixWorkingMixedASSIGNMENT() throws Exception { HintTest.create().setCaretMarker('|'). input("package test;\n" + "public class Test {\n" + " public static void main(String[] args) {\n" - + " String name =null, x = null, y = null;\n" - + " String v2 = nam|e + \" (\" + x + \", \" + y + ')';\n" + + " String foo;\n" + + " foo=\"Output contains \"|+4+\" entries\";\n" + " }\n" + "}\n"). run(ReplacePlusHint.class). - findWarning("4:23-4:23:hint:" + Bundle.DN_ReplacePlus()). + findWarning("4:30-4:30:hint:" + Bundle.DN_ReplacePlus()). applyFix(Bundle.LBL_ReplaceWithStringFormatFix()). assertCompilable(). assertOutput("package test;\n" + "public class Test {\n" + " public static void main(String[] args) {\n" - + " String name =null, x = null, y = null;\n" - + " String v2 = String.format(\"%s (%s, %s)\", name, x, y);\n" + + " String foo;\n" + + " foo=String.format(\"Output contains %s entries\", 4);\n" + " }\n" + "}\n"); } @Test - public void testFixWorkingLiteralPrefix() throws Exception { + public void testFixWorkingMixedB() throws Exception { HintTest.create().setCaretMarker('|'). input("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo=\"A\"+|4;\n" - + " }\n" - + "}\n"). + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String foo=\"Output contains \" +| 4 + \" entries\" + \" and more at \" + new java.util.Date();\n" + + " }\n" + + "}\n"). run(ReplacePlusHint.class). - findWarning("3:23-3:23:hint:" + Bundle.DN_ReplacePlus()). + findWarning("3:39-3:39:hint:" + Bundle.DN_ReplacePlus()). applyFix(Bundle.LBL_ReplaceWithStringFormatFix()). assertCompilable(). assertOutput("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo=String.format(\"A%s\", 4);\n" - + " }\n" - + "}\n"); + + "import java.util.Date;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String foo=String.format(\"Output contains %s entries and more at %s\", 4, new Date());\n" + + " }\n" + + "}\n"); + } - /** - * https://github.com/markiewb/nb-additional-hints/issues/1 - * @throws Exception - */ + @Test - public void testFixWorkingQuotedStrings1() throws Exception { - HintTest.create().setCaretMarker('|'). - input("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String b = \"Hello \\\"\"+|42+\"\\\"\";" - + " }\n" - + "}\n"). - run(ReplacePlusHint.class). - findWarning("3:23-3:23:hint:" + Bundle.DN_ReplacePlus()). - applyFix(Bundle.LBL_ReplaceWithStringFormatFix()). - assertCompilable(). - assertOutput("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String b = String.format(\"Hello \\\"%s\\\"\", 42);\n" - + " }\n" - + "}\n"); + public void testFixWorkingMixedMETHODINVOCATION() throws Exception { + HintTest.create().setCaretMarker('|'). + input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " System.out.println(\"Output contains \"|+4+\" entries\");\n" + + " }\n" + + "}\n"). + run(ReplacePlusHint.class). + findWarning("3:45-3:45:hint:" + Bundle.DN_ReplacePlus()). + applyFix(Bundle.LBL_ReplaceWithStringFormatFix()). + assertCompilable(). + assertOutput("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " System.out.println(String.format(\"Output contains %s entries\", 4));\n" + + " }\n" + + "}\n"); } @Test - public void testFixWorkingLiteralPostfix() throws Exception { + public void testFixWorkingMixedNEWCLASS() throws Exception { HintTest.create().setCaretMarker('|'). input("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo=4+\"|A\";\n" - + " }\n" - + "}\n"). + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " new RuntimeException(\"Output contains \"|+4+\" entries\");\n" + + " }\n" + + "}\n"). run(ReplacePlusHint.class). - findWarning("3:22-3:22:hint:" + Bundle.DN_ReplacePlus()). + findWarning("3:47-3:47:hint:" + Bundle.DN_ReplacePlus()). applyFix(Bundle.LBL_ReplaceWithStringFormatFix()). assertCompilable(). assertOutput("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo=String.format(\"%sA\", 4);\n" - + " }\n" - + "}\n"); + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " new RuntimeException(String.format(\"Output contains %s entries\", 4));\n" + + " }\n" + + "}\n"); } - + @Test - public void testOnlyLiterals() throws Exception { + public void testFixWorkingMixedTHROW() throws Exception { HintTest.create().setCaretMarker('|'). input("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo=\"A\"|+\"B\"+\"C\";\n" - + " }\n" - + "}\n"). + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " throw new RuntimeException(\"Output contains \"|+4+\" entries\");\n" + + " }\n" + + "}\n"). run(ReplacePlusHint.class). - assertNotContainsWarnings(Bundle.DN_ReplacePlus()); + findWarning("3:53-3:53:hint:" + Bundle.DN_ReplacePlus()). + applyFix(Bundle.LBL_ReplaceWithStringFormatFix()). + assertCompilable(). + assertOutput("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " throw new RuntimeException(String.format(\"Output contains %s entries\", 4));\n" + + " }\n" + + "}\n"); } @Test - public void testNoAssignment() throws Exception { - boolean compilable = true; + public void testFixWorkingMixedVARIABLE() throws Exception { HintTest.create().setCaretMarker('|'). input("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " \"A\"|+42+\"C\";\n" - + " }\n" - + "}\n", !compilable). + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String foo=\"Output contains \"|+4+\" entries\";\n" + + " }\n" + + "}\n"). run(ReplacePlusHint.class). - assertNotContainsWarnings(Bundle.DN_ReplacePlus()); + findWarning("3:37-3:37:hint:" + Bundle.DN_ReplacePlus()). + applyFix(Bundle.LBL_ReplaceWithStringFormatFix()). + assertCompilable(). + assertOutput("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String foo=String.format(\"Output contains %s entries\", 4);\n" + + " }\n" + + "}\n"); } + /** + * https://github.com/markiewb/nb-additional-hints/issues/1 + * + * @throws Exception + */ @Test - public void testFixWorkingMixedB() throws Exception { + public void testFixWorkingQuotedStrings1() throws Exception { HintTest.create().setCaretMarker('|'). input("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo=\"Output contains \" +| 4 + \" entries\" + \" and more at \" + new java.util.Date();\n" - + " }\n" - + "}\n"). + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String b = \"Hello \\\"\"+|42+\"\\\"\";" + + " }\n" + + "}\n"). run(ReplacePlusHint.class). - findWarning("3:39-3:39:hint:" + Bundle.DN_ReplacePlus()). + findWarning("3:23-3:23:hint:" + Bundle.DN_ReplacePlus()). applyFix(Bundle.LBL_ReplaceWithStringFormatFix()). assertCompilable(). assertOutput("package test;\n" - + "import java.util.Date;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo=String.format(\"Output contains %s entries and more at %s\", 4, new Date());\n" - + " }\n" - + "}\n"); - + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String b = String.format(\"Hello \\\"%s\\\"\", 42);\n" + + " }\n" + + "}\n"); } -@Test + + @Test public void testFixWorkingWithLineBreaks() throws Exception { HintTest.create().setCaretMarker('|'). input("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo=\"ABC\\nDEF\\r\"|+4+\"GHI\";\n" - + " }\n" - + "}\n"). + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String foo=\"ABC\\nDEF\\r\"|+4+\"GHI\";\n" + + " }\n" + + "}\n"). run(ReplacePlusHint.class). findWarning("3:31-3:31:hint:" + Bundle.DN_ReplacePlus()). applyFix(Bundle.LBL_ReplaceWithStringFormatFix()). assertCompilable(). assertOutput("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo=String.format(\"ABC\\nDEF\\r%sGHI\", 4);\n" - + " }\n" - + "}\n"); - } + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String foo=String.format(\"ABC\\nDEF\\r%sGHI\", 4);\n" + + " }\n" + + "}\n"); + } + + @Test + public void testNoAssignment() throws Exception { + boolean compilable = true; + HintTest.create().setCaretMarker('|'). + input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " \"A\"|+42+\"C\";\n" + + " }\n" + + "}\n", !compilable). + run(ReplacePlusHint.class). + assertNotContainsWarnings(Bundle.DN_ReplacePlus()); + } + + @Test + public void testOnlyLiterals() throws Exception { + HintTest.create().setCaretMarker('|'). + input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String foo=\"A\"|+\"B\"+\"C\";\n" + + " }\n" + + "}\n"). + run(ReplacePlusHint.class). + assertNotContainsWarnings(Bundle.DN_ReplacePlus()); + } + @Test public void testSingleLiteral() throws Exception { HintTest.create().setCaretMarker('|'). input("package test;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " String foo=\"A|\";\n" - + " }\n" - + "}\n"). + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " String foo=\"A|\";\n" + + " }\n" + + "}\n"). run(ReplacePlusHint.class). - assertNotContainsWarnings(Bundle.DN_ReplacePlus()); + assertNotContainsWarnings(Bundle.DN_ReplacePlus()); } } diff --git a/src/test/java/example/Example.java b/src/test/java/example/ReplacePlus.java similarity index 89% rename from src/test/java/example/Example.java rename to src/test/java/example/ReplacePlus.java index 16222cd..3c11fc7 100644 --- a/src/test/java/example/Example.java +++ b/src/test/java/example/ReplacePlus.java @@ -2,7 +2,7 @@ import java.util.Date; -public class Example { +public class ReplacePlus { public static void testA() { String foo = "O utput contains " + 4 + " entries"; @@ -11,6 +11,15 @@ public static void testA() { // String foo = new StringBuilder().append("Output contains ").append(4).append(" entries").toString(); } + + public static void testThrow() { + new RuntimeException("Output contains "+4+" entries"); + throw new RuntimeException("Output contains "+4+" entries"); + } + + public static void testMethodInvocation() { + System.err.println("Output contains "+4+" entries"); + } public static void testStringConcat() { String foo = "Output contains " + 4 + " entries" + " and more at " + new java.util.Date();