KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > usage > transmogrify > ASTUtil


1 // Transmogrify License
2
//
3
// Copyright (c) 2001, ThoughtWorks, Inc.
4
// All rights reserved.
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions
7
// are met:
8
// - Redistributions of source code must retain the above copyright notice,
9
// this list of conditions and the following disclaimer.
10
// - Redistributions in binary form must reproduce the above copyright
11
// notice, this list of conditions and the following disclaimer in the
12
// documentation and/or other materials provided with the distribution.
13
// Neither the name of the ThoughtWorks, Inc. nor the names of its
14
// contributors may be used to endorse or promote products derived from this
15
// software without specific prior written permission.
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27

28 package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
29
30 import java.io.File JavaDoc;
31
32 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33
34
35
36 /**
37  * <code>ASTUtil</code> is a <code>Utility Class</code> that contains utility code
38  * for managing our SymTabAST.
39  *
40  * @see Definition
41  * @see TypedDef
42  */

43
44 public class ASTUtil {
45
46     /**
47      * gets a line number for the tree; if the current SymTabAST node does not have one associated
48      * with it, traverse its children until a line number is found. Failure results in line
49      * number value of 0.
50      *
51      * @param tree the SymTabAST to process
52      *
53      * @return int the resulting line number (0 if none is found)
54      */

55     public static int getLine(SymTabAST tree) {
56         SymTabAST indexedNode = tree;
57
58         // find a node that actually has line number info
59
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                 // we're screwed
68
indexedNode = tree;
69             }
70         }
71
72         return indexedNode.getLineNo();
73     }
74
75     /**
76      * gets a column number for the tree; if the current SymTabAST node does not have one associated
77      * with it, traverse its children until a column number is found. Failure results in column
78      * number value of 0.
79      *
80      * @param tree the SymTabAST to process
81      *
82      * @return int the resulting line number (0 if none is found)
83      */

84     public static int getColumn(SymTabAST tree) {
85         SymTabAST indexedNode = tree;
86
87         // find a node that actually has line number info
88
// REDTAG -- a label's ':' is a real token and has (the wrong) column info
89
// because it is the parent of the ident node that people will want
90
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                 // we're screwed
100
indexedNode = tree;
101             }
102         }
103
104         return indexedNode.getColumnNo();
105     }
106
107     /**
108      * Builds the dotted name String representation of the object contained within
109      * the SymTabAST.
110      *
111      * @return String
112      * @param tree the SymTabAST contaning the entire hierarcy of the object
113      */

114     public static String JavaDoc constructDottedName(SymTabAST tree) {
115         String JavaDoc 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 JavaDoc buf = new StringBuffer JavaDoc();
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     /**
152      * Returns the Package name in the hierarchy represented by the SymTabAST.
153      *
154      * @return String
155      * @param tree the SymTabAST contaning the entire hierarcy of the object
156      */

157     public static String JavaDoc constructPackage(SymTabAST tree) {
158         String JavaDoc fullName = constructDottedName(tree);
159
160         return fullName.substring(0, fullName.lastIndexOf("."));
161     }
162
163     /**
164     * Returns the top Class name in the hierarchy represented by the SymTabAST.
165     *
166     * @return String
167     * @param tree the SymTabAST contaning the entire hierarcy of the object
168     */

169     public static String JavaDoc constructClass(SymTabAST tree) {
170         String JavaDoc 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 JavaDoc[] firstFiles,
180         SymTabAST secondRoot,
181         File JavaDoc[] 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 JavaDoc 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