Skip to content

New Check: LargeConstantCollection #18931

@yashvisharma1204

Description

@yashvisharma1204

I am proposing a new check within the coding category to detect large static collection initializers that exceed a specific element count. Currently, Checkstyle does not have a dedicated check to flag massive hard coded arrays, which can lead to significant bytecode bloat and poor separation of data and logic.

Rationale
In high-performance engineering, especially in AI and Data Engineering domains, developers sometimes embed lookup tables or model weights directly into Java source files.
This practice:

  • Increases the size of the compiled .class file.
  • Can lead to MethodTooLargeException if the static initializer grows too big.
  • Makes the code difficult to maintain and read.

By implementing LargeConstantCollectionCheck, we can enforce a threshold (e.g., maxElements = 10) to encourage moving such data to external resource files like .json or .csv.

Proposed Configuration
The check will be configurable with a maxElements property.

<module name="LargeConstantCollection">
  <property name="maxElements" value="10"/>
</module>

Example of Violation

public class Config {
    // Violation: 12 elements exceed the default max of 10
    int[] lookup = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; 
}

Technical Implementation
The check will target TokenTypes.ARRAY_INIT and count the EXPR child tokens. If the count exceeds the threshold, it will log a violation using a message key for internationalization.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions