KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.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 JavaDoc 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 JavaDoc getMethodSignature(IMethod method) {
36         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
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 JavaDoc getUnqualifiedTypeSignature(IType type) {
48         return type.getElementName();
49     }
50     
51     public static String JavaDoc getUnqualifiedMethodSignature(IMethod method, boolean includeName) {
52         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
53         if (includeName) {
54             buffer.append(method.getElementName());
55         }
56         buffer.append('(');
57         
58         String JavaDoc[] 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(", "); //$NON-NLS-1$
63
buffer.append(Signature.toString(types[i]));
64         }
65         
66         buffer.append(')');
67         
68         return buffer.toString();
69     }
70
71     public static String JavaDoc getUnqualifiedMethodSignature(IMethod method) {
72         return getUnqualifiedMethodSignature(method, true);
73     }
74 }
75
Popular Tags