Skip to content

Validate package name in banEmptyJavaFiles#23

Merged
shitikanth merged 11 commits into
mainfrom
feat/ban-empty-java-files-package-validation
May 8, 2026
Merged

Validate package name in banEmptyJavaFiles#23
shitikanth merged 11 commits into
mainfrom
feat/ban-empty-java-files-package-validation

Conversation

@shitikanth
Copy link
Copy Markdown
Owner

Summary

  • Extends banEmptyJavaFiles to also flag Java files whose package declaration doesn't match their directory path relative to the source root
  • Introduces CompilationUnitInfo record (package name + type names) and updates all four parser implementations (RecursiveDescentTLDParser, RegexBasedTLDParser, AntlrTLDParser, JavaParserTLDParser) to capture the package declaration
  • Files directly in the source root with no package declaration are allowed (default package); all other mismatches are reported in a second section of the error message

Error message format

Java files with incorrect package declaration:
    - src/main/java/com/example/Foo.java (expected: com.example, found: com.wrong)

Test plan

  • mvn test — 27 unit tests pass (parsers, EmptyJavaFileAnalyzer, existing rules)
  • JAVA_HOME=$(/usr/libexec/java_home -v 21) mvn verify — all 8 integration tests pass including new fail-ban-empty-java-files-wrong-package IT
  • Bumped maven.compiler.source/target to 17 (required for records)

🤖 Generated with Claude Code

shitikanth and others added 11 commits May 8, 2026 13:58
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Bump Java source/target to 17 to enable records; add CompilationUnitInfo
record (packageName + typeNames) and update TLDParser.parse() to return it.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Add PackageDeclarationState to extract the package name from Java
source, returning CompilationUnitInfo instead of List<String>. Update
AbstractTLDParser and stub other parsers to compile cleanly.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Add PACKAGE lexer token and packageDeclaration rule to JavaTLD.g4; update
AntlrTLDParser.parse() to extract and return the package name via CompilationUnitInfo.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@shitikanth shitikanth merged commit 51a6f86 into main May 8, 2026
4 checks passed
@shitikanth shitikanth deleted the feat/ban-empty-java-files-package-validation branch May 8, 2026 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant