1 11 package org.eclipse.jdt.internal.ui.text.java; 12 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import org.eclipse.jdt.core.CompletionProposal; 18 import org.eclipse.jdt.core.IJavaProject; 19 import org.eclipse.jdt.core.IMember; 20 import org.eclipse.jdt.core.IMethod; 21 import org.eclipse.jdt.core.IType; 22 import org.eclipse.jdt.core.ITypeParameter; 23 import org.eclipse.jdt.core.JavaModelException; 24 import org.eclipse.jdt.core.Signature; 25 26 import org.eclipse.jdt.internal.corext.template.java.SignatureUtil; 27 28 29 34 public final class MethodProposalInfo extends MemberProposalInfo { 35 36 40 private IMethod fFallbackMatch; 41 42 48 public MethodProposalInfo(IJavaProject project, CompletionProposal proposal) { 49 super(project, proposal); 50 } 51 52 59 protected IMember resolveMember() throws JavaModelException { 60 char[] declarationSignature= fProposal.getDeclarationSignature(); 61 String typeName= SignatureUtil.stripSignatureToFQN(String.valueOf(declarationSignature)); 62 IType type= fJavaProject.findType(typeName); 63 if (type != null) { 64 String name= String.valueOf(fProposal.getName()); 65 String [] parameters= Signature.getParameterTypes(String.valueOf(SignatureUtil.fix83600(fProposal.getSignature()))); 66 for (int i= 0; i < parameters.length; i++) { 67 parameters[i]= SignatureUtil.getLowerBound(parameters[i]); 68 } 69 boolean isConstructor= fProposal.isConstructor(); 70 71 return findMethod(name, parameters, isConstructor, type); 72 } 73 74 return null; 75 } 76 77 78 79 91 private IMethod findMethod(String name, String [] paramTypes, boolean isConstructor, IType type) throws JavaModelException { 92 Map typeVariables= computeTypeVariables(type); 93 return findMethod(name, paramTypes, isConstructor, type.getMethods(), typeVariables); 94 } 95 96 114 private Map computeTypeVariables(IType type) throws JavaModelException { 115 Map map= new HashMap (); 116 char[] declarationSignature= fProposal.getDeclarationSignature(); 117 if (declarationSignature == null) return map; 119 char[][] concreteParameters= Signature.getTypeArguments(declarationSignature); 120 121 ITypeParameter[] typeParameters= type.getTypeParameters(); 122 for (int i= 0; i < typeParameters.length; i++) { 123 String variable= typeParameters[i].getElementName(); 124 if (concreteParameters.length > i) 125 map.put(variable, SignatureUtil.getLowerBound(concreteParameters[i])); 127 else 128 map.put(variable, "Ljava.lang.Object;".toCharArray()); } 131 132 return map; 133 } 134 135 149 private IMethod findMethod(String name, String [] paramTypes, boolean isConstructor, IMethod[] methods, Map typeVariables) throws JavaModelException { 150 for (int i= methods.length - 1; i >= 0; i--) { 151 if (isSameMethodSignature(name, paramTypes, isConstructor, methods[i], typeVariables)) { 152 return methods[i]; 153 } 154 } 155 return fFallbackMatch; 156 } 157 158 172 private boolean isSameMethodSignature(String name, String [] paramTypes, boolean isConstructor, IMethod method, Map typeVariables) throws JavaModelException { 173 if (isConstructor || name.equals(method.getElementName())) { 174 if (isConstructor == method.isConstructor()) { 175 String [] otherParams= method.getParameterTypes(); if (paramTypes.length == otherParams.length) { 177 fFallbackMatch= method; 178 String signature= method.getSignature(); 179 String [] otherParamsFromSignature= Signature.getParameterTypes(signature); for (int i= 0; i < paramTypes.length; i++) { 183 String ourParamName= computeSimpleTypeName(paramTypes[i], typeVariables); 184 String otherParamName1= computeSimpleTypeName(otherParams[i], typeVariables); 185 String otherParamName2= computeSimpleTypeName(otherParamsFromSignature[i], typeVariables); 186 187 if (!ourParamName.equals(otherParamName1) && !ourParamName.equals(otherParamName2)) { 188 return false; 189 } 190 } 191 return true; 192 } 193 } 194 } 195 return false; 196 } 197 198 205 private String computeSimpleTypeName(String signature, Map typeVariables) { 206 String erasure= Signature.getTypeErasure(signature); 208 erasure= erasure.replaceAll("/", "."); String simpleName= Signature.getSimpleName(Signature.toString(erasure)); 210 char[] typeVar= (char[]) typeVariables.get(simpleName); 211 if (typeVar != null) 212 simpleName= String.valueOf(Signature.getSignatureSimpleName(typeVar)); 213 return simpleName; 214 } 215 } 216 | Popular Tags |