KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > NewMethodCompletionProposal


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
12 package org.eclipse.jdt.internal.ui.text.correction;
13
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17
18 import org.eclipse.swt.graphics.Image;
19
20 import org.eclipse.jdt.core.ICompilationUnit;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.dom.AST;
23 import org.eclipse.jdt.core.dom.ASTNode;
24 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
25 import org.eclipse.jdt.core.dom.ClassInstanceCreation;
26 import org.eclipse.jdt.core.dom.Expression;
27 import org.eclipse.jdt.core.dom.ExpressionStatement;
28 import org.eclipse.jdt.core.dom.IBinding;
29 import org.eclipse.jdt.core.dom.ITypeBinding;
30 import org.eclipse.jdt.core.dom.MethodDeclaration;
31 import org.eclipse.jdt.core.dom.MethodInvocation;
32 import org.eclipse.jdt.core.dom.Modifier;
33 import org.eclipse.jdt.core.dom.Name;
34 import org.eclipse.jdt.core.dom.ParameterizedType;
35 import org.eclipse.jdt.core.dom.SimpleName;
36 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
37 import org.eclipse.jdt.core.dom.SuperMethodInvocation;
38 import org.eclipse.jdt.core.dom.Type;
39 import org.eclipse.jdt.core.dom.TypeDeclaration;
40 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
41
42 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
43 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
44 import org.eclipse.jdt.internal.corext.dom.Bindings;
45
46 public class NewMethodCompletionProposal extends AbstractMethodCompletionProposal {
47
48     private static final String JavaDoc KEY_NAME= "name"; //$NON-NLS-1$
49
private static final String JavaDoc KEY_TYPE= "type"; //$NON-NLS-1$
50

51     private List JavaDoc fArguments;
52
53     // invocationNode is MethodInvocation, ConstructorInvocation, SuperConstructorInvocation, ClassInstanceCreation, SuperMethodInvocation
54
public NewMethodCompletionProposal(String JavaDoc label, ICompilationUnit targetCU, ASTNode invocationNode, List JavaDoc arguments, ITypeBinding binding, int relevance, Image image) {
55         super(label, targetCU, invocationNode, binding, relevance, image);
56         fArguments= arguments;
57     }
58
59     private int evaluateModifiers(ASTNode targetTypeDecl) {
60         if (getSenderBinding().isAnnotation()) {
61             return 0;
62         }
63         if (getSenderBinding().isInterface()) {
64             // for interface and annotation members copy the modifiers from an existing field
65
MethodDeclaration[] methodDecls= ((TypeDeclaration) targetTypeDecl).getMethods();
66             if (methodDecls.length > 0) {
67                 return methodDecls[0].getModifiers();
68             }
69             return 0;
70         }
71         ASTNode invocationNode= getInvocationNode();
72         if (invocationNode instanceof MethodInvocation) {
73             int modifiers= 0;
74             Expression expression= ((MethodInvocation)invocationNode).getExpression();
75             if (expression != null) {
76                 if (expression instanceof Name && ((Name) expression).resolveBinding().getKind() == IBinding.TYPE) {
77                     modifiers |= Modifier.STATIC;
78                 }
79             } else if (ASTResolving.isInStaticContext(invocationNode)) {
80                 modifiers |= Modifier.STATIC;
81             }
82             ASTNode node= ASTResolving.findParentType(invocationNode);
83             if (targetTypeDecl.equals(node)) {
84                 modifiers |= Modifier.PRIVATE;
85             } else if (node instanceof AnonymousClassDeclaration && ASTNodes.isParent(node, targetTypeDecl)) {
86                 modifiers |= Modifier.PROTECTED;
87                 if (ASTResolving.isInStaticContext(node)) {
88                     modifiers |= Modifier.STATIC;
89                 }
90             } else {
91                 modifiers |= Modifier.PUBLIC;
92             }
93             return modifiers;
94         }
95         return Modifier.PUBLIC;
96     }
97     
98     /* (non-Javadoc)
99      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#addNewModifiers(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List)
100      */

101     protected void addNewModifiers(ASTRewrite rewrite, ASTNode targetTypeDecl, List JavaDoc modifiers) {
102         modifiers.addAll(rewrite.getAST().newModifiers(evaluateModifiers(targetTypeDecl)));
103         ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(getLinkedProposalModel(), rewrite, modifiers, getSenderBinding().isInterface());
104     }
105
106
107     /*(non-Javadoc)
108      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#isConstructor()
109      */

110     protected boolean isConstructor() {
111         ASTNode node= getInvocationNode();
112
113         return node.getNodeType() != ASTNode.METHOD_INVOCATION && node.getNodeType() != ASTNode.SUPER_METHOD_INVOCATION;
114     }
115
116     /* (non-Javadoc)
117      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#getNewName(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)
118      */

119     protected SimpleName getNewName(ASTRewrite rewrite) {
120         ASTNode invocationNode= getInvocationNode();
121         String JavaDoc name;
122         if (invocationNode instanceof MethodInvocation) {
123             name= ((MethodInvocation)invocationNode).getName().getIdentifier();
124         } else if (invocationNode instanceof SuperMethodInvocation) {
125             name= ((SuperMethodInvocation)invocationNode).getName().getIdentifier();
126         } else {
127             name= getSenderBinding().getName(); // name of the class
128
}
129         AST ast= rewrite.getAST();
130         SimpleName newNameNode= ast.newSimpleName(name);
131         addLinkedPosition(rewrite.track(newNameNode), false, KEY_NAME);
132
133         ASTNode invocationName= getInvocationNameNode();
134         if (invocationName != null && invocationName.getAST() == ast) { // in the same CU
135
addLinkedPosition(rewrite.track(invocationName), true, KEY_NAME);
136         }
137         return newNameNode;
138     }
139
140     private ASTNode getInvocationNameNode() {
141         ASTNode node= getInvocationNode();
142         if (node instanceof MethodInvocation) {
143             return ((MethodInvocation)node).getName();
144         } else if (node instanceof SuperMethodInvocation) {
145             return ((SuperMethodInvocation)node).getName();
146         } else if (node instanceof ClassInstanceCreation) {
147             Type type= ((ClassInstanceCreation)node).getType();
148             while (type instanceof ParameterizedType) {
149                 type= ((ParameterizedType) type).getType();
150             }
151             return type;
152         }
153         return null;
154     }
155
156     /* (non-Javadoc)
157      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#getNewMethodType(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)
158      */

159     protected Type getNewMethodType(ASTRewrite rewrite) throws CoreException {
160         ASTNode node= getInvocationNode();
161         AST ast= rewrite.getAST();
162
163         Type newTypeNode= null;
164         ITypeBinding[] otherProposals= null;
165
166         if (node.getParent() instanceof MethodInvocation) {
167             MethodInvocation parent= (MethodInvocation) node.getParent();
168             if (parent.getExpression() == node) {
169                 ITypeBinding[] bindings= ASTResolving.getQualifierGuess(node.getRoot(), parent.getName().getIdentifier(), parent.arguments(), getSenderBinding());
170                 if (bindings.length > 0) {
171                     newTypeNode= getImportRewrite().addImport(bindings[0], ast);
172                     otherProposals= bindings;
173                 }
174             }
175         }
176         if (newTypeNode == null) {
177             ITypeBinding binding= ASTResolving.guessBindingForReference(node);
178             if (binding != null && binding.isWildcardType()) {
179                 binding= ASTResolving.normalizeWildcardType(binding, false, ast);
180             }
181             if (binding != null) {
182                 newTypeNode= getImportRewrite().addImport(binding, ast);
183             } else {
184                 ASTNode parent= node.getParent();
185                 if (parent instanceof ExpressionStatement) {
186                     return null;
187                 }
188                 newTypeNode= ASTResolving.guessTypeForReference(ast, node);
189                 if (newTypeNode == null) {
190                     newTypeNode= ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
191
}
192             }
193         }
194
195         addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
196         if (otherProposals != null) {
197             for (int i= 0; i < otherProposals.length; i++) {
198                 addLinkedPositionProposal(KEY_TYPE, otherProposals[i]);
199             }
200         }
201
202         return newTypeNode;
203     }
204
205     /* (non-Javadoc)
206      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#addNewParameters(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List, java.util.List)
207      */

208     protected void addNewParameters(ASTRewrite rewrite, List JavaDoc takenNames, List JavaDoc params) throws CoreException {
209         AST ast= rewrite.getAST();
210
211         List JavaDoc arguments= fArguments;
212
213         for (int i= 0; i < arguments.size(); i++) {
214             Expression elem= (Expression) arguments.get(i);
215             SingleVariableDeclaration param= ast.newSingleVariableDeclaration();
216
217             // argument type
218
String JavaDoc argTypeKey= "arg_type_" + i; //$NON-NLS-1$
219
Type type= evaluateParameterType(ast, elem, argTypeKey);
220             param.setType(type);
221
222             // argument name
223
String JavaDoc argNameKey= "arg_name_" + i; //$NON-NLS-1$
224
String JavaDoc name= evaluateParameterName(takenNames, elem, type, argNameKey);
225             param.setName(ast.newSimpleName(name));
226
227             params.add(param);
228
229             addLinkedPosition(rewrite.track(param.getType()), false, argTypeKey);
230             addLinkedPosition(rewrite.track(param.getName()), false, argNameKey);
231         }
232     }
233
234     private Type evaluateParameterType(AST ast, Expression elem, String JavaDoc key) throws CoreException {
235         ITypeBinding binding= Bindings.normalizeTypeBinding(elem.resolveTypeBinding());
236         if (binding != null && binding.isWildcardType()) {
237             binding= ASTResolving.normalizeWildcardType(binding, true, ast);
238         }
239         if (binding != null) {
240             ITypeBinding[] typeProposals= ASTResolving.getRelaxingTypes(ast, binding);
241             for (int i= 0; i < typeProposals.length; i++) {
242                 addLinkedPositionProposal(key, typeProposals[i]);
243             }
244             return getImportRewrite().addImport(binding, ast);
245         }
246         return ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
247
}
248
249     private String JavaDoc evaluateParameterName(List JavaDoc takenNames, Expression argNode, Type type, String JavaDoc key) {
250         IJavaProject project= getCompilationUnit().getJavaProject();
251         String JavaDoc[] names= StubUtility.getVariableNameSuggestions(StubUtility.PARAMETER, project, type, argNode, takenNames);
252         for (int i= 0; i < names.length; i++) {
253             addLinkedPositionProposal(key, names[i], null);
254         }
255         String JavaDoc favourite= names[0];
256         takenNames.add(favourite);
257         return favourite;
258     }
259
260     /* (non-Javadoc)
261      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#addNewExceptions(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List)
262      */

263     protected void addNewExceptions(ASTRewrite rewrite, List JavaDoc exceptions) throws CoreException {
264     }
265
266     /* (non-Javadoc)
267      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#addNewTypeParameters(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List, java.util.List)
268      */

269     protected void addNewTypeParameters(ASTRewrite rewrite, List JavaDoc takenNames, List JavaDoc params) throws CoreException {
270     }
271
272
273 }
274
Popular Tags