This is an automated email from the ASF dual-hosted git repository.
mbien 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 97be304ddf [jackpot] improved autoboxing optimization rules
new 80f298cf5e Merge pull request #3240 from mbien/valueof
97be304ddf is described below
commit 97be304ddf27e196c84710b7171fcd71782d0945
Author: Michael Bien <[email protected]>
AuthorDate: Tue Oct 12 21:33:10 2021 +0200
[jackpot] improved autoboxing optimization rules
- Boxed.valueOf() -> Boxed.parse[type]() (and vise versa) refactoring rule.
- Extracted 'new Boxed($v)' -> 'Boxed.valueOf($v)' to its own rule.
- added triggers for missing overloaded method variants.
- removed overlapping rules.
- added return from method/lambda case.
- added tests
---
.../modules/java/hints/errors/Utilities.java | 2 +-
.../modules/java/hints/perf/Bundle.properties | 12 +-
.../org/netbeans/modules/java/hints/perf/Tiny.java | 173 +++++++++++++----
.../netbeans/modules/java/hints/perf/TinyTest.java | 211 +++++++++++++++++++++
4 files changed, 352 insertions(+), 46 deletions(-)
diff --git
a/java/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java
b/java/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java
index 8ef45ac2eb..c6bb4f8d6b 100644
--- a/java/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java
+++ b/java/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java
@@ -1751,7 +1751,7 @@ public class Utilities {
}
}
- private static final Set<String> PRIMITIVE_NAMES = new HashSet<String>(7);
+ private static final Set<String> PRIMITIVE_NAMES = new HashSet<String>(8);
static {
PRIMITIVE_NAMES.add("java.lang.Integer"); // NOI18N
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 207d4a843d..fb967127f5 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
@@ -114,11 +114,15 @@ DN_RedundantToString=Redundant String.toString()
DESC_RedundantToString=Reports calls of String.toString(), which is entirely
useless, the String can be used directly.
DN_UnnecessaryTempFromString=Unnecessary temporary during conversion from
String
-DESC_UnnecessaryTempFromString=Finds occurrences of <i>new
Integer("111").intValue()</i> and similar constructions, where the \
- boxed instance is created just to parse the String parameter. Boxing types
have <i>parseXXX</i> methods, which perform the \
- conversion without creating the temporary instance.
+DESC_UnnecessaryTempFromString=Finds occurrences of
<i>Integer.valueOf($string)</i> and similar constructions, which \
+ are assigned to primitive types and replaces it with
<i>Integer.parseInt($string)</i> (vice versa for assignments to boxed types), \
+ to remove unnecessary temporary boxing.
DN_UnnecessaryTempToString=Unnecessary temporary during conversion to String
-DESC_UnnecessaryTempToString=Finds places like <i>new
Integer(11).toString()</i> where a temporary boxed instance is created to \
+DESC_UnnecessaryTempToString=Finds places like
<i>Integer.valueOf(11).toString()</i> where a temporary boxed instance is
created to \
just produce a String representation of a primitive. The boxed types have
<i>toString()</i> static method just for that purpose.
+DN_BoxedPrimitiveConstruction=Boxed Primitive instantiation
+DESC_BoxedPrimitiveConstruction=Replace the usage of deprecated boxed
primitive constructors (new Integer("5")) \
+ with their corresponding factory methods (Integer.valueOf("5")). This
inspection can be used in conjunction with \
+ <i>Unnecessary temporary during conversion from String</i>, to suggest
<i>parseXXX()</i> instead of <i>valueOf()</i> where applicable.
\ No newline at end of file
diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/perf/Tiny.java
b/java/java.hints/src/org/netbeans/modules/java/hints/perf/Tiny.java
index 6bf241aa43..0e158c4bc8 100644
--- a/java/java.hints/src/org/netbeans/modules/java/hints/perf/Tiny.java
+++ b/java/java.hints/src/org/netbeans/modules/java/hints/perf/Tiny.java
@@ -22,12 +22,15 @@ package org.netbeans.modules.java.hints.perf;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.LiteralTree;
import com.sun.source.tree.MemberSelectTree;
+import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.NewArrayTree;
+import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.ParameterizedTypeTree;
import com.sun.source.tree.ParenthesizedTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.util.TreePath;
+import com.sun.source.util.Trees;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
@@ -39,6 +42,7 @@ import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
+import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;
@@ -62,6 +66,10 @@ import
org.netbeans.spi.java.hints.JavaFix.TransformationContext;
import org.netbeans.spi.java.hints.JavaFixUtilities;
import org.openide.util.NbBundle;
+import static com.sun.source.tree.Tree.Kind.METHOD_INVOCATION;
+import static com.sun.source.tree.Tree.Kind.NEW_CLASS;
+import static javax.lang.model.type.TypeKind.EXECUTABLE;
+
/**
*
* @author lahvac
@@ -431,22 +439,32 @@ public class Tiny {
}
@TriggerPatterns({
- @TriggerPattern(value = "new java.lang.Byte($v).byteValue()",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
- @TriggerPattern(value = "new java.lang.Double($v).doubleValue()",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
- @TriggerPattern(value = "new java.lang.Float($v).floatValue()",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
- @TriggerPattern(value = "new java.lang.Integer($v).intValue()",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
- @TriggerPattern(value = "new java.lang.Long($v).longValue()",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
- @TriggerPattern(value = "new java.lang.Short($v).shortValue()",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
- @TriggerPattern(value = "new java.lang.Boolean($v).booleanValue()",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
-
-
- @TriggerPattern(value = "java.lang.Byte.valueOf($v).byteValue()",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
- @TriggerPattern(value = "java.lang.Double.valueOf($v).doubleValue()",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
- @TriggerPattern(value = "java.lang.Float.valueOf($v).floatValue()",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
- @TriggerPattern(value = "java.lang.Integer.valueOf($v).intValue()",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
- @TriggerPattern(value = "java.lang.Long.valueOf($v).longValue()",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
- @TriggerPattern(value = "java.lang.Short.valueOf($v).shortValue()",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
- @TriggerPattern(value =
"java.lang.Boolean.valueOf($v).booleanValue()", constraints =
@ConstraintVariableType(variable = "$v", type = "java.lang.String")),
+ @TriggerPattern(value = "java.lang.Integer.parseInt($v)", constraints
= @ConstraintVariableType(variable = "$v", type = "java.lang.String")),
+ @TriggerPattern(value = "java.lang.Byte.parseByte($v)", constraints =
@ConstraintVariableType(variable = "$v", type = "java.lang.String")),
+ @TriggerPattern(value = "java.lang.Short.parseShort($v)", constraints
= @ConstraintVariableType(variable = "$v", type = "java.lang.String")),
+ @TriggerPattern(value = "java.lang.Long.parseLong($v)", constraints =
@ConstraintVariableType(variable = "$v", type = "java.lang.String")),
+ @TriggerPattern(value = "java.lang.Float.parseFloat($v)", constraints
= @ConstraintVariableType(variable = "$v", type = "java.lang.String")),
+ @TriggerPattern(value = "java.lang.Double.parseDouble($v)",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
+ @TriggerPattern(value = "java.lang.Boolean.parseBoolean($v)",
constraints = @ConstraintVariableType(variable = "$v", type =
"java.lang.String")),
+
+ @TriggerPattern(value = "java.lang.Integer.parseInt($s, $r)",
constraints = {@ConstraintVariableType(variable = "$s", type =
"java.lang.String"), @ConstraintVariableType(variable = "$r", type = "int")}),
+ @TriggerPattern(value = "java.lang.Byte.parseByte($s, $r)",
constraints = {@ConstraintVariableType(variable = "$s", type =
"java.lang.String"), @ConstraintVariableType(variable = "$r", type = "int")}),
+ @TriggerPattern(value = "java.lang.Short.parseShort($s, $r)",
constraints = {@ConstraintVariableType(variable = "$s", type =
"java.lang.String"), @ConstraintVariableType(variable = "$r", type = "int")}),
+ @TriggerPattern(value = "java.lang.Long.parseLong($s, $r)",
constraints = {@ConstraintVariableType(variable = "$s", type =
"java.lang.String"), @ConstraintVariableType(variable = "$r", type = "int")}),
+
+
+ @TriggerPattern(value = "java.lang.Byte.valueOf($v)", constraints =
@ConstraintVariableType(variable = "$v", type = "java.lang.String")),
+ @TriggerPattern(value = "java.lang.Double.valueOf($v)", constraints =
@ConstraintVariableType(variable = "$v", type = "java.lang.String")),
+ @TriggerPattern(value = "java.lang.Float.valueOf($v)", constraints =
@ConstraintVariableType(variable = "$v", type = "java.lang.String")),
+ @TriggerPattern(value = "java.lang.Integer.valueOf($v)", constraints =
@ConstraintVariableType(variable = "$v", type = "java.lang.String")),
+ @TriggerPattern(value = "java.lang.Long.valueOf($v)", constraints =
@ConstraintVariableType(variable = "$v", type = "java.lang.String")),
+ @TriggerPattern(value = "java.lang.Short.valueOf($v)", constraints =
@ConstraintVariableType(variable = "$v", type = "java.lang.String")),
+ @TriggerPattern(value = "java.lang.Boolean.valueOf($v)", constraints =
@ConstraintVariableType(variable = "$v", type = "java.lang.String")),
+
+ @TriggerPattern(value = "java.lang.Byte.valueOf($v, $r)", constraints
= {@ConstraintVariableType(variable = "$v", type = "java.lang.String"),
@ConstraintVariableType(variable = "$r", type = "int")}),
+ @TriggerPattern(value = "java.lang.Integer.valueOf($v, $r)",
constraints = {@ConstraintVariableType(variable = "$v", type =
"java.lang.String"), @ConstraintVariableType(variable = "$r", type = "int")}),
+ @TriggerPattern(value = "java.lang.Long.valueOf($v, $r)", constraints
= {@ConstraintVariableType(variable = "$v", type = "java.lang.String"),
@ConstraintVariableType(variable = "$r", type = "int")}),
+ @TriggerPattern(value = "java.lang.Short.valueOf($v, $r)", constraints
= {@ConstraintVariableType(variable = "$v", type = "java.lang.String"),
@ConstraintVariableType(variable = "$r", type = "int")}),
})
@NbBundle.Messages({
"TEXT_UnnecessaryTempFromString=Unnecessary temporary when converting
from String",
@@ -462,36 +480,79 @@ public class Tiny {
suppressWarnings = "UnnecessaryTemporaryOnConversionFromString"
)
public static ErrorDescription unnecessaryTempFromString(HintContext ctx) {
- TypeMirror resType =
ctx.getInfo().getTrees().getTypeMirror(ctx.getPath());
- if (resType == null) {
+
+ String type;
+ String method;
+
+ // determine if the destination is primitive
+ TypeMirror destType = getDestinationType(ctx, ctx.getPath());
+ TypeMirror srcType =
ctx.getInfo().getTrees().getTypeMirror(ctx.getPath());
+
+ if (srcType == null || destType == null) {
return null;
+ } else if (destType.getKind().isPrimitive() &&
!srcType.getKind().isPrimitive()) {
+ srcType = ctx.getInfo().getTypes().unboxedType(srcType);
+ String[] replacement = PARSE_METHODS.get(srcType.getKind());
+ type = replacement[0];
+ method = replacement[1];
+ } else if (!destType.getKind().isPrimitive() &&
srcType.getKind().isPrimitive()) {
+ type = PARSE_METHODS.get(srcType.getKind())[0];
+ method = "valueOf"; // NOI18N
+ } else {
+ return null; // nothing to do, a different rule handles
.intValue() boxing problems
}
- if (resType.getKind() == TypeKind.BOOLEAN) {
- if
(ctx.getInfo().getSourceVersion().compareTo(SourceVersion.RELEASE_5) < 0) {
- // might alter new Boolean($v) to Boolean.valueOf($v), but
that's all we can do. JDK < 5 has no
- // primitive-valued pasre* method for booleans.
- return null;
+
+ if (srcType.getKind() == TypeKind.BOOLEAN &&
ctx.getInfo().getSourceVersion().compareTo(SourceVersion.RELEASE_5) < 0) {
+ return null; // JDK < 5 has no primitive-valued pasre* method for
booleans.
+ }
+
+ Fix fix = JavaFixUtilities.rewriteFix(ctx,
Bundle.FIX_UnnecessaryTempFromString1(type, method), ctx.getPath(), type + "."
+ method + "($v)"); // NOI18N
+ return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(),
Bundle.TEXT_UnnecessaryTempFromString(), fix); // NOI18N
+ }
+
+ private static TypeMirror getDestinationType(HintContext ctx, TreePath
path) {
+
+ TreePath parent = path.getParentPath();
+ Tree parentLeaf = parent.getLeaf();
+ Tree leaf = path.getLeaf();
+
+ Trees trees = ctx.getInfo().getTrees();
+
+ if (parentLeaf.getKind() == METHOD_INVOCATION) {
+
+ MethodInvocationTree met = (MethodInvocationTree) parentLeaf;
+ int index = met.getArguments().indexOf(leaf);
+ return paramTypeOfExecutable(trees.getTypeMirror(new
TreePath(path, met.getMethodSelect())), index);
+ } else if (parentLeaf.getKind() == NEW_CLASS) {
+
+ NewClassTree nct = (NewClassTree) parentLeaf;
+ int index = nct.getArguments().indexOf(leaf);
+ return paramTypeOfExecutable(trees.getElement(new TreePath(path,
nct)).asType(), index);
+ } else {
+
+ int pos = (int)
trees.getSourcePositions().getStartPosition(path.getCompilationUnit(), leaf);
+ List<? extends TypeMirror> type =
CreateElementUtilities.resolveType(
+ EnumSet.noneOf(ElementKind.class), ctx.getInfo(), parent,
leaf, pos, new TypeMirror[1], new int[1]);
+
+ if (!type.isEmpty()) {
+ return type.get(0);
}
}
- String[] arr = PARSE_METHODS.get(resType.getKind());
- if (arr == null) {
- return null; // just in case
+
+ return null;
+ }
+
+ private static TypeMirror paramTypeOfExecutable(TypeMirror executable, int
index) {
+ if (index != -1 && executable != null && executable.getKind() ==
EXECUTABLE) {
+ List<? extends TypeMirror> paramTypes = ((ExecutableType)
executable).getParameterTypes();
+ if (paramTypes.size() > index) {
+ return paramTypes.get(index);
+ }
}
- return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(),
Bundle.TEXT_UnnecessaryTempFromString(),
- JavaFixUtilities.rewriteFix(ctx,
Bundle.FIX_UnnecessaryTempFromString1(arr[0], arr[1]), ctx.getPath(),
- arr[0] + "." + arr[1] + "($v)")); // NOI18N
+ return null;
}
@TriggerPatterns({
- @TriggerPattern(value = "new java.lang.Byte($v).toString()",
constraints = @ConstraintVariableType(variable = "$v", type = "int")),
- @TriggerPattern(value = "new java.lang.Double($v).toString()",
constraints = @ConstraintVariableType(variable = "$v", type = "double")),
- @TriggerPattern(value = "new java.lang.Float($v).toString()",
constraints = @ConstraintVariableType(variable = "$v", type = "float")),
- @TriggerPattern(value = "new java.lang.Integer($v).toString()",
constraints = @ConstraintVariableType(variable = "$v", type = "int")),
- @TriggerPattern(value = "new java.lang.Long($v).toString()",
constraints = @ConstraintVariableType(variable = "$v", type = "long")),
- @TriggerPattern(value = "new java.lang.Short($v).toString()",
constraints = @ConstraintVariableType(variable = "$v", type = "int")),
- @TriggerPattern(value = "new java.lang.Boolean($v).toString()",
constraints = @ConstraintVariableType(variable = "$v", type = "boolean")),
-
-
@TriggerPattern(value = "java.lang.Byte.valueOf($v).toString()",
constraints = @ConstraintVariableType(variable = "$v", type = "int")),
@TriggerPattern(value = "java.lang.Double.valueOf($v).toString()",
constraints = @ConstraintVariableType(variable = "$v", type = "double")),
@TriggerPattern(value = "java.lang.Float.valueOf($v).toString()",
constraints = @ConstraintVariableType(variable = "$v", type = "float")),
@@ -523,7 +584,37 @@ public class Tiny {
return null; // just in case
}
return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(),
Bundle.TEXT_UnnecessaryTempFromString(),
- JavaFixUtilities.rewriteFix(ctx,
Bundle.FIX_UnnecessaryTempToString(arr[0]), ctx.getPath(),
- arr[0] + ".toString($v)")); // NOI18N
+ JavaFixUtilities.rewriteFix(ctx,
Bundle.FIX_UnnecessaryTempToString(arr[0]), ctx.getPath(), arr[0] +
".toString($v)")); // NOI18N
+ }
+
+ // TODO move to jdk package?
+ @TriggerPatterns({
+ @TriggerPattern(value = "new java.lang.Byte($v)"),
+ @TriggerPattern(value = "new java.lang.Double($v)"),
+ @TriggerPattern(value = "new java.lang.Float($v)"),
+ @TriggerPattern(value = "new java.lang.Integer($v)"),
+ @TriggerPattern(value = "new java.lang.Long($v)"),
+ @TriggerPattern(value = "new java.lang.Short($v)"),
+ @TriggerPattern(value = "new java.lang.Boolean($v)"),
+ })
+ @NbBundle.Messages({
+ "TEXT_BoxedPrimitiveConstruction=Replace usage of deprecated boxed
primitive constructors with factory methods.",
+ "# {0} - wrapper type simple name",
+ "FIX_BoxedPrimitiveConstruction=Replace with {0}.valueOf()",
+ })
+ @Hint(
+ displayName = "#DN_BoxedPrimitiveConstruction",
+ description = "#DESC_BoxedPrimitiveConstruction",
+ enabled = true,
+ category = "rules15",
+ suppressWarnings = "BoxedPrimitiveConstruction"
+ )
+ public static ErrorDescription boxedPrimitiveConstruction(HintContext ctx)
{
+ TypeMirror resType =
ctx.getInfo().getTrees().getTypeMirror(ctx.getPath());
+ if (resType == null) {
+ return null;
+ }
+ return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(),
Bundle.TEXT_BoxedPrimitiveConstruction(),
+ JavaFixUtilities.rewriteFix(ctx,
Bundle.FIX_BoxedPrimitiveConstruction(resType), ctx.getPath(), resType +
".valueOf($v)")); // NOI18N
}
-}
\ No newline at end of file
+}
diff --git
a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/perf/TinyTest.java
b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/perf/TinyTest.java
index 2cfebb4258..f5ca516df3 100644
---
a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/perf/TinyTest.java
+++
b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/perf/TinyTest.java
@@ -503,4 +503,215 @@ public class TinyTest extends NbTestCase {
.findWarning("4:22-4:29:verifier:ERR_Tiny_collectionsToArray")
.assertFixes();
}
+
+ public void testValueOfToParse1() throws Exception {
+ HintTest.create()
+ .input("package test;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " float prim = Float.parseFloat(\"5\");\n" +
+ " prim = Float.valueOf(\"6\");\n" +
+ " }\n" +
+ "}\n")
+ .run(Tiny.class)
+ .findWarning("4:16-4:34:verifier:Unnecessary temporary when
converting from String")
+ .applyFix()
+ .assertCompilable()
+ .assertOutput(
+ "package test;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " float prim = Float.parseFloat(\"5\");\n" +
+ " prim = Float.parseFloat(\"6\");\n" +
+ " }\n" +
+ "}\n");
+ }
+
+ public void testValueOfToParse2() throws Exception {
+ HintTest.create()
+ .input("package test;\n" +
+ "import java.util.ArrayList;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " ArrayList arrayList = new
ArrayList(Integer.valueOf(\"6\"));\n" +
+ " arrayList.get(Integer.parseInt(\"1\"));\n" +
+ " }\n" +
+ "}\n")
+ .run(Tiny.class)
+ .findWarning("4:45-4:65:verifier:Unnecessary temporary when
converting from String")
+ .applyFix()
+ .assertCompilable()
+ .assertOutput(
+ "package test;\n" +
+ "import java.util.ArrayList;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " ArrayList arrayList = new
ArrayList(Integer.parseInt(\"6\"));\n" +
+ " arrayList.get(Integer.parseInt(\"1\"));\n" +
+ " }\n" +
+ "}\n")
+ ;
+ }
+
+ public void testValueOfToParse3() throws Exception {
+ HintTest.create()
+ .input("package test;\n" +
+ "import java.util.ArrayList;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " ArrayList arrayList = new
ArrayList(Integer.parseInt(\"6\"));\n" +
+ " arrayList.get(Integer.valueOf(\"1\"));\n" +
+ " }\n" +
+ "}\n")
+ .run(Tiny.class)
+ .findWarning("5:23-5:43:verifier:Unnecessary temporary when
converting from String")
+ .applyFix()
+ .assertCompilable()
+ .assertOutput(
+ "package test;\n" +
+ "import java.util.ArrayList;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " ArrayList arrayList = new
ArrayList(Integer.parseInt(\"6\"));\n" +
+ " arrayList.get(Integer.parseInt(\"1\"));\n" +
+ " }\n" +
+ "}\n");
+ }
+
+ public void testValueOfToParseInMethodReturn() throws Exception {
+ HintTest.create()
+ .input("package test;\n" +
+ "public class Test {\n" +
+ " private int test() {\n" +
+ " return Integer.valueOf(\"1\");\n" +
+ " }\n" +
+ "}\n")
+ .run(Tiny.class)
+ .findWarning("3:16-3:36:verifier:Unnecessary temporary when
converting from String")
+ .applyFix()
+ .assertCompilable()
+ .assertOutput(
+ "package test;\n" +
+ "public class Test {\n" +
+ " private int test() {\n" +
+ " return Integer.parseInt(\"1\");\n" +
+ " }\n" +
+ "}\n");
+ }
+
+ public void testParseToValueOfInLambdaReturn() throws Exception {
+ HintTest.create()
+ .sourceLevel("8")
+ .input("package test;\n" +
+ "import java.util.concurrent.Callable;\n" +
+ "import java.util.concurrent.Executors;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " Executors.newFixedThreadPool(1).submit(() ->
{\n" +
+ " return Integer.parseInt(\"1\");\n" +
+ " });" +
+ " }\n" +
+ "}\n")
+ .run(Tiny.class)
+ .findWarning("6:20-6:41:verifier:Unnecessary temporary when
converting from String")
+ .applyFix()
+ .assertCompilable()
+ .assertOutput(
+ "package test;\n" +
+ "import java.util.concurrent.Callable;\n" +
+ "import java.util.concurrent.Executors;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " Executors.newFixedThreadPool(1).submit(() ->
{\n" +
+ " return Integer.valueOf(\"1\");\n" +
+ " });" +
+ " }\n" +
+ "}\n");
+ }
+
+ public void testParseToValueOf() throws Exception {
+ HintTest.create()
+ .input("package test;\n" +
+ "import java.util.HashSet;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " new
HashSet().add(Float.parseFloat(\"6\"));\n" +
+ " }\n" +
+ "}\n")
+ .run(Tiny.class)
+ .findWarning("4:27-4:48:verifier:Unnecessary temporary when
converting from String")
+ .applyFix()
+ .assertCompilable()
+ .assertOutput(
+ "package test;\n" +
+ "import java.util.HashSet;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " new HashSet().add(Float.valueOf(\"6\"));\n" +
+ " }\n" +
+ "}\n");
+ }
+
+ public void testNewObjectToValueOf_step1() throws Exception {
+ HintTest.create()
+ .input("package test;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " byte b = new Byte(\"5\");\n" +
+ " }\n" +
+ "}\n")
+ .run(Tiny.class)
+ .findWarning("3:18-3:31:verifier:Replace usage of deprecated
boxed primitive constructors with factory methods.")
+ .applyFix()
+ .assertCompilable()
+ .assertOutput(
+ "package test;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " byte b = Byte.valueOf(\"5\");\n" +
+ " }\n" +
+ "}\n");
+ }
+
+ public void testValueOfToParse_step2() throws Exception {
+ HintTest.create()
+ .input("package test;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " byte b = Byte.valueOf(\"5\");\n" +
+ " }\n" +
+ "}\n")
+ .run(Tiny.class)
+ .findWarning("3:18-3:35:verifier:Unnecessary temporary when
converting from String")
+ .applyFix()
+ .assertCompilable()
+ .assertOutput(
+ "package test;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " byte b = Byte.parseByte(\"5\");\n" +
+ " }\n" +
+ "}\n");
+ }
+
+ public void testValueOfToParseInBinaryOp() throws Exception {
+ HintTest.create()
+ .input("package test;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " if (0 < Byte.valueOf(\"5\")) {}\n" +
+ " }\n" +
+ "}\n")
+ .run(Tiny.class)
+ .findWarning("3:17-3:34:verifier:Unnecessary temporary when
converting from String")
+ .applyFix()
+ .assertCompilable()
+ .assertOutput(
+ "package test;\n" +
+ "public class Test {\n" +
+ " private void test() {\n" +
+ " if (0 < Byte.parseByte(\"5\")) {}\n" +
+ " }\n" +
+ "}\n");
+ }
}
\ No newline at end of file
---------------------------------------------------------------------
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