跪拜 Guibai
← Back to the summary

Why Commented-Out Interfaces Still Work: Class Files, Retention, and the Art of Proving a Build

Scenario: Interface still accessible after being commented out

Once I commented out a whitelist entry in ShiroConfig, and mvn compile passed normally, but the interface was still exposed in the test environment. Worse, the project wasn't even a git repository, and delivery audits required evidence that "I really changed it, and the change really made it into the artifact."

I stared at the source code for a long time — the comment was clearly there. But the key issue is: the source code and the actually running class may not be the same thing. If you only look at the source text, you can never prove whether your change is actually in the deployed set of class files.

Root cause: comments don't go into class files, but old class files persist

javac strips comments from source code during compilation, and comment content never enters the class file. So the fact that "the comment is in the source" cannot imply that "it's not in the artifact." Conversely, if the target string can still be found in the constant pool of the artifact class, it means either the class was compiled before the comment was added, or it was never recompiled at all.

Whether an annotation string can be found in the class byte stream depends on @Retention:

Another easily misjudged point: class-level annotations and method-level annotations are two different things. After you comment out a class-level annotation and search the class, the method-level annotation strings may still be in the constant pool. This does not mean the route is still active; it just means the strings happen to remain.

Why it's hard to trace: search tools can fool you, and method-level strings can fool you too

When debugging this kind of problem, routine operations often lead to wrong conclusions.

On one hand, grep or ripgrep skip binary files by default. If you directly search .class files, you'll likely get a "not found" result — a false negative. You must read the file byte by byte to search for strings in the constant pool.

On the other hand, even if you search byte-wise and find method-level annotation strings, you can't hastily conclude that "the annotation didn't take effect." For example, after commenting out @RestController and @RequestMapping("x") on the class, searching the class constant pool might show this:

But Spring determines whether a class is a handler based on the presence of @Controller or @RequestMapping at the class level. Once the class-level annotation is gone, method-level annotations won't be registered as routes. So to judge whether a route is deregistered, look at whether the class-level annotation string remains, not whether the method-level string is still present.

Quick location: read the byte stream + compare timestamps

The fastest verification method is not to look at the source code, but to directly read the class byte stream and search for strings. Node runs on any platform:

node -e '
const fs = require("fs");
const b = fs.readFileSync("target/classes/com/xxx/ShiroConfig.class");
for (const s of ["sys/oss", "/upload", "禁止上传"]) {
  console.log(b.includes(Buffer.from(s, "utf8")) ? "HIT " + s : "--- " + s);
}'

On Windows, you can also use PowerShell to read bytes and convert to string:

$p = 'target\classes\com\xxx\ShiroConfig.class'
$s = [System.Text.Encoding]::UTF8.GetString([System.IO.File]::ReadAllBytes($p))
$s.Contains('/file/**')

But finding or not finding a string isn't enough; you also need to cross-check with the compilation artifact's timestamp. After a full compilation, the target class's modification time must be later than the source's modification time; otherwise, what you're seeing is likely leftover artifacts from an old target.

node -e '
const fs = require("fs");
for (const p of ["src/main/java/com/xxx/ShiroConfig.java", "target/classes/com/xxx/ShiroConfig.class"]) {
  const s = fs.statSync(p);
  console.log(s.mtime.toISOString(), p);
}'

Fix and pitfalls: judgment needs layers, artifacts need clean

When investigating class-level annotations, don't use method-level annotation strings as evidence. The disappearance of the class-level annotation prefix represents the route actually being invalidated; method-level annotation strings remaining may just be compilation leftovers.

In terms of verification strength, the most reliable is to start the service and test the interface directly; the next is byte-stream searching the constant pool combined with compilation timestamps; full compilation with 0 ERROR only proves "it compiles", not "it compiled the version you have in mind"; the least reliable is looking only at source text, because comments and old files are invisible.

There's also a hard constraint: if you change config or annotations, you must mvn clean package. Incremental compilation leaves old class files. I've seen an instance where the source code commented out a whitelist entry at 16:52, but the target/classes/**/ShiroConfig.class timestamp was 16:34, and the byte stream still contained the commented-out string. In that case, reusing the old target for deployment means the change never took effect.

Acceptance rules

❌ Wrong practices

✅ Correct practices

If you've ever encountered "code changed but not effective, and no one can prove whether it was compiled," you can compare your .class and timestamps together — reading the constant pool is much more reliable than arguing.

Comments

Top 1 of 2 from juejin.cn, machine-translated. The original thread is authoritative.

六冬666

Arthas decompilation

用户11293611219

Good idea. jad decompiles the class the JVM actually loads, which is closer to the truth than reading target/classes from disk—the disk copy may not be the runtime-loaded one. And decompiling directly shows class-level annotations, neatly avoiding the article's pitfall of misjudging method-level string remnants. Thanks for the lesson.