]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 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 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.corext.refactoring.delegates; | |
12 | ||
13 | import java.util.List; | |
14 | ||
15 | import org.eclipse.core.runtime.Assert; | |
16 | ||
17 | import org.eclipse.jdt.core.JavaModelException; | |
18 | import org.eclipse.jdt.core.dom.ASTNode; | |
19 | import org.eclipse.jdt.core.dom.Block; | |
20 | import org.eclipse.jdt.core.dom.BodyDeclaration; | |
21 | import org.eclipse.jdt.core.dom.ChildPropertyDescriptor; | |
22 | import org.eclipse.jdt.core.dom.ConstructorInvocation; | |
23 | import org.eclipse.jdt.core.dom.Expression; | |
24 | import org.eclipse.jdt.core.dom.ExpressionStatement; | |
25 | import org.eclipse.jdt.core.dom.IBinding; | |
26 | import org.eclipse.jdt.core.dom.MethodDeclaration; | |
27 | import org.eclipse.jdt.core.dom.MethodInvocation; | |
28 | import org.eclipse.jdt.core.dom.MethodRef; | |
29 | import org.eclipse.jdt.core.dom.MethodRefParameter; | |
30 | import org.eclipse.jdt.core.dom.PrimitiveType; | |
31 | import org.eclipse.jdt.core.dom.ReturnStatement; | |
32 | import org.eclipse.jdt.core.dom.SimpleName; | |
33 | import org.eclipse.jdt.core.dom.SingleVariableDeclaration; | |
34 | import org.eclipse.jdt.core.dom.Statement; | |
35 | import org.eclipse.jdt.core.dom.Type; | |
36 | ||
37 | import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory; | |
38 | import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; | |
39 | import org.eclipse.jdt.internal.corext.refactoring.rename.RenameNonVirtualMethodProcessor; | |
40 | import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureProcessor.DeclarationUpdate; | |
41 | import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; | |
42 | import org.eclipse.jdt.internal.corext.refactoring.structure.MoveStaticMembersProcessor; | |
43 | ||
44 | /** | |
45 | * Delegate creator for static and non-static methods. | |
46 | * | |
47 | * @since 3.2 | |
48 | */ | |
49 | public class DelegateMethodCreator extends DelegateCreator { | |
50 | ||
51 | private ASTNode fDelegateInvocation; | |
52 | private MethodRef fDocMethodReference; | |
53 | ||
54 | @Override | |
55 | protected void initialize() { | |
56 | ||
57 | Assert.isTrue(getDeclaration() instanceof MethodDeclaration); | |
58 | ||
59 | if (getNewElementName() == null) | |
60 | setNewElementName(((MethodDeclaration) getDeclaration()).getName().getIdentifier()); | |
61 | ||
62 | setInsertBefore(true); | |
63 | } | |
64 | ||
65 | @Override | |
66 | protected ASTNode createBody(BodyDeclaration bd) throws JavaModelException { | |
67 | ||
68 | MethodDeclaration methodDeclaration= (MethodDeclaration) bd; | |
69 | ||
70 | // interface or abstract method ? => don't create a method body. | |
71 | if (methodDeclaration.getBody() == null) | |
72 | return null; | |
73 | ||
74 | return createDelegateMethodBody(methodDeclaration); | |
75 | } | |
76 | ||
77 | @Override | |
78 | protected ASTNode createDocReference(final BodyDeclaration declaration) throws JavaModelException { | |
79 | fDocMethodReference= getAst().newMethodRef(); | |
80 | fDocMethodReference.setName(getAst().newSimpleName(getNewElementName())); | |
81 | if (isMoveToAnotherFile()) | |
82 | fDocMethodReference.setQualifier(createDestinationTypeName()); | |
83 | createArguments((MethodDeclaration) declaration, fDocMethodReference.parameters(), false); | |
84 | return fDocMethodReference; | |
85 | } | |
86 | ||
87 | @Override | |
88 | protected ASTNode getBodyHead(BodyDeclaration result) { | |
89 | return result; | |
90 | } | |
91 | ||
92 | @Override | |
93 | protected ChildPropertyDescriptor getJavaDocProperty() { | |
94 | return MethodDeclaration.JAVADOC_PROPERTY; | |
95 | } | |
96 | ||
97 | @Override | |
98 | protected ChildPropertyDescriptor getBodyProperty() { | |
99 | return MethodDeclaration.BODY_PROPERTY; | |
100 | } | |
101 | ||
102 | /** | |
103 | * @return the delegate incovation, either a {@link ConstructorInvocation} | |
104 | * or a {@link MethodInvocation}. May be null if the delegate | |
105 | * method is abstract (and therefore has no body at all) | |
106 | */ | |
107 | public ASTNode getDelegateInvocation() { | |
108 | return fDelegateInvocation; | |
109 | } | |
110 | ||
111 | /** | |
112 | * @return the javadoc reference to the old method in the javadoc comment. | |
113 | * May be null if no comment was created. | |
114 | */ | |
115 | public MethodRef getJavadocReference() { | |
116 | return fDocMethodReference; | |
117 | } | |
118 | ||
119 | /** | |
120 | * Creates the corresponding statement for the method invocation, based on | |
121 | * the return type. | |
122 | * | |
123 | * @param declaration the method declaration where the invocation statement | |
124 | * is inserted | |
125 | * @param invocation the method invocation being encapsulated by the | |
126 | * resulting statement | |
127 | * @return the corresponding statement | |
128 | */ | |
129 | protected Statement createMethodInvocation(final MethodDeclaration declaration, final MethodInvocation invocation) { | |
130 | Assert.isNotNull(declaration); | |
131 | Assert.isNotNull(invocation); | |
132 | Statement statement= null; | |
133 | final Type type= declaration.getReturnType2(); | |
134 | if (type == null) | |
135 | statement= createExpressionStatement(invocation); | |
136 | else { | |
137 | if (type instanceof PrimitiveType) { | |
138 | final PrimitiveType primitive= (PrimitiveType) type; | |
139 | if (primitive.getPrimitiveTypeCode().equals(PrimitiveType.VOID)) | |
140 | statement= createExpressionStatement(invocation); | |
141 | else | |
142 | statement= createReturnStatement(invocation); | |
143 | } else | |
144 | statement= createReturnStatement(invocation); | |
145 | } | |
146 | return statement; | |
147 | } | |
148 | ||
149 | /** | |
150 | * {@inheritDoc} | |
151 | */ | |
152 | @Override | |
153 | protected IBinding getDeclarationBinding() { | |
154 | final MethodDeclaration declaration= (MethodDeclaration) getDeclaration(); | |
155 | return declaration.resolveBinding(); | |
156 | } | |
157 | ||
158 | @SuppressWarnings("unchecked") | |
159 | private void createArguments(final MethodDeclaration declaration, final List<? extends ASTNode> arguments, boolean methodInvocation) { | |
160 | Assert.isNotNull(declaration); | |
161 | Assert.isNotNull(arguments); | |
162 | SingleVariableDeclaration variable= null; | |
163 | final int size= declaration.parameters().size(); | |
164 | for (int index= 0; index < size; index++) { | |
165 | variable= (SingleVariableDeclaration) declaration.parameters().get(index); | |
166 | ||
167 | if (methodInvocation) { | |
168 | // we are creating method invocation parameters | |
169 | final SimpleName expression= getAst().newSimpleName(variable.getName().getIdentifier()); | |
170 | ((List<Expression>) arguments).add(expression); | |
171 | } else { | |
172 | // we are creating type info for the javadoc | |
173 | final MethodRefParameter parameter= getAst().newMethodRefParameter(); | |
174 | parameter.setType(ASTNodeFactory.newType(getAst(), variable)); | |
175 | if ((index == size - 1) && declaration.isVarargs()) | |
176 | parameter.setVarargs(true); | |
177 | ((List<MethodRefParameter>) arguments).add(parameter); | |
178 | } | |
179 | } | |
180 | } | |
181 | ||
182 | private Block createDelegateMethodBody(final MethodDeclaration declaration) { | |
183 | Assert.isNotNull(declaration); | |
184 | ||
185 | MethodDeclaration old= (MethodDeclaration) getDeclaration(); | |
186 | List<Expression> arguments; | |
187 | Statement call; | |
188 | if (old.isConstructor()) { | |
189 | ConstructorInvocation invocation= getAst().newConstructorInvocation(); | |
190 | arguments= invocation.arguments(); | |
191 | call= invocation; | |
192 | fDelegateInvocation= invocation; | |
193 | } else { | |
194 | MethodInvocation invocation= getAst().newMethodInvocation(); | |
195 | invocation.setName(getAst().newSimpleName(getNewElementName())); | |
196 | invocation.setExpression(getAccess()); | |
197 | arguments= invocation.arguments(); | |
198 | call= createMethodInvocation(declaration, invocation); | |
199 | fDelegateInvocation= invocation; | |
200 | } | |
201 | createArguments(declaration, arguments, true); | |
202 | ||
203 | final Block body= getAst().newBlock(); | |
204 | body.statements().add(call); | |
205 | ||
206 | return body; | |
207 | } | |
208 | ||
209 | /** | |
210 | * Creates a new expression statement for the method invocation. | |
211 | * | |
212 | * @param invocation the method invocation | |
213 | * @return the corresponding statement | |
214 | */ | |
215 | private ExpressionStatement createExpressionStatement(final MethodInvocation invocation) { | |
216 | Assert.isNotNull(invocation); | |
217 | return invocation.getAST().newExpressionStatement(invocation); | |
218 | } | |
219 | ||
220 | /** | |
221 | * Creates a new return statement for the method invocation. | |
222 | * | |
223 | * @param invocation the method invocation to create a return statement for | |
224 | * @return the corresponding statement | |
225 | */ | |
226 | private ReturnStatement createReturnStatement(final MethodInvocation invocation) { | |
227 | Assert.isNotNull(invocation); | |
228 | final ReturnStatement statement= invocation.getAST().newReturnStatement(); | |
229 | statement.setExpression(invocation); | |
230 | return statement; | |
231 | } | |
232 | ||
233 | @Override | |
234 | protected String getTextEditGroupLabel() { | |
235 | return RefactoringCoreMessages.DelegateMethodCreator_text_edit_group_field; | |
236 | } | |
237 | ||
238 | public void generated_4358444421719430250(CompilationUnitRewrite rewrite, RenameNonVirtualMethodProcessor renamenonvirtualmethodprocessor) throws JavaModelException { | |
239 | setDeclareDeprecated(renamenonvirtualmethodprocessor.getDeprecateDelegates()); | |
240 | setSourceRewrite(rewrite); | |
241 | setCopy(true); | |
242 | setNewElementName(renamenonvirtualmethodprocessor.getNewElementName()); | |
243 | prepareDelegate(); | |
244 | createEdit(); | |
245 | } | |
246 | ||
247 | public void generated_2813910481060680587(MoveStaticMembersProcessor movestaticmembersprocessor) throws JavaModelException { | |
248 | setSourceRewrite(movestaticmembersprocessor.fSource); | |
249 | setCopy(false); | |
250 | setNewLocation(movestaticmembersprocessor.getDestinationBinding()); | |
251 | prepareDelegate(); | |
252 | createEdit(); | |
253 | } | |
254 | ||
255 | public void generated_7071724438348472777(DeclarationUpdate declarationupdate) throws JavaModelException { | |
256 | setSourceRewrite(fCuRewrite); | |
257 | prepareDelegate(); | |
258 | } | |
259 | } |