KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > BindingKey


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.core;
12
13 import org.eclipse.jdt.internal.core.util.KeyKind;
14 import org.eclipse.jdt.internal.core.util.KeyToSignature;
15
16 /**
17  * Utility class to decode or create a binding key.
18  * <p>
19  * This class is not intended to be subclassed by clients.
20  * </p>
21  *
22  * @see org.eclipse.jdt.core.dom.IBinding#getKey()
23  * @since 3.1
24  */

25 public final class BindingKey {
26     
27     private String JavaDoc key;
28     
29     /**
30      * Creates a new binding key.
31      *
32      * @param key the key to decode
33      */

34     public BindingKey(String JavaDoc key) {
35         this.key = key;
36     }
37     
38     /**
39      * Creates a new array type binding key from the given type binding key and the given array dimension.
40      * <p>
41      * For example:
42      * <pre>
43      * <code>
44      * createArrayTypeBindingKey("Ljava/lang/Object;", 1) -> "[Ljava/lang/Object;"
45      * createArrayTypeBindingKey("I", 2) -> "[[I"
46      * </code>
47      * </pre>
48      * </p>
49      *
50      * @param typeKey the binding key of the given type
51      * @param arrayDimension the given array dimension
52      * @return a new array type binding key
53      */

54     public static String JavaDoc createArrayTypeBindingKey(String JavaDoc typeKey, int arrayDimension) {
55         // Note this implementation is heavily dependent on ArrayTypeBinding#computeUniqueKey()
56
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
57         while (arrayDimension-- > 0)
58             buffer.append('[');
59         buffer.append(typeKey);
60         return buffer.toString();
61     }
62     
63     /**
64      * Creates a new parameterized type binding key from the given generic type binding key and the given argument type binding keys.
65      * If the argument type keys array is empty, then a raw type binding key is created.
66      * <p>
67      * For example:
68      * <pre>
69      * <code>
70      * createParameterizedTypeBindingKey(
71      * "Ljava/util/Map&lt;TK;TV;&gt;;",
72      * new String[] {"Ljava/lang/String;", "Ljava/lang/Object;"}) -&gt;
73      * "Ljava/util/Map&lt;Ljava/lang/String;Ljava/lang/Object;&gt;;"
74      * createParameterizedTypeBindingKey(
75      * "Ljava/util/List&lt;TE;&gt;;", new String[] {}) -&gt;
76      * "Ljava/util/List&lt;&gt;;"
77      * </code>
78      * </pre>
79      * </p>
80      *
81      * @param genericTypeKey the binding key of the generic type
82      * @param argumentTypeKeys the possibly empty list of binding keys of argument types
83      * @return a new parameterized type binding key
84      */

85     public static String JavaDoc createParameterizedTypeBindingKey(String JavaDoc genericTypeKey, String JavaDoc[] argumentTypeKeys) {
86         // Note this implementation is heavily dependent on ParameterizedTypeBinding#computeUniqueKey() and its subclasses
87
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
88         buffer.append(Signature.getTypeErasure(genericTypeKey));
89         buffer.insert(buffer.length()-1, '<');
90         for (int i = 0, length = argumentTypeKeys.length; i < length; i++) {
91             String JavaDoc argumentTypeKey = argumentTypeKeys[i];
92             buffer.insert(buffer.length()-1, argumentTypeKey);
93         }
94         buffer.insert(buffer.length()-1, '>');
95         return buffer.toString();
96     }
97     
98     /**
99      * Creates a new type binding key from the given type name. The type name must be either
100      * a fully qualified name, an array type name or a primitive type name.
101      * If the type name is fully qualified, then it is expected to be dot-based.
102      * Note that inner types, generic types and parameterized types are not supported.
103      * <p>
104      * For example:
105      * <pre>
106      * <code>
107      * createTypeBindingKey("int") -> "I"
108      * createTypeBindingKey("java.lang.String") -> "Ljava/lang/String;"
109      * createTypeBindingKey("boolean[]") -> "[Z"
110      * </code>
111      * </pre>
112      * </p>
113      *
114      * @param typeName the possibly qualified type name
115      * @return a new type binding key
116      */

117     public static String JavaDoc createTypeBindingKey(String JavaDoc typeName) {
118         // Note this implementation is heavily dependent on TypeBinding#computeUniqueKey() and its subclasses
119
return Signature.createTypeSignature(typeName.replace('.', '/'), true/*resolved*/);
120     }
121     
122     /**
123      * Creates a new type variable binding key from the given type variable name and the given declaring key.
124      * The declaring key can either be a type binding key or a method binding key.
125      * <p>
126      * For example:
127      * <pre>
128      * <code>
129      * createTypeVariableBindingKey("T", "Ljava/util/List&lt;TE;&gt;;") -&gt;
130      * "Ljava/util/List&lt;TE;&gt;;:TT;"
131      * createTypeVariableBindingKey("SomeTypeVariable", "Lp/X;.foo()V") -&gt;
132      * "Lp/X;.foo()V:TSomeTypeVariable;"
133      * </code>
134      * </pre>
135      * </p>
136      *
137      * @param typeVariableName the name of the given type variable
138      * @param declaringKey the binding key of the type or method the type variable belongs to
139      * @return a new type variable binding key
140      */

141     public static String JavaDoc createTypeVariableBindingKey(String JavaDoc typeVariableName, String JavaDoc declaringKey) {
142         // Note this implementation is heavily dependent on TypeVariableBinding#computeUniqueKey()
143
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
144         buffer.append(declaringKey);
145         buffer.append(':');
146         buffer.append('T');
147         buffer.append(typeVariableName);
148         buffer.append(';');
149         return buffer.toString();
150     }
151     
152     /**
153      * Creates a new wildcard type binding key from the given type binding key and the given wildcard kind
154      * (one of {@link Signature#C_STAR}, {@link Signature#C_SUPER}, or {@link Signature#C_EXTENDS}.
155      * If the wildcard is {@link Signature#C_STAR}, the given type binding key is ignored.
156      * <p>
157      * For example:
158      * <pre>
159      * <code>
160      * createWilcardTypeBindingKey(null, Signature.C_STAR) -&gt; "*"
161      * createWilcardTypeBindingKey("Ljava/util/List&lt;TE;&gt;;",
162      * Signature.C_SUPER) -&gt; "-Ljava/util/List&lt;TE;&gt;;"
163      * createWilcardTypeBindingKey("Ljava/util/ArrayList;", Signature.C_EXTENDS) -&gt;
164      * "+Ljava/util/ArrayList;"
165      * </code>
166      * </pre>
167      * </p>
168      *
169      * @param typeKey the binding key of the given type
170      * @param kind one of {@link Signature#C_STAR}, {@link Signature#C_SUPER}, or {@link Signature#C_EXTENDS}
171      * @return a new wildcard type binding key
172      */

173     public static String JavaDoc createWilcardTypeBindingKey(String JavaDoc typeKey, char kind) {
174         // Note this implementation is heavily dependent on WildcardBinding#computeUniqueKey()
175
switch (kind) {
176             case Signature.C_STAR:
177                 return "*"; //$NON-NLS-1$
178
case Signature.C_SUPER:
179                 return '-' + typeKey;
180             case Signature.C_EXTENDS:
181                 return '+' + typeKey;
182         }
183         return null;
184     }
185
186     /**
187      * Returns the thrown exception signatures of the element represented by this binding key.
188      * If this binding key does not represent a method or does not throw any exception,
189      * returns an empty array.
190      *
191      * @return the thrown exceptions signatures
192      * @since 3.3
193      */

194     public String JavaDoc[] getThrownExceptions() {
195         KeyToSignature keyToSignature = new KeyToSignature(this.key, KeyToSignature.THROWN_EXCEPTIONS);
196         keyToSignature.parse();
197         return keyToSignature.getThrownExceptions();
198     }
199
200     /**
201      * Returns the type argument signatures of the element represented by this binding key.
202      * If this binding key doesn't represent a parameterized type or a parameterized method,
203      * returns an empty array.
204      *
205      * @return the type argument signatures
206      */

207     public String JavaDoc[] getTypeArguments() {
208         KeyToSignature keyToSignature = new KeyToSignature(this.key, KeyToSignature.TYPE_ARGUMENTS);
209         keyToSignature.parse();
210         return keyToSignature.getTypeArguments();
211     }
212     
213     /**
214      * Returns whether this binding key represents a raw type.
215      *
216      * @return whether this binding key represents a raw type
217      */

218     public boolean isRawType() {
219         KeyKind kind = new KeyKind(this.key);
220         kind.parse();
221         return (kind.flags & KeyKind.F_RAW_TYPE) != 0;
222     }
223     
224     /**
225      * Returns whether this binding key represents a parameterized type, or if its declaring type is a parameterized type.
226      *
227      * @return whether this binding key represents a parameterized type
228      */

229     public boolean isParameterizedType() {
230         KeyKind kind = new KeyKind(this.key);
231         kind.parse();
232         return (kind.flags & KeyKind.F_PARAMETERIZED_TYPE) != 0;
233     }
234     
235     /**
236      * Returns whether this binding key represents a parameterized method, or if its declaring method is a parameterized method.
237      *
238      * @return whether this binding key represents a parameterized method
239      */

240     public boolean isParameterizedMethod() {
241         KeyKind kind = new KeyKind(this.key);
242         kind.parse();
243         return (kind.flags & KeyKind.F_PARAMETERIZED_METHOD) != 0;
244     }
245     
246     /**
247      * Transforms this binding key into a resolved signature.
248      * If this binding key represents a field, the returned signature is
249      * the declaring type's signature.
250      *
251      * @return the resolved signature for this binding key
252      * @see Signature
253      * @since 3.2
254      */

255     public String JavaDoc toSignature() {
256         KeyToSignature keyToSignature = new KeyToSignature(this.key, KeyToSignature.SIGNATURE);
257         keyToSignature.parse();
258         return keyToSignature.signature.toString();
259     }
260     
261     /* (non-Javadoc)
262      * @see java.lang.Object#toString()
263      */

264     public String JavaDoc toString() {
265         return this.key;
266     }
267 }
268
Popular Tags