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.field;
010:
011: import java.util.Iterator;
012: import org.acm.seguin.refactor.ComplexTransform;
013: import org.acm.seguin.refactor.RefactoringException;
014: import org.acm.seguin.summary.FieldSummary;
015: import org.acm.seguin.summary.FileSummary;
016: import org.acm.seguin.summary.Summary;
017: import org.acm.seguin.summary.PackageSummary;
018:
019: /**
020: * Renames a field
021: *
022: *@author Chris Seguin
023: */
024: public class RenameFieldRefactoring extends FieldRefactoring {
025: private String newName;
026: private FieldSummary oldField;
027:
028: /**
029: * Constructor for the RenameFieldRefactoring object
030: */
031: public RenameFieldRefactoring() {
032: super ();
033: }
034:
035: /**
036: * Sets the NewName attribute of the RenameFieldRefactoring object
037: *
038: *@param value The new NewName value
039: */
040: public void setNewName(String value) {
041: newName = value;
042: }
043:
044: /**
045: * Gets the Description attribute of the RenameFieldRefactoring object
046: *
047: *@return The Description value
048: */
049: public String getDescription() {
050: return "Renames the field " + field + " to " + newName;
051: }
052:
053: /**
054: * Gets the ID attribute of the RenameFieldRefactoring object
055: *
056: *@return The ID value
057: */
058: public int getID() {
059: return RENAME_FIELD;
060: }
061:
062: /**
063: * Check that thsi refactoring can be performed
064: *
065: *@exception RefactoringException Description of Exception
066: */
067: protected void preconditions() throws RefactoringException {
068: Iterator iter = typeSummary.getFields();
069: if (iter == null) {
070: throw new RefactoringException(
071: typeSummary.getName()
072: + " has no fields associated with it, so it cannot be renamed");
073: }
074:
075: boolean found = false;
076: while (iter.hasNext()) {
077: FieldSummary next = (FieldSummary) iter.next();
078: if (next.getName().equals(field)) {
079: found = true;
080: oldField = next;
081: }
082: if (next.getName().equals(newName)) {
083: throw new RefactoringException("A field named "
084: + newName + " already exists in class "
085: + typeSummary.getName());
086: }
087: }
088:
089: if (!found) {
090: throw new RefactoringException("No field named " + field
091: + " is contained in " + typeSummary.getName());
092: }
093: }
094:
095: /**
096: * Applies the transformation to the system to rename the method
097: */
098: protected void transform() {
099: if (oldField.getName().equals(newName)) {
100: return;
101: }
102:
103: FileSummary fileSummary = (FileSummary) getFileSummary(typeSummary);
104: RenameFieldTransform rft = new RenameFieldTransform(oldField,
105: newName);
106: ComplexTransform transform = getComplexTransform();
107: transform.add(rft);
108: transform.apply(fileSummary.getFile(), fileSummary.getFile());
109:
110: if (oldField.isPrivate()) {
111: // We are done
112: } else if (oldField.isPackage()) {
113: RenameSystemTraversal rsv = new RenameSystemTraversal();
114: rsv.visit(getPackage(), new RenameFieldData(oldField,
115: newName, transform));
116: } else {
117: RenameSystemTraversal rsv = new RenameSystemTraversal();
118: rsv
119: .visit(new RenameFieldData(oldField, newName,
120: transform));
121: }
122: }
123:
124: /**
125: * Gets the Package attribute of the RenameFieldRefactoring object
126: *
127: *@return The Package value
128: */
129: private PackageSummary getPackage() {
130: Summary current = oldField;
131: while (!(current instanceof PackageSummary)) {
132: current = current.getParent();
133: }
134:
135: return (PackageSummary) current;
136: }
137: }
|