001/***
002 * ASM: a very small and fast Java bytecode manipulation framework
003 * Copyright (c) 2000-2013 INRIA, France Telecom
004 * All rights reserved.
005 *
006 * Redistribution and use in source and binary forms, with or without
007 * modification, are permitted provided that the following conditions
008 * are met:
009 * 1. Redistributions of source code must retain the above copyright
010 *    notice, this list of conditions and the following disclaimer.
011 * 2. Redistributions in binary form must reproduce the above copyright
012 *    notice, this list of conditions and the following disclaimer in the
013 *    documentation and/or other materials provided with the distribution.
014 * 3. Neither the name of the copyright holders nor the names of its
015 *    contributors may be used to endorse or promote products derived from
016 *    this software without specific prior written permission.
017 *
018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028 * THE POSSIBILITY OF SUCH DAMAGE.
029 */
030
031package org.springframework.asm;
032
033/**
034 * A reference to a type appearing in a class, field or method declaration, or
035 * on an instruction. Such a reference designates the part of the class where
036 * the referenced type is appearing (e.g. an 'extends', 'implements' or 'throws'
037 * clause, a 'new' instruction, a 'catch' clause, a type cast, a local variable
038 * declaration, etc).
039 * 
040 * @author Eric Bruneton
041 */
042public class TypeReference {
043
044    /**
045     * The sort of type references that target a type parameter of a generic
046     * class. See {@link #getSort getSort}.
047     */
048    public final static int CLASS_TYPE_PARAMETER = 0x00;
049
050    /**
051     * The sort of type references that target a type parameter of a generic
052     * method. See {@link #getSort getSort}.
053     */
054    public final static int METHOD_TYPE_PARAMETER = 0x01;
055
056    /**
057     * The sort of type references that target the super class of a class or one
058     * of the interfaces it implements. See {@link #getSort getSort}.
059     */
060    public final static int CLASS_EXTENDS = 0x10;
061
062    /**
063     * The sort of type references that target a bound of a type parameter of a
064     * generic class. See {@link #getSort getSort}.
065     */
066    public final static int CLASS_TYPE_PARAMETER_BOUND = 0x11;
067
068    /**
069     * The sort of type references that target a bound of a type parameter of a
070     * generic method. See {@link #getSort getSort}.
071     */
072    public final static int METHOD_TYPE_PARAMETER_BOUND = 0x12;
073
074    /**
075     * The sort of type references that target the type of a field. See
076     * {@link #getSort getSort}.
077     */
078    public final static int FIELD = 0x13;
079
080    /**
081     * The sort of type references that target the return type of a method. See
082     * {@link #getSort getSort}.
083     */
084    public final static int METHOD_RETURN = 0x14;
085
086    /**
087     * The sort of type references that target the receiver type of a method.
088     * See {@link #getSort getSort}.
089     */
090    public final static int METHOD_RECEIVER = 0x15;
091
092    /**
093     * The sort of type references that target the type of a formal parameter of
094     * a method. See {@link #getSort getSort}.
095     */
096    public final static int METHOD_FORMAL_PARAMETER = 0x16;
097
098    /**
099     * The sort of type references that target the type of an exception declared
100     * in the throws clause of a method. See {@link #getSort getSort}.
101     */
102    public final static int THROWS = 0x17;
103
104    /**
105     * The sort of type references that target the type of a local variable in a
106     * method. See {@link #getSort getSort}.
107     */
108    public final static int LOCAL_VARIABLE = 0x40;
109
110    /**
111     * The sort of type references that target the type of a resource variable
112     * in a method. See {@link #getSort getSort}.
113     */
114    public final static int RESOURCE_VARIABLE = 0x41;
115
116    /**
117     * The sort of type references that target the type of the exception of a
118     * 'catch' clause in a method. See {@link #getSort getSort}.
119     */
120    public final static int EXCEPTION_PARAMETER = 0x42;
121
122    /**
123     * The sort of type references that target the type declared in an
124     * 'instanceof' instruction. See {@link #getSort getSort}.
125     */
126    public final static int INSTANCEOF = 0x43;
127
128    /**
129     * The sort of type references that target the type of the object created by
130     * a 'new' instruction. See {@link #getSort getSort}.
131     */
132    public final static int NEW = 0x44;
133
134    /**
135     * The sort of type references that target the receiver type of a
136     * constructor reference. See {@link #getSort getSort}.
137     */
138    public final static int CONSTRUCTOR_REFERENCE = 0x45;
139
140    /**
141     * The sort of type references that target the receiver type of a method
142     * reference. See {@link #getSort getSort}.
143     */
144    public final static int METHOD_REFERENCE = 0x46;
145
146    /**
147     * The sort of type references that target the type declared in an explicit
148     * or implicit cast instruction. See {@link #getSort getSort}.
149     */
150    public final static int CAST = 0x47;
151
152    /**
153     * The sort of type references that target a type parameter of a generic
154     * constructor in a constructor call. See {@link #getSort getSort}.
155     */
156    public final static int CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT = 0x48;
157
158    /**
159     * The sort of type references that target a type parameter of a generic
160     * method in a method call. See {@link #getSort getSort}.
161     */
162    public final static int METHOD_INVOCATION_TYPE_ARGUMENT = 0x49;
163
164    /**
165     * The sort of type references that target a type parameter of a generic
166     * constructor in a constructor reference. See {@link #getSort getSort}.
167     */
168    public final static int CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT = 0x4A;
169
170    /**
171     * The sort of type references that target a type parameter of a generic
172     * method in a method reference. See {@link #getSort getSort}.
173     */
174    public final static int METHOD_REFERENCE_TYPE_ARGUMENT = 0x4B;
175
176    /**
177     * The type reference value in Java class file format.
178     */
179    private int value;
180
181    /**
182     * Creates a new TypeReference.
183     * 
184     * @param typeRef
185     *            the int encoded value of the type reference, as received in a
186     *            visit method related to type annotations, like
187     *            visitTypeAnnotation.
188     */
189    public TypeReference(int typeRef) {
190        this.value = typeRef;
191    }
192
193    /**
194     * Returns a type reference of the given sort.
195     * 
196     * @param sort
197     *            {@link #FIELD FIELD}, {@link #METHOD_RETURN METHOD_RETURN},
198     *            {@link #METHOD_RECEIVER METHOD_RECEIVER},
199     *            {@link #LOCAL_VARIABLE LOCAL_VARIABLE},
200     *            {@link #RESOURCE_VARIABLE RESOURCE_VARIABLE},
201     *            {@link #INSTANCEOF INSTANCEOF}, {@link #NEW NEW},
202     *            {@link #CONSTRUCTOR_REFERENCE CONSTRUCTOR_REFERENCE}, or
203     *            {@link #METHOD_REFERENCE METHOD_REFERENCE}.
204     * @return a type reference of the given sort.
205     */
206    public static TypeReference newTypeReference(int sort) {
207        return new TypeReference(sort << 24);
208    }
209
210    /**
211     * Returns a reference to a type parameter of a generic class or method.
212     * 
213     * @param sort
214     *            {@link #CLASS_TYPE_PARAMETER CLASS_TYPE_PARAMETER} or
215     *            {@link #METHOD_TYPE_PARAMETER METHOD_TYPE_PARAMETER}.
216     * @param paramIndex
217     *            the type parameter index.
218     * @return a reference to the given generic class or method type parameter.
219     */
220    public static TypeReference newTypeParameterReference(int sort,
221            int paramIndex) {
222        return new TypeReference((sort << 24) | (paramIndex << 16));
223    }
224
225    /**
226     * Returns a reference to a type parameter bound of a generic class or
227     * method.
228     * 
229     * @param sort
230     *            {@link #CLASS_TYPE_PARAMETER CLASS_TYPE_PARAMETER} or
231     *            {@link #METHOD_TYPE_PARAMETER METHOD_TYPE_PARAMETER}.
232     * @param paramIndex
233     *            the type parameter index.
234     * @param boundIndex
235     *            the type bound index within the above type parameters.
236     * @return a reference to the given generic class or method type parameter
237     *         bound.
238     */
239    public static TypeReference newTypeParameterBoundReference(int sort,
240            int paramIndex, int boundIndex) {
241        return new TypeReference((sort << 24) | (paramIndex << 16)
242                | (boundIndex << 8));
243    }
244
245    /**
246     * Returns a reference to the super class or to an interface of the
247     * 'implements' clause of a class.
248     * 
249     * @param itfIndex
250     *            the index of an interface in the 'implements' clause of a
251     *            class, or -1 to reference the super class of the class.
252     * @return a reference to the given super type of a class.
253     */
254    public static TypeReference newSuperTypeReference(int itfIndex) {
255        itfIndex &= 0xFFFF;
256        return new TypeReference((CLASS_EXTENDS << 24) | (itfIndex << 8));
257    }
258
259    /**
260     * Returns a reference to the type of a formal parameter of a method.
261     * 
262     * @param paramIndex
263     *            the formal parameter index.
264     * 
265     * @return a reference to the type of the given method formal parameter.
266     */
267    public static TypeReference newFormalParameterReference(int paramIndex) {
268        return new TypeReference((METHOD_FORMAL_PARAMETER << 24)
269                | (paramIndex << 16));
270    }
271
272    /**
273     * Returns a reference to the type of an exception, in a 'throws' clause of
274     * a method.
275     * 
276     * @param exceptionIndex
277     *            the index of an exception in a 'throws' clause of a method.
278     * 
279     * @return a reference to the type of the given exception.
280     */
281    public static TypeReference newExceptionReference(int exceptionIndex) {
282        return new TypeReference((THROWS << 24) | (exceptionIndex << 8));
283    }
284
285    /**
286     * Returns a reference to the type of the exception declared in a 'catch'
287     * clause of a method.
288     * 
289     * @param tryCatchBlockIndex
290     *            the index of a try catch block (using the order in which they
291     *            are visited with visitTryCatchBlock).
292     * 
293     * @return a reference to the type of the given exception.
294     */
295    public static TypeReference newTryCatchReference(int tryCatchBlockIndex) {
296        return new TypeReference((EXCEPTION_PARAMETER << 24)
297                | (tryCatchBlockIndex << 8));
298    }
299
300    /**
301     * Returns a reference to the type of a type argument in a constructor or
302     * method call or reference.
303     * 
304     * @param sort
305     *            {@link #CAST CAST},
306     *            {@link #CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT
307     *            CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT},
308     *            {@link #METHOD_INVOCATION_TYPE_ARGUMENT
309     *            METHOD_INVOCATION_TYPE_ARGUMENT},
310     *            {@link #CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT
311     *            CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT}, or
312     *            {@link #METHOD_REFERENCE_TYPE_ARGUMENT
313     *            METHOD_REFERENCE_TYPE_ARGUMENT}.
314     * @param argIndex
315     *            the type argument index.
316     * 
317     * @return a reference to the type of the given type argument.
318     */
319    public static TypeReference newTypeArgumentReference(int sort, int argIndex) {
320        return new TypeReference((sort << 24) | argIndex);
321    }
322
323    /**
324     * Returns the sort of this type reference.
325     * 
326     * @return {@link #CLASS_TYPE_PARAMETER CLASS_TYPE_PARAMETER},
327     *         {@link #METHOD_TYPE_PARAMETER METHOD_TYPE_PARAMETER},
328     *         {@link #CLASS_EXTENDS CLASS_EXTENDS},
329     *         {@link #CLASS_TYPE_PARAMETER_BOUND CLASS_TYPE_PARAMETER_BOUND},
330     *         {@link #METHOD_TYPE_PARAMETER_BOUND METHOD_TYPE_PARAMETER_BOUND},
331     *         {@link #FIELD FIELD}, {@link #METHOD_RETURN METHOD_RETURN},
332     *         {@link #METHOD_RECEIVER METHOD_RECEIVER},
333     *         {@link #METHOD_FORMAL_PARAMETER METHOD_FORMAL_PARAMETER},
334     *         {@link #THROWS THROWS}, {@link #LOCAL_VARIABLE LOCAL_VARIABLE},
335     *         {@link #RESOURCE_VARIABLE RESOURCE_VARIABLE},
336     *         {@link #EXCEPTION_PARAMETER EXCEPTION_PARAMETER},
337     *         {@link #INSTANCEOF INSTANCEOF}, {@link #NEW NEW},
338     *         {@link #CONSTRUCTOR_REFERENCE CONSTRUCTOR_REFERENCE},
339     *         {@link #METHOD_REFERENCE METHOD_REFERENCE}, {@link #CAST CAST},
340     *         {@link #CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT
341     *         CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT},
342     *         {@link #METHOD_INVOCATION_TYPE_ARGUMENT
343     *         METHOD_INVOCATION_TYPE_ARGUMENT},
344     *         {@link #CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT
345     *         CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT}, or
346     *         {@link #METHOD_REFERENCE_TYPE_ARGUMENT
347     *         METHOD_REFERENCE_TYPE_ARGUMENT}.
348     */
349    public int getSort() {
350        return value >>> 24;
351    }
352
353    /**
354     * Returns the index of the type parameter referenced by this type
355     * reference. This method must only be used for type references whose sort
356     * is {@link #CLASS_TYPE_PARAMETER CLASS_TYPE_PARAMETER},
357     * {@link #METHOD_TYPE_PARAMETER METHOD_TYPE_PARAMETER},
358     * {@link #CLASS_TYPE_PARAMETER_BOUND CLASS_TYPE_PARAMETER_BOUND} or
359     * {@link #METHOD_TYPE_PARAMETER_BOUND METHOD_TYPE_PARAMETER_BOUND}.
360     * 
361     * @return a type parameter index.
362     */
363    public int getTypeParameterIndex() {
364        return (value & 0x00FF0000) >> 16;
365    }
366
367    /**
368     * Returns the index of the type parameter bound, within the type parameter
369     * {@link #getTypeParameterIndex}, referenced by this type reference. This
370     * method must only be used for type references whose sort is
371     * {@link #CLASS_TYPE_PARAMETER_BOUND CLASS_TYPE_PARAMETER_BOUND} or
372     * {@link #METHOD_TYPE_PARAMETER_BOUND METHOD_TYPE_PARAMETER_BOUND}.
373     * 
374     * @return a type parameter bound index.
375     */
376    public int getTypeParameterBoundIndex() {
377        return (value & 0x0000FF00) >> 8;
378    }
379
380    /**
381     * Returns the index of the "super type" of a class that is referenced by
382     * this type reference. This method must only be used for type references
383     * whose sort is {@link #CLASS_EXTENDS CLASS_EXTENDS}.
384     * 
385     * @return the index of an interface in the 'implements' clause of a class,
386     *         or -1 if this type reference references the type of the super
387     *         class.
388     */
389    public int getSuperTypeIndex() {
390        return (short) ((value & 0x00FFFF00) >> 8);
391    }
392
393    /**
394     * Returns the index of the formal parameter whose type is referenced by
395     * this type reference. This method must only be used for type references
396     * whose sort is {@link #METHOD_FORMAL_PARAMETER METHOD_FORMAL_PARAMETER}.
397     * 
398     * @return a formal parameter index.
399     */
400    public int getFormalParameterIndex() {
401        return (value & 0x00FF0000) >> 16;
402    }
403
404    /**
405     * Returns the index of the exception, in a 'throws' clause of a method,
406     * whose type is referenced by this type reference. This method must only be
407     * used for type references whose sort is {@link #THROWS THROWS}.
408     * 
409     * @return the index of an exception in the 'throws' clause of a method.
410     */
411    public int getExceptionIndex() {
412        return (value & 0x00FFFF00) >> 8;
413    }
414
415    /**
416     * Returns the index of the try catch block (using the order in which they
417     * are visited with visitTryCatchBlock), whose 'catch' type is referenced by
418     * this type reference. This method must only be used for type references
419     * whose sort is {@link #EXCEPTION_PARAMETER EXCEPTION_PARAMETER} .
420     * 
421     * @return the index of an exception in the 'throws' clause of a method.
422     */
423    public int getTryCatchBlockIndex() {
424        return (value & 0x00FFFF00) >> 8;
425    }
426
427    /**
428     * Returns the index of the type argument referenced by this type reference.
429     * This method must only be used for type references whose sort is
430     * {@link #CAST CAST}, {@link #CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT
431     * CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT},
432     * {@link #METHOD_INVOCATION_TYPE_ARGUMENT METHOD_INVOCATION_TYPE_ARGUMENT},
433     * {@link #CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT
434     * CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT}, or
435     * {@link #METHOD_REFERENCE_TYPE_ARGUMENT METHOD_REFERENCE_TYPE_ARGUMENT}.
436     * 
437     * @return a type parameter index.
438     */
439    public int getTypeArgumentIndex() {
440        return value & 0xFF;
441    }
442
443    /**
444     * Returns the int encoded value of this type reference, suitable for use in
445     * visit methods related to type annotations, like visitTypeAnnotation.
446     * 
447     * @return the int encoded value of this type reference.
448     */
449    public int getValue() {
450        return value;
451    }
452}