Created
May 8, 2024 12:56
-
-
Save nrmancuso/d448521b185d09116788ef022a82b975 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
checkstyle git:(pull-14844) ✗ git diff -u | |
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ConstructorsDeclarationGroupingCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ConstructorsDeclarationGroupingCheck.java | |
index 843f7a998..7a984d4ac 100644 | |
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ConstructorsDeclarationGroupingCheck.java | |
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ConstructorsDeclarationGroupingCheck.java | |
@@ -19,6 +19,11 @@ | |
package com.puppycrawl.tools.checkstyle.checks.coding; | |
+import java.util.ArrayList; | |
+import java.util.List; | |
+import java.util.Optional; | |
+import java.util.stream.Collectors; | |
+ | |
import com.puppycrawl.tools.checkstyle.StatelessCheck; | |
import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | |
import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
@@ -78,33 +83,53 @@ public class ConstructorsDeclarationGroupingCheck extends AbstractCheck { | |
@Override | |
public void visitToken(DetailAST ast) { | |
- DetailAST currentToken = ast.findFirstToken(TokenTypes.OBJBLOCK).getFirstChild(); | |
- DetailAST previousCtor = null; | |
- int firstNonCtorSiblingLineNo = -1; | |
- boolean ctorOccured = false; | |
- boolean isViolation = false; | |
- | |
- while (currentToken != null) { | |
- if (currentToken.getType() == TokenTypes.CTOR_DEF | |
- || currentToken.getType() == TokenTypes.COMPACT_CTOR_DEF) { | |
- final DetailAST previousSibling = currentToken.getPreviousSibling(); | |
- final boolean isCtor = previousSibling.getType() == TokenTypes.CTOR_DEF | |
- || previousSibling.getType() == TokenTypes.COMPACT_CTOR_DEF; | |
- | |
- if (ctorOccured && !isViolation && !isCtor) { | |
- firstNonCtorSiblingLineNo = previousCtor.getNextSibling().getLineNo(); | |
- isViolation = true; | |
- } | |
- | |
- if (isViolation) { | |
- log(currentToken, MSG_KEY, firstNonCtorSiblingLineNo); | |
- } | |
- | |
- previousCtor = currentToken; | |
- ctorOccured = true; | |
- } | |
- | |
- currentToken = currentToken.getNextSibling(); | |
+ // first make a list of all child ASTs | |
+ final List<DetailAST> children = getChildList(ast); | |
+ | |
+ // find first constructor | |
+ final DetailAST firstConstructor = children.stream() | |
+ .filter(ConstructorsDeclarationGroupingCheck::isConstructor) | |
+ .findFirst() | |
+ .orElse(null); | |
+ | |
+ if (firstConstructor != null) { | |
+ | |
+ // get all children after the first constructor | |
+ final List<DetailAST> childrenAfterFirstConstructor = | |
+ children.subList(children.indexOf(firstConstructor) + 1, children.size()); | |
+ | |
+ // find first index of non-constructor AST after the first constructor, if present | |
+ final Optional<Integer> indexOfFirstNonConstructor = childrenAfterFirstConstructor.stream() | |
+ .filter(ch -> !isConstructor(ch)) | |
+ .findFirst() | |
+ .map(children::indexOf); | |
+ | |
+ // make list of all children after this | |
+ final List<DetailAST> childrenAfterFirstNonConstructor = indexOfFirstNonConstructor | |
+ .map(i -> children.subList(i, children.size())) | |
+ .orElseGet(ArrayList::new); | |
+ | |
+ // create a list of all constructors that are not grouped to log | |
+ final List<DetailAST> constructorsToLog = childrenAfterFirstNonConstructor.stream() | |
+ .filter(ConstructorsDeclarationGroupingCheck::isConstructor) | |
+ .collect(Collectors.toList()); | |
+ | |
+ constructorsToLog.forEach(ctor -> log(ctor, MSG_KEY, firstConstructor.getLineNo())); | |
+ } | |
+ } | |
+ | |
+ private static List<DetailAST> getChildList(DetailAST ast) { | |
+ final List<DetailAST> children = new ArrayList<>(); | |
+ DetailAST child = ast.findFirstToken(TokenTypes.OBJBLOCK).getFirstChild(); | |
+ while (child != null) { | |
+ children.add(child); | |
+ child = child.getNextSibling(); | |
} | |
+ return children; | |
+ } | |
+ | |
+ private static boolean isConstructor(DetailAST ast) { | |
+ return ast.getType() == TokenTypes.CTOR_DEF | |
+ || ast.getType() == TokenTypes.COMPACT_CTOR_DEF; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment