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 net.sourceforge.jrefactory.ast.SimpleNode;
013: import org.acm.seguin.refactor.AddImportTransform;
014: import org.acm.seguin.refactor.ComplexTransform;
015: import org.acm.seguin.summary.TraversalVisitor;
016: import org.acm.seguin.summary.TypeDeclSummary;
017: import org.acm.seguin.summary.TypeSummary;
018: import org.acm.seguin.summary.MethodSummary;
019: import org.acm.seguin.summary.Summary;
020: import org.acm.seguin.summary.query.GetTypeSummary;
021: import org.acm.seguin.summary.query.ImportsType;
022:
023: /**
024: * Add all imports associated with a particular method
025: *
026: *@author Chris Seguin
027: */
028: public class AddMethodTypeVisitor extends TraversalVisitor {
029: private boolean methodContents;
030:
031: /**
032: * Constructor for the AddMethodTypeVisitor object
033: */
034: public AddMethodTypeVisitor() {
035: methodContents = true;
036: }
037:
038: /**
039: * Constructor for the AddMethodTypeVisitor object
040: *
041: *@param traverseDependencies Description of Parameter
042: */
043: public AddMethodTypeVisitor(boolean traverseDependencies) {
044: methodContents = traverseDependencies;
045: }
046:
047: /**
048: * Visit a type declaration summary.
049: *
050: *@param node the summary that we are visiting
051: *@param data the data that was passed in
052: *@return the result
053: */
054: public Object visit(TypeDeclSummary node, Object data) {
055: if (node.isPrimitive()) {
056: return data;
057: }
058:
059: TypeSummary typeSummary = GetTypeSummary.query(node);
060: if (typeSummary != null) {
061: addTransform(data, new AddImportTransform(typeSummary));
062: }
063:
064: return data;
065: }
066:
067: /**
068: * Visit a method summary.
069: *
070: *@param node the summary that we are visiting
071: *@param data the data that was passed in
072: *@return the result
073: */
074: public Object visit(MethodSummary node, Object data) {
075: // First visit the return type
076: TypeDeclSummary decl = node.getReturnType();
077: if (decl != null) {
078: decl.accept(this , data);
079: }
080:
081: // Then visit the parameter types
082: Iterator iter = node.getParameters();
083: if (iter != null) {
084: while (iter.hasNext()) {
085: Summary next = (Summary) iter.next();
086: next.accept(this , data);
087: }
088: }
089:
090: // Third visit the exceptions
091: iter = node.getExceptions();
092: if (iter != null) {
093: while (iter.hasNext()) {
094: Summary next = (Summary) iter.next();
095: next.accept(this , data);
096: }
097: }
098:
099: // Finally visit the dependencies
100: if (methodContents) {
101: iter = node.getDependencies();
102: if (iter != null) {
103: while (iter.hasNext()) {
104: Summary next = (Summary) iter.next();
105: next.accept(this , data);
106: }
107: }
108: }
109:
110: return data;
111: }
112:
113: /**
114: * This transformation adds import statements. If the data object is a
115: * complex transform, it is added into the complex transform. If the data is
116: * a simple node, then the parse tree has been passed in and the transform
117: * should be applied directly to it.
118: *
119: *@param data the object we are updating
120: *@param transform the transformation
121: */
122: private void addTransform(Object data, AddImportTransform transform) {
123: if (data instanceof ComplexTransform) {
124: ComplexTransform complexTransform = (ComplexTransform) data;
125: complexTransform.add(transform);
126: } else if (data instanceof SimpleNode) {
127: SimpleNode root = (SimpleNode) data;
128: transform.update(root);
129: }
130: }
131: }
|