KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > refactoring > descriptors > RenameJavaElementDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.core.refactoring.descriptors;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.ltk.core.refactoring.RefactoringContribution;
16 import org.eclipse.ltk.core.refactoring.RefactoringCore;
17 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
18
19 import org.eclipse.jdt.core.IField;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IMethod;
22 import org.eclipse.jdt.core.IPackageFragment;
23 import org.eclipse.jdt.core.IPackageFragmentRoot;
24 import org.eclipse.jdt.core.IType;
25 import org.eclipse.jdt.core.ITypeParameter;
26 import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
27
28 import org.eclipse.jdt.internal.core.refactoring.descriptors.DescriptorMessages;
29
30 /**
31  * Refactoring descriptor for the rename java element refactoring.
32  * <p>
33  * An instance of this refactoring descriptor may be obtained by calling
34  * {@link RefactoringContribution#createDescriptor()} on a refactoring
35  * contribution requested by invoking
36  * {@link RefactoringCore#getRefactoringContribution(String)} with the
37  * appropriate refactoring id.
38  * </p>
39  * <p>
40  * Note: this class is not intended to be instantiated by clients.
41  * </p>
42  *
43  * @since 3.3
44  */

45 public final class RenameJavaElementDescriptor extends JavaRefactoringDescriptor {
46
47     /** The delegate attribute */
48     private static final String JavaDoc ATTRIBUTE_DELEGATE= "delegate"; //$NON-NLS-1$
49

50     /** The deprecate attribute */
51     private static final String JavaDoc ATTRIBUTE_DEPRECATE= "deprecate"; //$NON-NLS-1$
52

53     /** The hierarchical attribute */
54     private static final String JavaDoc ATTRIBUTE_HIERARCHICAL= "hierarchical"; //$NON-NLS-1$
55

56     /** The match strategy attribute */
57     private static final String JavaDoc ATTRIBUTE_MATCH_STRATEGY= "matchStrategy"; //$NON-NLS-1$
58

59     /** The parameter attribute */
60     private static final String JavaDoc ATTRIBUTE_PARAMETER= "parameter"; //$NON-NLS-1$
61

62     /** The patterns attribute */
63     private static final String JavaDoc ATTRIBUTE_PATTERNS= "patterns"; //$NON-NLS-1$
64

65     /** The qualified attribute */
66     private static final String JavaDoc ATTRIBUTE_QUALIFIED= "qualified"; //$NON-NLS-1$
67

68     /** The rename getter attribute */
69     private static final String JavaDoc ATTRIBUTE_RENAME_GETTER= "getter"; //$NON-NLS-1$
70

71     /** The rename setter attribute */
72     private static final String JavaDoc ATTRIBUTE_RENAME_SETTER= "setter"; //$NON-NLS-1$
73

74     /** The similar declarations attribute */
75     private static final String JavaDoc ATTRIBUTE_SIMILAR_DECLARATIONS= "similarDeclarations"; //$NON-NLS-1$
76

77     /** The textual matches attribute */
78     private static final String JavaDoc ATTRIBUTE_TEXTUAL_MATCHES= "textual"; //$NON-NLS-1$
79

80     /**
81      * Similar declaration updating strategy which finds exact names and
82      * embedded names as well (value: <code>2</code>).
83      */

84     public static final int STRATEGY_EMBEDDED= 2;
85
86     /**
87      * Similar declaration updating strategy which finds exact names only
88      * (value: <code>1</code>).
89      */

90     public static final int STRATEGY_EXACT= 1;
91
92     /**
93      * Similar declaration updating strategy which finds exact names, embedded
94      * names and name suffixes (value: <code>3</code>).
95      */

96     public static final int STRATEGY_SUFFIX= 3;
97
98     /** The delegate attribute */
99     private boolean fDelegate= false;
100
101     /** The deprecate attribute */
102     private boolean fDeprecate= false;
103
104     /** The hierarchical attribute */
105     private boolean fHierarchical= false;
106
107     /** The java element attribute */
108     private IJavaElement fJavaElement= null;
109
110     /** The match strategy */
111     private int fMatchStrategy= STRATEGY_EXACT;
112
113     /** The name attribute */
114     private String JavaDoc fName= null;
115
116     /** The patterns attribute */
117     private String JavaDoc fPatterns= null;
118
119     /** The qualified attribute */
120     private boolean fQualified= false;
121
122     /** The references attribute */
123     private boolean fReferences= false;
124
125     /** The rename getter attribute */
126     private boolean fRenameGetter= false;
127
128     /** The rename setter attribute */
129     private boolean fRenameSetter= false;
130
131     /** The similar declarations attribute */
132     private boolean fSimilarDeclarations= false;
133
134     /** The textual attribute */
135     private boolean fTextual= false;
136
137     /**
138      * Creates a new refactoring descriptor.
139      *
140      * @param id
141      * the unique id of the rename refactoring
142      * @see IJavaRefactorings
143      */

144     public RenameJavaElementDescriptor(final String JavaDoc id) {
145         super(id);
146         Assert.isLegal(checkId(id), "Refactoring id is not a rename refactoring id"); //$NON-NLS-1$
147
}
148
149     /**
150      * Checks whether the refactoring id is valid.
151      *
152      * @param id
153      * the refactoring id
154      * @return the outcome of the validation
155      */

156     private boolean checkId(final String JavaDoc id) {
157         Assert.isNotNull(id);
158         if (id.equals(IJavaRefactorings.RENAME_COMPILATION_UNIT))
159             return true;
160         else if (id.equals(IJavaRefactorings.RENAME_ENUM_CONSTANT))
161             return true;
162         else if (id.equals(IJavaRefactorings.RENAME_FIELD))
163             return true;
164         else if (id.equals(IJavaRefactorings.RENAME_JAVA_PROJECT))
165             return true;
166         else if (id.equals(IJavaRefactorings.RENAME_LOCAL_VARIABLE))
167             return true;
168         else if (id.equals(IJavaRefactorings.RENAME_METHOD))
169             return true;
170         else if (id.equals(IJavaRefactorings.RENAME_PACKAGE))
171             return true;
172         else if (id.equals(IJavaRefactorings.RENAME_RESOURCE))
173             return true;
174         else if (id.equals(IJavaRefactorings.RENAME_SOURCE_FOLDER))
175             return true;
176         else if (id.equals(IJavaRefactorings.RENAME_TYPE))
177             return true;
178         else if (id.equals(IJavaRefactorings.RENAME_TYPE_PARAMETER))
179             return true;
180         return false;
181     }
182
183     /**
184      * {@inheritDoc}
185      */

186     protected void populateArgumentMap() {
187         super.populateArgumentMap();
188         fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_NAME, fName);
189         if (getID().equals(IJavaRefactorings.RENAME_TYPE_PARAMETER)) {
190             final ITypeParameter parameter= (ITypeParameter) fJavaElement;
191             fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_INPUT, elementToHandle(getProject(), parameter.getDeclaringMember()));
192             fArguments.put(ATTRIBUTE_PARAMETER, parameter.getElementName());
193         } else
194             fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_INPUT, elementToHandle(getProject(), fJavaElement));
195         final int type= fJavaElement.getElementType();
196         if (type != IJavaElement.PACKAGE_FRAGMENT_ROOT)
197             fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_REFERENCES, Boolean.toString(fReferences));
198         if (type == IJavaElement.FIELD) {
199             fArguments.put(ATTRIBUTE_RENAME_GETTER, Boolean.toString(fRenameGetter));
200             fArguments.put(ATTRIBUTE_RENAME_SETTER, Boolean.toString(fRenameSetter));
201         }
202         switch (type) {
203             case IJavaElement.PACKAGE_FRAGMENT:
204             case IJavaElement.TYPE:
205             case IJavaElement.FIELD:
206                 fArguments.put(ATTRIBUTE_TEXTUAL_MATCHES, Boolean.toString(fTextual));
207             default:
208                 break;
209         }
210         switch (type) {
211             case IJavaElement.METHOD:
212             case IJavaElement.FIELD:
213                 fArguments.put(ATTRIBUTE_DEPRECATE, Boolean.toString(fDeprecate));
214                 fArguments.put(ATTRIBUTE_DELEGATE, Boolean.toString(fDelegate));
215             default:
216                 break;
217         }
218         switch (type) {
219             case IJavaElement.PACKAGE_FRAGMENT:
220             case IJavaElement.TYPE:
221                 fArguments.put(ATTRIBUTE_QUALIFIED, Boolean.toString(fQualified));
222                 if (fPatterns != null && !"".equals(fPatterns)) //$NON-NLS-1$
223
fArguments.put(ATTRIBUTE_PATTERNS, fPatterns);
224             default:
225                 break;
226         }
227         switch (type) {
228             case IJavaElement.TYPE:
229                 fArguments.put(ATTRIBUTE_SIMILAR_DECLARATIONS, Boolean.toString(fSimilarDeclarations));
230                 fArguments.put(ATTRIBUTE_MATCH_STRATEGY, Integer.toString(fMatchStrategy));
231             default:
232                 break;
233         }
234         switch (type) {
235             case IJavaElement.PACKAGE_FRAGMENT:
236                 fArguments.put(ATTRIBUTE_HIERARCHICAL, Boolean.toString(fHierarchical));
237             default:
238                 break;
239         }
240     }
241
242     /**
243      * Determines whether the delegate for a Java element should be declared as
244      * deprecated.
245      * <p>
246      * Note: Deprecation of the delegate is currently applicable to the Java elements
247      * {@link IMethod} and {@link IField}. The default is to not deprecate the
248      * delegate.
249      * </p>
250      *
251      * @param deprecate
252      * <code>true</code> to deprecate the delegate,
253      * <code>false</code> otherwise
254      */

255     public void setDeprecateDelegate(final boolean deprecate) {
256         fDeprecate= deprecate;
257     }
258
259     /**
260      * Sets the file name patterns to use during qualified name updating.
261      * <p>
262      * The syntax of the file name patterns is a sequence of individual name
263      * patterns, separated by comma. Additionally, wildcard characters '*' (any
264      * string) and '?' (any character) may be used.
265      * </p>
266      * <p>
267      * Note: If file name patterns are set, qualified name updating must be
268      * enabled by calling {@link #setUpdateQualifiedNames(boolean)}.
269      * </p>
270      * <p>
271      * Note: Qualified name updating is currently applicable to the Java elements
272      * {@link IPackageFragment} and {@link IType}. The default is to use no
273      * file name patterns (meaning that all files are processed).
274      * </p>
275      *
276      * @param patterns
277      * the non-empty file name patterns string
278      */

279     public void setFileNamePatterns(final String JavaDoc patterns) {
280         Assert.isNotNull(patterns);
281         Assert.isLegal(!"".equals(patterns), "Pattern must not be empty"); //$NON-NLS-1$ //$NON-NLS-2$
282
fPatterns= patterns;
283     }
284
285     /**
286      * Sets the Java element to be renamed.
287      * <p>
288      * Note: If the Java element to be renamed is of type
289      * {@link IJavaElement#JAVA_PROJECT}, clients are required to to set the
290      * project name to <code>null</code>.
291      * </p>
292      *
293      * @param element
294      * the Java element to be renamed
295      */

296     public void setJavaElement(final IJavaElement element) {
297         Assert.isNotNull(element);
298         fJavaElement= element;
299     }
300
301     /**
302      * Determines whether the the original Java element should be kept as
303      * delegate to the renamed one.
304      * <p>
305      * Note: Keeping of original elements as delegates is currently applicable to the Java
306      * elements {@link IMethod} and {@link IField}. The default is to not keep
307      * the original as delegate.
308      * </p>
309      *
310      * @param delegate
311      * <code>true</code> to keep the original, <code>false</code>
312      * otherwise
313      */

314     public void setKeepOriginal(final boolean delegate) {
315         fDelegate= delegate;
316     }
317
318     /**
319      * Determines which strategy should be used during similar declaration
320      * updating.
321      * <p>
322      * Valid arguments are {@link #STRATEGY_EXACT}, {@link #STRATEGY_EMBEDDED}
323      * or {@link #STRATEGY_SUFFIX}.
324      * </p>
325      * <p>
326      * Note: Similar declaration updating is currently applicable to Java elements of type
327      * {@link IType}. The default is to use the {@link #STRATEGY_EXACT} match
328      * strategy.
329      * </p>
330      *
331      * @param strategy
332      * the match strategy to use
333      */

334     public void setMatchStrategy(final int strategy) {
335         Assert.isLegal(strategy == STRATEGY_EXACT || strategy == STRATEGY_EMBEDDED || strategy == STRATEGY_SUFFIX, "Wrong match strategy argument"); //$NON-NLS-1$
336
fMatchStrategy= strategy;
337     }
338
339     /**
340      * Sets the new name to rename the Java element to.
341      *
342      * @param name
343      * the non-empty new name to set
344      */

345     public void setNewName(final String JavaDoc name) {
346         Assert.isNotNull(name);
347         Assert.isLegal(!"".equals(name), "Name must not be empty"); //$NON-NLS-1$//$NON-NLS-2$
348
fName= name;
349     }
350
351     /**
352      * Sets the project name of this refactoring.
353      * <p>
354      * Note: If the Java element to be renamed is of type
355      * {@link IJavaElement#JAVA_PROJECT}, clients are required to to set the
356      * project name to <code>null</code>.
357      * </p>
358      * <p>
359      * The default is to associate the refactoring with the workspace.
360      * </p>
361      *
362      * @param project
363      * the non-empty project name to set, or <code>null</code> for
364      * the workspace
365      *
366      * @see #getProject()
367      */

368     public void setProject(final String JavaDoc project) {
369         super.setProject(project);
370     }
371
372     /**
373      * Determines whether getter methods for the Java element should be renamed.
374      * <p>
375      * Note: Renaming of getter methods is applicable for {@link IField}
376      * elements which do not represent enum constants only. The default is to
377      * not rename any getter methods.
378      * </p>
379      *
380      * @param rename
381      * <code>true</code> to rename getter methods,
382      * <code>false</code> otherwise
383      */

384     public void setRenameGetters(final boolean rename) {
385         fRenameGetter= rename;
386     }
387
388     /**
389      * Determines whether setter methods for the Java element should be renamed.
390      * <p>
391      * Note: Renaming of setter methods is applicable for {@link IField}
392      * elements which do not represent enum constants only. The default is to
393      * not rename any setter methods.
394      * </p>
395      *
396      * @param rename
397      * <code>true</code> to rename setter methods,
398      * <code>false</code> otherwise
399      */

400     public void setRenameSetters(final boolean rename) {
401         fRenameSetter= rename;
402     }
403
404     /**
405      * Determines whether other Java elements in the hierarchy of the input
406      * element should be renamed as well.
407      * <p>
408      * Note: Hierarchical updating is currently applicable for Java elements of
409      * type {@link IPackageFragment}. The default is to not update Java
410      * elements hierarchically.
411      * </p>
412      *
413      * @param update
414      * <code>true</code> to update hierarchically,
415      * <code>false</code> otherwise
416      */

417     public void setUpdateHierarchy(final boolean update) {
418         fHierarchical= update;
419     }
420
421     /**
422      * Determines whether qualified names of the Java element should be renamed.
423      * <p>
424      * Qualified name updating adapts fully qualified names of the Java element
425      * to be renamed in non-Java text files. Clients may specify file name
426      * patterns by calling {@link #setFileNamePatterns(String)} to constrain the
427      * set of text files to be processed.
428      * </p>
429      * <p>
430      * Note: Qualified name updating is currently applicable to the Java elements
431      * {@link IPackageFragment} and {@link IType}. The default is to not rename
432      * qualified names.
433      * </p>
434      *
435      * @param update
436      * <code>true</code> to update qualified names,
437      * <code>false</code> otherwise
438      */

439     public void setUpdateQualifiedNames(final boolean update) {
440         fQualified= update;
441     }
442
443     /**
444      * Determines whether references to the Java element should be renamed.
445      * <p>
446      * Note: Reference updating is currently applicable to all Java element types except
447      * {@link IPackageFragmentRoot}. The default is to not update references.
448      * </p>
449      *
450      * @param update
451      * <code>true</code> to update references, <code>false</code>
452      * otherwise
453      */

454     public void setUpdateReferences(final boolean update) {
455         fReferences= update;
456     }
457
458     /**
459      * Determines whether similar declarations of the Java element should be
460      * updated.
461      * <p>
462      * Note: Similar declaration updating is currently applicable to Java elements of type
463      * {@link IType}. The default is to not update similar declarations.
464      * </p>
465      *
466      * @param update
467      * <code>true</code> to update similar declarations,
468      * <code>false</code> otherwise
469      */

470     public void setUpdateSimilarDeclarations(final boolean update) {
471         fSimilarDeclarations= update;
472     }
473
474     /**
475      * Determines whether textual occurrences of the Java element should be
476      * renamed.
477      * <p>
478      * Textual occurrence updating adapts textual occurrences of the Java
479      * element to be renamed in Java comments and Java strings.
480      * </p>
481      * <p>
482      * Note: Textual occurrence updating is currently applicable to the Java elements
483      * {@link IPackageFragment}, {@link IType} and {@link IField}. The default
484      * is to not rename textual occurrences.
485      * </p>
486      *
487      * @param update
488      * <code>true</code> to update occurrences, <code>false</code>
489      * otherwise
490      */

491     public void setUpdateTextualOccurrences(final boolean update) {
492         fTextual= update;
493     }
494
495     /**
496      * {@inheritDoc}
497      */

498     public RefactoringStatus validateDescriptor() {
499         RefactoringStatus status= super.validateDescriptor();
500         if (fName == null || "".equals(fName)) //$NON-NLS-1$
501
status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameResourceDescriptor_no_new_name));
502         if (fJavaElement == null)
503             status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameJavaElementDescriptor_no_java_element));
504         else {
505             final int type= fJavaElement.getElementType();
506             if (type == IJavaElement.JAVA_PROJECT && getProject() != null)
507                 status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameJavaElementDescriptor_project_constraint));
508             if (type == IJavaElement.PACKAGE_FRAGMENT_ROOT && fReferences)
509                 status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameJavaElementDescriptor_reference_constraint));
510             if (fTextual) {
511                 switch (type) {
512                     case IJavaElement.PACKAGE_FRAGMENT:
513                     case IJavaElement.TYPE:
514                     case IJavaElement.FIELD:
515                         break;
516                     default:
517                         status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameJavaElementDescriptor_textual_constraint));
518                 }
519             }
520             if (fDeprecate) {
521                 switch (type) {
522                     case IJavaElement.METHOD:
523                     case IJavaElement.FIELD:
524                         break;
525                     default:
526                         status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameJavaElementDescriptor_deprecation_constraint));
527                 }
528             }
529             if (fDelegate) {
530                 switch (type) {
531                     case IJavaElement.METHOD:
532                     case IJavaElement.FIELD:
533                         break;
534                     default:
535                         status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameJavaElementDescriptor_delegate_constraint));
536                 }
537             }
538             if (fRenameGetter || fRenameSetter) {
539                 if (type != IJavaElement.FIELD)
540                     status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameJavaElementDescriptor_accessor_constraint));
541             }
542             if (fQualified) {
543                 switch (type) {
544                     case IJavaElement.PACKAGE_FRAGMENT:
545                     case IJavaElement.TYPE:
546                         break;
547                     default:
548                         status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameJavaElementDescriptor_qualified_constraint));
549                 }
550             }
551             if (fSimilarDeclarations) {
552                 switch (type) {
553                     case IJavaElement.TYPE:
554                         break;
555                     default:
556                         status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameJavaElementDescriptor_similar_constraint));
557                 }
558             }
559             if (fHierarchical) {
560                 switch (type) {
561                     case IJavaElement.PACKAGE_FRAGMENT:
562                         break;
563                     default:
564                         status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.RenameJavaElementDescriptor_hierarchical_constraint));
565                 }
566             }
567         }
568         return status;
569     }
570 }
571
Popular Tags