001    /*******************************************************************************
002     * Copyright (c) 2005, 2008 IBM Corporation and others.
003     * All rights reserved. This program and the accompanying materials
004     * are made available under the terms of the Eclipse Public License v1.0
005     * which accompanies this distribution, and is available at
006     * http://www.eclipse.org/legal/epl-v10.html
007     *
008     * Contributors:
009     *     IBM Corporation - initial API and implementation
010     *******************************************************************************/
011    package org.eclipse.jdt.internal.corext.refactoring.scripting;
012    
013    import java.util.Map;
014    import java.util.StringTokenizer;
015    
016    import org.eclipse.core.runtime.CoreException;
017    import org.eclipse.core.runtime.IStatus;
018    import org.eclipse.core.runtime.Status;
019    
020    import org.eclipse.ltk.core.refactoring.Refactoring;
021    import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
022    import org.eclipse.ltk.core.refactoring.RefactoringStatus;
023    
024    import org.eclipse.jdt.core.ICompilationUnit;
025    import org.eclipse.jdt.core.IJavaElement;
026    import org.eclipse.jdt.core.IMethod;
027    import org.eclipse.jdt.core.ISourceRange;
028    import org.eclipse.jdt.core.JavaModelException;
029    import org.eclipse.jdt.core.dom.AST;
030    import org.eclipse.jdt.core.dom.ASTParser;
031    import org.eclipse.jdt.core.dom.CompilationUnit;
032    import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
033    import org.eclipse.jdt.core.refactoring.descriptors.InlineMethodDescriptor;
034    import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor;
035    
036    import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory;
037    import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringDescriptorUtil;
038    import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
039    import org.eclipse.jdt.internal.corext.refactoring.code.InlineMethodRefactoring;
040    import org.eclipse.jdt.internal.corext.util.Messages;
041    
042    import org.eclipse.jdt.internal.ui.JavaPlugin;
043    
044    /**
045     * Refactoring contribution for the inline method refactoring.
046     *
047     * @since 3.2
048     */
049    public final class InlineMethodRefactoringContribution extends JavaUIRefactoringContribution {
050    
051            /**
052             * {@inheritDoc}
053             */
054            public final Refactoring createRefactoring(JavaRefactoringDescriptor descriptor, RefactoringStatus status) throws CoreException {
055                    int selectionStart= -1;
056                    int selectionLength= -1;
057                    ICompilationUnit unit= null;
058                    CompilationUnit node= null;
059                    if (descriptor instanceof InlineMethodDescriptor) {
060                            InlineMethodDescriptor extended= (InlineMethodDescriptor) descriptor;
061                            Map arguments= retrieveArgumentMap(extended);
062                            final String selection= (String) arguments.get(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION);
063                            if (selection != null) {
064                                    int offset= -1;
065                                    int length= -1;
066                                    final StringTokenizer tokenizer= new StringTokenizer(selection);
067                                    if (tokenizer.hasMoreTokens())
068                                            offset= Integer.valueOf(tokenizer.nextToken()).intValue();
069                                    if (tokenizer.hasMoreTokens())
070                                            length= Integer.valueOf(tokenizer.nextToken()).intValue();
071                                    if (offset >= 0 && length >= 0) {
072                                            selectionStart= offset;
073                                            selectionLength= length;
074                                    } else
075                                            throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { selection, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION}), null));
076                            }
077                            final String handle= (String) arguments.get(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
078                            if (handle != null) {
079                                    final IJavaElement element= JavaRefactoringDescriptorUtil.handleToElement(descriptor.getProject(), handle, false);
080                                    if (element == null || !element.exists())
081                                            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));
082                                    else {
083                                            if (element instanceof ICompilationUnit) {
084                                                    unit= (ICompilationUnit) element;
085                                                    if (selection == null)
086                                                            throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION), null));
087                                            } else if (element instanceof IMethod) {
088                                                    final IMethod method= (IMethod) element;
089                                                    try {
090                                                            final ISourceRange range= method.getNameRange();
091                                                            if (range != null) {
092                                                                    selectionStart= range.getOffset();
093                                                                    selectionLength= range.getLength();
094                                                            } else
095                                                                    throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { handle, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT}), null));
096                                                    } catch (JavaModelException exception) {
097                                                            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));
098                                                    }
099                                                    unit= method.getCompilationUnit();
100                                            } else
101                                                    throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { handle, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT}), null));
102                                            final ASTParser parser= ASTParser.newParser(AST.JLS3);
103                                            parser.setResolveBindings(true);
104                                            parser.setSource(unit);
105                                            node= (CompilationUnit) parser.createAST(null);
106                                    }
107                            } else
108                                    throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT), null));
109                    } else
110                            throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments, null));
111                    return InlineMethodRefactoring.create(unit, node, selectionStart, selectionLength);
112            }
113            
114            /**
115             * {@inheritDoc}
116             */
117            public RefactoringDescriptor createDescriptor() {
118                    return RefactoringSignatureDescriptorFactory.createInlineMethodDescriptor();
119            }
120    
121            /**
122             * {@inheritDoc}
123             */
124            public final RefactoringDescriptor createDescriptor(final String id, final String project, final String description, final String comment, final Map arguments, final int flags) {
125                    return RefactoringSignatureDescriptorFactory.createInlineMethodDescriptor(project, description, comment, arguments, flags);
126            }
127    }