Fail closed on ambiguous simple types in implementation widening

Prevent getImplementations("Name") from widening to unrelated classes when multiple types share the same simple name across packages.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-13 21:53:37 +02:00
parent f3c82d5d7f
commit 4ca42ae3dc
2 changed files with 29 additions and 0 deletions

View File

@@ -250,6 +250,29 @@ class CodebaseContextTest {
assertThat(context.getEnumValues("b.OrderEvent")).containsExactly("b.OrderEvent.SHIP");
}
@Test
void getImplementationsShouldFailClosedForAmbiguousSimpleTypeName() throws IOException {
Files.createDirectories(tempDir.resolve("a"));
Files.createDirectories(tempDir.resolve("b"));
Files.writeString(tempDir.resolve("a/Service.java"),
"package a; public interface Service {}");
Files.writeString(tempDir.resolve("a/AServiceImpl.java"),
"package a; public class AServiceImpl implements Service {}");
Files.writeString(tempDir.resolve("b/Service.java"),
"package b; public interface Service {}");
Files.writeString(tempDir.resolve("b/BServiceImpl.java"),
"package b; public class BServiceImpl implements Service {}");
CodebaseContext context = new CodebaseContext();
context.scan(tempDir);
assertThat(context.getImplementations("Service"))
.as("ambiguous simple interface name must not widen to arbitrary impls")
.isEmpty();
assertThat(context.getImplementations("a.Service")).contains("a.AServiceImpl");
assertThat(context.getImplementations("b.Service")).contains("b.BServiceImpl");
}
@Test
void shouldFindClassWithFullyQualifiedAnnotation() throws IOException {
String source = """