1 package com.puppycrawl.tools.checkstyle.checks; 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 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 29 35 public abstract class DeclarationCollector extends Check 36 { 37 38 private FrameStack mFrames; 39 40 41 public void beginTree(DetailAST aRootAST) 42 { 43 mFrames = new FrameStack(); 44 } 45 46 47 public void visitToken(DetailAST aAST) 48 { 49 switch (aAST.getType()) { 50 case TokenTypes.PARAMETER_DEF : 51 case TokenTypes.VARIABLE_DEF : { 52 final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT); 53 this.mFrames.current().addName(nameAST.getText()); 54 break; 55 } 56 case TokenTypes.CLASS_DEF : 57 case TokenTypes.INTERFACE_DEF : 58 case TokenTypes.ENUM_DEF : 59 case TokenTypes.ANNOTATION_DEF : { 60 final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT); 61 this.mFrames.current().addName(nameAST.getText()); 62 this.mFrames.enter(new ClassFrame()); 63 break; 64 } 65 case TokenTypes.SLIST : 66 this.mFrames.enter(new BlockFrame()); 67 break; 68 case TokenTypes.METHOD_DEF : 69 case TokenTypes.CTOR_DEF : 70 this.mFrames.enter(new MethodFrame()); 71 break; 72 default: 73 } 75 } 76 77 78 79 public void leaveToken(DetailAST aAST) 80 { 81 switch (aAST.getType()) { 82 case TokenTypes.CLASS_DEF : 83 case TokenTypes.INTERFACE_DEF : 84 case TokenTypes.ENUM_DEF : 85 case TokenTypes.ANNOTATION_DEF : 86 case TokenTypes.SLIST : 87 case TokenTypes.METHOD_DEF : 88 case TokenTypes.CTOR_DEF : 89 this.mFrames.leave(); 90 break; 91 default : 92 } 94 } 95 96 102 protected final boolean isDeclared(String aName) 103 { 104 return (null != mFrames.findFrame(aName)); 105 } 106 107 112 protected final boolean isClassField(String aName) 113 { 114 return (mFrames.findFrame(aName) instanceof ClassFrame); 115 } 116 } 117 118 123 abstract class LexicalFrame 124 { 125 126 private HashSet mVarNames; 127 128 129 protected LexicalFrame() 130 { 131 mVarNames = new HashSet (); 132 } 133 134 137 void addName(String aNameToAdd) 138 { 139 this.mVarNames.add(aNameToAdd); 140 } 141 142 146 boolean contains(String aNameToFind) 147 { 148 return this.mVarNames.contains(aNameToFind); 149 } 150 } 151 152 156 class GlobalFrame extends LexicalFrame 157 { 158 159 GlobalFrame() 160 { 161 super(); 162 } 163 } 164 165 169 class MethodFrame extends LexicalFrame 170 { 171 172 MethodFrame() 173 { 174 super(); 175 } 176 } 177 178 184 class ClassFrame extends LexicalFrame 185 { 186 187 ClassFrame() 188 { 189 super(); 190 } 191 } 192 193 199 class BlockFrame extends LexicalFrame 200 { 201 202 BlockFrame() 203 { 204 super(); 205 } 206 } 207 208 212 class FrameStack 213 { 214 215 private LinkedList mFrameList; 216 217 218 FrameStack() 219 { 220 mFrameList = new LinkedList (); 221 this.enter(new GlobalFrame()); 222 } 223 224 228 void enter(LexicalFrame aNewFrame) 229 { 230 mFrameList.addFirst(aNewFrame); 231 } 232 233 234 void leave() 235 { 236 mFrameList.removeFirst(); 237 } 238 239 243 LexicalFrame current() 244 { 245 return (LexicalFrame) mFrameList.getFirst(); 246 } 247 248 253 LexicalFrame findFrame(String aNameToFind) 254 { 255 final Iterator it = mFrameList.iterator(); 256 while (it.hasNext()) { 257 final LexicalFrame thisFrame = (LexicalFrame) it.next(); 258 if (thisFrame.contains(aNameToFind)) { 259 return thisFrame; 260 } 261 } 262 return null; 263 } 264 } 265 | Popular Tags |