Skip to content

Commit

Permalink
Issue #14716: Fix the violation for modifiers wrapped in class def
Browse files Browse the repository at this point in the history
  • Loading branch information
kalpadiptyaroy committed Apr 1, 2024
1 parent 4fe8118 commit b638c94
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
Expand Up @@ -24,6 +24,7 @@
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import com.puppycrawl.tools.checkstyle.utils.TokenUtil;

/**
* Abstract base class for all handlers.
Expand Down Expand Up @@ -500,6 +501,10 @@ protected void checkModifiers() {
modifier != null;
modifier = modifier.getNextSibling()) {
if (isOnStartOfLine(modifier)
&& (!TokenUtil.isOfType(modifier,
TokenTypes.ANNOTATION, TokenTypes.STRICTFP, TokenTypes.LITERAL_STATIC)
|| modifier.getPreviousSibling() == null
|| !isPrecededByAnyModifier(modifier))
&& !getIndent().isAcceptable(expandedTabsColumnNo(modifier))) {
logError(modifier, "modifier",
expandedTabsColumnNo(modifier));
Expand Down Expand Up @@ -590,4 +595,20 @@ && isOnStartOfLine(lparen)) {
}
}

/**
* Check whether given node a preceding access modifier or not.
*
* @param node the node whose predecessors are to be checked.
* @return true if node is preceded by an access modifier else false.
*/
private boolean isPrecededByAnyModifier(DetailAST node) {
DetailAST tempNode = node;
while (tempNode.getPreviousSibling() != null
&& !TokenUtil.isOfType(tempNode.getPreviousSibling(),
TokenTypes.LITERAL_PUBLIC, TokenTypes.FINAL, TokenTypes.ABSTRACT)) {
tempNode = tempNode.getPreviousSibling();
}
return tempNode.getPreviousSibling() != null;
}

}
Expand Up @@ -3019,6 +3019,22 @@ public void testIndentationLineBreakVariableDeclaration()
verifyWarns(checkConfig, fileName, expected);
}

@Test
public void testClassDeclarationWrapped() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
checkConfig.addProperty("tabWidth", "4");
checkConfig.addProperty("basicOffset", "2");
checkConfig.addProperty("braceAdjustment", "2");
checkConfig.addProperty("lineWrappingIndentation", "4");
checkConfig.addProperty("caseIndent", "2");
checkConfig.addProperty("throwsIndent", "2");
checkConfig.addProperty("arrayInitIndent", "2");

final String fileName = getPath("InputIndentationClassDeclarationWrapped.java");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWarns(checkConfig, fileName, expected);
}

private static final class IndentAudit implements AuditListener {

private final IndentComment[] comments;
Expand Down
@@ -0,0 +1,30 @@
package com.puppycrawl.tools.checkstyle.checks.indentation.indentation;//indent:0 exp:0

public class InputIndentationClassDeclarationWrapped {//indent:0 exp:0

public class A {};//indent:2 exp:2

public//indent:2 exp:2
class B {};//indent:6 exp:6

final//indent:2 exp:2
static class C {};//indent:6 exp:6

abstract//indent:2 exp:2
strictfp class G {};//indent:6 exp:6

public//indent:2 exp:2
class T {};//indent:6 exp:6

public//indent:2 exp:2
@X class D {};//indent:6 exp:6

abstract @Y//indent:2 exp:2
@Z strictfp class E {};//indent:6 exp:6

@X class F {};//indent:2 exp:2

@interface X{}//indent:2 exp:2
@interface Y{}//indent:2 exp:2
@interface Z{}//indent:2 exp:2
}//indent:0 exp:0

0 comments on commit b638c94

Please sign in to comment.