1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import org.eclipse.jdt.core.IJavaElement; 14 import org.eclipse.jdt.core.IMethod; 15 import org.eclipse.jdt.core.IType; 16 import org.eclipse.jdt.core.Signature; 17 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 18 19 public class PrettySignature { 20 21 public static String getSignature(IJavaElement element) { 22 if (element == null) 23 return null; 24 else 25 switch (element.getElementType()) { 26 case IJavaElement.METHOD: 27 return getMethodSignature((IMethod)element); 28 case IJavaElement.TYPE: 29 return JavaModelUtil.getFullyQualifiedName((IType)element); 30 default: 31 return element.getElementName(); 32 } 33 } 34 35 public static String getMethodSignature(IMethod method) { 36 StringBuffer buffer= new StringBuffer (); 37 buffer.append(JavaModelUtil.getFullyQualifiedName(method.getDeclaringType())); 38 boolean isConstructor= method.getElementName().equals(method.getDeclaringType().getElementName()); 39 if (!isConstructor) { 40 buffer.append('.'); 41 } 42 buffer.append(getUnqualifiedMethodSignature(method, !isConstructor)); 43 44 return buffer.toString(); 45 } 46 47 public static String getUnqualifiedTypeSignature(IType type) { 48 return type.getElementName(); 49 } 50 51 public static String getUnqualifiedMethodSignature(IMethod method, boolean includeName) { 52 StringBuffer buffer= new StringBuffer (); 53 if (includeName) { 54 buffer.append(method.getElementName()); 55 } 56 buffer.append('('); 57 58 String [] types= method.getParameterTypes(); 59 if (types.length > 0) 60 buffer.append(Signature.toString(types[0])); 61 for (int i= 1; i < types.length; i++) { 62 buffer.append(", "); buffer.append(Signature.toString(types[i])); 64 } 65 66 buffer.append(')'); 67 68 return buffer.toString(); 69 } 70 71 public static String getUnqualifiedMethodSignature(IMethod method) { 72 return getUnqualifiedMethodSignature(method, true); 73 } 74 } 75 | Popular Tags |