task-4: SourceScan 点名普查落地 8 例(RED 实录 1 Failure+7 Errors);台账 15 调用点/8 调用方/10 裸名,plan 原文「9 支文件」经核为把 SourceScan.java:22 声明行算进分母
This commit is contained in:
@@ -5,6 +5,8 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -16,6 +18,83 @@ public final class SourceScan {
|
||||
private SourceScan() {
|
||||
}
|
||||
|
||||
/** 一处 {@code sourceText("<裸名>")} 字面量点名。 */
|
||||
public record CallSite(String bareName, String relativeFile, int line) {
|
||||
}
|
||||
|
||||
private static final Pattern CALL_SITE = Pattern.compile("sourceText\\(\\s*\"([^\"]+)\"\\s*\\)");
|
||||
|
||||
/** {@code src/test/java} 的绝对根,与 {@link #mainSources()} 同一条定位分支。 */
|
||||
public static Path testSourcesRoot() {
|
||||
Path root = moduleRoot().resolve("src/test/java");
|
||||
if (!Files.isDirectory(root)) {
|
||||
throw new IllegalStateException("语料根不存在: " + root + "(cwd=" + Path.of("").toAbsolutePath() + ")");
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/** 点名普查:列出 {@code src/test/java} 里每一处 {@code sourceText("<裸名>")} 字面量调用。 */
|
||||
public static List<CallSite> censusSourceTextCallSites() {
|
||||
return censusSourceTextCallSites(testSourcesRoot());
|
||||
}
|
||||
|
||||
/**
|
||||
* 点名普查(可指定语料根)。三条设计决定都是被坑逼出来的:
|
||||
* <ol>
|
||||
* <li><b>数的是代码不是文字</b>:判据打在 {@link #withoutComments} 上,注释里提一句
|
||||
* {@code sourceText("Fake.java")} 不计入——否则台账的 15 会被 javadoc 喂成任意大的数。</li>
|
||||
* <li><b>只配字面量</b>:正则要求紧跟一对引号,所以 {@code SourceScan.java:22} 那行
|
||||
* 被调方自己的声明(参数是 {@code String fileName})天然不算调用点。plan 原文的「9 支文件」
|
||||
* 正是 {@code grep -rl 'sourceText('} 把声明也算了进去;真实调用点是 15 处/8 支文件。</li>
|
||||
* <li><b>语料根不存在必须抛</b>,不许返回空表:空表会被读成「已经没人用裸名口径了」。
|
||||
* 目录存在且确实没有 {@code .java} 才返回空——那是"语料真的没了",与"尺子架错地方"是两回事。</li>
|
||||
* </ol>
|
||||
*/
|
||||
public static List<CallSite> censusSourceTextCallSites(Path corpusRoot) {
|
||||
Path root = corpusRoot.toAbsolutePath();
|
||||
if (!Files.isDirectory(root)) {
|
||||
throw new IllegalStateException("语料根不存在: " + root + "(cwd=" + Path.of("").toAbsolutePath() + ")");
|
||||
}
|
||||
Path base = moduleRoot();
|
||||
List<CallSite> sites = new java.util.ArrayList<>();
|
||||
try (Stream<Path> walk = Files.walk(root)) {
|
||||
List<Path> files = walk.filter(Files::isRegularFile)
|
||||
.filter(p -> p.getFileName().toString().endsWith(".java"))
|
||||
.sorted(java.util.Comparator.comparing(p -> relativizeForCensus(p, root, base).replace('\\', '/')))
|
||||
.toList();
|
||||
for (Path file : files) {
|
||||
String code = withoutComments(read(file));
|
||||
String relative = relativizeForCensus(file, root, base).replace('\\', '/');
|
||||
Matcher matcher = CALL_SITE.matcher(code);
|
||||
while (matcher.find()) {
|
||||
sites.add(new CallSite(matcher.group(1), relative, lineAt(code, matcher.start())));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("遍历 " + root + " 失败: " + e.getMessage(), e);
|
||||
}
|
||||
return List.copyOf(sites);
|
||||
}
|
||||
|
||||
private static String relativizeForCensus(Path file, Path corpusRoot, Path moduleRoot) {
|
||||
Path absolute = file.toAbsolutePath();
|
||||
if (absolute.startsWith(moduleRoot)) {
|
||||
return moduleRoot.relativize(absolute).toString();
|
||||
}
|
||||
return corpusRoot.relativize(absolute).toString();
|
||||
}
|
||||
|
||||
/** 偏移不变的 {@link #withoutComments} 保证:剥完再数换行,行号仍等于原文件行号。 */
|
||||
private static int lineAt(String code, int offset) {
|
||||
int line = 1;
|
||||
for (int i = 0; i < offset; i++) {
|
||||
if (code.charAt(i) == '\n') {
|
||||
line++;
|
||||
}
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
/** 按文件名(不含目录)取 {@code src/main/java} 下那份源码的<b>全文字符串</b>。
|
||||
* ⚠️ 返回 String,与 InternalTokenSingleOutletTest 里那个返回 Path 的私有 {@code sourceOf} 不是一回事,
|
||||
* 别指望复用它的——那个是 private。 */
|
||||
|
||||
Reference in New Issue
Block a user