1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.  Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25/*
26 * This file is available under and governed by the GNU General Public
27 * License version 2 only, as published by the Free Software Foundation.
28 * However, the following notice accompanied the original version of this
29 * file:
30 *
31 * ASM: a very small and fast Java bytecode manipulation framework
32 * Copyright (c) 2000-2013 INRIA, France Telecom
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 *    notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 *    notice, this list of conditions and the following disclaimer in the
42 *    documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the copyright holders nor the names of its
44 *    contributors may be used to endorse or promote products derived from
45 *    this software without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
57 * THE POSSIBILITY OF SUCH DAMAGE.
58 */
59
60package jdk.internal.org.objectweb.asm;
61
62/**
63 * A reference to a type appearing in a class, field or method declaration, or
64 * on an instruction. Such a reference designates the part of the class where
65 * the referenced type is appearing (e.g. an 'extends', 'implements' or 'throws'
66 * clause, a 'new' instruction, a 'catch' clause, a type cast, a local variable
67 * declaration, etc).
68 *
69 * @author Eric Bruneton
70 */
71public class TypeReference {
72
73    /**
74     * The sort of type references that target a type parameter of a generic
75     * class. See {@link #getSort getSort}.
76     */
77    public final static int CLASS_TYPE_PARAMETER = 0x00;
78
79    /**
80     * The sort of type references that target a type parameter of a generic
81     * method. See {@link #getSort getSort}.
82     */
83    public final static int METHOD_TYPE_PARAMETER = 0x01;
84
85    /**
86     * The sort of type references that target the super class of a class or one
87     * of the interfaces it implements. See {@link #getSort getSort}.
88     */
89    public final static int CLASS_EXTENDS = 0x10;
90
91    /**
92     * The sort of type references that target a bound of a type parameter of a
93     * generic class. See {@link #getSort getSort}.
94     */
95    public final static int CLASS_TYPE_PARAMETER_BOUND = 0x11;
96
97    /**
98     * The sort of type references that target a bound of a type parameter of a
99     * generic method. See {@link #getSort getSort}.
100     */
101    public final static int METHOD_TYPE_PARAMETER_BOUND = 0x12;
102
103    /**
104     * The sort of type references that target the type of a field. See
105     * {@link #getSort getSort}.
106     */
107    public final static int FIELD = 0x13;
108
109    /**
110     * The sort of type references that target the return type of a method. See
111     * {@link #getSort getSort}.
112     */
113    public final static int METHOD_RETURN = 0x14;
114
115    /**
116     * The sort of type references that target the receiver type of a method.
117     * See {@link #getSort getSort}.
118     */
119    public final static int METHOD_RECEIVER = 0x15;
120
121    /**
122     * The sort of type references that target the type of a formal parameter of
123     * a method. See {@link #getSort getSort}.
124     */
125    public final static int METHOD_FORMAL_PARAMETER = 0x16;
126
127    /**
128     * The sort of type references that target the type of an exception declared
129     * in the throws clause of a method. See {@link #getSort getSort}.
130     */
131    public final static int THROWS = 0x17;
132
133    /**
134     * The sort of type references that target the type of a local variable in a
135     * method. See {@link #getSort getSort}.
136     */
137    public final static int LOCAL_VARIABLE = 0x40;
138
139    /**
140     * The sort of type references that target the type of a resource variable
141     * in a method. See {@link #getSort getSort}.
142     */
143    public final static int RESOURCE_VARIABLE = 0x41;
144
145    /**
146     * The sort of type references that target the type of the exception of a
147     * 'catch' clause in a method. See {@link #getSort getSort}.
148     */
149    public final static int EXCEPTION_PARAMETER = 0x42;
150
151    /**
152     * The sort of type references that target the type declared in an
153     * 'instanceof' instruction. See {@link #getSort getSort}.
154     */
155    public final static int INSTANCEOF = 0x43;
156
157    /**
158     * The sort of type references that target the type of the object created by
159     * a 'new' instruction. See {@link #getSort getSort}.
160     */
161    public final static int NEW = 0x44;
162
163    /**
164     * The sort of type references that target the receiver type of a
165     * constructor reference. See {@link #getSort getSort}.
166     */
167    public final static int CONSTRUCTOR_REFERENCE = 0x45;
168
169    /**
170     * The sort of type references that target the receiver type of a method
171     * reference. See {@link #getSort getSort}.
172     */
173    public final static int METHOD_REFERENCE = 0x46;
174
175    /**
176     * The sort of type references that target the type declared in an explicit
177     * or implicit cast instruction. See {@link #getSort getSort}.
178     */
179    public final static int CAST = 0x47;
180
181    /**
182     * The sort of type references that target a type parameter of a generic
183     * constructor in a constructor call. See {@link #getSort getSort}.
184     */
185    public final static int CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT = 0x48;
186
187    /**
188     * The sort of type references that target a type parameter of a generic
189     * method in a method call. See {@link #getSort getSort}.
190     */
191    public final static int METHOD_INVOCATION_TYPE_ARGUMENT = 0x49;
192
193    /**
194     * The sort of type references that target a type parameter of a generic
195     * constructor in a constructor reference. See {@link #getSort getSort}.
196     */
197    public final static int CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT = 0x4A;
198
199    /**
200     * The sort of type references that target a type parameter of a generic
201     * method in a method reference. See {@link #getSort getSort}.
202     */
203    public final static int METHOD_REFERENCE_TYPE_ARGUMENT = 0x4B;
204
205    /**
206     * The type reference value in Java class file format.
207     */
208    private int value;
209
210    /**
211     * Creates a new TypeReference.
212     *
213     * @param typeRef
214     *            the int encoded value of the type reference, as received in a
215     *            visit method related to type annotations, like
216     *            visitTypeAnnotation.
217     */
218    public TypeReference(int typeRef) {
219        this.value = typeRef;
220    }
221
222    /**
223     * Returns a type reference of the given sort.
224     *
225     * @param sort
226     *            {@link #FIELD FIELD}, {@link #METHOD_RETURN METHOD_RETURN},
227     *            {@link #METHOD_RECEIVER METHOD_RECEIVER},
228     *            {@link #LOCAL_VARIABLE LOCAL_VARIABLE},
229     *            {@link #RESOURCE_VARIABLE RESOURCE_VARIABLE},
230     *            {@link #INSTANCEOF INSTANCEOF}, {@link #NEW NEW},
231     *            {@link #CONSTRUCTOR_REFERENCE CONSTRUCTOR_REFERENCE}, or
232     *            {@link #METHOD_REFERENCE METHOD_REFERENCE}.
233     * @return a type reference of the given sort.
234     */
235    public static TypeReference newTypeReference(int sort) {
236        return new TypeReference(sort << 24);
237    }
238
239    /**
240     * Returns a reference to a type parameter of a generic class or method.
241     *
242     * @param sort
243     *            {@link #CLASS_TYPE_PARAMETER CLASS_TYPE_PARAMETER} or
244     *            {@link #METHOD_TYPE_PARAMETER METHOD_TYPE_PARAMETER}.
245     * @param paramIndex
246     *            the type parameter index.
247     * @return a reference to the given generic class or method type parameter.
248     */
249    public static TypeReference newTypeParameterReference(int sort,
250            int paramIndex) {
251        return new TypeReference((sort << 24) | (paramIndex << 16));
252    }
253
254    /**
255     * Returns a reference to a type parameter bound of a generic class or
256     * method.
257     *
258     * @param sort
259     *            {@link #CLASS_TYPE_PARAMETER CLASS_TYPE_PARAMETER} or
260     *            {@link #METHOD_TYPE_PARAMETER METHOD_TYPE_PARAMETER}.
261     * @param paramIndex
262     *            the type parameter index.
263     * @param boundIndex
264     *            the type bound index within the above type parameters.
265     * @return a reference to the given generic class or method type parameter
266     *         bound.
267     */
268    public static TypeReference newTypeParameterBoundReference(int sort,
269            int paramIndex, int boundIndex) {
270        return new TypeReference((sort << 24) | (paramIndex << 16)
271                | (boundIndex << 8));
272    }
273
274    /**
275     * Returns a reference to the super class or to an interface of the
276     * 'implements' clause of a class.
277     *
278     * @param itfIndex
279     *            the index of an interface in the 'implements' clause of a
280     *            class, or -1 to reference the super class of the class.
281     * @return a reference to the given super type of a class.
282     */
283    public static TypeReference newSuperTypeReference(int itfIndex) {
284        itfIndex &= 0xFFFF;
285        return new TypeReference((CLASS_EXTENDS << 24) | (itfIndex << 8));
286    }
287
288    /**
289     * Returns a reference to the type of a formal parameter of a method.
290     *
291     * @param paramIndex
292     *            the formal parameter index.
293     *
294     * @return a reference to the type of the given method formal parameter.
295     */
296    public static TypeReference newFormalParameterReference(int paramIndex) {
297        return new TypeReference((METHOD_FORMAL_PARAMETER << 24)
298                | (paramIndex << 16));
299    }
300
301    /**
302     * Returns a reference to the type of an exception, in a 'throws' clause of
303     * a method.
304     *
305     * @param exceptionIndex
306     *            the index of an exception in a 'throws' clause of a method.
307     *
308     * @return a reference to the type of the given exception.
309     */
310    public static TypeReference newExceptionReference(int exceptionIndex) {
311        return new TypeReference((THROWS << 24) | (exceptionIndex << 8));
312    }
313
314    /**
315     * Returns a reference to the type of the exception declared in a 'catch'
316     * clause of a method.
317     *
318     * @param tryCatchBlockIndex
319     *            the index of a try catch block (using the order in which they
320     *            are visited with visitTryCatchBlock).
321     *
322     * @return a reference to the type of the given exception.
323     */
324    public static TypeReference newTryCatchReference(int tryCatchBlockIndex) {
325        return new TypeReference((EXCEPTION_PARAMETER << 24)
326                | (tryCatchBlockIndex << 8));
327    }
328
329    /**
330     * Returns a reference to the type of a type argument in a constructor or
331     * method call or reference.
332     *
333     * @param sort
334     *            {@link #CAST CAST},
335     *            {@link #CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT
336     *            CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT},
337     *            {@link #METHOD_INVOCATION_TYPE_ARGUMENT
338     *            METHOD_INVOCATION_TYPE_ARGUMENT},
339     *            {@link #CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT
340     *            CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT}, or
341     *            {@link #METHOD_REFERENCE_TYPE_ARGUMENT
342     *            METHOD_REFERENCE_TYPE_ARGUMENT}.
343     * @param argIndex
344     *            the type argument index.
345     *
346     * @return a reference to the type of the given type argument.
347     */
348    public static TypeReference newTypeArgumentReference(int sort, int argIndex) {
349        return new TypeReference((sort << 24) | argIndex);
350    }
351
352    /**
353     * Returns the sort of this type reference.
354     *
355     * @return {@link #CLASS_TYPE_PARAMETER CLASS_TYPE_PARAMETER},
356     *         {@link #METHOD_TYPE_PARAMETER METHOD_TYPE_PARAMETER},
357     *         {@link #CLASS_EXTENDS CLASS_EXTENDS},
358     *         {@link #CLASS_TYPE_PARAMETER_BOUND CLASS_TYPE_PARAMETER_BOUND},
359     *         {@link #METHOD_TYPE_PARAMETER_BOUND METHOD_TYPE_PARAMETER_BOUND},
360     *         {@link #FIELD FIELD}, {@link #METHOD_RETURN METHOD_RETURN},
361     *         {@link #METHOD_RECEIVER METHOD_RECEIVER},
362     *         {@link #METHOD_FORMAL_PARAMETER METHOD_FORMAL_PARAMETER},
363     *         {@link #THROWS THROWS}, {@link #LOCAL_VARIABLE LOCAL_VARIABLE},
364     *         {@link #RESOURCE_VARIABLE RESOURCE_VARIABLE},
365     *         {@link #EXCEPTION_PARAMETER EXCEPTION_PARAMETER},
366     *         {@link #INSTANCEOF INSTANCEOF}, {@link #NEW NEW},
367     *         {@link #CONSTRUCTOR_REFERENCE CONSTRUCTOR_REFERENCE},
368     *         {@link #METHOD_REFERENCE METHOD_REFERENCE}, {@link #CAST CAST},
369     *         {@link #CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT
370     *         CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT},
371     *         {@link #METHOD_INVOCATION_TYPE_ARGUMENT
372     *         METHOD_INVOCATION_TYPE_ARGUMENT},
373     *         {@link #CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT
374     *         CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT}, or
375     *         {@link #METHOD_REFERENCE_TYPE_ARGUMENT
376     *         METHOD_REFERENCE_TYPE_ARGUMENT}.
377     */
378    public int getSort() {
379        return value >>> 24;
380    }
381
382    /**
383     * Returns the index of the type parameter referenced by this type
384     * reference. This method must only be used for type references whose sort
385     * is {@link #CLASS_TYPE_PARAMETER CLASS_TYPE_PARAMETER},
386     * {@link #METHOD_TYPE_PARAMETER METHOD_TYPE_PARAMETER},
387     * {@link #CLASS_TYPE_PARAMETER_BOUND CLASS_TYPE_PARAMETER_BOUND} or
388     * {@link #METHOD_TYPE_PARAMETER_BOUND METHOD_TYPE_PARAMETER_BOUND}.
389     *
390     * @return a type parameter index.
391     */
392    public int getTypeParameterIndex() {
393        return (value & 0x00FF0000) >> 16;
394    }
395
396    /**
397     * Returns the index of the type parameter bound, within the type parameter
398     * {@link #getTypeParameterIndex}, referenced by this type reference. This
399     * method must only be used for type references whose sort is
400     * {@link #CLASS_TYPE_PARAMETER_BOUND CLASS_TYPE_PARAMETER_BOUND} or
401     * {@link #METHOD_TYPE_PARAMETER_BOUND METHOD_TYPE_PARAMETER_BOUND}.
402     *
403     * @return a type parameter bound index.
404     */
405    public int getTypeParameterBoundIndex() {
406        return (value & 0x0000FF00) >> 8;
407    }
408
409    /**
410     * Returns the index of the "super type" of a class that is referenced by
411     * this type reference. This method must only be used for type references
412     * whose sort is {@link #CLASS_EXTENDS CLASS_EXTENDS}.
413     *
414     * @return the index of an interface in the 'implements' clause of a class,
415     *         or -1 if this type reference references the type of the super
416     *         class.
417     */
418    public int getSuperTypeIndex() {
419        return (short) ((value & 0x00FFFF00) >> 8);
420    }
421
422    /**
423     * Returns the index of the formal parameter whose type is referenced by
424     * this type reference. This method must only be used for type references
425     * whose sort is {@link #METHOD_FORMAL_PARAMETER METHOD_FORMAL_PARAMETER}.
426     *
427     * @return a formal parameter index.
428     */
429    public int getFormalParameterIndex() {
430        return (value & 0x00FF0000) >> 16;
431    }
432
433    /**
434     * Returns the index of the exception, in a 'throws' clause of a method,
435     * whose type is referenced by this type reference. This method must only be
436     * used for type references whose sort is {@link #THROWS THROWS}.
437     *
438     * @return the index of an exception in the 'throws' clause of a method.
439     */
440    public int getExceptionIndex() {
441        return (value & 0x00FFFF00) >> 8;
442    }
443
444    /**
445     * Returns the index of the try catch block (using the order in which they
446     * are visited with visitTryCatchBlock), whose 'catch' type is referenced by
447     * this type reference. This method must only be used for type references
448     * whose sort is {@link #EXCEPTION_PARAMETER EXCEPTION_PARAMETER} .
449     *
450     * @return the index of an exception in the 'throws' clause of a method.
451     */
452    public int getTryCatchBlockIndex() {
453        return (value & 0x00FFFF00) >> 8;
454    }
455
456    /**
457     * Returns the index of the type argument referenced by this type reference.
458     * This method must only be used for type references whose sort is
459     * {@link #CAST CAST}, {@link #CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT
460     * CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT},
461     * {@link #METHOD_INVOCATION_TYPE_ARGUMENT METHOD_INVOCATION_TYPE_ARGUMENT},
462     * {@link #CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT
463     * CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT}, or
464     * {@link #METHOD_REFERENCE_TYPE_ARGUMENT METHOD_REFERENCE_TYPE_ARGUMENT}.
465     *
466     * @return a type parameter index.
467     */
468    public int getTypeArgumentIndex() {
469        return value & 0xFF;
470    }
471
472    /**
473     * Returns the int encoded value of this type reference, suitable for use in
474     * visit methods related to type annotations, like visitTypeAnnotation.
475     *
476     * @return the int encoded value of this type reference.
477     */
478    public int getValue() {
479        return value;
480    }
481}
482