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 org.acm.seguin.refactor.ComplexTransform;
012: import org.acm.seguin.refactor.Refactoring;
013: import org.acm.seguin.refactor.RefactoringException;
014: import org.acm.seguin.summary.FileSummary;
015: import org.acm.seguin.summary.MethodSummary;
016: import org.acm.seguin.summary.ParameterSummary;
017: import org.acm.seguin.summary.Summary;
018:
019: /**
020: * Refactoring to allow a user to rename a parameter
021: *
022: *@author Chris Seguin
023: */
024: public class RenameParameterRefactoring extends Refactoring {
025: private String newName;
026: private ParameterSummary param;
027: private MethodSummary method;
028:
029: /**
030: * Constructor for the RenameParameterRefactoring object
031: */
032: protected RenameParameterRefactoring() {
033: newName = null;
034: param = null;
035: method = null;
036: }
037:
038: /**
039: * Sets the NewName attribute of the RenameParameterRefactoring object
040: *
041: *@param value The new NewName value
042: */
043: public void setNewName(String value) {
044: newName = value;
045: }
046:
047: /**
048: * Sets the ParameterSummary attribute of the RenameParameterRefactoring
049: * object
050: *
051: *@param value The new ParameterSummary value
052: */
053: public void setParameterSummary(ParameterSummary value) {
054: param = value;
055: }
056:
057: /**
058: * Sets the MethodSummary attribute of the RenameParameterRefactoring object
059: *
060: *@param value The new MethodSummary value
061: */
062: public void setMethodSummary(MethodSummary value) {
063: method = value;
064: }
065:
066: /**
067: * Gets the Description attribute of the RenameParameterRefactoring object
068: *
069: *@return The Description value
070: */
071: public String getDescription() {
072: return "Renaming " + param.getName() + " to " + newName
073: + " in " + method.toString();
074: }
075:
076: /**
077: * Gets the ID attribute of the RenameParameterRefactoring object
078: *
079: *@return The ID value
080: */
081: public int getID() {
082: return RENAME_PARAMETER;
083: }
084:
085: /**
086: * Description of the Method
087: *
088: *@exception RefactoringException Description of Exception
089: */
090: protected void preconditions() throws RefactoringException {
091: if ((newName == null) || (newName.length() == 0)) {
092: throw new RefactoringException("No new name specified");
093: }
094:
095: if (param == null) {
096: throw new RefactoringException("No parameter specified");
097: }
098:
099: if (method == null) {
100: throw new RefactoringException("No method specified");
101: }
102: }
103:
104: /**
105: * Perform the transformation
106: */
107: protected void transform() {
108: if (param.getName().equals(newName))
109: return;
110:
111: // Get the complex transformation
112: ComplexTransform transform = getComplexTransform();
113:
114: // Add the parameter rename transformation
115: RenameParameterTransform rpt = new RenameParameterTransform();
116: rpt.setMethod(method);
117: rpt.setParameter(param);
118: rpt.setNewName(newName);
119: transform.add(rpt);
120:
121: // Apply the refactoring
122: Summary current = method;
123: while (!(current instanceof FileSummary)) {
124: current = current.getParent();
125: }
126: FileSummary fileSummary = (FileSummary) current;
127: transform.apply(fileSummary.getFile(), fileSummary.getFile());
128: }
129: }
|