KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > delegates > DelegateMethodCreator


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.delegates;
12
13 import java.util.List JavaDoc;
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.ExpressionStatement;
24 import org.eclipse.jdt.core.dom.IBinding;
25 import org.eclipse.jdt.core.dom.MethodDeclaration;
26 import org.eclipse.jdt.core.dom.MethodInvocation;
27 import org.eclipse.jdt.core.dom.MethodRef;
28 import org.eclipse.jdt.core.dom.MethodRefParameter;
29 import org.eclipse.jdt.core.dom.PrimitiveType;
30 import org.eclipse.jdt.core.dom.ReturnStatement;
31 import org.eclipse.jdt.core.dom.SimpleName;
32 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
33 import org.eclipse.jdt.core.dom.Statement;
34 import org.eclipse.jdt.core.dom.Type;
35
36 import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
37 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
38
39 /**
40  * Delegate creator for static and non-static methods.
41  *
42  * @since 3.2
43  */

44 public class DelegateMethodCreator extends DelegateCreator {
45
46     private ASTNode fDelegateInvocation;
47     private MethodRef fDocMethodReference;
48
49     protected void initialize() {
50
51         Assert.isTrue(getDeclaration() instanceof MethodDeclaration);
52
53         if (getNewElementName() == null)
54             setNewElementName(((MethodDeclaration) getDeclaration()).getName().getIdentifier());
55         
56         setInsertBefore(true);
57     }
58
59     protected ASTNode createBody(BodyDeclaration bd) throws JavaModelException {
60
61         MethodDeclaration methodDeclaration= (MethodDeclaration) bd;
62
63         // interface or abstract method ? => don't create a method body.
64
if (methodDeclaration.getBody() == null)
65             return null;
66
67         return createDelegateMethodBody(methodDeclaration);
68     }
69
70     protected ASTNode createDocReference(final BodyDeclaration declaration) throws JavaModelException {
71         fDocMethodReference= getAst().newMethodRef();
72         fDocMethodReference.setName(getAst().newSimpleName(getNewElementName()));
73         if (isMoveToAnotherFile())
74             fDocMethodReference.setQualifier(createDestinationTypeName());
75         createArguments((MethodDeclaration) declaration, fDocMethodReference.parameters(), false);
76         return fDocMethodReference;
77     }
78
79     protected ASTNode getBodyHead(BodyDeclaration result) {
80         return result;
81     }
82
83     protected ChildPropertyDescriptor getJavaDocProperty() {
84         return MethodDeclaration.JAVADOC_PROPERTY;
85     }
86
87     protected ChildPropertyDescriptor getBodyProperty() {
88         return MethodDeclaration.BODY_PROPERTY;
89     }
90
91     /**
92      * @return the delegate incovation, either a {@link ConstructorInvocation}
93      * or a {@link MethodInvocation}. May be null if the delegate
94      * method is abstract (and therefore has no body at all)
95      */

96     public ASTNode getDelegateInvocation() {
97         return fDelegateInvocation;
98     }
99
100     /**
101      * @return the javadoc reference to the old method in the javadoc comment.
102      * May be null if no comment was created.
103      */

104     public MethodRef getJavadocReference() {
105         return fDocMethodReference;
106     }
107
108     /**
109      * Creates the corresponding statement for the method invocation, based on
110      * the return type.
111      *
112      * @param declaration the method declaration where the invocation statement
113      * is inserted
114      * @param invocation the method invocation being encapsulated by the
115      * resulting statement
116      * @return the corresponding statement
117      */

118     protected Statement createMethodInvocation(final MethodDeclaration declaration, final MethodInvocation invocation) {
119         Assert.isNotNull(declaration);
120         Assert.isNotNull(invocation);
121         Statement statement= null;
122         final Type type= declaration.getReturnType2();
123         if (type == null)
124             statement= createExpressionStatement(invocation);
125         else {
126             if (type instanceof PrimitiveType) {
127                 final PrimitiveType primitive= (PrimitiveType) type;
128                 if (primitive.getPrimitiveTypeCode().equals(PrimitiveType.VOID))
129                     statement= createExpressionStatement(invocation);
130                 else
131                     statement= createReturnStatement(invocation);
132             } else
133                 statement= createReturnStatement(invocation);
134         }
135         return statement;
136     }
137
138     /**
139      * {@inheritDoc}
140      */

141     protected IBinding getDeclarationBinding() {
142         final MethodDeclaration declaration= (MethodDeclaration) getDeclaration();
143         return declaration.resolveBinding();
144     }
145
146     private void createArguments(final MethodDeclaration declaration, final List JavaDoc arguments, boolean methodInvocation) throws JavaModelException {
147         Assert.isNotNull(declaration);
148         Assert.isNotNull(arguments);
149         SingleVariableDeclaration variable= null;
150         final int size= declaration.parameters().size();
151         for (int index= 0; index < size; index++) {
152             variable= (SingleVariableDeclaration) declaration.parameters().get(index);
153
154             if (methodInvocation) {
155                 // we are creating method invocation parameters
156
final SimpleName expression= getAst().newSimpleName(variable.getName().getIdentifier());
157                 arguments.add(expression);
158             } else {
159                 // we are creating type info for the javadoc
160
final MethodRefParameter parameter= getAst().newMethodRefParameter();
161                 parameter.setType(ASTNodeFactory.newType(getAst(), variable));
162                 if ((index == size - 1) && declaration.isVarargs())
163                     parameter.setVarargs(true);
164                 arguments.add(parameter);
165             }
166         }
167     }
168
169     private Block createDelegateMethodBody(final MethodDeclaration declaration) throws JavaModelException {
170         Assert.isNotNull(declaration);
171
172         MethodDeclaration old= (MethodDeclaration) getDeclaration();
173         List JavaDoc arguments;
174         Statement call;
175         if (old.isConstructor()) {
176             ConstructorInvocation invocation= getAst().newConstructorInvocation();
177             arguments= invocation.arguments();
178             call= invocation;
179             fDelegateInvocation= invocation;
180         } else {
181             MethodInvocation invocation= getAst().newMethodInvocation();
182             invocation.setName(getAst().newSimpleName(getNewElementName()));
183             invocation.setExpression(getAccess());
184             arguments= invocation.arguments();
185             call= createMethodInvocation(declaration, invocation);
186             fDelegateInvocation= invocation;
187         }
188         createArguments(declaration, arguments, true);
189
190         final Block body= getAst().newBlock();
191         body.statements().add(call);
192
193         return body;
194     }
195
196     /**
197      * Creates a new expression statement for the method invocation.
198      *
199      * @param invocation the method invocation
200      * @return the corresponding statement
201      */

202     private ExpressionStatement createExpressionStatement(final MethodInvocation invocation) {
203         Assert.isNotNull(invocation);
204         return invocation.getAST().newExpressionStatement(invocation);
205     }
206
207     /**
208      * Creates a new return statement for the method invocation.
209      *
210      * @param invocation the method invocation to create a return statement for
211      * @return the corresponding statement
212      */

213     private ReturnStatement createReturnStatement(final MethodInvocation invocation) {
214         Assert.isNotNull(invocation);
215         final ReturnStatement statement= invocation.getAST().newReturnStatement();
216         statement.setExpression(invocation);
217         return statement;
218     }
219
220     protected String JavaDoc getTextEditGroupLabel() {
221         return RefactoringCoreMessages.DelegateMethodCreator_text_edit_group_field;
222     }
223 }
224
Popular Tags