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 org.acm.seguin.refactor.ComplexTransform;
012: import org.acm.seguin.summary.FieldSummary;
013: import org.acm.seguin.summary.PackageSummary;
014: import org.acm.seguin.summary.Summary;
015: import org.acm.seguin.summary.TypeSummary;
016: import org.acm.seguin.summary.query.Ancestor;
017:
018: /**
019: * Object used to store the state of the rename field visitor
020: *
021: * @author Chris Seguin
022: * @since 2.4.0
023: */
024: class RenameFieldData {
025: private String oldName;
026: private String newName;
027: private boolean this Required;
028: private Summary current;
029: private boolean mustInsertThis;
030: private TypeSummary typeSummary;
031: private boolean canBeFirst;
032: private boolean canBeThis;
033: private FieldSummary oldField;
034: private ComplexTransform transform;
035: private String fullName;
036: private String importedName;
037:
038: /**
039: * Constructor for the RenameFieldData object
040: *
041: * @param oldField Description of Parameter
042: * @param newName the new field name
043: * @since 2.4.0
044: */
045: public RenameFieldData(FieldSummary oldField, String newName) {
046: this .oldName = oldField.getName();
047: this .newName = newName;
048: this .oldField = oldField;
049: this Required = false;
050: canBeFirst = false;
051: canBeThis = false;
052: mustInsertThis = false;
053: current = null;
054: typeSummary = (TypeSummary) oldField.getParent();
055: initNames(oldField);
056: }
057:
058: /**
059: * Constructor for the RenameFieldData object
060: *
061: * @param oldField Description of Parameter
062: * @param newName the new field name
063: * @param transform Description of Parameter
064: * @since 2.4.0
065: */
066: public RenameFieldData(FieldSummary oldField, String newName,
067: ComplexTransform transform) {
068: this .newName = newName;
069: this .oldField = oldField;
070: this .transform = transform;
071: oldName = oldField.getName();
072: typeSummary = (TypeSummary) oldField.getParent();
073: }
074:
075: /**
076: * Sets the ThisRequired attribute of the RenameFieldData object
077: *
078: * @param way The new ThisRequired value
079: * @since 2.4.0
080: */
081: public void setThisRequired(boolean way) {
082: this Required = way;
083: }
084:
085: /**
086: * Sets the CurrentSummary attribute of the RenameFieldData object
087: *
088: * @param value The new CurrentSummary value
089: * @since 2.4.0
090: */
091: public void setCurrentSummary(Summary value) {
092: current = value;
093: if (current instanceof TypeSummary) {
094: check((TypeSummary) current);
095: }
096: }
097:
098: /**
099: * Sets the MustInsertThis attribute of the RenameFieldData object
100: *
101: * @param value The new MustInsertThis value
102: * @since 2.4.0
103: */
104: public void setMustInsertThis(boolean value) {
105: mustInsertThis = value;
106: }
107:
108: /**
109: * Gets the OldName attribute of the RenameFieldData object
110: *
111: * @return The OldName value
112: * @since 2.4.0
113: */
114: public String getOldName() {
115: return oldName;
116: }
117:
118: /**
119: * Gets the NewName attribute of the RenameFieldData object
120: *
121: * @return The NewName value
122: * @since 2.4.0
123: */
124: public String getNewName() {
125: return newName;
126: }
127:
128: /**
129: * Gets the ThisRequired attribute of the RenameFieldData object
130: *
131: * @return The ThisRequired value
132: * @since 2.4.0
133: */
134: public boolean isThisRequired() {
135: return this Required;
136: }
137:
138: /**
139: * Gets the CurrentSummary attribute of the RenameFieldData object
140: *
141: * @return The CurrentSummary value
142: * @since 2.4.0
143: */
144: public Summary getCurrentSummary() {
145: return current;
146: }
147:
148: /**
149: * Gets the MustInsertThis attribute of the RenameFieldData object
150: *
151: * @return The MustInsertThis value
152: * @since 2.4.0
153: */
154: public boolean isMustInsertThis() {
155: return mustInsertThis;
156: }
157:
158: /**
159: * Returns the type summary where the field is changing
160: *
161: * @return The TypeSummary value
162: * @since 2.4.0
163: */
164: public TypeSummary getTypeSummary() {
165: return typeSummary;
166: }
167:
168: /**
169: * Gets the AllowedToChangeFirst attribute of the RenameFieldData object
170: *
171: * @return The AllowedToChangeFirst value
172: * @since 2.4.0
173: */
174: public boolean isAllowedToChangeFirst() {
175: return canBeFirst;
176: }
177:
178: /**
179: * Gets the AllowedToChangeThis attribute of the RenameFieldData object
180: *
181: * @return The AllowedToChangeThis value
182: * @since 2.4.0
183: */
184: public boolean isAllowedToChangeThis() {
185: return canBeThis;
186: }
187:
188: /**
189: * Gets the OldField attribute of the RenameFieldData object
190: *
191: * @return The OldField value
192: * @since 2.4.0
193: */
194: public FieldSummary getOldField() {
195: return oldField;
196: }
197:
198: /**
199: * Gets the ComplexTransform attribute of the RenameFieldData object
200: *
201: * @return The ComplexTransform value
202: * @since 2.4.0
203: */
204: public ComplexTransform getComplexTransform() {
205: return transform;
206: }
207:
208: /**
209: * Gets the FullName attribute of the RenameFieldData object
210: *
211: * @return The FullName value
212: * @since 2.4.0
213: */
214: public String getFullName() {
215: return fullName;
216: }
217:
218: /**
219: * Gets the ImportedName attribute of the RenameFieldData object
220: *
221: * @return The ImportedName value
222: * @since 2.4.0
223: */
224: public String getImportedName() {
225: return importedName;
226: }
227:
228: /**
229: * Converts to a String representation of the object.
230: *
231: * @return A string representation of the object.
232: * @since 2.4.0
233: */
234: public String toString() {
235: StringBuffer sb = new StringBuffer();
236: sb.append(" oldName=").append(oldName);
237: sb.append("\n newName=").append(newName);
238: sb.append("\n thisRequired=").append(this Required);
239: sb.append("\n current=").append(current);
240: sb.append("\n mustInsertThis=").append(mustInsertThis);
241: sb.append("\n typeSummary=").append(typeSummary);
242: sb.append("\n canBeFirst=").append(canBeFirst);
243: sb.append("\n canBeThis=").append(canBeThis);
244: sb.append("\n oldField=").append(oldField);
245: sb.append("\n transform=").append(transform);
246: sb.append("\n fullName=").append(fullName);
247: sb.append("\n importedName=").append(importedName)
248: .append("\n");
249: return sb.toString();
250: }
251:
252: /**
253: * Returns true if the system can change the first name in an array
254: *
255: * @param current the type summary in question
256: * @since 2.4.0
257: */
258: private void check(TypeSummary current) {
259: if ((current == typeSummary)
260: || Ancestor.query(current, typeSummary)) {
261: canBeFirst = true;
262: canBeThis = true;
263: return;
264: }
265:
266: Summary cs = current;
267: while (cs != null) {
268: if (cs == typeSummary) {
269: canBeThis = false;
270: canBeFirst = true;
271: return;
272: }
273:
274: cs = cs.getParent();
275: }
276:
277: canBeThis = false;
278: canBeFirst = false;
279: }
280:
281: /**
282: * Initialize the names
283: *
284: * @param field the field summary
285: * @since 2.4.0
286: */
287: private void initNames(FieldSummary field) {
288: StringBuffer buffer = new StringBuffer(field.getName());
289:
290: Summary current = field;
291: while (current != null) {
292: if (current instanceof TypeSummary) {
293: buffer.insert(0, ".");
294: buffer.insert(0, current.getName());
295: }
296:
297: if (current instanceof PackageSummary) {
298: importedName = buffer.toString();
299: buffer.insert(0, ".");
300: buffer.insert(0, current.getName());
301: fullName = buffer.toString();
302: return;
303: }
304:
305: current = current.getParent();
306: }
307:
308: // We should never get here
309: importedName = buffer.toString();
310: fullName = buffer.toString();
311: }
312:
313: }
|