1 11 package org.eclipse.jdt.core.refactoring.descriptors; 12 13 import java.text.MessageFormat ; 14 import java.util.HashMap ; 15 import java.util.Map ; 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.IStatus; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.core.runtime.Status; 23 24 import org.eclipse.core.resources.IResource; 25 import org.eclipse.core.resources.IWorkspaceRoot; 26 import org.eclipse.core.resources.ResourcesPlugin; 27 28 import org.eclipse.ltk.core.refactoring.Refactoring; 29 import org.eclipse.ltk.core.refactoring.RefactoringContribution; 30 import org.eclipse.ltk.core.refactoring.RefactoringCore; 31 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 32 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 33 34 import org.eclipse.jdt.core.IJavaElement; 35 import org.eclipse.jdt.core.IJavaProject; 36 import org.eclipse.jdt.core.IMethod; 37 import org.eclipse.jdt.core.JavaCore; 38 import org.eclipse.jdt.core.WorkingCopyOwner; 39 40 import org.eclipse.jdt.internal.core.manipulation.JavaManipulationPlugin; 41 import org.eclipse.jdt.internal.core.refactoring.descriptors.DescriptorMessages; 42 43 55 public abstract class JavaRefactoringDescriptor extends RefactoringDescriptor { 56 57 71 protected static final String ATTRIBUTE_ELEMENT= "element"; 73 83 protected static final String ATTRIBUTE_INPUT= "input"; 85 92 protected static final String ATTRIBUTE_NAME= "name"; 94 102 protected static final String ATTRIBUTE_REFERENCES= "references"; 104 111 protected static final String ATTRIBUTE_SELECTION= "selection"; 113 114 protected static final String ATTRIBUTE_VERSION= "version"; 116 128 public static final int JAR_MIGRATION= 1 << 16; 129 130 142 public static final int JAR_REFACTORING= 1 << 19; 143 144 155 public static final int JAR_SOURCE_ATTACHMENT= 1 << 18; 156 157 158 protected static final String VALUE_VERSION_1_0= "1.0"; 160 169 protected static String elementToHandle(final String project, final IJavaElement element) { 170 final String handle= element.getHandleIdentifier(); 171 if (project != null && !(element instanceof IJavaProject)) { 172 final String id= element.getJavaProject().getHandleIdentifier(); 173 return handle.substring(id.length()); 174 } 175 return handle; 176 } 177 178 188 protected static IJavaElement handleToElement(final String project, final String handle) { 189 return handleToElement(project, handle, true); 190 } 191 192 205 protected static IJavaElement handleToElement(final String project, final String handle, final boolean check) { 206 return handleToElement(null, project, handle, check); 207 } 208 209 224 protected static IJavaElement handleToElement(final WorkingCopyOwner owner, final String project, final String handle, final boolean check) { 225 IJavaElement element= null; 226 if (owner != null) 227 element= JavaCore.create(handle, owner); 228 else 229 element= JavaCore.create(handle); 230 if (element == null && project != null) { 231 final IJavaProject javaProject= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProject(project); 232 final String identifier= javaProject.getHandleIdentifier(); 233 if (owner != null) 234 element= JavaCore.create(identifier + handle, owner); 235 else 236 element= JavaCore.create(identifier + handle); 237 } 238 if (check && element instanceof IMethod) { 239 final IMethod method= (IMethod) element; 240 final IMethod[] methods= method.getDeclaringType().findMethods(method); 241 if (methods != null && methods.length > 0) 242 element= methods[0]; 243 } 244 if (element != null && (!check || element.exists())) 245 return element; 246 return null; 247 } 248 249 261 protected static IResource handleToResource(final String project, final String handle) { 262 final IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 263 if ("".equals(handle)) return null; 265 final IPath path= Path.fromPortableString(handle); 266 if (path == null) 267 return null; 268 if (project != null && !"".equals(project)) return root.getProject(project).findMember(path); 270 return root.findMember(path); 271 } 272 273 283 protected static String resourceToHandle(final String project, final IResource resource) { 284 if (project != null && !"".equals(project)) return resource.getProjectRelativePath().toPortableString(); 286 return resource.getFullPath().toPortableString(); 287 } 288 289 290 protected final Map fArguments; 291 292 298 protected JavaRefactoringDescriptor(final String id) { 299 this(id, new HashMap ()); 300 } 301 302 310 protected JavaRefactoringDescriptor(final String id, final Map arguments) { 311 super(id, null, DescriptorMessages.JavaRefactoringDescriptor_not_available, null, RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE); 312 fArguments= arguments; 313 fArguments.put(ATTRIBUTE_VERSION, VALUE_VERSION_1_0); 314 } 315 316 319 public Refactoring createRefactoring(final RefactoringStatus status) throws CoreException { 320 populateArgumentMap(); 321 Refactoring refactoring= null; 322 final String id= getID(); 323 final RefactoringContribution contribution= RefactoringCore.getRefactoringContribution(id); 324 if (contribution != null) { 325 final RefactoringDescriptor descriptor= contribution.createDescriptor(id, getProject(), getDescription(), getComment(), fArguments, getFlags()); 326 if (descriptor != null) { 327 refactoring= descriptor.createRefactoring(status); 328 } else 329 JavaManipulationPlugin.log(new Status(IStatus.ERROR, JavaManipulationPlugin.getPluginId(), 0, MessageFormat.format(DescriptorMessages.JavaRefactoringDescriptor_no_resulting_descriptor, new Object [] { id}), null)); 330 } 331 return refactoring; 332 } 333 334 347 protected Map getArguments() { 348 populateArgumentMap(); 349 return new HashMap (fArguments); 350 } 351 352 356 protected void populateArgumentMap() { 357 Assert.isTrue(!validateDescriptor().hasFatalError(), "Validation returns a fatal error status."); } 359 360 374 public void setComment(final String comment) { 375 super.setComment(comment); 376 } 377 378 390 public void setDescription(final String description) { 391 super.setDescription(description); 392 } 393 394 418 public void setFlags(final int flags) { 419 super.setFlags(flags); 420 } 421 422 436 public void setProject(final String project) { 437 super.setProject(project); 438 } 439 440 453 public RefactoringStatus validateDescriptor() { 454 RefactoringStatus status= new RefactoringStatus(); 455 String description= getDescription(); 456 if (description == null || "".equals(description)) status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.JavaRefactoringDescriptor_no_description)); 458 return status; 459 } 460 } | Popular Tags |