task-6: moduleSources 目录聚合入口落地 8 例(RED 实录 8 格全红);实测发现 plan 原文「相对 src/main/java 解析」与本仓布局不符——模块目录实相对包基址 com/nanri/aiimage,只按字面解析读回 0 支,正是要防的死法

This commit is contained in:
2026-10-16 16:22:43 +08:00
parent d4107ca904
commit 77b4f49f55
2 changed files with 147 additions and 0 deletions
@@ -22,6 +22,9 @@ public final class SourceScan {
public record CallSite(String bareName, String relativeFile, int line) {
}
/** 本仓主源码的包基址:模块目录实际相对的是它,不是 src/main/java 本身。 */
private static final String PACKAGE_BASE = "com/nanri/aiimage";
private static final Pattern CALL_SITE = Pattern.compile("sourceText\\(\\s*\"([^\"]+)\"\\s*\\)");
/** {@code src/test/java} 的绝对根,与 {@link #mainSources()} 同一条定位分支。 */
@@ -134,7 +137,59 @@ public final class SourceScan {
return java.util.Map.copyOf(grouped);
}
/**
* 模块聚合入口(N6 定「正常返回」):列出 {@code src/main/java} 下该模块目录里所有 {@code .java}。
*
* <p>为什么需要它:归属类护栏此前只能 {@code sourceText("XxxService.java")} 逐文件点,而一座上帝类拆成
* {@code service/} + {@code service/support/} 之后,被禁写法只要搬进 support 文件,旧格子<b>照样绿</b>。
* 目录口径才是这条护栏的成立前提,所以这里按目录求值,且递归任意深度(N7)。</p>
*
* <p>本格只定「目录存在 ⇒ 返回不可变、按相对路径字典序的 {@code List<Path>}」。
* 「目录不存在 ⇒ 抛」由 N10 定义、「0 支 .java ⇒ 抛」由 N11 定义(零文件绝不许读成零违规)。</p>
*/
public static List<Path> moduleSources(String moduleRelDir) {
return moduleSources(mainSourcesRoot(), moduleRelDir);
}
/** 可注入语料根的同义重载:合成树场景(中文目录、越界、空目录)必须在不动业务树的前提下被测。 */
public static List<Path> moduleSources(Path javaRoot, String moduleRelDir) {
Path root = javaRoot.toAbsolutePath();
if (!Files.isDirectory(root)) {
throw new IllegalStateException("源码根不存在: " + root + "(cwd=" + Path.of("").toAbsolutePath() + ")");
}
String normalized = moduleRelDir == null ? "" : moduleRelDir.replace('\\', '/');
while (normalized.startsWith("/")) {
normalized = normalized.substring(1);
}
while (normalized.endsWith("/")) {
normalized = normalized.substring(0, normalized.length() - 1);
}
Path module = root.resolve(normalized).normalize();
if (!Files.isDirectory(module)) {
/*
* ⚠ plan N6 原文写「相对 {@code src/main/java} 解析目录」,但本仓的模块目录实际相对的是
* <b>包基址</b>:真实路径是 {@code src/main/java/com/nanri/aiimage/modules/appearancepatent}。
* 只按 plan 字面解析会得到 0 支文件——而那正是 N11 要防的死法(读不到被当成没违规)。
* 所以这里先按字面试、再按包基址试;两条都不存在时保留字面路径,交给 N10 的「目录不存在 ⇒ 抛」。
*/
Path underBase = root.resolve(PACKAGE_BASE).resolve(normalized).normalize();
if (Files.isDirectory(underBase)) {
module = underBase;
}
}
if (!normalized.isEmpty() && !module.startsWith(root)) {
throw new IllegalArgumentException("模块目录越界出 src/main/java: 请求「" + moduleRelDir
+ "」解析为 " + module + "(根=" + root + ",cwd=" + Path.of("").toAbsolutePath() + ")"
+ " —— 聚合面只许吃主源码树,越界就等于把整台机器读进来");
}
if (!Files.isDirectory(module)) {
return List.of(); // N10 会把它改成抛;N6 只定正常返回
}
return List.copyOf(collectJavaFiles(module));
}
private static List<Path> collectJavaFiles(Path root) {
try (Stream<Path> walk = Files.walk(root)) {
return walk.filter(Files::isRegularFile)
.filter(p -> p.getFileName().toString().endsWith(".java"))
@@ -262,4 +262,96 @@ class SourceScanTest {
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("cwd=");
}
// ---------------------------------------------------------------- N6 moduleSources
@Test
void moduleSources_appearancepatent返回28支含既有support件() {
List<Path> files = SourceScan.moduleSources("modules/appearancepatent");
assertThat(files)
.as("真实集成:对真仓目录求值。28 支=1 controller+1 client+1 service 主件+3 spi/cache+"
+ "5 service/support+16 model(dto7/vo9),与 find 实测同号")
.hasSize(28);
assertThat(files.stream().map(p -> p.toString().replace('\\', '/')).toList())
.as("support 目录里的既有件必须在面里——聚合入口存在的理由就是拆分后的新文件要自动被看到")
.anyMatch(rel -> rel.endsWith("service/support/AppearancePatentRowNormalizer.java"));
}
@Test
void moduleSources_返回列表不可变() {
List<Path> files = SourceScan.moduleSources("modules/appearancepatent");
assertThatThrownBy(() -> files.add(Path.of("塞一支进去.java")))
.as("调用方改坏面 ⇒ 同一轮里后一格读到的就不是真面了")
.isInstanceOf(UnsupportedOperationException.class);
}
@Test
void moduleSources_相对路径与绝对路径两种写法都接受() {
List<Path> plain = SourceScan.moduleSources("modules/appearancepatent");
assertThat(SourceScan.moduleSources("/modules/appearancepatent"))
.as("前导斜杠写法与裸相对写法必须同面,否则同一个模块会有两种口径")
.isEqualTo(plain);
assertThat(SourceScan.moduleSources("modules/appearancepatent/"))
.as("尾斜杠同理")
.isEqualTo(plain);
assertThat(SourceScan.moduleSources("modules\\appearancepatent"))
.as("Windows 反斜杠写法")
.isEqualTo(plain);
}
@Test
void moduleSources_空目录场景留给N11本task先返回空() throws IOException {
Path javaRoot = Files.createDirectories(tmp.resolve("N6空场景树/src/main/java"));
Files.createDirectories(javaRoot.resolve("modules/zzz_empty"));
assertThat(SourceScan.moduleSources(javaRoot, "modules/zzz_empty"))
.as("N6 只定「正常返回」:目录存在且为空 ⇒ 先返回空表不抛。"
+ "N11 会把这条改成抛(零文件绝不许读成零违规),届时更新本例")
.isEmpty();
}
@Test
void moduleSources_深层相对路径越界到src外时拒绝() {
assertThatThrownBy(() -> SourceScan.moduleSources("modules/../../../Windows"))
.as("`..` 逃逸出 src/main/java ⇒ 必须挡。挡不住就等于聚合面能把整台机器读进来")
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("越界");
}
@Test
void moduleSources_路径含中文时正确解析() throws IOException {
Path javaRoot = Files.createDirectories(tmp.resolve("N6中文树/src/main/java"));
Path module = Files.createDirectories(javaRoot.resolve("modules/模块中文"));
Files.writeString(module.resolve("中文件.java"), "class A {}", StandardCharsets.UTF_8);
List<Path> chinese = SourceScan.moduleSources(javaRoot, "modules/模块中文");
assertThat(chinese)
.as("主源码树今天没有中文名件,但语料根与相对目录都得过 UTF-8 这条道:这里用合成树把它钉住")
.hasSize(1);
assertThat(chinese.get(0).getFileName().toString()).isEqualTo("中文件.java");
}
@Test
void moduleSources_返回类型是Path不是字符串() {
List<Path> files = SourceScan.moduleSources("modules/appearancepatent");
assertThat(files).allSatisfy(p -> assertThat(p).isInstanceOf(Path.class));
assertThat(files).allSatisfy(p -> assertThat(p.isAbsolute()).isTrue());
assertThat(files).allSatisfy(p -> assertThat(Files.isRegularFile(p)).isTrue());
}
@Test
void moduleSources_找不到源码根时抛并回显cwd() {
Path wrong = tmp.resolve("少爬了一层/src/main/java");
assertThatThrownBy(() -> SourceScan.moduleSources(wrong, "modules/appearancepatent"))
.as("根目录不存在必须抛并回显 cwd——静默返回空表会让整条聚合护栏恒绿")
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("cwd=")
.hasMessageContaining(Path.of("").toAbsolutePath().toString());
}
}