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.type;
010:
011: import java.io.File;
012: import java.io.IOException;
013: import java.util.Iterator;
014: import java.util.LinkedList;
015: import net.sourceforge.jrefactory.ast.ASTName;
016: import net.sourceforge.jrefactory.ast.SimpleNode;
017: import net.sourceforge.jrefactory.query.PackageNameGetter;
018: import org.acm.seguin.refactor.ComplexTransform;
019: import org.acm.seguin.refactor.EliminatePackageImportVisitor;
020: import org.acm.seguin.refactor.Refactoring;
021: import org.acm.seguin.refactor.RefactoringException;
022: import org.acm.seguin.summary.*;
023: import org.acm.seguin.summary.query.GetTypeSummary;
024: import org.acm.seguin.summary.query.PackageContainsClass;
025: import org.acm.seguin.summary.query.TopLevelDirectory;
026:
027: /**
028: * Main program for renaming a class.
029: *
030: *@author Chris Seguin
031: */
032: public class RenameClassRefactoring extends Refactoring {
033: // Instance Variables
034: private String initDir;
035:
036: /**
037: * The root directory
038: */
039: protected File base;
040: /**
041: * The name of the class
042: */
043: protected String oldClassName;
044: /**
045: * The new name of the class
046: */
047: protected String newClassName;
048: /**
049: * The source package
050: */
051: protected String srcPackage;
052:
053: /**
054: * Constructor for repackage
055: */
056: protected RenameClassRefactoring() {
057: initDir = System.getProperty("user.dir");
058: }
059:
060: /**
061: * Set the directory
062: *
063: *@param dir the initial directory
064: */
065: public void setDirectory(String dir) {
066: initDir = dir;
067: }
068:
069: /**
070: * Sets the OldClassName attribute of the RenameClass object
071: *
072: *@param value The new OldClassName value
073: */
074: public void setOldClassName(String value) {
075: oldClassName = value;
076: }
077:
078: /**
079: * Sets the NewClassName attribute of the RenameClass object
080: *
081: *@param value The new NewClassName value
082: */
083: public void setNewClassName(String value) {
084: newClassName = value;
085: }
086:
087: /**
088: * Gets the description of the refactoring
089: *
090: *@return the description
091: */
092: public String getDescription() {
093: return "Renames a class from " + oldClassName + " to "
094: + newClassName;
095: }
096:
097: /**
098: * Gets the id for this refactoring to track which refactorings are used.
099: *
100: *@return the id
101: */
102: public int getID() {
103: return RENAME_CLASS;
104: }
105:
106: /**
107: * Gets the file summary that we are changing
108: *
109: *@return The FileSummary value
110: */
111: protected FileSummary getFileSummary() {
112: PackageSummary packageSummary = PackageSummary
113: .getPackageSummary(srcPackage);
114: TypeSummary typeSummary = GetTypeSummary.query(packageSummary,
115: oldClassName);
116: return (FileSummary) typeSummary.getParent();
117: }
118:
119: /**
120: * Preconditions for the refactoring to be applied
121: *
122: *@exception RefactoringException Description of Exception
123: */
124: protected void preconditions() throws RefactoringException {
125: if (oldClassName == null) {
126: throw new RefactoringException("No old class specified");
127: }
128:
129: if (newClassName == null) {
130: throw new RefactoringException("No new class specified");
131: }
132:
133: File startDir = new File(initDir);
134: String firstFilename = oldClassName + ".java";
135: ASTName srcPackageName = PackageNameGetter.query(startDir,
136: firstFilename);
137: srcPackage = "";
138: if (srcPackageName != null) {
139: srcPackage = srcPackageName.getName();
140: }
141:
142: base = TopLevelDirectory.query(startDir, firstFilename);
143:
144: String topLevelDir = base.getPath();
145: try {
146: topLevelDir = base.getCanonicalPath();
147: } catch (IOException ioe) {
148: }
149: (new SummaryTraversal(topLevelDir)).run();
150:
151: if (PackageContainsClass.query(srcPackage, newClassName)) {
152: throw new RefactoringException(srcPackage
153: + " already contains a class named " + newClassName);
154: }
155: }
156:
157: /**
158: * The transformation of all the source files
159: */
160: protected void transform() {
161: System.out.println("Renaming " + oldClassName + " to "
162: + newClassName);
163:
164: ComplexTransform complex = getComplexTransform();
165:
166: EliminatePackageImportVisitor epiv = new EliminatePackageImportVisitor(
167: complex);
168: epiv.setPackageSummary(PackageSummary
169: .getPackageSummary(srcPackage));
170: epiv.addFilterClass(oldClassName);
171: epiv.visit(null);
172:
173: RenameClassVisitor rcv = new RenameClassVisitor(srcPackage,
174: oldClassName, newClassName, base, complex);
175: rcv.visit(null);
176: }
177: }
|