Skip to content

Commit

Permalink
Issue checkstyle#16081: FinalLocalVariable should check interface par…
Browse files Browse the repository at this point in the history
…ameters
  • Loading branch information
Machine-Maker committed Jan 18, 2025
1 parent 8fedfc2 commit 12fc12c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ public void visitToken(DetailAST ast) {
&& ast.findFirstToken(TokenTypes.MODIFIERS)
.findFirstToken(TokenTypes.FINAL) == null
&& !isInAbstractOrNativeMethod(ast)
&& !ScopeUtil.isInInterfaceBlock(ast)
&& (!ScopeUtil.isInInterfaceBlock(ast)
|| ScopeUtil.isStaticDefaultOrPrivateMethod(ast))
&& !isMultipleTypeCatch(ast)
&& !CheckUtil.isReceiverParameter(ast)) {
insertParameter(ast);
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/puppycrawl/tools/checkstyle/utils/ScopeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,25 @@ public static boolean isInScope(DetailAST ast, Scope scope) {
return surroundingScopeOfAstToken == scope;
}

/**
* Check if given parameter definition is for a static, default or private method.
*
* @param parameterDefAst parameter definition
* @return true if it is for a static, default or private method, false otherwise
*/
public static boolean isStaticDefaultOrPrivateMethod(DetailAST parameterDefAst) {
boolean staticDefaultOrPrivate = false;
DetailAST currentAst = parameterDefAst;
while (currentAst != null && !staticDefaultOrPrivate) {
if (currentAst.getType() == TokenTypes.METHOD_DEF) {
final DetailAST modifiers =
currentAst.findFirstToken(TokenTypes.MODIFIERS);
staticDefaultOrPrivate = modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) != null
|| modifiers.findFirstToken(TokenTypes.LITERAL_DEFAULT) != null
|| modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) != null;
}
currentAst = currentAst.getParent();
}
return staticDefaultOrPrivate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,11 @@ public void testInputFinalLocalVariable2One() throws Exception {
public void testInputFinalLocalVariable2Two() throws Exception {

final String[] excepted = {
"78:36: " + getCheckMessage(MSG_KEY, "_o"),
"83:37: " + getCheckMessage(MSG_KEY, "_o1"),
"53:36: " + getCheckMessage(MSG_KEY, "bParam"),
"56:34: " + getCheckMessage(MSG_KEY, "cParam"),
"59:36: " + getCheckMessage(MSG_KEY, "dParam"),
"87:36: " + getCheckMessage(MSG_KEY, "_o"),
"92:37: " + getCheckMessage(MSG_KEY, "_o1"),
};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariable2Two.java"), excepted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ class InnerClass
interface Inter2
{
void method(int aParam);

default void defaultMethod(int bParam) { // violation
}

static void staticMethod(int cParam) { // violation
}

private void privateMethod(int dParam) { // violation
}
}

abstract class AbstractClass2
Expand Down

0 comments on commit 12fc12c

Please sign in to comment.