KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > NamedMember


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.core;
12
13 import org.eclipse.jdt.core.BindingKey;
14 import org.eclipse.jdt.core.ICompilationUnit;
15 import org.eclipse.jdt.core.IField;
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.IMember;
18 import org.eclipse.jdt.core.IMethod;
19 import org.eclipse.jdt.core.IPackageFragment;
20 import org.eclipse.jdt.core.IType;
21 import org.eclipse.jdt.core.ITypeParameter;
22 import org.eclipse.jdt.core.JavaModelException;
23 import org.eclipse.jdt.core.Signature;
24 import org.eclipse.jdt.core.compiler.CharOperation;
25
26 public abstract class NamedMember extends Member {
27
28     /*
29      * This element's name, or an empty <code>String</code> if this
30      * element does not have a name.
31      */

32     protected String JavaDoc name;
33     
34     public NamedMember(JavaElement parent, String JavaDoc name) {
35         super(parent);
36         this.name = name;
37     }
38     
39     private void appendTypeParameters(StringBuffer JavaDoc buffer) throws JavaModelException {
40         ITypeParameter[] typeParameters = getTypeParameters();
41         int length = typeParameters.length;
42         if (length == 0) return;
43         buffer.append('<');
44         for (int i = 0; i < length; i++) {
45             ITypeParameter typeParameter = typeParameters[i];
46             buffer.append(typeParameter.getElementName());
47             String JavaDoc[] bounds = typeParameter.getBounds();
48             int boundsLength = bounds.length;
49             if (boundsLength > 0) {
50                 buffer.append(" extends "); //$NON-NLS-1$
51
for (int j = 0; j < boundsLength; j++) {
52                     buffer.append(bounds[j]);
53                     if (j < boundsLength-1)
54                         buffer.append(" & "); //$NON-NLS-1$
55
}
56             }
57             if (i < length-1)
58                 buffer.append(", "); //$NON-NLS-1$
59
}
60         buffer.append('>');
61     }
62
63     public String JavaDoc getElementName() {
64         return this.name;
65     }
66     
67     protected String JavaDoc getKey(IField field, boolean forceOpen) throws JavaModelException {
68         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
69         
70         // declaring class
71
String JavaDoc declaringKey = getKey((IType) field.getParent(), forceOpen);
72         key.append(declaringKey);
73         
74         // field name
75
key.append('.');
76         key.append(field.getElementName());
77
78         return key.toString();
79     }
80     
81     protected String JavaDoc getKey(IMethod method, boolean forceOpen) throws JavaModelException {
82         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
83         
84         // declaring class
85
String JavaDoc declaringKey = getKey((IType) method.getParent(), forceOpen);
86         key.append(declaringKey);
87         
88         // selector
89
key.append('.');
90         String JavaDoc selector = method.getElementName();
91         key.append(selector);
92         
93         // type parameters
94
if (forceOpen) {
95             ITypeParameter[] typeParameters = method.getTypeParameters();
96             int length = typeParameters.length;
97             if (length > 0) {
98                 key.append('<');
99                 for (int i = 0; i < length; i++) {
100                     ITypeParameter typeParameter = typeParameters[i];
101                     String JavaDoc[] bounds = typeParameter.getBounds();
102                     int boundsLength = bounds.length;
103                     char[][] boundSignatures = new char[boundsLength][];
104                     for (int j = 0; j < boundsLength; j++) {
105                         boundSignatures[j] = Signature.createCharArrayTypeSignature(bounds[j].toCharArray(), method.isBinary());
106                         CharOperation.replace(boundSignatures[j], '.', '/');
107                     }
108                     char[] sig = Signature.createTypeParameterSignature(typeParameter.getElementName().toCharArray(), boundSignatures);
109                     key.append(sig);
110                 }
111                 key.append('>');
112             }
113         }
114         
115         // parameters
116
key.append('(');
117         String JavaDoc[] parameters = method.getParameterTypes();
118         for (int i = 0, length = parameters.length; i < length; i++)
119             key.append(parameters[i].replace('.', '/'));
120         key.append(')');
121         
122         // return type
123
if (forceOpen)
124             key.append(method.getReturnType().replace('.', '/'));
125         else
126             key.append('V');
127         
128         return key.toString();
129     }
130     
131     protected String JavaDoc getKey(IType type, boolean forceOpen) throws JavaModelException {
132         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
133         key.append('L');
134         String JavaDoc packageName = type.getPackageFragment().getElementName();
135         key.append(packageName.replace('.', '/'));
136         if (packageName.length() > 0)
137             key.append('/');
138         String JavaDoc typeQualifiedName = type.getTypeQualifiedName('$');
139         ICompilationUnit cu = (ICompilationUnit) type.getAncestor(IJavaElement.COMPILATION_UNIT);
140         if (cu != null) {
141             String JavaDoc cuName = cu.getElementName();
142             String JavaDoc mainTypeName = cuName.substring(0, cuName.lastIndexOf('.'));
143             int end = typeQualifiedName.indexOf('$');
144             if (end == -1)
145                 end = typeQualifiedName.length();
146             String JavaDoc topLevelTypeName = typeQualifiedName.substring(0, end);
147             if (!mainTypeName.equals(topLevelTypeName)) {
148                 key.append(mainTypeName);
149                 key.append('~');
150             }
151         }
152         key.append(typeQualifiedName);
153         key.append(';');
154         return key.toString();
155     }
156
157     protected String JavaDoc getFullyQualifiedParameterizedName(String JavaDoc fullyQualifiedName, String JavaDoc uniqueKey) throws JavaModelException {
158         String JavaDoc[] typeArguments = new BindingKey(uniqueKey).getTypeArguments();
159         int length = typeArguments.length;
160         if (length == 0) return fullyQualifiedName;
161         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
162         buffer.append(fullyQualifiedName);
163         buffer.append('<');
164         for (int i = 0; i < length; i++) {
165             String JavaDoc typeArgument = typeArguments[i];
166             buffer.append(Signature.toString(typeArgument));
167             if (i < length-1)
168                 buffer.append(',');
169         }
170         buffer.append('>');
171         return buffer.toString();
172     }
173     
174     protected IPackageFragment getPackageFragment() {
175         return null;
176     }
177     
178     public String JavaDoc getFullyQualifiedName(char enclosingTypeSeparator, boolean showParameters) throws JavaModelException {
179         String JavaDoc packageName = getPackageFragment().getElementName();
180         if (packageName.equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) {
181             return getTypeQualifiedName(enclosingTypeSeparator, showParameters);
182         }
183         return packageName + '.' + getTypeQualifiedName(enclosingTypeSeparator, showParameters);
184     }
185
186     public String JavaDoc getTypeQualifiedName(char enclosingTypeSeparator, boolean showParameters) throws JavaModelException {
187         NamedMember declaringType;
188         switch (this.parent.getElementType()) {
189             case IJavaElement.COMPILATION_UNIT:
190                 if (showParameters) {
191                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(this.name);
192                     appendTypeParameters(buffer);
193                     return buffer.toString();
194                 }
195                 return this.name;
196             case IJavaElement.CLASS_FILE:
197                 String JavaDoc classFileName = this.parent.getElementName();
198                 String JavaDoc typeName;
199                 if (classFileName.indexOf('$') == -1) {
200                     // top level class file: name of type is same as name of class file
201
typeName = this.name;
202                 } else {
203                     // anonymous or local class file
204
typeName = classFileName.substring(0, classFileName.lastIndexOf('.')); // remove .class
205
}
206                 if (showParameters) {
207                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(typeName);
208                     appendTypeParameters(buffer);
209                     return buffer.toString();
210                 }
211                 return typeName;
212             case IJavaElement.TYPE:
213                 declaringType = (NamedMember) this.parent;
214                 break;
215             case IJavaElement.FIELD:
216             case IJavaElement.INITIALIZER:
217             case IJavaElement.METHOD:
218                 declaringType = (NamedMember) ((IMember) this.parent).getDeclaringType();
219                 break;
220             default:
221                 return null;
222         }
223         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(declaringType.getTypeQualifiedName(enclosingTypeSeparator, showParameters));
224         buffer.append(enclosingTypeSeparator);
225         String JavaDoc simpleName = this.name.length() == 0 ? Integer.toString(this.occurrenceCount) : this.name;
226         buffer.append(simpleName);
227         if (showParameters) {
228             appendTypeParameters(buffer);
229         }
230         return buffer.toString();
231     }
232     
233     protected ITypeParameter[] getTypeParameters() throws JavaModelException {
234         return null;
235     }
236 }
237
Popular Tags