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 org.acm.seguin.awt.Question;
013: import org.acm.seguin.summary.TraversalVisitor;
014: import org.acm.seguin.summary.MethodSummary;
015: import org.acm.seguin.summary.FileSummary;
016: import org.acm.seguin.summary.TypeSummary;
017: import org.acm.seguin.summary.TypeDeclSummary;
018: import org.acm.seguin.refactor.ComplexTransform;
019: import org.acm.seguin.summary.query.Ancestor;
020: import org.acm.seguin.summary.query.GetTypeSummary;
021:
022: /**
023: * Removes the method from all subclasses of a particular class.
024: *
025: *@author Chris Seguin
026: */
027: public class RemoveMethodFromSubclassVisitor extends TraversalVisitor {
028: private MethodSummary target;
029: private TypeSummary ancestor;
030: private ComplexTransform complex;
031: private TypeSummary notHere;
032:
033: /**
034: * Constructor for the RemoveMethodFromSubclassVisitor object
035: *
036: *@param type the ancestor type
037: *@param init the method
038: *@param notThisOne a type to skip
039: */
040: public RemoveMethodFromSubclassVisitor(TypeSummary type,
041: MethodSummary init, TypeSummary notThisOne,
042: ComplexTransform initComplex) {
043: target = init;
044: ancestor = type;
045: notHere = notThisOne;
046: complex = initComplex;
047: }
048:
049: /**
050: * Visits a file summary node and updates it if necessary
051: *
052: *@param fileSummary Description of Parameter
053: *@param data Description of Parameter
054: *@return Description of the Returned Value
055: */
056: public Object visit(FileSummary fileSummary, Object data) {
057: complex.clear();
058: super .visit(fileSummary, data);
059: if (complex.hasAnyChanges()) {
060: String title = "Removing " + target.getName()
061: + " from children of " + ancestor.getName();
062: String question = "Would you like to remove\n"
063: + target.toString() + "\nfrom "
064: + fileSummary.getName();
065: if (Question.isYes(title, question)) {
066: complex.apply(fileSummary.getFile(), fileSummary
067: .getFile());
068: }
069: }
070: return data;
071: }
072:
073: /**
074: * Visits a type summary and updates it
075: *
076: *@param typeSummary Description of Parameter
077: *@param data Description of Parameter
078: *@return Description of the Returned Value
079: */
080: public Object visit(TypeSummary typeSummary, Object data) {
081: if ((typeSummary != notHere)
082: && Ancestor.query(typeSummary, ancestor)) {
083: Iterator iter = typeSummary.getMethods();
084: if (iter != null) {
085: while (iter.hasNext()) {
086: visit((MethodSummary) iter.next(), data);
087: }
088: }
089: }
090: return data;
091: }
092:
093: /**
094: * Visits the method summary and determines if it should be removed.
095: *
096: *@param methodSummary Description of Parameter
097: *@param data Description of Parameter
098: *@return Description of the Returned Value
099: */
100: public Object visit(MethodSummary methodSummary, Object data) {
101: if (methodSummary.equals(target)) {
102: complex.add(new RemoveMethodTransform(target));
103: }
104:
105: return data;
106: }
107: }
|