1 28 package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify; 29 30 import java.io.File ; 31 32 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 33 34 35 36 43 44 public class ASTUtil { 45 46 55 public static int getLine(SymTabAST tree) { 56 SymTabAST indexedNode = tree; 57 58 if (indexedNode.getLineNo() == 0) { 60 indexedNode = (SymTabAST) indexedNode.getFirstChild(); 61 62 while (indexedNode != null && indexedNode.getLineNo() == 0) { 63 indexedNode = (SymTabAST) indexedNode.getNextSibling(); 64 } 65 66 if (indexedNode == null) { 67 indexedNode = tree; 69 } 70 } 71 72 return indexedNode.getLineNo(); 73 } 74 75 84 public static int getColumn(SymTabAST tree) { 85 SymTabAST indexedNode = tree; 86 87 if (indexedNode.getColumnNo() == 0 91 || indexedNode.getType() == TokenTypes.LABELED_STAT) { 92 indexedNode = (SymTabAST) indexedNode.getFirstChild(); 93 94 while (indexedNode != null && indexedNode.getColumnNo() == 0) { 95 indexedNode = (SymTabAST) indexedNode.getNextSibling(); 96 } 97 98 if (indexedNode == null) { 99 indexedNode = tree; 101 } 102 } 103 104 return indexedNode.getColumnNo(); 105 } 106 107 114 public static String constructDottedName(SymTabAST tree) { 115 String result; 116 117 if (tree.getType() == TokenTypes.DOT) { 118 SymTabAST left = (SymTabAST) tree.getFirstChild(); 119 SymTabAST right = (SymTabAST) left.getNextSibling(); 120 121 result = 122 constructDottedName(left) + "." + constructDottedName(right); 123 } 124 else if (tree.getType() == TokenTypes.ARRAY_DECLARATOR) { 125 StringBuffer buf = new StringBuffer (); 126 SymTabAST left = (SymTabAST) tree.getFirstChild(); 127 SymTabAST right = (SymTabAST) left.getNextSibling(); 128 129 buf.append(constructDottedName(left)); 130 131 if (right != null) { 132 buf.append("."); 133 buf.append(constructDottedName(right)); 134 } 135 136 buf.append(" []"); 137 138 result = buf.toString(); 139 } 140 else if (tree.getType() == TokenTypes.METHOD_CALL) { 141 result = 142 constructDottedName((SymTabAST) tree.getFirstChild()) + "()"; 143 } 144 else { 145 result = tree.getText(); 146 } 147 148 return result; 149 } 150 151 157 public static String constructPackage(SymTabAST tree) { 158 String fullName = constructDottedName(tree); 159 160 return fullName.substring(0, fullName.lastIndexOf(".")); 161 } 162 163 169 public static String constructClass(SymTabAST tree) { 170 String fullName = constructDottedName(tree); 171 172 return fullName.substring( 173 fullName.lastIndexOf(".") + 1, 174 fullName.length()); 175 } 176 177 public static boolean treesBelowFilesAreEqual( 178 SymTabAST firstRoot, 179 File [] firstFiles, 180 SymTabAST secondRoot, 181 File [] secondFiles) { 182 boolean result = true; 183 184 if (firstFiles.length == secondFiles.length) { 185 for (int i = 0; i < firstFiles.length; i++) { 186 SymTabAST firstTree = 187 (SymTabAST) getFileNode(firstRoot, firstFiles[i]) 188 .getFirstChild(); 189 SymTabAST secondTree = 190 (SymTabAST) getFileNode(secondRoot, secondFiles[i]) 191 .getFirstChild(); 192 193 if (!firstTree.equalsList(secondTree)) { 194 result = false; 195 break; 196 } 197 } 198 } 199 else { 200 result = false; 201 } 202 203 return result; 204 } 205 206 public static SymTabAST getFileNode(SymTabAST root, File file) 207 { 208 SymTabAST result = null; 209 210 SymTabAST fileNode = (SymTabAST) root.getFirstChild(); 211 while (fileNode != null && result == null) { 212 if (file.equals(fileNode.getFile())) { 213 result = fileNode; 214 } 215 fileNode = (SymTabAST) fileNode.getNextSibling(); 216 } 217 218 return result; 219 } 220 } 221 | Popular Tags |