KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > PatternStrings


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc 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 JavaDoc getMethodSignature(IMethod method) {
40         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
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 JavaDoc getUnqualifiedMethodSignature(IMethod method, boolean includeName) {
54         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
55         if (includeName) {
56             buffer.append(method.getElementName());
57         }
58         buffer.append('(');
59         
60         String JavaDoc[] types= method.getParameterTypes();
61         for (int i= 0; i < types.length; i++) {
62             if (i > 0)
63                 buffer.append(", "); //$NON-NLS-1$
64
String JavaDoc typeSig= Signature.toString(types[i]);
65             buffer.append(typeSig);
66         }
67         buffer.append(')');
68         
69         return buffer.toString();
70     }
71
72     public static String JavaDoc getUnqualifiedMethodSignature(IMethod method) {
73         return getUnqualifiedMethodSignature(method, true);
74     }
75
76     public static String JavaDoc 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 JavaDoc getFieldSignature(IField field) {
82         return JavaElementLabels.getElementLabel(field, JavaElementLabels.F_FULLY_QUALIFIED);
83     }
84 }
85
Popular Tags