1 11 package org.eclipse.jdt.internal.ui.text.correction; 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.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 42 public class NewDefiningMethodProposal extends AbstractMethodCompletionProposal { 43 44 private final IMethodBinding fMethod; 45 private final String [] fParamNames; 46 47 public NewDefiningMethodProposal(String label, ICompilationUnit targetCU, ASTNode invocationNode, ITypeBinding binding, IMethodBinding method, String [] 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 59 protected boolean isConstructor() { 60 return fMethod.isConstructor(); 61 } 62 63 66 protected void addNewParameters(ASTRewrite rewrite, List takenNames, List 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 [][] paramNames= StubUtility.suggestArgumentNamesWithProposals(project, fParamNames); 73 74 for (int i= 0; i < bindings.length; i++) { 75 ITypeBinding curr= bindings[i]; 76 77 String [] 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 groupId= "arg_name_" + i; 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 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 119 protected void addNewModifiers(ASTRewrite rewrite, ASTNode targetTypeDecl, List modifiers) { 120 modifiers.addAll(rewrite.getAST().newModifiers(evaluateModifiers())); 121 } 122 123 126 protected Type getNewMethodType(ASTRewrite rewrite) throws CoreException { 127 return getImportRewrite().addImport(fMethod.getReturnType(), rewrite.getAST()); 128 } 129 130 133 protected void addNewExceptions(ASTRewrite rewrite, List 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 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); } 144 } 145 146 149 protected void addNewTypeParameters(ASTRewrite rewrite, List takenNames, List params) throws CoreException { 150 151 } 152 153 } 154 | Popular Tags |