KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > scripting > InlineMethodRefactoringContribution


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.scripting;
12
13 import java.util.StringTokenizer JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18
19 import org.eclipse.ltk.core.refactoring.Refactoring;
20 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
21 import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
22
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.IMethod;
26 import org.eclipse.jdt.core.ISourceRange;
27 import org.eclipse.jdt.core.JavaModelException;
28 import org.eclipse.jdt.core.dom.AST;
29 import org.eclipse.jdt.core.dom.ASTParser;
30 import org.eclipse.jdt.core.dom.CompilationUnit;
31 import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
32
33 import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
34 import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringContribution;
35 import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptor;
36 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
37 import org.eclipse.jdt.internal.corext.refactoring.code.InlineMethodRefactoring;
38 import org.eclipse.jdt.internal.corext.util.Messages;
39
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41
42 /**
43  * Refactoring contribution for the inline method refactoring.
44  *
45  * @since 3.2
46  */

47 public final class InlineMethodRefactoringContribution extends JDTRefactoringContribution {
48
49     /**
50      * {@inheritDoc}
51      */

52     public final Refactoring createRefactoring(final RefactoringDescriptor descriptor) throws CoreException {
53         int selectionStart= -1;
54         int selectionLength= -1;
55         ICompilationUnit unit= null;
56         CompilationUnit node= null;
57         RefactoringArguments arguments= null;
58         if (descriptor instanceof JDTRefactoringDescriptor) {
59             final JDTRefactoringDescriptor extended= (JDTRefactoringDescriptor) descriptor;
60             arguments= extended.createArguments();
61         }
62         if (arguments instanceof JavaRefactoringArguments) {
63             final JavaRefactoringArguments extended= (JavaRefactoringArguments) arguments;
64             final String JavaDoc selection= extended.getAttribute(JDTRefactoringDescriptor.ATTRIBUTE_SELECTION);
65             if (selection != null) {
66                 int offset= -1;
67                 int length= -1;
68                 final StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(selection);
69                 if (tokenizer.hasMoreTokens())
70                     offset= Integer.valueOf(tokenizer.nextToken()).intValue();
71                 if (tokenizer.hasMoreTokens())
72                     length= Integer.valueOf(tokenizer.nextToken()).intValue();
73                 if (offset >= 0 && length >= 0) {
74                     selectionStart= offset;
75                     selectionLength= length;
76                 } else
77                     throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object JavaDoc[] { selection, JDTRefactoringDescriptor.ATTRIBUTE_SELECTION}), null));
78             }
79             final String JavaDoc handle= extended.getAttribute(JDTRefactoringDescriptor.ATTRIBUTE_INPUT);
80             if (handle != null) {
81                 final IJavaElement element= JDTRefactoringDescriptor.handleToElement(extended.getProject(), handle, false);
82                 if (element == null || !element.exists())
83                     throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_inputs_do_not_exist, new String JavaDoc[] { RefactoringCoreMessages.InlineMethodRefactoring_name, IJavaRefactorings.INLINE_METHOD}), null));
84                 else {
85                     if (element instanceof ICompilationUnit) {
86                         unit= (ICompilationUnit) element;
87                         if (selection == null)
88                             throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JDTRefactoringDescriptor.ATTRIBUTE_SELECTION), null));
89                     } else if (element instanceof IMethod) {
90                         final IMethod method= (IMethod) element;
91                         try {
92                             final ISourceRange range= method.getNameRange();
93                             if (range != null) {
94                                 selectionStart= range.getOffset();
95                                 selectionLength= range.getLength();
96                             } else
97                                 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object JavaDoc[] { handle, JDTRefactoringDescriptor.ATTRIBUTE_INPUT}), null));
98                         } catch (JavaModelException exception) {
99                             throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_inputs_do_not_exist, new String JavaDoc[] { RefactoringCoreMessages.InlineMethodRefactoring_name, IJavaRefactorings.INLINE_METHOD}), exception));
100                         }
101                         unit= method.getCompilationUnit();
102                     } else
103                         throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object JavaDoc[] { handle, JDTRefactoringDescriptor.ATTRIBUTE_INPUT}), null));
104                     final ASTParser parser= ASTParser.newParser(AST.JLS3);
105                     parser.setResolveBindings(true);
106                     parser.setSource(unit);
107                     node= (CompilationUnit) parser.createAST(null);
108                 }
109             } else
110                 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JDTRefactoringDescriptor.ATTRIBUTE_INPUT), null));
111         } else
112             throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments, null));
113         return InlineMethodRefactoring.create(unit, node, selectionStart, selectionLength);
114     }
115 }
116
Popular Tags