KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > CreateMethodOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.core;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
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 /**
33  * <p>This operation creates an instance method.
34  *
35  * <p>Required Attributes:<ul>
36  * <li>Containing type
37  * <li>The source code for the method. No verification of the source is
38  * performed.
39  * </ul>
40  */

41 public class CreateMethodOperation extends CreateTypeMemberOperation {
42     
43     protected String JavaDoc[] parameterTypes;
44     
45 /**
46  * When executed, this operation will create a method
47  * in the given type with the specified source.
48  */

49 public CreateMethodOperation(IType parentElement, String JavaDoc source, boolean force) {
50     super(parentElement, source, force);
51 }
52 /**
53  * Returns the type signatures of the parameter types of the
54  * current <code>MethodDeclaration</code>
55  */

56 protected String JavaDoc[] convertASTMethodTypesToSignatures() {
57     if (this.parameterTypes == null) {
58         if (this.createdNode != null) {
59             MethodDeclaration methodDeclaration = (MethodDeclaration) this.createdNode;
60             List JavaDoc parameters = methodDeclaration.parameters();
61             int size = parameters.size();
62             this.parameterTypes = new String JavaDoc[size];
63             Iterator JavaDoc iterator = parameters.iterator();
64             // convert the AST types to signatures
65
for (int i = 0; i < size; i++) {
66                 SingleVariableDeclaration parameter = (SingleVariableDeclaration) iterator.next();
67                 String JavaDoc 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 /**
84  * @see CreateElementInCUOperation#generateResultHandle
85  */

86 protected IJavaElement generateResultHandle() {
87     String JavaDoc[] types = convertASTMethodTypesToSignatures();
88     String JavaDoc name = getASTNodeName();
89     return getType().getMethod(name, types);
90 }
91 private String JavaDoc getASTNodeName() {
92     return ((MethodDeclaration) this.createdNode).getName().getIdentifier();
93 }
94 /**
95  * @see CreateElementInCUOperation#getMainTaskName()
96  */

97 public String JavaDoc 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 /**
107  * @see CreateTypeMemberOperation#verifyNameCollision
108  */

109 protected IJavaModelStatus verifyNameCollision() {
110     if (this.createdNode != null) {
111         IType type = getType();
112         String JavaDoc name;
113         if (((MethodDeclaration) this.createdNode).isConstructor())
114             name = type.getElementName();
115         else
116             name = getASTNodeName();
117         String JavaDoc[] 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