001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.refactor.method;
010:
011: import java.util.Iterator;
012: import java.util.LinkedList;
013: import java.util.StringTokenizer;
014: import org.acm.seguin.refactor.ComplexTransform;
015: import org.acm.seguin.summary.FieldAccessSummary;
016: import org.acm.seguin.summary.FieldSummary;
017: import org.acm.seguin.summary.FileSummary;
018: import org.acm.seguin.summary.LocalVariableSummary;
019: import org.acm.seguin.summary.MessageSendSummary;
020: import org.acm.seguin.summary.MethodSummary;
021: import org.acm.seguin.summary.ParameterSummary;
022: import org.acm.seguin.summary.Summary;
023: import org.acm.seguin.summary.TraversalVisitor;
024: import org.acm.seguin.summary.TypeDeclSummary;
025: import org.acm.seguin.summary.TypeSummary;
026: import org.acm.seguin.summary.VariableSummary;
027:
028: /**
029: * All items that want to visit a summary tree should implement this
030: * interface.
031: *
032: *@author Chris Seguin
033: *@created May 15, 1999
034: */
035: public class RenameSystemTraversal extends TraversalVisitor {
036: /**
037: * Visit a file summary.
038: *
039: *@param node the summary that we are visiting
040: *@param data the data that was passed in
041: *@return the result
042: */
043: public Object visit(FileSummary node, Object data) {
044: if (node.getFile() == null) {
045: return data; // this file summary is from a stub (probably the JDK).
046: }
047: System.out.println("RenameSystemTraversal.visit(FileSummary "
048: + node + ", Object data)");
049:
050: // Over the types
051: Boolean result = Boolean.FALSE;
052: Iterator iter = node.getTypes();
053: if (iter != null) {
054: while (iter.hasNext() && result.equals(Boolean.FALSE)) {
055: TypeSummary next = (TypeSummary) iter.next();
056: result = (Boolean) next.accept(this , data);
057: }
058: }
059:
060: if (result.booleanValue()) {
061: // Apply the refactoring to this file
062: System.out
063: .println("Updating: " + node.getFile().getPath());
064: RenameMethodData rfd = (RenameMethodData) data;
065: transform(node, rfd.getOldMethod(), rfd.getNewName(), rfd
066: .getComplexTransform());
067: }
068:
069: // Return some value
070: return data;
071: }
072:
073: /**
074: * Visit a type summary.
075: *
076: *@param node the summary that we are visiting
077: *@param data the data that was passed in
078: *@return the result
079: */
080: public Object visit(TypeSummary node, Object data) {
081: System.out.println("RenameSystemTraversal.visit(TypeSummary "
082: + node + ", Object data)");
083: RenameMethodData rfd = (RenameMethodData) data;
084: if (node.equals(rfd.getTypeSummary())) {
085: System.out.println(" - not found");
086: return Boolean.FALSE;
087: }
088:
089: // Over the fields
090: Iterator iter = node.getMethods();
091: if (iter != null) {
092: while (iter.hasNext()) {
093: MethodSummary next = (MethodSummary) iter.next();
094: Boolean result = (Boolean) next.accept(this , data);
095: if (result.equals(Boolean.TRUE)) {
096: System.out.println(" - found");
097: return result;
098: }
099: }
100: }
101:
102: // Over the types
103: iter = node.getTypes();
104: if (iter != null) {
105: while (iter.hasNext()) {
106: TypeSummary next = (TypeSummary) iter.next();
107: Boolean result = (Boolean) next.accept(this , data);
108: if (result.equals(Boolean.TRUE)) {
109: System.out.println(" - found");
110: return result;
111: }
112: }
113: }
114:
115: System.out.println(" - not found");
116: // Return some value
117: return Boolean.FALSE;
118: }
119:
120: /**
121: * Visit a method summary.
122: *
123: *@param node the summary that we are visiting
124: *@param data the data that was passed in
125: *@return the result
126: */
127: public Object visit(MethodSummary node, Object data) {
128: System.out.println("RenameSystemTraversal.visit(MethodSummary "
129: + node + ", Object data)");
130: // Finally visit the dependencies
131: Iterator iter = node.getDependencies();
132: if (iter != null) {
133: while (iter.hasNext()) {
134: Summary next = (Summary) iter.next();
135: Boolean result = (Boolean) next.accept(this , data);
136: if (result.equals(Boolean.TRUE)) {
137: System.out.println(" - found");
138: return result;
139: }
140: }
141: }
142:
143: System.out.println(" - not found");
144: return Boolean.FALSE;
145: }
146:
147: /**
148: * Visit a field summary.
149: *
150: *@param node the summary that we are visiting
151: *@param data the data that was passed in
152: *@return the result
153: */
154: public Object visit(FieldSummary node, Object data) {
155: return Boolean.FALSE;
156: }
157:
158: /**
159: * Visit a parameter summary.
160: *
161: *@param node the summary that we are visiting
162: *@param data the data that was passed in
163: *@return the result
164: */
165: public Object visit(ParameterSummary node, Object data) {
166: return Boolean.FALSE;
167: }
168:
169: /**
170: * Visit a local variable summary.
171: *
172: *@param node the summary that we are visiting
173: *@param data the data that was passed in
174: *@return the result
175: */
176: public Object visit(LocalVariableSummary node, Object data) {
177: return Boolean.FALSE;
178: }
179:
180: /**
181: * Visit a variable summary.
182: *
183: *@param node the summary that we are visiting
184: *@param data the data that was passed in
185: *@return the result
186: */
187: public Object visit(VariableSummary node, Object data) {
188: return Boolean.FALSE;
189: }
190:
191: /**
192: * Visit a type declaration summary.
193: *
194: *@param node the summary that we are visiting
195: *@param data the data that was passed in
196: *@return the result
197: */
198: public Object visit(TypeDeclSummary node, Object data) {
199: return Boolean.FALSE;
200: }
201:
202: /**
203: * Visit a message send summary.
204: *
205: *@param node the summary that we are visiting
206: *@param data the data that was passed in
207: *@return the result
208: */
209: public Object visit(MessageSendSummary node, Object data) {
210: System.out
211: .println("RenameSystemTraversal.visit(MessageSendSummary "
212: + node + ", Object data)");
213: String message = node.toString();
214:
215: RenameMethodData rfd = (RenameMethodData) data;
216: String oldName = rfd.getOldName();
217: StringTokenizer tok = new StringTokenizer(message, ".");
218:
219: while (tok.hasMoreTokens()) {
220: String next = tok.nextToken();
221: int index = next.indexOf("(");
222: if (index > 0) {
223: next = next.substring(0, index);
224: }
225: System.out.println(" next=" + next + ", oldName="
226: + oldName);
227: if (next.equals(oldName)) {
228: System.out.println(" - found");
229: return Boolean.TRUE;
230: }
231: }
232:
233: System.out.println(" - not found");
234: return Boolean.FALSE;
235: }
236:
237: /**
238: * Visit a field access summary.
239: *
240: *@param node the summary that we are visiting
241: *@param data the data that was passed in
242: *@return the result
243: */
244: public Object visit(FieldAccessSummary node, Object data) {
245: System.out
246: .println("RenameSystemTraversal.visit(FieldAccessSummary "
247: + node + ", Object data)");
248: String message = node.getName();
249:
250: RenameMethodData rfd = (RenameMethodData) data;
251: StringTokenizer tok = new StringTokenizer(message, ".");
252:
253: while (tok.hasMoreTokens()) {
254: String next = tok.nextToken();
255: if (next.equals(rfd.getOldName())) {
256: System.out.println(" - found");
257: return Boolean.TRUE;
258: }
259: }
260:
261: System.out.println(" - not found");
262: return Boolean.FALSE;
263: }
264:
265: /**
266: * Perform the rename transformation on this particular file
267: *
268: *@param fileSummary Description of Parameter
269: *@param oldField Description of Parameter
270: *@param newName Description of Parameter
271: *@param transform Description of Parameter
272: */
273: private void transform(FileSummary fileSummary,
274: MethodSummary oldMethod, String newName,
275: ComplexTransform transform) {
276: System.out.println("RenameSystemTraversal.transform()");
277: transform.clear();
278:
279: RenameMethodTransform rft = new RenameMethodTransform(
280: oldMethod, newName);
281: transform.add(rft);
282: transform.apply(fileSummary.getFile(), fileSummary.getFile());
283: }
284: }
|