#!/usr/bin/env python3 """ Remove JD-Core decompiler noise from Java sources under maven-*: - Line prefix comments: /* */ and /* N */ (line numbers) - Trailing metadata block: /* Location: ... JD-Core Version: ... */ Does not strip normal Javadoc /** ... */ or arbitrary block comments. """ from __future__ import annotations import argparse import re import sys from pathlib import Path RE_LINE_EMPTY_PREFIX = re.compile(r"^/\* +\*/\s*") RE_LINE_NUM_PREFIX = re.compile(r"^/\* \d+ \*/\s*") # Ends with line like " */" (space + */) after "* JD-Core Version: ..." RE_TAIL_META = re.compile( r"(?:^|\n)/\* Location:.*?\n\s*\*/\s*", re.DOTALL, ) def strip_content(text: str) -> str: text = text.replace("\r\n", "\n").replace("\r", "\n") lines = text.split("\n") stripped = [] for line in lines: line = RE_LINE_EMPTY_PREFIX.sub("", line) line = RE_LINE_NUM_PREFIX.sub("", line) stripped.append(line) joined = "\n".join(stripped) joined = RE_TAIL_META.sub("\n", joined) if joined and not joined.endswith("\n"): joined += "\n" return joined def process_file(path: Path, dry_run: bool) -> bool: raw = path.read_text(encoding="utf-8", errors="replace") new = strip_content(raw) raw_norm = raw.replace("\r\n", "\n").replace("\r", "\n") if raw_norm and not raw_norm.endswith("\n"): raw_norm += "\n" if new == raw_norm: return False if not dry_run: path.write_text(new, encoding="utf-8", newline="\n") return True def main() -> int: ap = argparse.ArgumentParser(description=__doc__) ap.add_argument( "roots", nargs="*", type=Path, help="Optional roots (default: all maven-* under repo root next to scripts/)", ) ap.add_argument("--dry-run", action="store_true", help="Report only, do not write") args = ap.parse_args() script_dir = Path(__file__).resolve().parent repo = script_dir.parent if args.roots: roots = [Path(p).resolve() for p in args.roots] else: roots = sorted(repo.glob("maven-*")) roots = [p for p in roots if p.is_dir()] changed = 0 scanned = 0 for root in roots: if not root.exists(): print(f"skip missing: {root}", file=sys.stderr) continue for path in root.rglob("*.java"): if "/target/" in str(path).replace("\\", "/"): continue scanned += 1 if process_file(path, args.dry_run): changed += 1 if args.dry_run: print(f"would update: {path}") print(f"Scanned: {scanned} Java files under {len(roots)} root(s)") print(f"{'Would change' if args.dry_run else 'Changed'}: {changed} file(s)") return 0 if __name__ == "__main__": sys.exit(main())