jlahoda commented on a change in pull request #3240:
URL: https://github.com/apache/netbeans/pull/3240#discussion_r777135856



##########
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) {

Review comment:
       There is 
`java/java.hints/src/org/netbeans/modules/java/hints/errors/CreateElementUtilities.java#resolveType`
 which presumably does mostly what is needed here. There are some tricks played 
there, but I hope that could work.

##########
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)) {

Review comment:
       Generally, the javac APIs discourage `instanceof` and encourage use of 
`.getKind() == <type>` (or probably `.getKind().isPrimitive()` in this case), 
even though that obviously lacks the implicit null check. The reason is that 
one implementation class may implement many of these interfaces (applies to all 
`javax.lang.model.**` and `com.sun.source.**` that have kinds). As a real-world 
example, the `com.sun.tools.javac.code.Type.ClassType` implements both 
`DeclaredType` and `ErrorType`, so doing `instanceof DeclaredType` or 
`instanceof ErrorType` would not work properly. The `.getKind()` returns the 
correct kind for the given type.

##########
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:
       As a general note - doing `Element.asType()` will return the type of the 
generic declaration (i.e. a type with the generic type parameters, like e.g. 
`(E)boolean` for `List.add`). What is likely needed here is the actual types 
used for the method invocation, with generic types (if any) resolved (e.g. 
`(Integer)boolean` for `List<Integer> l; l.add(...)`). 
`trees.getTypeMirror(<method-select>)` should return that, AFAIK.
   
   (Note there's a slight inconsistency/difference between the `getElement` and 
`getTypeMirror` methods:
   -if the method select is passed into the methods, they will return the 
`ExecutableElement` and `ExecutableType` for the invocation
   -if the method invocation is passed into the methods, `getElement` will 
still return the `ExecutableElement`, but `getTypeMirror` will return the 
actual return type (with all generics resolved as needed)
   )




-- 
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

Reply via email to