new Check: OpenjdkMethodParameterAlignmentCheck
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words
According to openjdk's updated java-style guidelines on Wrapping Method Declarations:
Wrapping Method Declarations Method declarations can be formatted by listing the arguments vertically, or by a new line and +8 extra spaces
Dos
int someMethod(String aString,
List<Integer> aList,
Map<String, String> aMap,
int anInt,
long aLong,
Set<Number> aSet,
double aDouble) {
…
}
int someMethod(String aString, List<Integer> aList,
Map<String, String> aMap, int anInt, long aLong,
double aDouble, long aLong) {
…
}Don’ts
// If aligning the parameters vertically, don't put two
// parameters on one line
int someMethod(String aString,
List<Integer> aList,
Map<String, String> aMap,
int anInt, long aLong,
Set<Number> aSet,
double aDouble) {
…
}Attention throws alignment will be separate Check https://github.com/checkstyle/checkstyle/issues/20639
Is your feature request related to a problem? Please describe.
This rule Method declarations can be formatted by listing the arguments vertically, or by a new line and +8 extra spaces can be inforced partially by Identation but as mentioned in the don'ts section that if the parameters of method are aligned vertically than there should be only one parameter per line. Currently checkstyle can not inforce this rule.
Solution
A new Check is needed to follow these rules . Structure of the check :
<module name="OpenjdkMethodParameterAlignment"/>
Rules covered by the proposed check -
- Allowed single parameter per line if they are aligned vertically.
Sample Inputs
C:\Users\amanc\Downloads\Ai_Interviewer\PracticeCheckStyle\src\temp>cat Test.java
package temp;
public class Test {
public void testMethod(int a, // no violation
int b,
int c) {
}
public void testMethod(int a,
int b, int d, // violation here
int c) {
}
public void testMethod(int a, int e, // violation here
int b, int d, // violation here
int c) {
}
}
Source: checkstyle/checkstyle