1 11 package org.eclipse.jdt.internal.corext.refactoring; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 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 52 public final class JavaRefactoringDescriptor extends RefactoringDescriptor { 53 54 69 public static final String ATTRIBUTE_ELEMENT= "element"; 71 81 public static final String ATTRIBUTE_INPUT= "input"; 83 90 public static final String ATTRIBUTE_NAME= "name"; 92 99 public static final String ATTRIBUTE_SELECTION= "selection"; 101 102 private static final String ATTRIBUTE_VERSION= "version"; 104 113 public static final int DEPRECATION_RESOLVING= 1 << 17; 114 115 123 public static final int JAR_IMPORTABLE= 1 << 16; 124 125 134 public static final int JAR_REFACTORABLE= 1 << 19; 135 136 143 public static final int JAR_SOURCE_ATTACHMENT= 1 << 18; 144 145 146 private static final String VALUE_VERSION_1_0= "1.0"; 148 157 public static String elementToHandle(final String project, final IJavaElement element) { 158 final String handle= element.getHandleIdentifier(); 159 if (project != null && !(element instanceof IJavaProject)) { 160 final String id= element.getJavaProject().getHandleIdentifier(); 161 return handle.substring(id.length()); 162 } 163 return handle; 164 } 165 166 176 public static IJavaElement handleToElement(final String project, final String handle) { 177 return handleToElement(project, handle, true); 178 } 179 180 193 public static IJavaElement handleToElement(final String project, final String handle, final boolean check) { 194 return handleToElement(null, project, handle, check); 195 } 196 197 212 public static IJavaElement handleToElement(final WorkingCopyOwner owner, final String project, final String 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 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 249 public static IResource handleToResource(final String project, final String handle) { 250 final IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 251 if ("".equals(handle)) return null; 253 final IPath path= Path.fromPortableString(handle); 254 if (path == null) 255 return null; 256 if (project != null && !"".equals(project)) return root.getProject(project).findMember(path); 258 return root.findMember(path); 259 } 260 261 271 public static String resourceToHandle(final String project, final IResource resource) { 272 if (project != null && !"".equals(project)) return resource.getProjectRelativePath().toPortableString(); 274 return resource.getFullPath().toPortableString(); 275 } 276 277 278 private final Map fArguments; 279 280 281 private JavaRefactoringContribution fContribution; 282 283 301 public JavaRefactoringDescriptor(final JavaRefactoringContribution contribution, final String id, final String project, final String description, final String comment, final Map arguments, final int flags) { 302 super(id, project, description, comment, flags); 303 Assert.isNotNull(arguments); 304 fContribution= contribution; 305 fArguments= arguments; 306 } 307 308 324 public JavaRefactoringDescriptor(final String id, final String project, final String description, final String comment, final Map arguments, final int flags) { 325 this(null, id, project, description, comment, arguments, flags); 326 } 327 328 333 public RefactoringArguments createArguments() { 334 final JavaRefactoringArguments arguments= new JavaRefactoringArguments(getProject()); 335 for (final Iterator iterator= fArguments.entrySet().iterator(); iterator.hasNext();) { 336 final Map.Entry entry= (Entry) iterator.next(); 337 final String name= (String ) entry.getKey(); 338 final String value= (String ) entry.getValue(); 339 if (name != null && !"".equals(name) && value != null) arguments.setAttribute(name, value); 341 } 342 return arguments; 343 } 344 345 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 378 public String elementToHandle(final IJavaElement element) { 379 Assert.isNotNull(element); 380 return elementToHandle(getProject(), element); 381 } 382 383 388 public Map getArguments() { 389 final Map map= new HashMap (fArguments); 390 map.put(ATTRIBUTE_VERSION, VALUE_VERSION_1_0); 391 return map; 392 } 393 394 399 public JavaRefactoringContribution getContribution() { 400 return fContribution; 401 } 402 } | Popular Tags |