mbien commented on a change in pull request #3240:
URL: https://github.com/apache/netbeans/pull/3240#discussion_r777225444
##########
File path: java/java.hints/src/org/netbeans/modules/java/hints/perf/Tiny.java
##########
@@ -462,36 +485,94 @@ public static ErrorDescription
redundantToString(HintContext ctx) {
suppressWarnings = "UnnecessaryTemporaryOnConversionFromString"
)
public static ErrorDescription unnecessaryTempFromString(HintContext ctx) {
- TypeMirror resType =
ctx.getInfo().getTrees().getTypeMirror(ctx.getPath());
- if (resType == null) {
- return 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 (destType instanceof PrimitiveType && !(srcType instanceof
PrimitiveType)) {
+ srcType = ctx.getInfo().getTypes().unboxedType(srcType);
+ String[] replacement = PARSE_METHODS.get(srcType.getKind());
+ type = replacement[0];
+ method = replacement[1];
+ } else if (!(destType instanceof PrimitiveType) && srcType instanceof
PrimitiveType) {
+ 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.
}
- String[] arr = PARSE_METHODS.get(resType.getKind());
- if (arr == null) {
- return null; // just in case
+
+ 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();
+
+ Types types = ctx.getInfo().getTypes();
+ Trees trees = ctx.getInfo().getTrees();
+
+ if (parentLeaf instanceof MethodInvocationTree) {
+
+ MethodInvocationTree met = (MethodInvocationTree) parentLeaf;
+
+ int index = met.getArguments().indexOf(path.getLeaf());
+
+ TypeMirror method = trees.getElement(new TreePath(path,
met.getMethodSelect())).asType();
Review comment:
I tried getTypeMirror first actually and it worked for methods, but I
could not figure out how to make the NewClassTree work too. So instead of using
two ways of handling very similar branches of an if, i decided to solve both
the same.
The current version I just committed uses getTypeMirror for the method
branch and Element.asType for the class branch. I would need some help to make
both use getTypeMirror (i am missing the getMethodSelect() equivalent for
constructors) - since i could not figure out how.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
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