1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import org.eclipse.jdt.core.ICompilationUnit; 17 import org.eclipse.jdt.core.IJavaElement; 18 import org.eclipse.jdt.core.IJavaModelStatus; 19 import org.eclipse.jdt.core.IJavaModelStatusConstants; 20 import org.eclipse.jdt.core.IType; 21 import org.eclipse.jdt.core.JavaModelException; 22 import org.eclipse.jdt.core.Signature; 23 import org.eclipse.jdt.core.dom.ASTNode; 24 import org.eclipse.jdt.core.dom.MethodDeclaration; 25 import org.eclipse.jdt.core.dom.SimpleName; 26 import org.eclipse.jdt.core.dom.SingleVariableDeclaration; 27 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 28 import org.eclipse.jdt.internal.core.util.Messages; 29 import org.eclipse.jdt.internal.core.util.Util; 30 import org.eclipse.jface.text.IDocument; 31 32 41 public class CreateMethodOperation extends CreateTypeMemberOperation { 42 43 protected String [] parameterTypes; 44 45 49 public CreateMethodOperation(IType parentElement, String source, boolean force) { 50 super(parentElement, source, force); 51 } 52 56 protected String [] convertASTMethodTypesToSignatures() { 57 if (this.parameterTypes == null) { 58 if (this.createdNode != null) { 59 MethodDeclaration methodDeclaration = (MethodDeclaration) this.createdNode; 60 List parameters = methodDeclaration.parameters(); 61 int size = parameters.size(); 62 this.parameterTypes = new String [size]; 63 Iterator iterator = parameters.iterator(); 64 for (int i = 0; i < size; i++) { 66 SingleVariableDeclaration parameter = (SingleVariableDeclaration) iterator.next(); 67 String typeSig = Util.getSignature(parameter.getType()); 68 int extraDimensions = parameter.getExtraDimensions(); 69 if (methodDeclaration.isVarargs() && i == size-1) 70 extraDimensions++; 71 this.parameterTypes[i] = Signature.createArraySignature(typeSig, extraDimensions); 72 } 73 } 74 } 75 return this.parameterTypes; 76 } 77 protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException { 78 ASTNode node = super.generateElementAST(rewriter, document, cu); 79 if (node.getNodeType() != ASTNode.METHOD_DECLARATION) 80 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS)); 81 return node; 82 } 83 86 protected IJavaElement generateResultHandle() { 87 String [] types = convertASTMethodTypesToSignatures(); 88 String name = getASTNodeName(); 89 return getType().getMethod(name, types); 90 } 91 private String getASTNodeName() { 92 return ((MethodDeclaration) this.createdNode).getName().getIdentifier(); 93 } 94 97 public String getMainTaskName(){ 98 return Messages.operation_createMethodProgress; 99 } 100 protected SimpleName rename(ASTNode node, SimpleName newName) { 101 MethodDeclaration method = (MethodDeclaration) node; 102 SimpleName oldName = method.getName(); 103 method.setName(newName); 104 return oldName; 105 } 106 109 protected IJavaModelStatus verifyNameCollision() { 110 if (this.createdNode != null) { 111 IType type = getType(); 112 String name; 113 if (((MethodDeclaration) this.createdNode).isConstructor()) 114 name = type.getElementName(); 115 else 116 name = getASTNodeName(); 117 String [] types = convertASTMethodTypesToSignatures(); 118 if (type.getMethod(name, types).exists()) { 119 return new JavaModelStatus( 120 IJavaModelStatusConstants.NAME_COLLISION, 121 Messages.bind(Messages.status_nameCollision, name)); 122 } 123 } 124 return JavaModelStatus.VERIFIED_OK; 125 } 126 } 127 | Popular Tags |