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.ASTClassBody;
013: import net.sourceforge.jrefactory.ast.ASTClassBodyDeclaration;
014: import net.sourceforge.jrefactory.ast.ASTInterfaceBody;
015: import net.sourceforge.jrefactory.ast.ASTInterfaceMemberDeclaration;
016: import net.sourceforge.jrefactory.ast.ASTNestedClassDeclaration;
017: import net.sourceforge.jrefactory.ast.ASTNestedInterfaceDeclaration;
018: import net.sourceforge.jrefactory.ast.ASTFormalParameter;
019: import net.sourceforge.jrefactory.ast.ASTType;
020: import net.sourceforge.jrefactory.ast.ASTPrimitiveType;
021: import net.sourceforge.jrefactory.ast.ASTName;
022: import net.sourceforge.jrefactory.ast.SimpleNode;
023: import net.sourceforge.jrefactory.parser.ChildrenVisitor;
024: import org.acm.seguin.summary.MethodSummary;
025: import org.acm.seguin.summary.ParameterSummary;
026:
027: /**
028: * Visitor that traverses an AST and removes a specified method
029: *
030: *@author Chris Seguin
031: */
032: public class RemoveMethodVisitor extends IdentifyMethodVisitor {
033: private SimpleNode methodDecl;
034:
035: /**
036: * Constructor for RemoveMethodVisitor that specifies the method to remove
037: *
038: *@param method the name of the method
039: */
040: public RemoveMethodVisitor(MethodSummary method) {
041: super (method);
042: methodDecl = null;
043: }
044:
045: /**
046: * Gets the MethodDeclaration attribute of the RemoveMethodVisitor object
047: *
048: *@return The MethodDeclaration value
049: */
050: public SimpleNode getMethodDeclaration() {
051: return methodDecl;
052: }
053:
054: /**
055: * Visit a class body
056: *
057: *@param node the class body node
058: *@param data the data for the visitor
059: *@return the method if it is found
060: */
061: public Object visit(ASTClassBody node, Object data) {
062: Object result = removeMethod(node);
063: if (result == null) {
064: result = super .visit(node, data);
065: }
066: return result;
067: }
068:
069: /**
070: * Visit an interface body
071: *
072: *@param node the interface body node
073: *@param data data for the visitor
074: *@return the method that was removed
075: */
076: public Object visit(ASTInterfaceBody node, Object data) {
077: Object result = removeMethod(node);
078: if (result == null) {
079: result = super .visit(node, data);
080: }
081: return result;
082: }
083:
084: /**
085: * Skip nested classes
086: *
087: *@param node the nested class
088: *@param data the data for the visitor
089: *@return the method if it is found
090: */
091: public Object visit(ASTNestedClassDeclaration node, Object data) {
092: return null;
093: }
094:
095: /**
096: * Skip nested interfaces
097: *
098: *@param node the nested interface
099: *@param data the data for the visitor
100: *@return the method if it is found
101: */
102: public Object visit(ASTNestedInterfaceDeclaration node, Object data) {
103: return null;
104: }
105:
106: /**
107: * Remove the method, if it is the correct one. Return the method
108: * declaration that was removed
109: *
110: *@param node The node we are considering removing the method from
111: *@return The method declaration
112: */
113: private Object removeMethod(SimpleNode node) {
114: int loop = node.jjtGetNumChildren();
115: for (int ndx = 0; ndx < loop; ndx++) {
116: SimpleNode next = (SimpleNode) node.jjtGetChild(ndx);
117: SimpleNode possible = (SimpleNode) next.jjtGetFirstChild();
118: if (isFound(possible)) {
119: removeSingle(node, next, ndx);
120: return next;
121: }
122: }
123: return null;
124: }
125:
126: /**
127: * Removes a method declaration where only a single variable was declared
128: *
129: *@param node the class or interface body node
130: *@param next the container for the method declaration
131: *@param ndx the index of the node to delete
132: */
133: private void removeSingle(SimpleNode node, SimpleNode next, int ndx) {
134: methodDecl = next;
135: node.jjtDeleteChild(ndx);
136: }
137: }
|