001: package org.acm.seguin.refactor.type;
002:
003: import java.io.File;
004: import org.acm.seguin.summary.Summary;
005: import org.acm.seguin.summary.PackageSummary;
006: import org.acm.seguin.summary.FileSummary;
007: import org.acm.seguin.summary.TypeSummary;
008: import org.acm.seguin.summary.query.GetTypeSummary;
009: import net.sourceforge.jrefactory.ast.ASTName;
010: import org.acm.seguin.refactor.AddImportTransform;
011: import org.acm.seguin.refactor.ComplexTransform;
012:
013: /**
014: * The visitor object for removing a class from the system.
015: *
016: *@author Chris Seguin
017: */
018: public class RemoveClassVisitor extends RenameClassVisitor {
019: private String parentPackage;
020: private String oldPackage;
021:
022: /**
023: * Constructor for the remove class visitor object
024: *
025: *@param packageName the package name
026: *@param oldClass the name of the class being deleted
027: *@param newClass the parent class of that being deleted
028: *@param base the base directory
029: *@param initParentPackage Description of Parameter
030: *@param complex Description of Parameter
031: */
032: public RemoveClassVisitor(String packageName, String oldClass,
033: String initParentPackage, String newClass, File base,
034: ComplexTransform complex) {
035: super (packageName, oldClass, newClass, base, complex);
036:
037: parentPackage = initParentPackage;
038: oldPackage = packageName;
039: }
040:
041: /**
042: * Gets the New Imports transform
043: *
044: *@param node the file summary
045: *@param className the name of the class that is changing
046: *@return The NewImports value
047: */
048: protected AddImportTransform getNewImports(FileSummary node,
049: String className) {
050: if (newClassName.equals("Object")) {
051: return null;
052: } else if (GetTypeSummary.query(node, newClassName) == null) {
053: return new AddImportTransform(parentPackage, newClassName);
054: } else {
055: return null;
056: }
057: }
058:
059: /**
060: * Gets the new name
061: *
062: *@return an ASTName containing the new name
063: */
064: protected ASTName getNewName() {
065: if (newClassName.equals("Object")) {
066: ASTName result = new ASTName();
067: result.addNamePart(newClassName);
068: return result;
069: }
070: if (oldPackage.equals(parentPackage)) {
071: return super .getNewName();
072: }
073:
074: ASTName result = new ASTName();
075: result.fromString(parentPackage);
076: result.addNamePart(newClassName);
077: return result;
078: }
079:
080: /**
081: * We are performing the transformation on a refactoring that already has
082: * that type imported from another class
083: *
084: *@param refactoring the complex transformation
085: *@param oldOne the old class name
086: *@param node the file that is being changed
087: *@param importedType the type that we are supposedly importing
088: */
089: protected void alreadyImportsType(ComplexTransform refactoring,
090: ASTName oldOne, FileSummary node, TypeSummary importedType) {
091: if (isSamePackage(node, importedType) || isParent(importedType)) {
092: ASTName newOne = new ASTName();
093: newOne.addNamePart(newClassName);
094:
095: refactoring.add(new RenameTypeTransform(oldOne, newOne,
096: null));
097: } else {
098: refactoring.add(new RenameTypeTransform(oldOne,
099: getNewName(), null));
100: }
101: }
102:
103: /**
104: * Gets the SamePackage attribute of the RemoveClassVisitor object
105: *
106: *@param fileSummary Description of Parameter
107: *@param typeSummary Description of Parameter
108: *@return The SamePackage value
109: */
110: private boolean isSamePackage(FileSummary fileSummary,
111: TypeSummary typeSummary) {
112: Summary one = fileSummary;
113: Summary two = typeSummary;
114:
115: while (!(one instanceof PackageSummary)) {
116: one = one.getParent();
117: }
118:
119: while (!(two instanceof PackageSummary)) {
120: two = two.getParent();
121: }
122:
123: return one.equals(two);
124: }
125:
126: /**
127: * Gets the Parent attribute of the RemoveClassVisitor object
128: *
129: *@param typeSummary Description of Parameter
130: *@return The Parent value
131: */
132: private boolean isParent(TypeSummary typeSummary) {
133: Summary one = typeSummary;
134:
135: while (!(one instanceof PackageSummary)) {
136: one = one.getParent();
137: }
138:
139: PackageSummary packageSummary = (PackageSummary) one;
140: String packageName = packageSummary.getName();
141:
142: return packageName.equals(parentPackage);
143: }
144: }
|