Skip to content

[java] New rule: TypeNameMismatch #7033

Closed

Description

@zbynek

Proposed Rule Name: TypeNameMismatch

Proposed Category: Code Style Best Practices

Description:
Top level types should be defined in a file with a matching name. For public types this is enforced by the compiler, but for package-private types it's possible to have a mismatch. That can be confusing for developers who expect to be able to locate a source file based on class name.

Code Sample: This should include code, that should be flagged by the rule. If possible, the "correct" code
according to this new rule should also be demonstrated.

// content of Foo.java

class Bar { // violation, should be defined in Bar.java

}
// content of Baz.java
class Baz {
  class Foo { } // OK, nested classes are allowed
}

class Bar { // violation, second top-level class should be in its own file
}

Possible Properties:

None.

Activity

  1. added
    a:new-rule
    on Sep 4, 2026
  2. UncleOwen commented on Sep 5, 2026

    @UncleOwen
    Member

    What do you think about checking the directory structure as well? For example, making sure that a class in the foo.bar.baz package is located in a directory called .../foo/bar/baz?

  3. added this to the 7.28.0 milestone on Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:new-rule

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions