-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Description
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
MethodTooLargeExceptionif 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.