Skip to content

Commit

Permalink
Issue checkstyle#14765: fix OverloadMethodsDeclarationOrderCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Zopsss committed Apr 14, 2024
1 parent 86195e0 commit 24ab0e9
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
Expand Up @@ -110,7 +110,11 @@ private void checkOverloadMethodsGrouping(DetailAST objectBlock) {
final String methodName =
currentToken.findFirstToken(TokenTypes.IDENT).getText();
final Integer previousIndex = methodIndexMap.get(methodName);
if (previousIndex != null && currentIndex - previousIndex > allowedDistance) {
final DetailAST previousSibling = currentToken.getPreviousSibling();
final boolean isMethod = previousSibling.getType() == TokenTypes.METHOD_DEF;

if (previousIndex != null
&& (!isMethod || currentIndex - previousIndex > allowedDistance)) {
final int previousLineWithOverloadMethod =
methodLineNumberMap.get(methodName);
log(currentToken, MSG_KEY,
Expand Down
Expand Up @@ -41,8 +41,12 @@ public void testDefault() throws Exception {
final String[] expected = {
"32:5: " + getCheckMessage(MSG_KEY, 21),
"60:9: " + getCheckMessage(MSG_KEY, 49),
"72:5: " + getCheckMessage(MSG_KEY, 70),
"115:5: " + getCheckMessage(MSG_KEY, 104),
"65:9: " + getCheckMessage(MSG_KEY, 60),
"80:5: " + getCheckMessage(MSG_KEY, 78),
"124:5: " + getCheckMessage(MSG_KEY, 113),
"135:5: " + getCheckMessage(MSG_KEY, 129),
"146:9: " + getCheckMessage(MSG_KEY, 140),
"149:5: " + getCheckMessage(MSG_KEY, 135),
};
verifyWithInlineConfigParser(
getPath("InputOverloadMethodsDeclarationOrder.java"), expected);
Expand All @@ -55,6 +59,7 @@ public void testOverloadMethodsDeclarationOrderRecords() throws Exception {
"21:9: " + getCheckMessage(MSG_KEY, 15),
"41:9: " + getCheckMessage(MSG_KEY, 35),
"57:9: " + getCheckMessage(MSG_KEY, 50),
"63:9: " + getCheckMessage(MSG_KEY, 57),
};
verifyWithInlineConfigParser(
getNonCompilablePath("InputOverloadMethodsDeclarationOrderRecords.java"),
Expand Down
Expand Up @@ -60,7 +60,7 @@ public void foo() { // violation
public MyClass() {
}

public void foo(int i, String s) {
public void foo(int i, String s) { // violation
}

public void foo(String s, int i) {
Expand Down
Expand Up @@ -60,6 +60,14 @@ public void fooMethod()
public void overloadMethod(String s, Boolean b, int i) // violation
{
//some foo code
};

public void overloadMethod(double d) // violation
// because there is an unnecessary semicolon at the
// end of the above method,
// separating the overloaded methods.
{
// some foo code
}
};
}
Expand Down Expand Up @@ -101,6 +109,7 @@ public void overloadMethod(String s)
//some foo code
}

// comments between overloaded methods are allowed.
public void overloadMethod(boolean b)
{
//some foo code
Expand All @@ -116,6 +125,28 @@ public void overloadMethod(String s, Boolean b, int i) // violation
{
//some foo code
}

void test() {}

String str;

private interface Testing {}

void test(int x) {} // violation

private class Inner {
void test() {}

void test(String str) {}

void test2() {}

String str;

void test(int x) {} // violation
}

void test(double d) {} // violation
}

enum Foo2 {
Expand Down
Expand Up @@ -10,6 +10,8 @@
public void foo(int i) {}
public void foo(String s) {}
public void foo(String s, int i) {}
// comments between overloaded methods are allowed.
public void foo(int i, String s) {}
public void notFoo() {}
private interface Testing() {}
// xdoc section -- end
Expand Up @@ -11,5 +11,6 @@ public void foo(int i) {} // OK
public void foo(String s) {} // OK
public void notFoo() {} // violation. Have to be after foo(String s, int i)
public void foo(int i, String s) {}
public void foo(String s, int i) {}
private interface Testing() {}
public void foo(String s, int i) {} // violation. Have to be after foo(int i, String s)
// xdoc section -- end
5 changes: 4 additions & 1 deletion src/xdocs/checks/coding/overloadmethodsdeclarationorder.xml
Expand Up @@ -30,16 +30,19 @@
public void foo(int i) {}
public void foo(String s) {}
public void foo(String s, int i) {}
// comments between overloaded methods are allowed.
public void foo(int i, String s) {}
public void notFoo() {}
private interface Testing() {}
</source>
<p id="Example2-code">Example of incorrect grouping of overloaded methods:</p>
<source>
public void foo(int i) {} // OK
public void foo(String s) {} // OK
public void notFoo() {} // violation. Have to be after foo(String s, int i)
public void foo(int i, String s) {}
public void foo(String s, int i) {}
private interface Testing() {}
public void foo(String s, int i) {} // violation. Have to be after foo(int i, String s)
</source>
</subsection>

Expand Down

0 comments on commit 24ab0e9

Please sign in to comment.