001: package org.eclipse.jdt.internal.corext.refactoring;
002:
003: import org.eclipse.core.runtime.IPath;
004: import org.eclipse.core.runtime.Path;
005:
006: import org.eclipse.core.resources.IResource;
007: import org.eclipse.core.resources.IWorkspaceRoot;
008: import org.eclipse.core.resources.ResourcesPlugin;
009:
010: import org.eclipse.jdt.core.IJavaElement;
011: import org.eclipse.jdt.core.IJavaProject;
012: import org.eclipse.jdt.core.IMethod;
013: import org.eclipse.jdt.core.JavaCore;
014: import org.eclipse.jdt.core.WorkingCopyOwner;
015:
016: public class JavaRefactoringDescriptorUtil {
017: /* TODO: share implementation with
018: * org.eclipse.jdt.internal.core.refactoring.descriptors.JavaRefactoringDescriptorUtil
019: */
020:
021: private JavaRefactoringDescriptorUtil() {
022: }
023:
024: /**
025: * Predefined argument called <code>element<Number></code>.
026: * <p>
027: * This argument should be used to describe the elements being refactored.
028: * The value of this argument does not necessarily have to uniquely identify
029: * the elements. However, it must be possible to uniquely identify the
030: * elements using the value of this argument in conjunction with the values
031: * of the other user-defined attributes.
032: * </p>
033: * <p>
034: * The element arguments are simply distinguished by appending a number to
035: * the argument name, e.g. element1. The indices of this argument are non
036: * zero-based.
037: * </p>
038: */
039: public static final String ATTRIBUTE_ELEMENT = "element"; //$NON-NLS-1$
040:
041: /**
042: * Predefined argument called <code>input</code>.
043: * <p>
044: * This argument should be used to describe the element being refactored.
045: * The value of this argument does not necessarily have to uniquely identify
046: * the input element. However, it must be possible to uniquely identify the
047: * input element using the value of this argument in conjunction with the
048: * values of the other user-defined attributes.
049: * </p>
050: */
051: public static final String ATTRIBUTE_INPUT = "input"; //$NON-NLS-1$
052:
053: /**
054: * Predefined argument called <code>name</code>.
055: * <p>
056: * This argument should be used to name the element being refactored. The
057: * value of this argument may be shown in the user interface.
058: * </p>
059: */
060: public static final String ATTRIBUTE_NAME = "name"; //$NON-NLS-1$
061:
062: /**
063: * Predefined argument called <code>references</code>.
064: * <p>
065: * This argument should be used to describe whether references to the
066: * elements being refactored should be updated as well.
067: * </p>
068: */
069: public static final String ATTRIBUTE_REFERENCES = "references"; //$NON-NLS-1$
070:
071: /**
072: * Predefined argument called <code>selection</code>.
073: * <p>
074: * This argument should be used to describe user input selections within a
075: * text file. The value of this argument has the format "offset length".
076: * </p>
077: */
078: public static final String ATTRIBUTE_SELECTION = "selection"; //$NON-NLS-1$
079:
080: /**
081: * Converts the specified element to an input handle.
082: *
083: * @param project
084: * the project, or <code>null</code> for the workspace
085: * @param element
086: * the element
087: * @return a corresponding input handle
088: * Note: if the given project is not the element's project, then the full handle is returned
089: */
090: public static String elementToHandle(final String project,
091: final IJavaElement element) {
092: final String handle = element.getHandleIdentifier();
093: if (project != null && !(element instanceof IJavaProject)) {
094: IJavaProject javaProject = element.getJavaProject();
095: if (project.equals(javaProject.getElementName())) {
096: final String id = javaProject.getHandleIdentifier();
097: return handle.substring(id.length());
098: }
099: }
100: return handle;
101: }
102:
103: /**
104: * Converts an input handle back to the corresponding java element.
105: *
106: * @param project
107: * the project, or <code>null</code> for the workspace
108: * @param handle
109: * the input handle
110: * @return the corresponding java element, or <code>null</code> if no such
111: * element exists
112: */
113: public static IJavaElement handleToElement(final String project,
114: final String handle) {
115: return handleToElement(project, handle, true);
116: }
117:
118: /**
119: * Converts an input handle back to the corresponding java element.
120: *
121: * @param project
122: * the project, or <code>null</code> for the workspace
123: * @param handle
124: * the input handle
125: * @param check
126: * <code>true</code> to check for existence of the element,
127: * <code>false</code> otherwise
128: * @return the corresponding java element, or <code>null</code> if no such
129: * element exists
130: */
131: public static IJavaElement handleToElement(final String project,
132: final String handle, final boolean check) {
133: return handleToElement(null, project, handle, check);
134: }
135:
136: /**
137: * Converts an input handle back to the corresponding java element.
138: *
139: * @param owner
140: * the working copy owner
141: * @param project
142: * the project, or <code>null</code> for the workspace
143: * @param handle
144: * the input handle
145: * @param check
146: * <code>true</code> to check for existence of the element,
147: * <code>false</code> otherwise
148: * @return the corresponding java element, or <code>null</code> if no such
149: * element exists
150: */
151: public static IJavaElement handleToElement(
152: final WorkingCopyOwner owner, final String project,
153: final String handle, final boolean check) {
154: IJavaElement element = null;
155: if (owner != null)
156: element = JavaCore.create(handle, owner);
157: else
158: element = JavaCore.create(handle);
159: if (element == null && project != null) {
160: final IJavaProject javaProject = JavaCore.create(
161: ResourcesPlugin.getWorkspace().getRoot())
162: .getJavaProject(project);
163: final String identifier = javaProject.getHandleIdentifier();
164: if (owner != null)
165: element = JavaCore.create(identifier + handle, owner);
166: else
167: element = JavaCore.create(identifier + handle);
168: }
169: if (check && element instanceof IMethod) {
170: /*
171: * Resolve the method based on simple names of parameter types
172: * (to accommodate for different qualifications when refactoring is e.g.
173: * recorded in source but applied on binary method):
174: */
175: final IMethod method = (IMethod) element;
176: final IMethod[] methods = method.getDeclaringType()
177: .findMethods(method);
178: if (methods != null && methods.length > 0)
179: element = methods[0];
180: }
181: if (element != null && (!check || element.exists()))
182: return element;
183: return null;
184: }
185:
186: /**
187: * Converts an input handle with the given prefix back to the corresponding
188: * resource.
189: *
190: * @param project
191: * the project, or <code>null</code> for the workspace
192: * @param handle
193: * the input handle
194: *
195: * @return the corresponding resource, or <code>null</code> if no such
196: * resource exists.
197: * Note: if the given handle is absolute, the project is not used to resolve.
198: */
199: public static IResource handleToResource(final String project,
200: final String handle) {
201: final IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
202: .getRoot();
203: if ("".equals(handle)) //$NON-NLS-1$
204: return null;
205: final IPath path = Path.fromPortableString(handle);
206: if (path == null)
207: return null;
208: if (project != null
209: && !"".equals(project) && !path.isAbsolute()) //$NON-NLS-1$
210: return root.getProject(project).findMember(path);
211: return root.findMember(path);
212: }
213:
214: /**
215: * Converts the specified resource to an input handle.
216: *
217: * @param project
218: * the project, or <code>null</code> for the workspace
219: * @param resource
220: * the resource
221: *
222: * @return the input handle.
223: * Note: if the given project is not the resource's project, then the full handle is returned.
224: */
225: public static String resourceToHandle(final String project,
226: final IResource resource) {
227: if (project != null
228: && !"".equals(project) && project.equals(resource.getProject().getName())) //$NON-NLS-1$
229: return resource.getProjectRelativePath().toPortableString();
230: return resource.getFullPath().toPortableString();
231: }
232:
233: }
|