1 package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify; 20 21 import java.io.File ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import antlr.collections.AST; 29 30 import com.puppycrawl.tools.checkstyle.checks.usage.AbstractUsageCheck; 31 32 37 public final class ASTManager 38 { 39 40 private static final ASTManager INSTANCE = new ASTManager(); 41 42 48 private Map mMap = new HashMap (); 49 50 51 private SymTabAST mCompleteTree = null; 52 53 54 private Map mTrees = new HashMap (); 55 56 57 private Map mCheckNodes = new HashMap (); 58 59 60 private ASTManager() 61 { 62 } 63 64 68 public static ASTManager getInstance() 69 { 70 return INSTANCE; 71 } 72 73 80 public void addTree(String aFileName, AST aRoot) 81 { 82 clear(); 83 mTrees.put(aFileName, aRoot); 84 } 85 86 89 private void clear() { 90 mCheckNodes.clear(); 91 mTrees.clear(); 92 mMap.clear(); 93 mCompleteTree = null; 94 } 95 96 100 private void buildTree() 101 throws SymbolTableException 102 { 103 mCompleteTree = SymTabASTFactory.create(0, "AST Root"); 104 final Set keys = mTrees.keySet(); 105 final Iterator it = keys.iterator(); 106 while (it.hasNext()) { 107 final String fileName = (String ) it.next(); 108 final File file = new File (fileName); 109 final AST rootAST = (AST) mTrees.get(fileName); 110 addToCompleteTree(file, rootAST); 111 } 112 113 new TableMaker(mCompleteTree).getTable(); 116 } 117 118 124 private void addToCompleteTree(File aFile, AST aAST) 125 { 126 final SymTabAST fileNode = 128 SymTabASTFactory.create(0, aFile.getAbsolutePath()); 129 fileNode.setFile(aFile); 130 mCompleteTree.addChild(fileNode); 131 fileNode.setParent(mCompleteTree); 132 133 final SymTabAST child = SymTabASTFactory.create(aAST); 135 child.setFile(aFile); 136 fileNode.addChild(child); 137 child.setParent(fileNode); 138 fileNode.finishDefinition(aFile, mCompleteTree); 139 } 140 141 146 public void registerCheckNode(AbstractUsageCheck aCheck, AST aNode) 147 { 148 Set nodeSet = (Set ) mCheckNodes.get(aCheck); 149 if (nodeSet == null) { 150 nodeSet = new HashSet (); 151 nodeSet.add(aNode); 152 mCheckNodes.put(aCheck, nodeSet); 153 } 154 else { 155 nodeSet.add(aNode); 156 } 157 } 158 159 165 public Set getCheckNodes(AbstractUsageCheck aCheck) 166 throws SymbolTableException 167 { 168 if (mCompleteTree == null) { 170 buildTree(); 171 } 172 Set result = (Set ) mCheckNodes.get(aCheck); 173 if (result == null) { 174 result = new HashSet (); 175 } 176 return result; 177 } 178 179 184 public void put(AST aAST, SymTabAST aSymTabAST) 185 { 186 mMap.put(aAST, aSymTabAST); 187 } 188 189 194 public SymTabAST get(AST aAST) 195 { 196 return (SymTabAST) mMap.get(aAST); 197 } 198 199 202 public void clearDetailsMap() 203 { 204 mMap.clear(); 205 } 206 207 211 public boolean isEmptyDetailsMap() 212 { 213 return mMap.isEmpty(); 214 } 215 216 221 public void removeCheck(AbstractUsageCheck aCheck) 222 { 223 mCheckNodes.remove(aCheck); 224 if (mCheckNodes.isEmpty()) { 225 clear(); 226 } 227 } 228 } 229 | Popular Tags |