1 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.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 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 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 96 public ASTNode getDelegateInvocation() { 97 return fDelegateInvocation; 98 } 99 100 104 public MethodRef getJavadocReference() { 105 return fDocMethodReference; 106 } 107 108 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 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 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 final SimpleName expression= getAst().newSimpleName(variable.getName().getIdentifier()); 157 arguments.add(expression); 158 } else { 159 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 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 202 private ExpressionStatement createExpressionStatement(final MethodInvocation invocation) { 203 Assert.isNotNull(invocation); 204 return invocation.getAST().newExpressionStatement(invocation); 205 } 206 207 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 getTextEditGroupLabel() { 221 return RefactoringCoreMessages.DelegateMethodCreator_text_edit_group_field; 222 } 223 } 224 | Popular Tags |