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 net.sourceforge.jrefactory.ast.Node;
012: import net.sourceforge.jrefactory.ast.ASTArguments;
013: import net.sourceforge.jrefactory.ast.ASTBlockStatement;
014: import net.sourceforge.jrefactory.ast.ASTLocalVariableDeclaration;
015: import net.sourceforge.jrefactory.ast.ASTMethodDeclaration;
016: import net.sourceforge.jrefactory.ast.ASTConstructorDeclaration;
017: import net.sourceforge.jrefactory.ast.ASTName;
018: import net.sourceforge.jrefactory.ast.ASTPrimaryExpression;
019: import net.sourceforge.jrefactory.ast.ASTPrimaryPrefix;
020: import net.sourceforge.jrefactory.ast.ASTPrimarySuffix;
021: import net.sourceforge.jrefactory.ast.ASTUnmodifiedClassDeclaration;
022: import net.sourceforge.jrefactory.ast.ASTUnmodifiedInterfaceDeclaration;
023: import net.sourceforge.jrefactory.ast.ASTVariableDeclaratorId;
024: import net.sourceforge.jrefactory.ast.SimpleNode;
025: import org.acm.seguin.summary.MethodSummary;
026:
027: /**
028: * Visitor that seeks out and update the tree
029: *
030: *@author Chris Seguin
031: */
032: class RenameParameterVisitor extends IdentifyMethodVisitor {
033: /**
034: * Constructor for the RenameParameterVisitor object
035: *
036: *@param init Description of Parameter
037: */
038: public RenameParameterVisitor(MethodSummary init) {
039: super (init);
040: }
041:
042: /**
043: * Search and find the correct method
044: *
045: *@param node Description of Parameter
046: *@param data Description of Parameter
047: *@return Description of the Returned Value
048: */
049: public Object visit(ASTMethodDeclaration node, Object data) {
050: RenameParameterTransform rpt = (RenameParameterTransform) data;
051: if (isFound(node)) {
052: rpt.setRightTree(true);
053:
054: // Update the javadoc
055:
056: // Continue traversal
057: super .visit(node, data);
058:
059: rpt.setRightTree(false);
060: return null;
061: }
062:
063: return super .visit(node, data);
064: }
065:
066: /**
067: * Search and find the correct method
068: *
069: *@param node Description of Parameter
070: *@param data Description of Parameter
071: *@return Description of the Returned Value
072: */
073: public Object visit(ASTConstructorDeclaration node, Object data) {
074: RenameParameterTransform rpt = (RenameParameterTransform) data;
075: if (isFound(node)) {
076: rpt.setRightTree(true);
077:
078: // Update the javadoc
079:
080: // Continue traversal
081: super .visit(node, data);
082:
083: rpt.setRightTree(false);
084: return null;
085: }
086:
087: return super .visit(node, data);
088: }
089:
090: /**
091: * Updates the parameter name
092: *
093: *@param node Description of Parameter
094: *@param data Description of Parameter
095: *@return Description of the Returned Value
096: */
097: public Object visit(ASTVariableDeclaratorId node, Object data) {
098: RenameParameterTransform rpt = (RenameParameterTransform) data;
099: if (rpt.isRightTree()) {
100: if (node.getName().equals(rpt.getParameter().getName())) {
101: node.setName(rpt.getNewName());
102: }
103: }
104:
105: return null;
106: }
107:
108: /**
109: * Visits a block node. Stops traversing the tree if we come to a new class.
110: *
111: *@param node Description of Parameter
112: *@param data Description of Parameter
113: *@return Description of the Returned Value
114: */
115: public Object visit(ASTBlockStatement node, Object data) {
116: RenameParameterTransform rpt = (RenameParameterTransform) data;
117: if (rpt.isRightTree()) {
118: Node child = node.jjtGetFirstChild();
119: if ((child instanceof ASTUnmodifiedClassDeclaration)
120: || (child instanceof ASTUnmodifiedInterfaceDeclaration)) {
121: return null;
122: }
123: }
124:
125: return super .visit(node, data);
126: }
127:
128: /**
129: * Updates where that parameter is used
130: *
131: *@param node Description of Parameter
132: *@param data Description of Parameter
133: *@return Description of the Returned Value
134: */
135: public Object visit(ASTPrimaryExpression node, Object data) {
136: RenameParameterTransform rpt = (RenameParameterTransform) data;
137: if (rpt.isRightTree()) {
138: ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) node
139: .jjtGetFirstChild();
140: ASTPrimarySuffix suffix = null;
141: if (node.jjtGetNumChildren() > 1) {
142: suffix = (ASTPrimarySuffix) node.jjtGetChild(1);
143: }
144:
145: if ((prefix.jjtGetNumChildren() > 0)
146: && (prefix.jjtGetFirstChild() instanceof ASTName)
147: && ((suffix == null)
148: || (suffix.jjtGetNumChildren() == 0) || !isMethodCall(
149: prefix, suffix))) {
150: ASTName name = (ASTName) prefix.jjtGetFirstChild();
151: if (name.getNamePart(0).equals(
152: rpt.getParameter().getName())) {
153: name.setNamePart(0, rpt.getNewName());
154: }
155: }
156: }
157:
158: return super .visit(node, data);
159: }
160:
161: private boolean isMethodCall(ASTPrimaryPrefix prefix,
162: ASTPrimarySuffix suffix) {
163: if (!(suffix.jjtGetFirstChild() instanceof ASTArguments))
164: return false;
165:
166: ASTName name = (ASTName) prefix.jjtGetFirstChild();
167: return (name.getNameSize() == 1);
168: }
169: }
|