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 11, 2024
1 parent ffbeac5 commit 1f7b5dc
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 4 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 @@ -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),
"79:5: " + getCheckMessage(MSG_KEY, 77),
"122:5: " + getCheckMessage(MSG_KEY, 111),
"133:5: " + getCheckMessage(MSG_KEY, 127),
"144:9: " + getCheckMessage(MSG_KEY, 138),
"147:5: " + getCheckMessage(MSG_KEY, 133),
};
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,13 @@ 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 method
{
// some foo code
}
};
}
Expand Down Expand Up @@ -116,6 +123,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
3 changes: 3 additions & 0 deletions src/xdocs/checks/coding/overloadmethodsdeclarationorder.xml
Expand Up @@ -32,6 +32,7 @@ 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() {}
</source>
<p id="Example2-code">Example of incorrect grouping of overloaded methods:</p>
<source>
Expand All @@ -40,6 +41,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 1f7b5dc

Please sign in to comment.