Skip to content

Commit

Permalink
Refactor MethodMatcher#matchesTargetType (openrewrite#3372)
Browse files Browse the repository at this point in the history
Extracts `MethodMatcher#matchesTargetType` into `TypeUtils#isOfTypeWithName`.

This makes is easier for external actors to implement cusotm `MethodMatcher`
implementations without copying and pasting logic out of `MethodMatcher`.

Signed-off-by: Jonathan Leitschuh <[email protected]>
  • Loading branch information
JLLeitschuh authored Jun 30, 2023
1 parent 35d936d commit 19d2132
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
30 changes: 5 additions & 25 deletions rewrite-java/src/main/java/org/openrewrite/java/MethodMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,31 +148,11 @@ private boolean matchesTargetTypeName(String fullyQualifiedTypeName) {
}

boolean matchesTargetType(@Nullable JavaType.FullyQualified type) {
if (type == null || type instanceof JavaType.Unknown) {
return false;
}

if (matchesTargetTypeName(type.getFullyQualifiedName())) {
return true;
}

if (matchOverrides) {
if (!"java.lang.Object".equals(type.getFullyQualifiedName()) && matchesTargetType(OBJECT_CLASS)) {
return true;
}

if (matchesTargetType(type.getSupertype())) {
return true;
}

for (JavaType.FullyQualified anInterface : type.getInterfaces()) {
if (matchesTargetType(anInterface)) {
return true;
}
}
}

return false;
return TypeUtils.isOfTypeWithName(
type,
matchOverrides,
this::matchesTargetTypeName
);
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
package org.openrewrite.java.tree;

import org.openrewrite.Incubating;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaTypeSignatureBuilder;
import org.openrewrite.java.internal.DefaultJavaTypeSignatureBuilder;

import java.util.*;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -137,6 +139,42 @@ public static boolean isOfClassType(@Nullable JavaType type, String fqn) {
return false;
}

/**
* @param type The declaring type of the method invocation or constructor.
* @param matchOverride Whether to match the {@code Object} type.
* @return True if the declaring type matches the criteria of this matcher.
*/
@Incubating(since = "8.1.4")
public static boolean isOfTypeWithName(
@Nullable JavaType.FullyQualified type,
boolean matchOverride,
Predicate<String> matcher
) {
if (type == null || type instanceof JavaType.Unknown) {
return false;
}
if (matcher.test(type.getFullyQualifiedName())) {
return true;
}
if (matchOverride) {
if (!"java.lang.Object".equals(type.getFullyQualifiedName()) &&
isOfTypeWithName(TYPE_OBJECT, true, matcher)) {
return true;
}

if (isOfTypeWithName(type.getSupertype(), true, matcher)) {
return true;
}

for (JavaType.FullyQualified anInterface : type.getInterfaces()) {
if (isOfTypeWithName(anInterface, true, matcher)) {
return true;
}
}
}
return false;
}

public static boolean isAssignableTo(@Nullable JavaType to, @Nullable JavaType from) {
try {
if (to instanceof JavaType.Unknown || from instanceof JavaType.Unknown) {
Expand Down

0 comments on commit 19d2132

Please sign in to comment.