KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > JDTRefactoringDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2006 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.internal.corext.refactoring;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Map.Entry;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
21
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IWorkspaceRoot;
24 import org.eclipse.core.resources.ResourcesPlugin;
25
26 import org.eclipse.ltk.core.refactoring.Refactoring;
27 import org.eclipse.ltk.core.refactoring.RefactoringContribution;
28 import org.eclipse.ltk.core.refactoring.RefactoringCore;
29 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
30
31 import org.eclipse.jdt.core.IJavaElement;
32 import org.eclipse.jdt.core.IJavaProject;
33 import org.eclipse.jdt.core.IMethod;
34 import org.eclipse.jdt.core.JavaCore;
35 import org.eclipse.jdt.core.WorkingCopyOwner;
36 import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor;
37
38 import org.eclipse.jdt.internal.corext.refactoring.tagging.IScriptableRefactoring;
39 import org.eclipse.jdt.internal.corext.util.Messages;
40
41 /**
42  * Descriptor object of a JDT refactoring.
43  *
44  * @since 3.2
45  */

46 public class JDTRefactoringDescriptor extends JavaRefactoringDescriptor {
47
48     /**
49      * Predefined argument called <code>element&lt;Number&gt;</code>.
50      * <p>
51      * This argument should be used to describe the elements being refactored.
52      * The value of this argument does not necessarily have to uniquely identify
53      * the elements. However, it must be possible to uniquely identify the
54      * elements using the value of this argument in conjunction with the values
55      * of the other user-defined attributes.
56      * </p>
57      * <p>
58      * The element arguments are simply distinguished by appending a number to
59      * the argument name, e.g. element1. The indices of this argument are non
60      * zero-based.
61      * </p>
62      */

63     public static final String JavaDoc ATTRIBUTE_ELEMENT= "element"; //$NON-NLS-1$
64

65     /**
66      * Predefined argument called <code>input</code>.
67      * <p>
68      * This argument should be used to describe the element being refactored.
69      * The value of this argument does not necessarily have to uniquely identify
70      * the input element. However, it must be possible to uniquely identify the
71      * input element using the value of this argument in conjunction with the
72      * values of the other user-defined attributes.
73      * </p>
74      */

75     public static final String JavaDoc ATTRIBUTE_INPUT= "input"; //$NON-NLS-1$
76

77     /**
78      * Predefined argument called <code>name</code>.
79      * <p>
80      * This argument should be used to name the element being refactored. The
81      * value of this argument may be shown in the user interface.
82      * </p>
83      */

84     public static final String JavaDoc ATTRIBUTE_NAME= "name"; //$NON-NLS-1$
85

86     /**
87      * Predefined argument called <code>references</code>.
88      * <p>
89      * This argument should be used to describe whether references to the
90      * elements being refactored should be updated as well.
91      * </p>
92      */

93     public static final String JavaDoc ATTRIBUTE_REFERENCES= "references"; //$NON-NLS-1$
94

95     /**
96      * Predefined argument called <code>selection</code>.
97      * <p>
98      * This argument should be used to describe user input selections within a
99      * text file. The value of this argument has the format "offset length".
100      * </p>
101      */

102     public static final String JavaDoc ATTRIBUTE_SELECTION= "selection"; //$NON-NLS-1$
103

104     /**
105      * Constant describing the deprecation resolving flag.
106      * <p>
107      * Clients should set this flag to indicate that the refactoring can used to
108      * resolve deprecation problems of members. Refactorings which can run on
109      * binary targets, but require a source attachment to work correctly, should
110      * set the <code>JAR_SOURCE_ATTACHMENT</code> flag as well.
111      * </p>
112      */

113     public static final int DEPRECATION_RESOLVING= 1 << 17;
114
115     /**
116      * Converts the specified element to an input handle.
117      *
118      * @param project
119      * the project, or <code>null</code> for the workspace
120      * @param element
121      * the element
122      * @return a corresponding input handle
123      */

124     public static String JavaDoc elementToHandle(final String JavaDoc project, final IJavaElement element) {
125         final String JavaDoc handle= element.getHandleIdentifier();
126         if (project != null && !(element instanceof IJavaProject)) {
127             final String JavaDoc id= element.getJavaProject().getHandleIdentifier();
128             return handle.substring(id.length());
129         }
130         return handle;
131     }
132
133     /**
134      * Converts an input handle back to the corresponding java element.
135      *
136      * @param project
137      * the project, or <code>null</code> for the workspace
138      * @param handle
139      * the input handle
140      * @return the corresponding java element, or <code>null</code> if no such
141      * element exists
142      */

143     public static IJavaElement handleToElement(final String JavaDoc project, final String JavaDoc handle) {
144         return handleToElement(project, handle, true);
145     }
146
147     /**
148      * Converts an input handle back to the corresponding java element.
149      *
150      * @param project
151      * the project, or <code>null</code> for the workspace
152      * @param handle
153      * the input handle
154      * @param check
155      * <code>true</code> to check for existence of the element,
156      * <code>false</code> otherwise
157      * @return the corresponding java element, or <code>null</code> if no such
158      * element exists
159      */

160     public static IJavaElement handleToElement(final String JavaDoc project, final String JavaDoc handle, final boolean check) {
161         return handleToElement(null, project, handle, check);
162     }
163
164     /**
165      * Converts an input handle back to the corresponding java element.
166      *
167      * @param owner
168      * the working copy owner
169      * @param project
170      * the project, or <code>null</code> for the workspace
171      * @param handle
172      * the input handle
173      * @param check
174      * <code>true</code> to check for existence of the element,
175      * <code>false</code> otherwise
176      * @return the corresponding java element, or <code>null</code> if no such
177      * element exists
178      */

179     public static IJavaElement handleToElement(final WorkingCopyOwner owner, final String JavaDoc project, final String JavaDoc handle, final boolean check) {
180         IJavaElement element= null;
181         if (owner != null)
182             element= JavaCore.create(handle, owner);
183         else
184             element= JavaCore.create(handle);
185         if (element == null && project != null) {
186             final IJavaProject javaProject= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProject(project);
187             final String JavaDoc identifier= javaProject.getHandleIdentifier();
188             if (owner != null)
189                 element= JavaCore.create(identifier + handle, owner);
190             else
191                 element= JavaCore.create(identifier + handle);
192         }
193         if (check && element instanceof IMethod) {
194             final IMethod method= (IMethod) element;
195             final IMethod[] methods= method.getDeclaringType().findMethods(method);
196             if (methods != null && methods.length > 0)
197                 element= methods[0];
198         }
199         if (element != null && (!check || element.exists()))
200             return element;
201         return null;
202     }
203
204     /**
205      * Converts an input handle with the given prefix back to the corresponding
206      * resource.
207      *
208      * @param project
209      * the project, or <code>null</code> for the workspace
210      * @param handle
211      * the input handle
212      *
213      * @return the corresponding resource, or <code>null</code> if no such
214      * resource exists
215      */

216     public static IResource handleToResource(final String JavaDoc project, final String JavaDoc handle) {
217         final IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
218         if ("".equals(handle)) //$NON-NLS-1$
219
return null;
220         final IPath path= Path.fromPortableString(handle);
221         if (path == null)
222             return null;
223         if (project != null && !"".equals(project)) //$NON-NLS-1$
224
return root.getProject(project).findMember(path);
225         return root.findMember(path);
226     }
227
228     /**
229      * Converts the specified resource to an input handle.
230      *
231      * @param project
232      * the project, or <code>null</code> for the workspace
233      * @param resource
234      * the resource
235      *
236      * @return the input handle
237      */

238     public static String JavaDoc resourceToHandle(final String JavaDoc project, final IResource resource) {
239         if (project != null && !"".equals(project)) //$NON-NLS-1$
240
return resource.getProjectRelativePath().toPortableString();
241         return resource.getFullPath().toPortableString();
242     }
243
244     /**
245      * Creates a new JDT refactoring descriptor.
246      *
247      * @param id
248      * the unique id of the refactoring
249      * @param project
250      * the project name, or <code>null</code>
251      * @param description
252      * the description
253      * @param comment
254      * the comment, or <code>null</code>
255      * @param arguments
256      * the argument map
257      * @param flags
258      * the flags
259      */

260     public JDTRefactoringDescriptor(final String JavaDoc id, final String JavaDoc project, final String JavaDoc description, final String JavaDoc comment, final Map JavaDoc arguments, final int flags) {
261         super(id, arguments);
262         setProject(project);
263         setDescription(description);
264         setComment(comment);
265         setFlags(flags);
266     }
267
268     /**
269      * Creates refactoring arguments for this refactoring descriptor.
270      *
271      * @return the refactoring arguments
272      */

273     public JavaRefactoringArguments createArguments() {
274         final JavaRefactoringArguments arguments= new JavaRefactoringArguments(getProject());
275         for (final Iterator JavaDoc iterator= getArguments().entrySet().iterator(); iterator.hasNext();) {
276             final Map.Entry JavaDoc entry= (Entry) iterator.next();
277             final String JavaDoc name= (String JavaDoc) entry.getKey();
278             final String JavaDoc value= (String JavaDoc) entry.getValue();
279             if (name != null && !"".equals(name) && value != null) //$NON-NLS-1$
280
arguments.setAttribute(name, value);
281         }
282         return arguments;
283     }
284
285     /**
286      * {@inheritDoc}
287      */

288     public Refactoring createRefactoring(final RefactoringStatus status) throws CoreException {
289         Refactoring refactoring= null;
290         final RefactoringContribution contribution= RefactoringCore.getRefactoringContribution(getID());
291         if (contribution instanceof JDTRefactoringContribution) {
292             final JDTRefactoringContribution extended= (JDTRefactoringContribution) contribution;
293             refactoring= extended.createRefactoring(this);
294         }
295         if (refactoring != null) {
296             if (refactoring instanceof IScriptableRefactoring) {
297                 final JavaRefactoringArguments arguments= createArguments();
298                 if (arguments != null)
299                     status.merge(((IScriptableRefactoring) refactoring).initialize(arguments));
300                 else
301                     status.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments));
302             } else
303                 status.merge(RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_initialization_error, getID())));
304         }
305         return refactoring;
306     }
307
308     /**
309      * Converts the specified element to an input handle.
310      *
311      * @param element
312      * the element
313      * @return a corresponding input handle
314      */

315     public String JavaDoc elementToHandle(final IJavaElement element) {
316         Assert.isNotNull(element);
317         return elementToHandle(getProject(), element);
318     }
319
320     /**
321      * {@inheritDoc}
322      */

323     public Map JavaDoc getArguments() {
324         return super.getArguments();
325     }
326
327     /**
328      * Converts the specified resource to an input handle.
329      *
330      * @param resource
331      * the resource
332      * @return a corresponding input handle
333      */

334     public String JavaDoc resourceToHandle(final IResource resource) {
335         Assert.isNotNull(resource);
336         return resourceToHandle(getProject(), resource);
337     }
338 }
Popular Tags