1 package com.puppycrawl.tools.checkstyle.checks.usage; 20 21 import com.puppycrawl.tools.checkstyle.api.Check; 22 import com.puppycrawl.tools.checkstyle.api.DetailAST; 23 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 24 import com.puppycrawl.tools.checkstyle.api.Utils; 25 import com.puppycrawl.tools.checkstyle.checks.usage.transmogrify.ASTManager; 26 import com.puppycrawl.tools.checkstyle.checks.usage.transmogrify.ClassManager; 27 import com.puppycrawl.tools.checkstyle.checks.usage.transmogrify.Definition; 28 import com.puppycrawl.tools.checkstyle.checks.usage.transmogrify.SymTabAST; 29 import com.puppycrawl.tools.checkstyle.checks.usage.transmogrify. 30 SymbolTableException; 31 import java.util.Iterator ; 32 import java.util.Set ; 33 import java.util.regex.Pattern ; 34 import java.util.regex.PatternSyntaxException ; 35 36 import org.apache.commons.beanutils.ConversionException; 37 38 42 public abstract class AbstractUsageCheck 43 extends Check 44 { 45 46 private Pattern mRegexp; 47 48 private String mIgnoreFormat; 49 50 53 public AbstractUsageCheck() 54 { 55 setIgnoreFormat("^$"); 56 } 57 58 63 public void setIgnoreFormat(String aFormat) 64 throws ConversionException 65 { 66 try { 67 mRegexp = Utils.getPattern(aFormat); 68 mIgnoreFormat = aFormat; 69 } 70 catch (PatternSyntaxException e) { 71 throw new ConversionException("unable to parse " + aFormat, e); 72 } 73 } 74 75 76 public Pattern getRegexp() 77 { 78 return mRegexp; 79 } 80 81 82 public String getIgnoreFormat() 83 { 84 return mIgnoreFormat; 85 } 86 87 88 public void beginTree(DetailAST aRootAST) 89 { 90 ClassManager.setClassLoader(getClassLoader()); 92 93 final String fileName = getFileContents().getFilename(); 94 getASTManager().addTree(fileName, aRootAST); 95 } 96 97 98 public void visitToken(DetailAST aAST) 99 { 100 if (mustCheckReferenceCount(aAST)) { 101 final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT); 102 Pattern regexp = getRegexp(); 103 if ((regexp == null) 104 || !regexp.matcher(nameAST.getText()).find()) 105 { 106 getASTManager().registerCheckNode(this, nameAST); 107 } 108 } 109 } 110 111 112 public void finishTree(DetailAST aAST) 113 { 114 if (aAST == null) { 115 return; 117 } 118 try { 119 final Set nodes = getASTManager().getCheckNodes(this); 120 if (nodes != null) { 121 applyTo(nodes); 122 } 123 } 124 catch (SymbolTableException ste) { 125 logError(ste); 126 } 127 getASTManager().removeCheck(this); 128 } 129 130 134 public void logError(Exception aException) 135 { 136 log(0, "general.exception", new String [] {aException.getMessage()}); 137 Utils.getExceptionLogger().debug("An exception occured.", aException); 138 } 139 140 145 private int getReferenceCount(DetailAST aAST) 146 { 147 final SymTabAST ident = getASTManager().get(aAST); 148 if (ident == null) { 149 return 0; 150 } 151 final Definition definition = 152 (Definition) ident.getDefinition(); 153 if (definition != null) { 154 return definition.getNumReferences(); 155 } 156 157 return 0; 158 } 159 160 164 public abstract String getErrorKey(); 165 166 171 public abstract boolean mustCheckReferenceCount(DetailAST aAST); 172 173 177 public void applyTo(Set aNodes) 178 { 179 final Iterator it = aNodes.iterator(); 180 while (it.hasNext()) { 181 final DetailAST nameAST = (DetailAST) it.next(); 182 if (getReferenceCount(nameAST) == 1) { 183 log( 184 nameAST.getLineNo(), 185 nameAST.getColumnNo(), 186 getErrorKey(), 187 nameAST.getText()); 188 } 189 } 190 } 191 192 196 protected ASTManager getASTManager() 197 { 198 return ASTManager.getInstance(); 199 } 200 } 201 | Popular Tags |