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