-
Notifications
You must be signed in to change notification settings - Fork 79
Handle static method calls when removing unnecessary explicit type arguments. #547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle static method calls when removing unnecessary explicit type arguments. #547
Conversation
@@ -64,7 +80,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu | |||
.count() > 1) { | |||
return m; | |||
} | |||
enclosingType = enclosingMethod.getType(); | |||
enclosingType = m.getMethodType().getReturnType(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change might be to simplistic, but when the enclosing
is a method invocation:
We're dealing with GenericClass.<String>someTypedBuilder(String.class)
, and enclosing
is GenericClass.<String>someTypedBuilder(String.class).build()
<String>
is already kept and returned earlier if the argument of the method does not do type inference (e.g. it does not take Class<T> arg
. If it does do type inference, the method is its own enclosing type.
E.G.
When GenericClass.<String>someTypedBuilder(String.class)
returns a type inferred instance GenericClassBuild<String>
,
GenericClass.<String>someTypedBuilder(String.class).build()
should be the same as
GenericClassBuild<String> gcb = GenericClass.someTypedBuilder(String.class);
gcb.build();
and so the explicit generic type can be removed?
Please double check my logic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assigning the returnType to enclosingType
is a bit of an ugly way of forcing the existing code to remove explicit generic type information.
Maybe I should rename enclosingType
to inferedType
?
if (enclosingType != null && TypeUtils.isOfType(enclosingType, m.getMethodType().getReturnType())) {
m = m.withTypeParameters(null);
}
if the return type of the method invocation is the same as the inferred type, the explicit type arguments may be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I should rename enclosingType to inferedType?
Yeah do that, that's way easier to understand if you read this recipe for the first time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, leaving this open for @timtebeek to have this context when he reviews.
src/main/java/org/openrewrite/staticanalysis/UnnecessaryExplicitTypeArguments.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/UnnecessaryExplicitTypeArguments.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- src/main/resources/META-INF/rewrite/examples.yml
- lines 636-652
- lines 698-697
- lines 2311-2310
- lines 2326-2338
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix here! Great to not make incorrect changes where we can avoid it.
…oves-necessary-generic-type-argument-from-builder-method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- src/main/resources/META-INF/rewrite/examples.yml
- lines 474-473
- lines 636-652
- lines 698-697
- lines 2311-2310
- lines 2326-2338
What's changed?
Adde rules to the removing unnecessary explicit type arguments recipe to deal with static method calls.
What's your motivation?
The recipe currently removes explicit type arguments from method calls that need it.