1 package com.puppycrawl.tools.checkstyle.checks.coding; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.FullIdent; 23 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 24 import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck; 25 import com.puppycrawl.tools.checkstyle.checks.CheckUtils; 26 27 import java.util.HashSet ; 28 import java.util.Set ; 29 30 44 public final class IllegalTypeCheck extends AbstractFormatCheck 45 { 46 47 private static final String DEFAULT_FORMAT = "^(.*[\\.])?Abstract.*$"; 48 49 private static final String [] DEFAULT_LEGAL_ABSTRACT_NAMES = {}; 50 51 private static final String [] DEFAULT_ILLEGAL_TYPES = { 52 "GregorianCalendar", 53 "Hashtable", 54 "HashSet", 55 "HashMap", 56 "ArrayList", 57 "LinkedList", 58 "LinkedHashMap", 59 "LinkedHashSet", 60 "TreeSet", 61 "TreeMap", 62 "Vector", 63 "java.util.GregorianCalendar", 64 "java.util.Hashtable", 65 "java.util.HashSet", 66 "java.util.HashMap", 67 "java.util.ArrayList", 68 "java.util.LinkedList", 69 "java.util.LinkedHashMap", 70 "java.util.LinkedHashSet", 71 "java.util.TreeSet", 72 "java.util.TreeMap", 73 "java.util.Vector", 74 }; 75 76 77 private static final String [] DEFAULT_IGNORED_METHOD_NAMES = { 78 "getInitialContext", 79 "getEnvironment", 80 }; 81 82 83 private final Set mIllegalClassNames = new HashSet (); 84 85 private final Set mLegalAbstractClassNames = new HashSet (); 86 87 private final Set mIgnoredMethodNames = new HashSet (); 88 89 90 public IllegalTypeCheck() 91 { 92 super(DEFAULT_FORMAT); 93 setIllegalClassNames(DEFAULT_ILLEGAL_TYPES); 94 setLegalAbstractClassNames(DEFAULT_LEGAL_ABSTRACT_NAMES); 95 setIgnoredMethodNames(DEFAULT_IGNORED_METHOD_NAMES); 96 } 97 98 99 public int[] getDefaultTokens() 100 { 101 return new int[] { 102 TokenTypes.VARIABLE_DEF, 103 TokenTypes.PARAMETER_DEF, 104 TokenTypes.METHOD_DEF, 105 }; 106 } 107 108 109 public void visitToken(DetailAST aAST) 110 { 111 switch (aAST.getType()) { 112 case TokenTypes.METHOD_DEF: 113 visitMethodDef(aAST); 114 break; 115 case TokenTypes.VARIABLE_DEF: 116 visitVariableDef(aAST); 117 break; 118 case TokenTypes.PARAMETER_DEF: 119 visitParameterDef(aAST); 120 break; 121 default: 122 throw new IllegalStateException (aAST.toString()); 123 } 124 } 125 126 130 private void visitMethodDef(DetailAST aAST) 131 { 132 if (isCheckedMethod(aAST)) { 133 checkClassName(aAST); 134 } 135 } 136 137 141 private void visitParameterDef(DetailAST aAST) 142 { 143 final DetailAST grandParentAST = aAST.getParent().getParent(); 144 145 if ((grandParentAST.getType() == TokenTypes.METHOD_DEF) 146 && isCheckedMethod(grandParentAST)) 147 { 148 checkClassName(aAST); 149 } 150 } 151 152 156 private void visitVariableDef(DetailAST aAST) 157 { 158 checkClassName(aAST); 159 } 160 161 165 private void checkClassName(DetailAST aAST) 166 { 167 final DetailAST type = aAST.findFirstToken(TokenTypes.TYPE); 168 final FullIdent ident = CheckUtils.createFullType(type); 169 170 if (isMatchingClassName(ident.getText())) { 171 log(ident.getLineNo(), ident.getColumnNo(), 172 "illegal.type", ident.getText()); 173 } 174 } 175 176 181 private boolean isMatchingClassName(String aClassName) 182 { 183 return mIllegalClassNames.contains(aClassName) 184 || (!mLegalAbstractClassNames.contains(aClassName) 185 && getRegexp().matcher(aClassName).find()); 186 } 187 188 192 private boolean isCheckedMethod(DetailAST aAST) 193 { 194 final String methodName = 195 aAST.findFirstToken(TokenTypes.IDENT).getText(); 196 return !mIgnoredMethodNames.contains(methodName); 197 } 198 199 203 public void setIllegalClassNames(String [] aClassNames) 204 { 205 mIllegalClassNames.clear(); 206 for (int i = 0; i < aClassNames.length; i++) { 207 final String name = aClassNames[i]; 208 mIllegalClassNames.add(name); 209 final int lastDot = name.lastIndexOf("."); 210 if ((lastDot > 0) && (lastDot < (name.length() - 1))) { 211 final String shortName = 212 name.substring(name.lastIndexOf(".") + 1); 213 mIllegalClassNames.add(shortName); 214 } 215 } 216 } 217 218 222 public String [] getIllegalClassNames() 223 { 224 return (String []) mIllegalClassNames.toArray(new String [0]); 225 } 226 227 231 public void setIgnoredMethodNames(String [] aMethodNames) 232 { 233 mIgnoredMethodNames.clear(); 234 for (int i = 0; i < aMethodNames.length; i++) { 235 mIgnoredMethodNames.add(aMethodNames[i]); 236 } 237 } 238 239 243 public String [] getIgnoredMethodNames() 244 { 245 return (String []) mIgnoredMethodNames.toArray(new String [0]); 246 } 247 248 252 public void setLegalAbstractClassNames(String [] aClassNames) 253 { 254 mLegalAbstractClassNames.clear(); 255 for (int i = 0; i < aClassNames.length; i++) { 256 mLegalAbstractClassNames.add(aClassNames[i]); 257 } 258 } 259 260 264 public String [] getLegalAbstractClassNames() 265 { 266 return (String []) mLegalAbstractClassNames.toArray(new String [0]); 267 } 268 } 269 | Popular Tags |