1 11 package org.eclipse.jdt.internal.corext.refactoring; 12 13 import java.util.Iterator ; 14 import java.util.Map ; 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 46 public class JDTRefactoringDescriptor extends JavaRefactoringDescriptor { 47 48 63 public static final String ATTRIBUTE_ELEMENT= "element"; 65 75 public static final String ATTRIBUTE_INPUT= "input"; 77 84 public static final String ATTRIBUTE_NAME= "name"; 86 93 public static final String ATTRIBUTE_REFERENCES= "references"; 95 102 public static final String ATTRIBUTE_SELECTION= "selection"; 104 113 public static final int DEPRECATION_RESOLVING= 1 << 17; 114 115 124 public static String elementToHandle(final String project, final IJavaElement element) { 125 final String handle= element.getHandleIdentifier(); 126 if (project != null && !(element instanceof IJavaProject)) { 127 final String id= element.getJavaProject().getHandleIdentifier(); 128 return handle.substring(id.length()); 129 } 130 return handle; 131 } 132 133 143 public static IJavaElement handleToElement(final String project, final String handle) { 144 return handleToElement(project, handle, true); 145 } 146 147 160 public static IJavaElement handleToElement(final String project, final String handle, final boolean check) { 161 return handleToElement(null, project, handle, check); 162 } 163 164 179 public static IJavaElement handleToElement(final WorkingCopyOwner owner, final String project, final String 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 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 216 public static IResource handleToResource(final String project, final String handle) { 217 final IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 218 if ("".equals(handle)) return null; 220 final IPath path= Path.fromPortableString(handle); 221 if (path == null) 222 return null; 223 if (project != null && !"".equals(project)) return root.getProject(project).findMember(path); 225 return root.findMember(path); 226 } 227 228 238 public static String resourceToHandle(final String project, final IResource resource) { 239 if (project != null && !"".equals(project)) return resource.getProjectRelativePath().toPortableString(); 241 return resource.getFullPath().toPortableString(); 242 } 243 244 260 public JDTRefactoringDescriptor(final String id, final String project, final String description, final String comment, final Map arguments, final int flags) { 261 super(id, arguments); 262 setProject(project); 263 setDescription(description); 264 setComment(comment); 265 setFlags(flags); 266 } 267 268 273 public JavaRefactoringArguments createArguments() { 274 final JavaRefactoringArguments arguments= new JavaRefactoringArguments(getProject()); 275 for (final Iterator iterator= getArguments().entrySet().iterator(); iterator.hasNext();) { 276 final Map.Entry entry= (Entry) iterator.next(); 277 final String name= (String ) entry.getKey(); 278 final String value= (String ) entry.getValue(); 279 if (name != null && !"".equals(name) && value != null) arguments.setAttribute(name, value); 281 } 282 return arguments; 283 } 284 285 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 315 public String elementToHandle(final IJavaElement element) { 316 Assert.isNotNull(element); 317 return elementToHandle(getProject(), element); 318 } 319 320 323 public Map getArguments() { 324 return super.getArguments(); 325 } 326 327 334 public String resourceToHandle(final IResource resource) { 335 Assert.isNotNull(resource); 336 return resourceToHandle(getProject(), resource); 337 } 338 } | Popular Tags |