1 2 29 package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify; 30 31 import java.io.File ; 32 import java.util.Iterator ; 33 34 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 35 36 37 38 42 public class QueryEngine { 43 private SymbolTable symbolTable; 44 private ScopeIndex index; 45 46 public QueryEngine(SymbolTable aSymbolTable) { 47 this.symbolTable = aSymbolTable; 48 setIndex(); 49 } 50 51 54 private void setIndex() { 55 index = symbolTable.getScopeIndex(); 56 } 57 58 66 public Reference getSymbol(String name, Occurrence location) { 67 Scope enclosingScope = index.lookup(location); 68 Reference result = enclosingScope.getSymbol(name, location); 69 70 if (result == null) { 73 result = enclosingScope.getParentScope().getSymbol(name, location); 74 } 75 76 return result; 77 } 78 79 88 public IDefinition getDefinition(String name, Occurrence location) { 89 Reference symbol = getSymbol(name, location); 90 91 98 return resolveDefinition(symbol); 99 } 100 101 public IDefinition getDefinition(Occurrence location) { 102 IDefinition result = null; 103 104 SymTabAST node = getWordNodeAtOccurrence(location); 105 if ( node != null ) { 106 result = node.getDefinition(); 107 } 108 109 return result; 110 } 111 112 private IDefinition resolveDefinition(Reference reference) { 113 IDefinition result = null; 114 115 if ( reference != null ) { 116 result = reference.getDefinition(); 117 } 118 119 return result; 120 } 121 122 130 public Iterator getReferences(String name, Occurrence location) { 131 Reference symbol = getSymbol(name, location); 132 return resolveReferences(symbol); 133 } 134 135 public Iterator getReferences(Occurrence location) { 136 Iterator result = null; 137 138 SymTabAST node = getWordNodeAtOccurrence(location); 139 if ( node != null && node.getDefinition() != null ) { 140 result = node.getDefinition().getReferences(); 141 } 142 143 return result; 144 } 145 146 private Iterator resolveReferences(Reference reference) { 147 return reference.getDefinition().getReferences(); 148 } 149 150 public SymTabAST getFileNode(File file) { 151 return ASTUtil.getFileNode(symbolTable.getTree(), file); 152 } 153 154 private SymTabAST getWordNodeAtOccurrence(Occurrence location) { 155 SymTabAST result = null; 156 157 SymTabAST fileNode = getFileNode(location.getFile()); 158 if ( fileNode != null ) { 159 SymTabAST node = fileNode.getEnclosingNode(location.getLine(), 160 location.getColumn()); 161 162 if ( (node != null) && (node.getType() == TokenTypes.IDENT) ) { 163 result = node; 164 } 165 } 166 167 return result; 168 } 169 170 public String getWordAtOccurrence(Occurrence location ) { 171 String result = null; 172 173 SymTabAST node = getWordNodeAtOccurrence(location); 174 if ( node != null ) { 175 result = node.getText(); 176 } 177 178 return result; 179 } 180 181 } 182 183 184 185 186 | Popular Tags |