]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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.proposals; | |
12 | ||
13 | import java.util.List; | |
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.IExtendedModifier; | |
24 | import org.eclipse.jdt.core.dom.IMethodBinding; | |
25 | import org.eclipse.jdt.core.dom.ITypeBinding; | |
26 | import org.eclipse.jdt.core.dom.Modifier; | |
27 | import org.eclipse.jdt.core.dom.Name; | |
28 | import org.eclipse.jdt.core.dom.SimpleName; | |
29 | import org.eclipse.jdt.core.dom.SingleVariableDeclaration; | |
30 | import org.eclipse.jdt.core.dom.Type; | |
31 | import org.eclipse.jdt.core.dom.TypeParameter; | |
32 | import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; | |
33 | import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; | |
34 | ||
35 | import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; | |
36 | import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory; | |
37 | ||
38 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
39 | import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider; | |
40 | ||
41 | public class NewDefiningMethodProposal extends AbstractMethodCorrectionProposal { | |
42 | ||
43 | private final IMethodBinding fMethod; | |
44 | private final String[] fParamNames; | |
45 | ||
46 | public NewDefiningMethodProposal(String label, ICompilationUnit targetCU, ASTNode invocationNode, ITypeBinding binding, IMethodBinding method, String[] paramNames, int relevance) { | |
47 | super(label,targetCU,invocationNode,binding,relevance,null); | |
48 | fMethod= method; | |
49 | fParamNames= paramNames; | |
50 | ||
51 | ImageDescriptor desc= JavaElementImageProvider.getMethodImageDescriptor(binding.isInterface() || binding.isAnnotation(), method.getModifiers()); | |
52 | setImage(JavaPlugin.getImageDescriptorRegistry().get(desc)); | |
53 | } | |
54 | ||
55 | /* (non-Javadoc) | |
56 | * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#isConstructor() | |
57 | */ | |
58 | @Override | |
59 | protected boolean isConstructor() { | |
60 | return fMethod.isConstructor(); | |
61 | } | |
62 | ||
63 | /* (non-Javadoc) | |
64 | * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#addNewParameters(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List, java.util.List) | |
65 | */ | |
66 | @Override | |
67 | protected void addNewParameters(ASTRewrite rewrite, List<String> takenNames, List<SingleVariableDeclaration> params) throws CoreException { | |
68 | AST ast= rewrite.getAST(); | |
69 | ImportRewrite importRewrite= getImportRewrite(); | |
70 | ITypeBinding[] bindings= fMethod.getParameterTypes(); | |
71 | ||
72 | IJavaProject project= getCompilationUnit().getJavaProject(); | |
73 | String[][] paramNames= StubUtility.suggestArgumentNamesWithProposals(project, fParamNames); | |
74 | ||
75 | for (int i= 0; i < bindings.length; i++) { | |
76 | ITypeBinding curr= bindings[i]; | |
77 | ||
78 | String[] proposedNames= paramNames[i]; | |
79 | ||
80 | SingleVariableDeclaration newParam= ast.newSingleVariableDeclaration(); | |
81 | ||
82 | newParam.setType(importRewrite.addImport(curr, ast)); | |
83 | newParam.setName(ast.newSimpleName(proposedNames[0])); | |
84 | ||
85 | params.add(newParam); | |
86 | ||
87 | String groupId= "arg_name_" + i; //$NON-NLS-1$ | |
88 | addLinkedPosition(rewrite.track(newParam.getName()), false, groupId); | |
89 | ||
90 | for (int k= 0; k < proposedNames.length; k++) { | |
91 | addLinkedPositionProposal(groupId, proposedNames[k], null); | |
92 | } | |
93 | } | |
94 | } | |
95 | ||
96 | /* (non-Javadoc) | |
97 | * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#getNewName(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) | |
98 | */ | |
99 | @Override | |
100 | protected SimpleName getNewName(ASTRewrite rewrite) { | |
101 | AST ast= rewrite.getAST(); | |
102 | SimpleName nameNode= ast.newSimpleName(fMethod.getName()); | |
103 | return nameNode; | |
104 | } | |
105 | ||
106 | private int evaluateModifiers() { | |
107 | if (getSenderBinding().isInterface()) { | |
108 | return 0; | |
109 | } else { | |
110 | int modifiers= fMethod.getModifiers(); | |
111 | if (Modifier.isPrivate(modifiers)) { | |
112 | modifiers |= Modifier.PROTECTED; | |
113 | } | |
114 | return modifiers & (Modifier.PUBLIC | Modifier.PROTECTED | Modifier.ABSTRACT | Modifier.STRICTFP); | |
115 | } | |
116 | } | |
117 | ||
118 | /* (non-Javadoc) | |
119 | * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#addNewModifiers(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, org.eclipse.jdt.core.dom.ASTNode, java.util.List) | |
120 | */ | |
121 | @Override | |
122 | protected void addNewModifiers(ASTRewrite rewrite, ASTNode targetTypeDecl, List<IExtendedModifier> modifiers) { | |
123 | modifiers.addAll(rewrite.getAST().newModifiers(evaluateModifiers())); | |
124 | } | |
125 | ||
126 | /* (non-Javadoc) | |
127 | * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#getNewMethodType(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) | |
128 | */ | |
129 | @Override | |
130 | protected Type getNewMethodType(ASTRewrite rewrite) throws CoreException { | |
131 | return getImportRewrite().addImport(fMethod.getReturnType(), rewrite.getAST()); | |
132 | } | |
133 | ||
134 | /* (non-Javadoc) | |
135 | * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#addNewExceptions(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List) | |
136 | */ | |
137 | @Override | |
138 | protected void addNewExceptions(ASTRewrite rewrite, List<Name> exceptions) throws CoreException { | |
139 | AST ast= rewrite.getAST(); | |
140 | ImportRewrite importRewrite= getImportRewrite(); | |
141 | ITypeBinding[] bindings= fMethod.getExceptionTypes(); | |
142 | for (int i= 0; i < bindings.length; i++) { | |
143 | String typeName= importRewrite.addImport(bindings[i]); | |
144 | Name newNode= ASTNodeFactory.newName(ast, typeName); | |
145 | exceptions.add(newNode); | |
146 | ||
147 | addLinkedPosition(rewrite.track(newNode), false, "exc_type_" + i); //$NON-NLS-1$ | |
148 | } | |
149 | } | |
150 | ||
151 | /* (non-Javadoc) | |
152 | * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#addNewTypeParameters(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List, java.util.List) | |
153 | */ | |
154 | @Override | |
155 | protected void addNewTypeParameters(ASTRewrite rewrite, List<String> takenNames, List<TypeParameter> params) throws CoreException { | |
156 | ||
157 | } | |
158 | ||
159 | } |