001: /* ====================================================================
002: * The JRefactory License, Version 1.0
003: *
004: * Copyright (c) 2001 JRefactory. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by the
021: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "JRefactory" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please contact seguin@acm.org.
028: *
029: * 5. Products derived from this software may not be called "JRefactory",
030: * nor may "JRefactory" appear in their name, without prior written
031: * permission of Chris Seguin.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of JRefactory. For more information on
049: * JRefactory, please see
050: * <http://www.sourceforge.org/projects/jrefactory>.
051: */
052: package org.acm.seguin.summary;
053:
054: import java.util.Iterator;
055: import java.util.LinkedList;
056:
057: /**
058: * All items that want to visit a summary tree should implement this interface.
059: *
060: *@author Chris Seguin
061: *@created May 15, 1999
062: */
063: public class TraversalVisitor implements SummaryVisitor {
064: /**
065: * Visit a summary node. This is the default method.
066: *
067: *@param node the summary that we are visiting
068: *@param data the data that was passed in
069: *@return the result
070: */
071: public Object visit(Summary node, Object data) {
072: // Shouldn't have to do anything about one of these nodes
073: return data;
074: }
075:
076: /**
077: * Visit all nodes.
078: *
079: *@param data the data that was passed in
080: */
081: public void visit(Object data) {
082: Iterator iter = PackageSummary.getAllPackages();
083: if (iter != null) {
084: // Create a temporary list to avoid concurrant modification problems
085: LinkedList list = new LinkedList();
086: while (iter.hasNext()) {
087: list.add(iter.next());
088: }
089:
090: iter = list.iterator();
091: while (iter.hasNext()) {
092: PackageSummary summary = (PackageSummary) iter.next();
093: summary.accept(this , data);
094: }
095: }
096: }
097:
098: /**
099: * Visit a package summary.
100: *
101: *@param node the summary that we are visiting
102: *@param data the data that was passed in
103: *@return the result
104: */
105: public Object visit(PackageSummary node, Object data) {
106: Iterator iter = node.getFileSummaries();
107:
108: if (iter != null) {
109: while (iter.hasNext()) {
110: FileSummary next = (FileSummary) iter.next();
111: next.accept(this , data);
112: }
113: }
114:
115: return data;
116: }
117:
118: /**
119: * Visit a file summary.
120: *
121: *@param node the summary that we are visiting
122: *@param data the data that was passed in
123: *@return the result
124: */
125: public Object visit(FileSummary node, Object data) {
126: // Over the imports
127: Iterator iter = node.getImports();
128:
129: if (iter != null) {
130: while (iter.hasNext()) {
131: ImportSummary next = (ImportSummary) iter.next();
132: next.accept(this , data);
133: }
134: }
135:
136: // Over the types
137: iter = node.getTypes();
138: if (iter != null) {
139: while (iter.hasNext()) {
140: TypeSummary next = (TypeSummary) iter.next();
141: next.accept(this , data);
142: }
143: }
144:
145: // Return some value
146: return data;
147: }
148:
149: /**
150: * Visit a import summary.
151: *
152: *@param node the summary that we are visiting
153: *@param data the data that was passed in
154: *@return the result
155: */
156: public Object visit(ImportSummary node, Object data) {
157: // No children so just return
158: return data;
159: }
160:
161: /**
162: * Visit a type summary.
163: *
164: *@param node the summary that we are visiting
165: *@param data the data that was passed in
166: *@return the result
167: */
168: public Object visit(TypeSummary node, Object data) {
169: // Over the fields
170: Iterator iter = node.getFields();
171: if (iter != null) {
172: while (iter.hasNext()) {
173: FieldSummary next = (FieldSummary) iter.next();
174: next.accept(this , data);
175: }
176: }
177:
178: // Over the methods
179: iter = node.getMethods();
180: if (iter != null) {
181: while (iter.hasNext()) {
182: MethodSummary next = (MethodSummary) iter.next();
183: next.accept(this , data);
184: }
185: }
186:
187: // Over the types
188: iter = node.getTypes();
189: if (iter != null) {
190: while (iter.hasNext()) {
191: TypeSummary next = (TypeSummary) iter.next();
192: next.accept(this , data);
193: }
194: }
195:
196: // Return some value
197: return data;
198: }
199:
200: /**
201: * Visit a method summary.
202: *
203: *@param node the summary that we are visiting
204: *@param data the data that was passed in
205: *@return the result
206: */
207: public Object visit(MethodSummary node, Object data) {
208: // First visit the return type
209: if (node.getReturnType() != null) {
210: node.getReturnType().accept(this , data);
211: }
212:
213: // Then visit the parameter types
214: Iterator iter = node.getParameters();
215: if (iter != null) {
216: while (iter.hasNext()) {
217: Summary next = (Summary) iter.next();
218: next.accept(this , data);
219: }
220: }
221:
222: // Third visit the exceptions
223: iter = node.getExceptions();
224: if (iter != null) {
225: while (iter.hasNext()) {
226: Summary next = (Summary) iter.next();
227: next.accept(this , data);
228: }
229: }
230:
231: // Finally visit the dependencies
232: iter = node.getDependencies();
233: if (iter != null) {
234: while (iter.hasNext()) {
235: Summary next = (Summary) iter.next();
236: next.accept(this , data);
237: }
238: }
239:
240: return data;
241: }
242:
243: /**
244: * Visit a field summary.
245: *
246: *@param node the summary that we are visiting
247: *@param data the data that was passed in
248: *@return the result
249: */
250: public Object visit(FieldSummary node, Object data) {
251: node.getTypeDecl().accept(this , data);
252: return data;
253: }
254:
255: /**
256: * Visit a parameter summary.
257: *
258: *@param node the summary that we are visiting
259: *@param data the data that was passed in
260: *@return the result
261: */
262: public Object visit(ParameterSummary node, Object data) {
263: node.getTypeDecl().accept(this , data);
264: return data;
265: }
266:
267: /**
268: * Visit a local variable summary.
269: *
270: *@param node the summary that we are visiting
271: *@param data the data that was passed in
272: *@return the result
273: */
274: public Object visit(LocalVariableSummary node, Object data) {
275: node.getTypeDecl().accept(this , data);
276: return data;
277: }
278:
279: /**
280: * Visit a variable summary.
281: *
282: *@param node the summary that we are visiting
283: *@param data the data that was passed in
284: *@return the result
285: */
286: public Object visit(VariableSummary node, Object data) {
287: node.getTypeDecl().accept(this , data);
288: return data;
289: }
290:
291: /**
292: * Visit a type declaration summary.
293: *
294: *@param node the summary that we are visiting
295: *@param data the data that was passed in
296: *@return the result
297: */
298: public Object visit(TypeDeclSummary node, Object data) {
299: // This is a leaf. It does not contain any summary objects
300: return data;
301: }
302:
303: /**
304: * Visit a message send summary.
305: *
306: *@param node the summary that we are visiting
307: *@param data the data that was passed in
308: *@return the result
309: */
310: public Object visit(MessageSendSummary node, Object data) {
311: TypeDeclSummary typeDecl = node.getTypeDecl();
312: if (typeDecl != null) {
313: typeDecl.accept(this , data);
314: }
315: return data;
316: }
317:
318: /**
319: * Visit a field access summary.
320: *
321: *@param node the summary that we are visiting
322: *@param data the data that was passed in
323: *@return the result
324: */
325: public Object visit(FieldAccessSummary node, Object data) {
326: TypeDeclSummary typeDecl = node.getTypeDecl();
327: if (typeDecl != null) {
328: typeDecl.accept(this, data);
329: }
330: return data;
331: }
332: }
|