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 9, 2024
1 parent 571e547 commit 8a7c6d5
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 2 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
&& (currentIndex - previousIndex > allowedDistance || !isMethod)) {
final int previousLineWithOverloadMethod =
methodLineNumberMap.get(methodName);
log(currentToken, MSG_KEY,
Expand Down
Expand Up @@ -43,6 +43,9 @@ public void testDefault() throws Exception {
"60:9: " + getCheckMessage(MSG_KEY, 49),
"72:5: " + getCheckMessage(MSG_KEY, 70),
"115:5: " + getCheckMessage(MSG_KEY, 104),
"126:5: " + getCheckMessage(MSG_KEY, 120),
"137:9: " + getCheckMessage(MSG_KEY, 131),
"140:5: " + getCheckMessage(MSG_KEY, 126),
};
verifyWithInlineConfigParser(
getPath("InputOverloadMethodsDeclarationOrder.java"), expected);
Expand All @@ -55,6 +58,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 @@ -116,6 +116,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 @@ -12,4 +12,5 @@ public void foo(String s) {}
public void foo(String s, int i) {}
public void foo(int i, String s) {}
public void notFoo() {}
private interface Testing() {}
// xdoc section -- end
Expand Up @@ -12,4 +12,6 @@ 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(double d) {} // violation. Have to be after foo(String s, int i)
// xdoc section -- end
2 changes: 2 additions & 0 deletions src/xdocs/checks/coding/overloadmethodsdeclarationorder.xml
Expand Up @@ -40,6 +40,8 @@ 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(double d) {} // violation. Have to be after foo(String s, int i)
</source>
</subsection>

Expand Down

0 comments on commit 8a7c6d5

Please sign in to comment.