跪拜 Guibai
← All articles
Java

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

By 用户11293611219 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Every Java developer who has debugged "changed code but it didn't take effect" needs this: the artifact, not the source, is the ground truth. Knowing how to inspect class files and timestamps turns a long argument into a five-minute check.

Summary

A Java developer hit a classic phantom bug: commenting out a whitelist entry in a ShiroConfig class, running mvn compile, and still seeing the endpoint accessible in a test environment. The project wasn't under git, so there was no diff to prove the change ever reached the artifact. The root cause is that javac strips comments from source, so a comment in source says nothing about the class file. If the string still appears in the constant pool, the class was either built before the comment or never recompiled.

Annotation strings only appear in class files depending on @Retention. Runtime-retained annotations like @RestController and @RequestMapping land in RuntimeVisibleAnnotations and are searchable. SOURCE-level ones like @Override don't survive. A subtle trap: method-level annotation strings often remain in the constant pool even after the class-level annotation is removed, but Spring only registers handlers based on class-level @Controller or @RequestMapping. So you must check the class-level annotation string, not the method-level one.

Standard search tools mislead here: grep and ripgrep skip binary files by default, producing false negatives. Reading the .class as raw bytes and searching the constant pool works. Timestamps add context: after a full build, the class mtime must be newer than the source mtime; otherwise you're staring at stale target output. The author's hard rule: after changing config or annotations, always run mvn clean package, because incremental compilation leaves old classes behind. The most reliable proof is starting the service and testing the endpoint; byte-stream search plus timestamps is second-best; source text inspection is worthless.

Takeaways
— javac strips comments from source, so commenting out code in source does not remove it from the compiled class.
— Annotation strings appear in class files only when their @Retention is RUNTIME (e.g., @RestController, @RequestMapping); SOURCE-level annotations like @Override are absent, and CLASS-level ones only exist in the Annotations attribute.
— Method-level annotation strings can remain in the constant pool even after the class-level annotation is removed, but Spring routes are determined by class-level annotations, so method-level strings are not evidence of an active route.
— grep and ripgrep skip binary files by default; searching .class files requires reading them as raw bytes.
— A full build with 0 ERROR only proves compilation succeeded, not that the compiled class matches the latest source.
— After changing config or annotations, run mvn clean package; incremental compilation leaves old class files in target.
— Verify with service-level tests (starting the server) as the most reliable method, followed by byte-stream constant-pool search combined with mtime comparison.
— If a class's mtime is earlier than the source mtime, the class is likely stale.
Conclusions

The scenario highlights a failure mode that git-based workflows hide: without version control, you can't diff what changed, so you have no way to prove what got compiled. The byte-stream check becomes the only arbiter.

Spring's class-level annotation detection is a source of confusion: method-level annotation strings persist in the constant pool, and developers who don't know the framework's lookup logic draw the wrong conclusion from string presence.

The advice to always run clean package is a blunt counter to incremental build tools: they optimize for speed but silently produce stale artifacts when annotations or configs change.

The author's ranking of verification methods—live test > byte search + timestamps > compile output > source text—is a practical checklist that applies to any language where artifacts diverge from source.

Concepts & terms
Constant pool
A table in a Java class file that stores all string literals, method names, field names, and other constants referenced by the bytecode. Searching this pool can reveal whether a particular annotation string survived compilation.
@Retention
An annotation meta-annotation that controls how long an annotation is kept: SOURCE means it's discarded by the compiler, CLASS means it stays in the class file but isn't loaded at runtime, RUNTIME means it's available via reflection and appears in the class file's visible annotations.
mvn clean package
A Maven command that deletes the target directory and performs a full rebuild before packaging. It avoids the problem of incremental compilation leaving stale class files from an earlier build.
Incremental compilation
A build mode that recompiles only changed source files. It is faster than a full build but can leave outdated .class files when dependencies, annotations, or configurations change in ways the build tool doesn't detect.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗