]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/core refactoring/org/eclipse/jdt/internal/corext/refactoring/scripting/InlineMethodRefactoringContribution.java
Some talks, mostly identical.
[ifi-stolz-refaktor.git] / case-study / jdt-after / core refactoring / org / eclipse / jdt / internal / corext / refactoring / scripting / InlineMethodRefactoringContribution.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2005, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jdt.internal.corext.refactoring.scripting;
12
13import java.util.Map;
14import java.util.StringTokenizer;
15
16import org.eclipse.core.runtime.CoreException;
17import org.eclipse.core.runtime.IStatus;
18import org.eclipse.core.runtime.Status;
19
20import org.eclipse.ltk.core.refactoring.Refactoring;
21import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
22import org.eclipse.ltk.core.refactoring.RefactoringStatus;
23
24import org.eclipse.jdt.core.ICompilationUnit;
25import org.eclipse.jdt.core.IJavaElement;
26import org.eclipse.jdt.core.IMethod;
27import org.eclipse.jdt.core.ISourceRange;
28import org.eclipse.jdt.core.JavaModelException;
29import org.eclipse.jdt.core.dom.ASTParser;
30import org.eclipse.jdt.core.dom.CompilationUnit;
31import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
32import org.eclipse.jdt.core.refactoring.descriptors.InlineMethodDescriptor;
33import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor;
34
35import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory;
36import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringDescriptorUtil;
37import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
38import org.eclipse.jdt.internal.corext.refactoring.code.InlineMethodRefactoring;
39import org.eclipse.jdt.internal.corext.util.Messages;
40
41import org.eclipse.jdt.internal.ui.JavaPlugin;
42import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
43
44/**
45 * Refactoring contribution for the inline method refactoring.
46 *
47 * @since 3.2
48 */
49public final class InlineMethodRefactoringContribution extends JavaUIRefactoringContribution {
50
51 /**
52 * {@inheritDoc}
53 */
54 @Override
55 public final Refactoring createRefactoring(JavaRefactoringDescriptor descriptor, RefactoringStatus status) throws CoreException {
56 int selectionStart= -1;
57 int selectionLength= -1;
58 ICompilationUnit unit= null;
59 CompilationUnit node= null;
60 if (descriptor instanceof InlineMethodDescriptor) {
61 InlineMethodDescriptor extended= (InlineMethodDescriptor) descriptor;
62 Map<String, String> arguments= retrieveArgumentMap(extended);
63 final String selection= arguments.get(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION);
64 if (selection != null) {
65 int offset= -1;
66 int length= -1;
67 final StringTokenizer tokenizer= new StringTokenizer(selection);
68 if (tokenizer.hasMoreTokens())
69 offset= Integer.valueOf(tokenizer.nextToken()).intValue();
70 if (tokenizer.hasMoreTokens())
71 length= Integer.valueOf(tokenizer.nextToken()).intValue();
72 if (offset >= 0 && length >= 0) {
73 selectionStart= offset;
74 selectionLength= length;
75 } else
76 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { selection, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION}), null));
77 }
78 final String handle= arguments.get(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
79 if (handle != null) {
80 final IJavaElement element= JavaRefactoringDescriptorUtil.handleToElement(descriptor.getProject(), handle, false);
81 if (element == null || !element.exists())
82 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_inputs_do_not_exist, new String[] { RefactoringCoreMessages.InlineMethodRefactoring_name, IJavaRefactorings.INLINE_METHOD}), null));
83 else {
84 if (element instanceof ICompilationUnit) {
85 unit= (ICompilationUnit) element;
86 if (selection == null)
87 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION), null));
88 } else if (element instanceof IMethod) {
89 final IMethod method= (IMethod) element;
90 try {
91 final ISourceRange range= method.getNameRange();
92 if (range != null) {
93 selectionStart= range.getOffset();
94 selectionLength= range.getLength();
95 } else
96 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { handle, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT}), null));
97 } catch (JavaModelException exception) {
98 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_inputs_do_not_exist, new String[] { RefactoringCoreMessages.InlineMethodRefactoring_name, IJavaRefactorings.INLINE_METHOD}), exception));
99 }
100 unit= method.getCompilationUnit();
101 } else
102 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { handle, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT}), null));
103 final ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
104 parser.setResolveBindings(true);
105 parser.setSource(unit);
106 node= (CompilationUnit) parser.createAST(null);
107 }
108 } else
109 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT), null));
110 } else
111 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments, null));
112 return InlineMethodRefactoring.create(unit, node, selectionStart, selectionLength);
113 }
114
115 /**
116 * {@inheritDoc}
117 */
118 @Override
119 public RefactoringDescriptor createDescriptor() {
120 return RefactoringSignatureDescriptorFactory.createInlineMethodDescriptor();
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 @Override
127 public final RefactoringDescriptor createDescriptor(final String id, final String project, final String description, final String comment, final Map arguments, final int flags) {
128 return RefactoringSignatureDescriptorFactory.createInlineMethodDescriptor(project, description, comment, arguments, flags);
129 }
130}