diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/search/FindTypesTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/search/FindTypesTest.java index d85bd134a47..321f560b763 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/search/FindTypesTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/search/FindTypesTest.java @@ -47,6 +47,10 @@ public static void stat() {} @Test void simpleName() { rewriteRun( + spec -> spec.dataTable(TypeUses.Row.class, rows -> assertThat(rows) + .containsExactly( + new TypeUses.Row("B.java", "A1", "a.A1") + )), java( """ import a.A1; diff --git a/rewrite-java/src/main/java/org/openrewrite/java/search/FindTypes.java b/rewrite-java/src/main/java/org/openrewrite/java/search/FindTypes.java index 3b9c7f4d254..f9e4a051cf7 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/search/FindTypes.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/search/FindTypes.java @@ -206,11 +206,14 @@ public J visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext ctx) { private J2 found(J2 j, ExecutionContext ctx) { JavaType.FullyQualified fqn = TypeUtils.asFullyQualified(j.getType()); - typeUses.insertRow(ctx, new TypeUses.Row( - getCursor().firstEnclosingOrThrow(SourceFile.class).getSourcePath().toString(), - j.printTrimmed(getCursor().getParentTreeCursor()), - fqn == null ? j.getType().toString() : fqn.getFullyQualifiedName() - )); + if (!j.getMarkers().findFirst(SearchResult.class).isPresent()) { + // Avoid double-counting results in the data table + typeUses.insertRow(ctx, new TypeUses.Row( + getCursor().firstEnclosingOrThrow(SourceFile.class).getSourcePath().toString(), + j.printTrimmed(getCursor().getParentTreeCursor()), + fqn == null ? j.getType().toString() : fqn.getFullyQualifiedName() + )); + } return SearchResult.found(j); } }