1 /*******************************************************************************
2  * Copyright (c) 2005, 2016 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jdt.internal.corext.refactoring.scripting;
15 
16 import java.util.Map;
17 
18 import org.eclipse.ltk.core.refactoring.Refactoring;
19 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
20 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
21 import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;
22 
23 import org.eclipse.jdt.core.IMethod;
24 import org.eclipse.jdt.core.JavaModelException;
25 import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
26 import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor;
27 
28 import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory;
29 import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
30 import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringDescriptorUtil;
31 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
32 import org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor;
33 import org.eclipse.jdt.internal.corext.refactoring.rename.MethodChecks;
34 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameNonVirtualMethodProcessor;
35 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameVirtualMethodProcessor;
36 import org.eclipse.jdt.internal.corext.util.Messages;
37 
38 import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
39 
40 /**
41  * Refactoring contribution for the rename method refactoring.
42  *
43  * @since 3.2
44  */
45 public final class RenameMethodRefactoringContribution extends JavaUIRefactoringContribution {
46 
47 	@Override
createRefactoring(JavaRefactoringDescriptor descriptor, RefactoringStatus status)48 	public Refactoring createRefactoring(JavaRefactoringDescriptor descriptor, RefactoringStatus status) throws JavaModelException {
49 		JavaRefactoringArguments arguments= new JavaRefactoringArguments(descriptor.getProject(), retrieveArgumentMap(descriptor));
50 
51 		String input= arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
52 		IMethod method= (IMethod) JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), input);
53 		if (method == null) {
54 			status.addFatalError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoringContribution_could_not_create, new Object[] { BasicElementLabels.getResourceName(arguments.getProject()), input }));
55 			return null;
56 		}
57 
58 		JavaRenameProcessor processor;
59 		if (MethodChecks.isVirtual(method)) {
60 			processor= new RenameVirtualMethodProcessor(method, arguments, status);
61 		} else {
62 			processor= new RenameNonVirtualMethodProcessor(method, arguments, status);
63 		}
64 		return new RenameRefactoring(processor);
65 	}
66 
67 	@Override
createDescriptor()68 	public RefactoringDescriptor createDescriptor() {
69 		return RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_METHOD);
70 	}
71 
72 	@Override
createDescriptor(String id, String project, String description, String comment, Map<String, String> arguments, int flags)73 	public RefactoringDescriptor createDescriptor(String id, String project, String description, String comment, Map<String, String> arguments, int flags) {
74 		return RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(id, project, description, comment, arguments, flags);
75 	}
76 }
77