KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Map.Entry;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.Status;
24
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.IWorkspaceRoot;
27 import org.eclipse.core.resources.ResourcesPlugin;
28
29 import org.eclipse.ltk.core.refactoring.Refactoring;
30 import org.eclipse.ltk.core.refactoring.RefactoringContribution;
31 import org.eclipse.ltk.core.refactoring.RefactoringCore;
32 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
33 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
34 import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
35
36 import org.eclipse.jdt.core.IJavaElement;
37 import org.eclipse.jdt.core.IJavaProject;
38 import org.eclipse.jdt.core.IMethod;
39 import org.eclipse.jdt.core.JavaCore;
40 import org.eclipse.jdt.core.WorkingCopyOwner;
41
42 import org.eclipse.jdt.internal.corext.refactoring.tagging.IScriptableRefactoring;
43 import org.eclipse.jdt.internal.corext.util.Messages;
44
45 import org.eclipse.jdt.internal.ui.JavaPlugin;
46
47 /**
48  * Descriptor object of a java refactoring.
49  *
50  * @since 3.2
51  */

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

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

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

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

83     /**
84      * Predefined argument called <code>name</code>.
85      * <p>
86      * This argument should be used to name the element being refactored. The
87      * value of this argument may be shown in the user interface.
88      * </p>
89      */

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

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

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

101     /** The version attribute */
102     private static final String JavaDoc ATTRIBUTE_VERSION= "version"; //$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      * Constant describing the jar importable flag.
117      * <p>
118      * Clients should set this flag to indicate that the refactoring can be
119      * imported from a JAR file. If this flag is set,
120      * <code>JAR_REFACTORABLE</code> should be set as well.
121      * </p>
122      */

123     public static final int JAR_IMPORTABLE= 1 << 16;
124
125     /**
126      * Constant describing the jar refactorable flag.
127      * <p>
128      * Clients should set this flag to indicate that the refactoring can be
129      * performed on a JAR file. Refactorings which can run on binary targets,
130      * but require a source attachment to work correctly, should set the
131      * <code>JAR_SOURCE_ATTACHMENT</code> flag as well.
132      * </p>
133      */

134     public static final int JAR_REFACTORABLE= 1 << 19;
135
136     /**
137      * Constant describing the jar source attachment flag.
138      * <p>
139      * Clients should set this flag to indicate that the refactoring can be
140      * performed on a JAR file if and only if it contains a source attachment.
141      * </p>
142      */

143     public static final int JAR_SOURCE_ATTACHMENT= 1 << 18;
144
145     /** The version value 1.0 */
146     private static final String JavaDoc VALUE_VERSION_1_0= "1.0"; //$NON-NLS-1$
147

148     /**
149      * Converts the specified element to an input handle.
150      *
151      * @param project
152      * the project, or <code>null</code> for the workspace
153      * @param element
154      * the element
155      * @return a corresponding input handle
156      */

157     public static String JavaDoc elementToHandle(final String JavaDoc project, final IJavaElement element) {
158         final String JavaDoc handle= element.getHandleIdentifier();
159         if (project != null && !(element instanceof IJavaProject)) {
160             final String JavaDoc id= element.getJavaProject().getHandleIdentifier();
161             return handle.substring(id.length());
162         }
163         return handle;
164     }
165
166     /**
167      * Converts an input handle back to the corresponding java element.
168      *
169      * @param project
170      * the project, or <code>null</code> for the workspace
171      * @param handle
172      * the input handle
173      * @return the corresponding java element, or <code>null</code> if no such
174      * element exists
175      */

176     public static IJavaElement handleToElement(final String JavaDoc project, final String JavaDoc handle) {
177         return handleToElement(project, handle, true);
178     }
179
180     /**
181      * Converts an input handle back to the corresponding java element.
182      *
183      * @param project
184      * the project, or <code>null</code> for the workspace
185      * @param handle
186      * the input handle
187      * @param check
188      * <code>true</code> to check for existence of the element,
189      * <code>false</code> otherwise
190      * @return the corresponding java element, or <code>null</code> if no such
191      * element exists
192      */

193     public static IJavaElement handleToElement(final String JavaDoc project, final String JavaDoc handle, final boolean check) {
194         return handleToElement(null, project, handle, check);
195     }
196
197     /**
198      * Converts an input handle back to the corresponding java element.
199      *
200      * @param owner
201      * the working copy owner
202      * @param project
203      * the project, or <code>null</code> for the workspace
204      * @param handle
205      * the input handle
206      * @param check
207      * <code>true</code> to check for existence of the element,
208      * <code>false</code> otherwise
209      * @return the corresponding java element, or <code>null</code> if no such
210      * element exists
211      */

212     public static IJavaElement handleToElement(final WorkingCopyOwner owner, final String JavaDoc project, final String JavaDoc handle, final boolean check) {
213         IJavaElement element= null;
214         if (owner != null)
215             element= JavaCore.create(handle, owner);
216         else
217             element= JavaCore.create(handle);
218         if (element == null && project != null) {
219             final IJavaProject javaProject= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProject(project);
220             final String JavaDoc identifier= javaProject.getHandleIdentifier();
221             if (owner != null)
222                 element= JavaCore.create(identifier + handle, owner);
223             else
224                 element= JavaCore.create(identifier + handle);
225         }
226         if (check && element instanceof IMethod) {
227             final IMethod method= (IMethod) element;
228             final IMethod[] methods= method.getDeclaringType().findMethods(method);
229             if (methods != null && methods.length > 0)
230                 element= methods[0];
231         }
232         if (element != null && (!check || element.exists()))
233             return element;
234         return null;
235     }
236
237     /**
238      * Converts an input handle with the given prefix back to the corresponding
239      * resource.
240      *
241      * @param project
242      * the project, or <code>null</code> for the workspace
243      * @param handle
244      * the input handle
245      *
246      * @return the corresponding resource, or <code>null</code> if no such
247      * resource exists
248      */

249     public static IResource handleToResource(final String JavaDoc project, final String JavaDoc handle) {
250         final IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
251         if ("".equals(handle)) //$NON-NLS-1$
252
return null;
253         final IPath path= Path.fromPortableString(handle);
254         if (path == null)
255             return null;
256         if (project != null && !"".equals(project)) //$NON-NLS-1$
257
return root.getProject(project).findMember(path);
258         return root.findMember(path);
259     }
260
261     /**
262      * Converts the specified resource to an input handle.
263      *
264      * @param project
265      * the project, or <code>null</code> for the workspace
266      * @param resource
267      * the resource
268      *
269      * @return the input handle
270      */

271     public static String JavaDoc resourceToHandle(final String JavaDoc project, final IResource resource) {
272         if (project != null && !"".equals(project)) //$NON-NLS-1$
273
return resource.getProjectRelativePath().toPortableString();
274         return resource.getFullPath().toPortableString();
275     }
276
277     /** The map of arguments (element type: &lt;String, String&gt;) */
278     private final Map JavaDoc fArguments;
279
280     /** The refactoring contribution, or <code>null</code> */
281     private JavaRefactoringContribution fContribution;
282
283     /**
284      * Creates a new java refactoring descriptor.
285      *
286      * @param contribution
287      * the refactoring contribution, or <code>null</code>
288      * @param id
289      * the unique id of the refactoring
290      * @param project
291      * the project name, or <code>null</code>
292      * @param description
293      * the description
294      * @param comment
295      * the comment, or <code>null</code>
296      * @param arguments
297      * the argument map
298      * @param flags
299      * the flags
300      */

301     public JavaRefactoringDescriptor(final JavaRefactoringContribution contribution, final String JavaDoc id, final String JavaDoc project, final String JavaDoc description, final String JavaDoc comment, final Map JavaDoc arguments, final int flags) {
302         super(id, project, description, comment, flags);
303         Assert.isNotNull(arguments);
304         fContribution= contribution;
305         fArguments= arguments;
306     }
307
308     /**
309      * Creates a new java refactoring descriptor.
310      *
311      * @param id
312      * the unique id of the refactoring
313      * @param project
314      * the project name, or <code>null</code>
315      * @param description
316      * the description
317      * @param comment
318      * the comment, or <code>null</code>
319      * @param arguments
320      * the argument map
321      * @param flags
322      * the flags
323      */

324     public JavaRefactoringDescriptor(final String JavaDoc id, final String JavaDoc project, final String JavaDoc description, final String JavaDoc comment, final Map JavaDoc arguments, final int flags) {
325         this(null, id, project, description, comment, arguments, flags);
326     }
327
328     /**
329      * Creates refactoring arguments for this refactoring descriptor.
330      *
331      * @return the refactoring arguments
332      */

333     public RefactoringArguments createArguments() {
334         final JavaRefactoringArguments arguments= new JavaRefactoringArguments(getProject());
335         for (final Iterator JavaDoc iterator= fArguments.entrySet().iterator(); iterator.hasNext();) {
336             final Map.Entry JavaDoc entry= (Entry) iterator.next();
337             final String JavaDoc name= (String JavaDoc) entry.getKey();
338             final String JavaDoc value= (String JavaDoc) entry.getValue();
339             if (name != null && !"".equals(name) && value != null) //$NON-NLS-1$
340
arguments.setAttribute(name, value);
341         }
342         return arguments;
343     }
344
345     /**
346      * {@inheritDoc}
347      */

348     public Refactoring createRefactoring(final RefactoringStatus status) throws CoreException {
349         Refactoring refactoring= null;
350         if (fContribution != null)
351             refactoring= fContribution.createRefactoring(this);
352         else {
353             final RefactoringContribution contribution= RefactoringCore.getRefactoringContribution(getID());
354             if (contribution instanceof JavaRefactoringContribution) {
355                 fContribution= (JavaRefactoringContribution) contribution;
356                 refactoring= fContribution.createRefactoring(this);
357             }
358         }
359         if (refactoring != null) {
360             if (refactoring instanceof IScriptableRefactoring) {
361                 final RefactoringStatus result= ((IScriptableRefactoring) refactoring).initialize(createArguments());
362                 if (result.hasFatalError())
363                     throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, result.getMessageMatchingSeverity(RefactoringStatus.FATAL), null));
364                 status.merge(result);
365             } else
366                 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.JavaRefactoringDescriptor_initialization_error, getDescription()), null));
367         }
368         return refactoring;
369     }
370
371     /**
372      * Converts the specified element to an input handle.
373      *
374      * @param element
375      * the element
376      * @return a corresponding input handle
377      */

378     public String JavaDoc elementToHandle(final IJavaElement element) {
379         Assert.isNotNull(element);
380         return elementToHandle(getProject(), element);
381     }
382
383     /**
384      * Returns the argument map
385      *
386      * @return the argument map.
387      */

388     public Map JavaDoc getArguments() {
389         final Map JavaDoc map= new HashMap JavaDoc(fArguments);
390         map.put(ATTRIBUTE_VERSION, VALUE_VERSION_1_0);
391         return map;
392     }
393
394     /**
395      * Returns the refactoring contribution.
396      *
397      * @return the refactoring contribution, or <code>null</code>
398      */

399     public JavaRefactoringContribution getContribution() {
400         return fContribution;
401     }
402 }
Popular Tags