You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2022/03/02 13:03:00 UTC
[GitHub] [netbeans] mbien commented on a change in pull request #3240: [jackpot] Updates to autoboxing inspections
mbien commented on a change in pull request #3240:
URL: https://github.com/apache/netbeans/pull/3240#discussion_r817669270
##########
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:
@jlahoda ping :)
--
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: notifications-unsubscribe@netbeans.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists