001: // Transmogrify License
002: //
003: // Copyright (c) 2001, ThoughtWorks, Inc.
004: // All rights reserved.
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions
007: // are met:
008: // - Redistributions of source code must retain the above copyright notice,
009: // this list of conditions and the following disclaimer.
010: // - Redistributions in binary form must reproduce the above copyright
011: // notice, this list of conditions and the following disclaimer in the
012: // documentation and/or other materials provided with the distribution.
013: // Neither the name of the ThoughtWorks, Inc. nor the names of its
014: // contributors may be used to endorse or promote products derived from this
015: // software without specific prior written permission.
016: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
017: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
018: // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
019: // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
020: // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
021: // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
022: // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
023: // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
024: // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
025: // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
026: // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027:
028: package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
029:
030: import java.io.File;
031:
032: import com.puppycrawl.tools.checkstyle.api.TokenTypes;
033:
034: /**
035: * <code>ASTUtil</code> is a <code>Utility Class</code> that contains utility code
036: * for managing our SymTabAST.
037: *
038: * @see Definition
039: * @see TypedDef
040: */
041:
042: public class ASTUtil {
043:
044: /**
045: * gets a line number for the tree; if the current SymTabAST node does not have one associated
046: * with it, traverse its children until a line number is found. Failure results in line
047: * number value of 0.
048: *
049: * @param tree the SymTabAST to process
050: *
051: * @return int the resulting line number (0 if none is found)
052: */
053: public static int getLine(SymTabAST tree) {
054: SymTabAST indexedNode = tree;
055:
056: // find a node that actually has line number info
057: if (indexedNode.getLineNo() == 0) {
058: indexedNode = (SymTabAST) indexedNode.getFirstChild();
059:
060: while (indexedNode != null && indexedNode.getLineNo() == 0) {
061: indexedNode = (SymTabAST) indexedNode.getNextSibling();
062: }
063:
064: if (indexedNode == null) {
065: // we're screwed
066: indexedNode = tree;
067: }
068: }
069:
070: return indexedNode.getLineNo();
071: }
072:
073: /**
074: * gets a column number for the tree; if the current SymTabAST node does not have one associated
075: * with it, traverse its children until a column number is found. Failure results in column
076: * number value of 0.
077: *
078: * @param tree the SymTabAST to process
079: *
080: * @return int the resulting line number (0 if none is found)
081: */
082: public static int getColumn(SymTabAST tree) {
083: SymTabAST indexedNode = tree;
084:
085: // find a node that actually has line number info
086: // REDTAG -- a label's ':' is a real token and has (the wrong) column info
087: // because it is the parent of the ident node that people will want
088: if (indexedNode.getColumnNo() == 0
089: || indexedNode.getType() == TokenTypes.LABELED_STAT) {
090: indexedNode = (SymTabAST) indexedNode.getFirstChild();
091:
092: while (indexedNode != null
093: && indexedNode.getColumnNo() == 0) {
094: indexedNode = (SymTabAST) indexedNode.getNextSibling();
095: }
096:
097: if (indexedNode == null) {
098: // we're screwed
099: indexedNode = tree;
100: }
101: }
102:
103: return indexedNode.getColumnNo();
104: }
105:
106: /**
107: * Builds the dotted name String representation of the object contained within
108: * the SymTabAST.
109: *
110: * @return String
111: * @param tree the SymTabAST contaning the entire hierarcy of the object
112: */
113: public static String constructDottedName(SymTabAST tree) {
114: String result;
115:
116: if (tree.getType() == TokenTypes.DOT) {
117: SymTabAST left = (SymTabAST) tree.getFirstChild();
118: SymTabAST right = (SymTabAST) left.getNextSibling();
119:
120: result = constructDottedName(left) + "."
121: + constructDottedName(right);
122: } else if (tree.getType() == TokenTypes.ARRAY_DECLARATOR) {
123: StringBuffer buf = new StringBuffer();
124: SymTabAST left = (SymTabAST) tree.getFirstChild();
125: SymTabAST right = (SymTabAST) left.getNextSibling();
126:
127: buf.append(constructDottedName(left));
128:
129: if (right != null) {
130: buf.append(".");
131: buf.append(constructDottedName(right));
132: }
133:
134: buf.append(" []");
135:
136: result = buf.toString();
137: } else if (tree.getType() == TokenTypes.METHOD_CALL) {
138: result = constructDottedName((SymTabAST) tree
139: .getFirstChild())
140: + "()";
141: } else {
142: result = tree.getText();
143: }
144:
145: return result;
146: }
147:
148: /**
149: * Returns the Package name in the hierarchy represented by the SymTabAST.
150: *
151: * @return String
152: * @param tree the SymTabAST contaning the entire hierarcy of the object
153: */
154: public static String constructPackage(SymTabAST tree) {
155: String fullName = constructDottedName(tree);
156:
157: return fullName.substring(0, fullName.lastIndexOf("."));
158: }
159:
160: /**
161: * Returns the top Class name in the hierarchy represented by the SymTabAST.
162: *
163: * @return String
164: * @param tree the SymTabAST contaning the entire hierarcy of the object
165: */
166: public static String constructClass(SymTabAST tree) {
167: String fullName = constructDottedName(tree);
168:
169: return fullName.substring(fullName.lastIndexOf(".") + 1,
170: fullName.length());
171: }
172:
173: public static boolean treesBelowFilesAreEqual(SymTabAST firstRoot,
174: File[] firstFiles, SymTabAST secondRoot, File[] secondFiles) {
175: boolean result = true;
176:
177: if (firstFiles.length == secondFiles.length) {
178: for (int i = 0; i < firstFiles.length; i++) {
179: SymTabAST firstTree = (SymTabAST) getFileNode(
180: firstRoot, firstFiles[i]).getFirstChild();
181: SymTabAST secondTree = (SymTabAST) getFileNode(
182: secondRoot, secondFiles[i]).getFirstChild();
183:
184: if (!firstTree.equalsList(secondTree)) {
185: result = false;
186: break;
187: }
188: }
189: } else {
190: result = false;
191: }
192:
193: return result;
194: }
195:
196: public static SymTabAST getFileNode(SymTabAST root, File file) {
197: SymTabAST result = null;
198:
199: SymTabAST fileNode = (SymTabAST) root.getFirstChild();
200: while (fileNode != null && result == null) {
201: if (file.equals(fileNode.getFile())) {
202: result = fileNode;
203: }
204: fileNode = (SymTabAST) fileNode.getNextSibling();
205: }
206:
207: return result;
208: }
209: }
|