1 package com.puppycrawl.tools.checkstyle.checks.design; 20 21 import java.util.HashSet ; 22 import java.util.Set ; 23 import java.util.regex.Pattern ; 24 import java.util.regex.PatternSyntaxException ; 25 26 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 27 import com.puppycrawl.tools.checkstyle.api.Check; 28 import com.puppycrawl.tools.checkstyle.api.DetailAST; 29 import com.puppycrawl.tools.checkstyle.api.Utils; 30 import com.puppycrawl.tools.checkstyle.api.ScopeUtils; 31 import org.apache.commons.beanutils.ConversionException; 32 33 import antlr.collections.AST; 34 35 47 public class VisibilityModifierCheck 48 extends Check 49 { 50 51 private boolean mProtectedAllowed; 52 53 54 private boolean mPackageAllowed; 55 56 63 private String mPublicMemberFormat = "^serialVersionUID$"; 64 65 66 private Pattern mPublicMemberPattern; 67 68 69 public VisibilityModifierCheck() 70 { 71 setPublicMemberPattern(mPublicMemberFormat); 72 } 73 74 75 public boolean isProtectedAllowed() 76 { 77 return mProtectedAllowed; 78 } 79 80 84 public void setProtectedAllowed(boolean aProtectedAllowed) 85 { 86 mProtectedAllowed = aProtectedAllowed; 87 } 88 89 90 public boolean isPackageAllowed() 91 { 92 return mPackageAllowed; 93 } 94 95 99 public void setPackageAllowed(boolean aPackageAllowed) 100 { 101 mPackageAllowed = aPackageAllowed; 102 } 103 104 108 public void setPublicMemberPattern(String aPattern) 109 { 110 try { 111 mPublicMemberPattern = Utils.getPattern(aPattern); 112 mPublicMemberFormat = aPattern; 113 } 114 catch (final PatternSyntaxException e) { 115 throw new ConversionException("unable to parse " + aPattern, e); 116 } 117 } 118 119 122 private Pattern getPublicMemberRegexp() 123 { 124 return mPublicMemberPattern; 125 } 126 127 128 public int[] getDefaultTokens() 129 { 130 return new int[] {TokenTypes.VARIABLE_DEF}; 131 } 132 133 134 public void visitToken(DetailAST aAST) 135 { 136 if ((aAST.getType() != TokenTypes.VARIABLE_DEF) 137 || (aAST.getParent().getType() != TokenTypes.OBJBLOCK)) 138 { 139 return; 140 } 141 142 final DetailAST varNameAST = getVarNameAST(aAST); 143 final String varName = varNameAST.getText(); 144 final boolean inInterfaceOrAnnotationBlock = 145 ScopeUtils.inInterfaceOrAnnotationBlock(aAST); 146 final Set mods = getModifiers(aAST); 147 final String declaredScope = getVisibilityScope(mods); 148 final String variableScope = 149 inInterfaceOrAnnotationBlock ? "public" : declaredScope; 150 151 if (!("private".equals(variableScope) 152 || inInterfaceOrAnnotationBlock || (mods.contains("static") && mods.contains("final")) 154 || ("package".equals(variableScope) && isPackageAllowed()) 155 || ("protected".equals(variableScope) && isProtectedAllowed()) 156 || ("public".equals(variableScope) 157 && getPublicMemberRegexp().matcher(varName).find()))) 158 { 159 log(varNameAST.getLineNo(), varNameAST.getColumnNo(), 160 "variable.notPrivate", varName); 161 } 162 } 163 164 169 private DetailAST getVarNameAST(DetailAST aVariableDefAST) 170 { 171 AST ast = aVariableDefAST.getFirstChild(); 172 while (ast != null) { 173 final AST nextSibling = ast.getNextSibling(); 174 if (ast.getType() == TokenTypes.TYPE) { 175 return (DetailAST) nextSibling; 176 } 177 ast = nextSibling; 178 } 179 return null; 180 } 181 182 187 private Set getModifiers(DetailAST aVariableDefAST) 188 { 189 final AST modifiersAST = aVariableDefAST.getFirstChild(); 190 if (modifiersAST.getType() != TokenTypes.MODIFIERS) { 191 throw new IllegalStateException ("Strange parse tree"); 192 } 193 final Set retVal = new HashSet (); 194 AST modifier = modifiersAST.getFirstChild(); 195 while (modifier != null) { 196 retVal.add(modifier.getText()); 197 modifier = modifier.getNextSibling(); 198 } 199 return retVal; 200 201 } 202 203 208 private String getVisibilityScope(Set aModifiers) 209 { 210 final String [] explicitModifiers = {"public", "private", "protected"}; 211 for (int i = 0; i < explicitModifiers.length; i++) { 212 final String candidate = explicitModifiers[i]; 213 if (aModifiers.contains(candidate)) { 214 return candidate; 215 } 216 } 217 return "package"; 218 } 219 } 220 | Popular Tags |