KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.text.correction;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18
19 import org.eclipse.jdt.core.ICompilationUnit;
20 import org.eclipse.jdt.core.IJavaProject;
21 import org.eclipse.jdt.core.dom.AST;
22 import org.eclipse.jdt.core.dom.ASTNode;
23 import org.eclipse.jdt.core.dom.IMethodBinding;
24 import org.eclipse.jdt.core.dom.ITypeBinding;
25 import org.eclipse.jdt.core.dom.Modifier;
26 import org.eclipse.jdt.core.dom.Name;
27 import org.eclipse.jdt.core.dom.SimpleName;
28 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
29 import org.eclipse.jdt.core.dom.Type;
30 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
31 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
32
33 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
34 import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
35
36 import org.eclipse.jdt.internal.ui.JavaPlugin;
37 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
38
39 /**
40  *
41  */

42 public class NewDefiningMethodProposal extends AbstractMethodCompletionProposal {
43
44     private final IMethodBinding fMethod;
45     private final String JavaDoc[] fParamNames;
46
47     public NewDefiningMethodProposal(String JavaDoc label, ICompilationUnit targetCU, ASTNode invocationNode, ITypeBinding binding, IMethodBinding method, String JavaDoc[] paramNames, int relevance) {
48         super(label,targetCU,invocationNode,binding,relevance,null);
49         fMethod= method;
50         fParamNames= paramNames;
51
52         ImageDescriptor desc= JavaElementImageProvider.getMethodImageDescriptor(binding.isInterface() || binding.isAnnotation(), method.getModifiers());
53         setImage(JavaPlugin.getImageDescriptorRegistry().get(desc));
54     }
55
56     /* (non-Javadoc)
57      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#isConstructor()
58      */

59     protected boolean isConstructor() {
60         return fMethod.isConstructor();
61     }
62
63     /* (non-Javadoc)
64      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#addNewParameters(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List, java.util.List)
65      */

66     protected void addNewParameters(ASTRewrite rewrite, List JavaDoc takenNames, List JavaDoc params) throws CoreException {
67         AST ast= rewrite.getAST();
68         ImportRewrite importRewrite= getImportRewrite();
69         ITypeBinding[] bindings= fMethod.getParameterTypes();
70
71         IJavaProject project= getCompilationUnit().getJavaProject();
72         String JavaDoc[][] paramNames= StubUtility.suggestArgumentNamesWithProposals(project, fParamNames);
73
74         for (int i= 0; i < bindings.length; i++) {
75             ITypeBinding curr= bindings[i];
76
77             String JavaDoc[] proposedNames= paramNames[i];
78             
79             SingleVariableDeclaration newParam= ast.newSingleVariableDeclaration();
80
81             newParam.setType(importRewrite.addImport(curr, ast));
82             newParam.setName(ast.newSimpleName(proposedNames[0]));
83
84             params.add(newParam);
85
86             String JavaDoc groupId= "arg_name_" + i; //$NON-NLS-1$
87
addLinkedPosition(rewrite.track(newParam.getName()), false, groupId);
88             
89             for (int k= 0; k < proposedNames.length; k++) {
90                 addLinkedPositionProposal(groupId, proposedNames[k], null);
91             }
92         }
93     }
94
95     /* (non-Javadoc)
96      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#getNewName()
97      */

98     protected SimpleName getNewName(ASTRewrite rewrite) {
99         AST ast= rewrite.getAST();
100         SimpleName nameNode= ast.newSimpleName(fMethod.getName());
101         return nameNode;
102     }
103
104     private int evaluateModifiers() {
105         if (getSenderBinding().isInterface()) {
106             return 0;
107         } else {
108             int modifiers= fMethod.getModifiers();
109             if (Modifier.isPrivate(modifiers)) {
110                 modifiers |= Modifier.PROTECTED;
111             }
112             return modifiers & (Modifier.PUBLIC | Modifier.PROTECTED | Modifier.ABSTRACT | Modifier.STRICTFP);
113         }
114     }
115     
116     /* (non-Javadoc)
117      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#addNewModifiers(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List)
118      */

119     protected void addNewModifiers(ASTRewrite rewrite, ASTNode targetTypeDecl, List JavaDoc modifiers) {
120         modifiers.addAll(rewrite.getAST().newModifiers(evaluateModifiers()));
121     }
122
123     /* (non-Javadoc)
124      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#getNewMethodType(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)
125      */

126     protected Type getNewMethodType(ASTRewrite rewrite) throws CoreException {
127         return getImportRewrite().addImport(fMethod.getReturnType(), rewrite.getAST());
128     }
129
130     /* (non-Javadoc)
131      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#addNewExceptions(org.eclipse.jdt.core.dom.AST, java.util.List)
132      */

133     protected void addNewExceptions(ASTRewrite rewrite, List JavaDoc exceptions) throws CoreException {
134         AST ast= rewrite.getAST();
135         ImportRewrite importRewrite= getImportRewrite();
136         ITypeBinding[] bindings= fMethod.getExceptionTypes();
137         for (int i= 0; i < bindings.length; i++) {
138             String JavaDoc typeName= importRewrite.addImport(bindings[i]);
139             Name newNode= ASTNodeFactory.newName(ast, typeName);
140             exceptions.add(newNode);
141
142             addLinkedPosition(rewrite.track(newNode), false, "exc_type_" + i); //$NON-NLS-1$
143
}
144     }
145
146     /* (non-Javadoc)
147      * @see org.eclipse.jdt.internal.ui.text.correction.AbstractMethodCompletionProposal#addNewTypeParameters(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List, java.util.List)
148      */

149     protected void addNewTypeParameters(ASTRewrite rewrite, List JavaDoc takenNames, List JavaDoc params) throws CoreException {
150
151     }
152
153 }
154
Popular Tags