Skip to content

Commit

Permalink
fix build & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timo-abele committed Jan 19, 2024
1 parent 172f644 commit 758e7cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,22 @@ private static boolean hasLambdaArgument(J.MethodInvocation method) {
return hasLambdaArgument;
}

// Check whether a method has overloading methods in the declaring class
static boolean hasAmbiguousMethodOverloading(J.MethodInvocation method) {
// Check whether a method has overloading methods in the declaring class
private static boolean hasAmbiguousMethodOverloading(J.MethodInvocation method) {
JavaType.Method methodType = method.getMethodType();
String methodName = methodType.getName();
return methodType != null && hasAmbiguousMethodOverloading(methodType, method.getArguments(), methodName);
}

if(methodType == null) {
return false;
}
int numberOfArguments = method.getArguments().size();
static boolean hasAmbiguousMethodOverloading(MethodCall method) {
JavaType.Method methodType = method.getMethodType();
String methodName = methodType.getName();
return methodType != null && hasAmbiguousMethodOverloading(methodType, method.getArguments(), methodName);
}

// TODO this is actually more complex in the presence of generics and inheritance
String methodName = methodType.getName();
static boolean hasAmbiguousMethodOverloading(JavaType.Method methodType, List<Expression> arguments, String methodName) {
int numberOfArguments = arguments.size();

//all methods of the given type
List<JavaType.Method> methodsOfType = Optional.of(methodType)
Expand All @@ -128,7 +133,7 @@ static boolean hasAmbiguousMethodOverloading(J.MethodInvocation method) {
// then there is no ambiguity
for (int i = 0; i < numberOfArguments; i++) {
int finalI = i;
if (method.getArguments().get(i) instanceof J.Lambda) {
if (arguments.get(i) instanceof J.Lambda) {
long distinctElementsCount = potentiallyOverLoadedMethods.stream()
.map(m -> m.getParameterTypes().get(finalI))
.distinct().count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.List;
import java.util.Set;

import static org.openrewrite.staticanalysis.LambdaBlockToExpression.hasMethodOverloading;
import static org.openrewrite.staticanalysis.LambdaBlockToExpression.hasAmbiguousMethodOverloading;

@Incubating(since = "7.23.0")
public class RemoveRedundantTypeCast extends Recipe {
Expand Down Expand Up @@ -74,8 +74,7 @@ public J visitTypeCast(J.TypeCast typeCast, ExecutionContext ctx) {
} else if (parentValue instanceof MethodCall) {
MethodCall methodCall = (MethodCall) parentValue;
JavaType.Method methodType = methodCall.getMethodType();
int numberOfArguments = methodCall.getArguments().size();
if (methodType == null || hasMethodOverloading(methodType, numberOfArguments)) {
if (methodType == null || hasAmbiguousMethodOverloading(methodCall)) {
return visited;
} else if (!methodType.getParameterTypes().isEmpty()) {
List<Expression> arguments = methodCall.getArguments();
Expand Down

0 comments on commit 758e7cf

Please sign in to comment.