1 11 12 package org.eclipse.jdt.internal.ui.text.correction; 13 14 import java.util.List ; 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 KEY_NAME= "name"; private static final String KEY_TYPE= "type"; 51 private List fArguments; 52 53 public NewMethodCompletionProposal(String label, ICompilationUnit targetCU, ASTNode invocationNode, List 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 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 101 protected void addNewModifiers(ASTRewrite rewrite, ASTNode targetTypeDecl, List modifiers) { 102 modifiers.addAll(rewrite.getAST().newModifiers(evaluateModifiers(targetTypeDecl))); 103 ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(getLinkedProposalModel(), rewrite, modifiers, getSenderBinding().isInterface()); 104 } 105 106 107 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 119 protected SimpleName getNewName(ASTRewrite rewrite) { 120 ASTNode invocationNode= getInvocationNode(); 121 String 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(); } 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) { 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 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")); } 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 208 protected void addNewParameters(ASTRewrite rewrite, List takenNames, List params) throws CoreException { 209 AST ast= rewrite.getAST(); 210 211 List 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 String argTypeKey= "arg_type_" + i; Type type= evaluateParameterType(ast, elem, argTypeKey); 220 param.setType(type); 221 222 String argNameKey= "arg_name_" + i; String 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 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")); } 248 249 private String evaluateParameterName(List takenNames, Expression argNode, Type type, String key) { 250 IJavaProject project= getCompilationUnit().getJavaProject(); 251 String [] 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 favourite= names[0]; 256 takenNames.add(favourite); 257 return favourite; 258 } 259 260 263 protected void addNewExceptions(ASTRewrite rewrite, List exceptions) throws CoreException { 264 } 265 266 269 protected void addNewTypeParameters(ASTRewrite rewrite, List takenNames, List params) throws CoreException { 270 } 271 272 273 } 274 | Popular Tags |