1 package com.puppycrawl.tools.checkstyle.checks.design; 20 21 import com.puppycrawl.tools.checkstyle.api.Check; 22 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 23 import com.puppycrawl.tools.checkstyle.api.DetailAST; 24 25 36 public class HideUtilityClassConstructorCheck extends Check 37 { 38 39 public int[] getDefaultTokens() 40 { 41 return new int[] {TokenTypes.CLASS_DEF}; 42 } 43 44 45 public void visitToken(DetailAST aAST) 46 { 47 final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK); 48 DetailAST child = (DetailAST) objBlock.getFirstChild(); 49 boolean hasMethod = false; 50 boolean hasNonStaticMethod = false; 51 boolean hasDefaultCtor = true; 52 boolean hasPublicCtor = false; 53 54 while (child != null) { 55 if (child.getType() == TokenTypes.METHOD_DEF) { 56 hasMethod = true; 57 final DetailAST modifiers = 58 child.findFirstToken(TokenTypes.MODIFIERS); 59 if (!modifiers.branchContains(TokenTypes.LITERAL_STATIC)) { 60 hasNonStaticMethod = true; 61 } 62 } 63 if (child.getType() == TokenTypes.CTOR_DEF) { 64 hasDefaultCtor = false; 65 final DetailAST modifiers = 66 child.findFirstToken(TokenTypes.MODIFIERS); 67 if (!modifiers.branchContains(TokenTypes.LITERAL_PRIVATE) 68 && !modifiers.branchContains(TokenTypes.LITERAL_PROTECTED)) 69 { 70 hasPublicCtor = true; 73 } 74 75 } 76 child = (DetailAST) child.getNextSibling(); 77 } 78 79 final boolean hasAccessibleCtor = (hasDefaultCtor || hasPublicCtor); 80 81 final boolean extendsJLO = aAST.findFirstToken(TokenTypes.EXTENDS_CLAUSE) == null; 87 88 if (extendsJLO 89 && hasMethod && !hasNonStaticMethod && hasAccessibleCtor) 90 { 91 log(aAST.getLineNo(), aAST.getColumnNo(), "hide.utility.class"); 92 } 93 } 94 } 95 | Popular Tags |