• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // ASM: a very small and fast Java bytecode manipulation framework
2 // Copyright (c) 2000-2011 INRIA, France Telecom
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 //    notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 //    notice, this list of conditions and the following disclaimer in the
12 //    documentation and/or other materials provided with the distribution.
13 // 3. Neither the name of the copyright holders nor the names of its
14 //    contributors may be used to endorse or promote products derived from
15 //    this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 // THE POSSIBILITY OF SUCH DAMAGE.
28 
29 package org.objectweb.asm.tree;
30 
31 import java.util.List;
32 import org.objectweb.asm.Label;
33 import org.objectweb.asm.MethodVisitor;
34 import org.objectweb.asm.Opcodes;
35 import org.objectweb.asm.TypePath;
36 
37 /**
38  * A node that represents a type annotation on a local or resource variable.
39  *
40  * @author Eric Bruneton
41  */
42 public class LocalVariableAnnotationNode extends TypeAnnotationNode {
43 
44   /**
45    * The fist instructions corresponding to the continuous ranges that make the scope of this local
46    * variable (inclusive). Must not be {@literal null}.
47    */
48   public List<LabelNode> start;
49 
50   /**
51    * The last instructions corresponding to the continuous ranges that make the scope of this local
52    * variable (exclusive). This list must have the same size as the 'start' list. Must not be
53    * {@literal null}.
54    */
55   public List<LabelNode> end;
56 
57   /**
58    * The local variable's index in each range. This list must have the same size as the 'start'
59    * list. Must not be {@literal null}.
60    */
61   public List<Integer> index;
62 
63   /**
64    * Constructs a new {@link LocalVariableAnnotationNode}. <i>Subclasses must not use this
65    * constructor</i>. Instead, they must use the {@link #LocalVariableAnnotationNode(int, TypePath,
66    * LabelNode[], LabelNode[], int[], String)} version.
67    *
68    * @param typeRef a reference to the annotated type. See {@link org.objectweb.asm.TypeReference}.
69    * @param typePath the path to the annotated type argument, wildcard bound, array element type, or
70    *     static inner type within 'typeRef'. May be {@literal null} if the annotation targets
71    *     'typeRef' as a whole.
72    * @param start the fist instructions corresponding to the continuous ranges that make the scope
73    *     of this local variable (inclusive).
74    * @param end the last instructions corresponding to the continuous ranges that make the scope of
75    *     this local variable (exclusive). This array must have the same size as the 'start' array.
76    * @param index the local variable's index in each range. This array must have the same size as
77    *     the 'start' array.
78    * @param descriptor the class descriptor of the annotation class.
79    */
LocalVariableAnnotationNode( final int typeRef, final TypePath typePath, final LabelNode[] start, final LabelNode[] end, final int[] index, final String descriptor)80   public LocalVariableAnnotationNode(
81       final int typeRef,
82       final TypePath typePath,
83       final LabelNode[] start,
84       final LabelNode[] end,
85       final int[] index,
86       final String descriptor) {
87     this(/* latest api = */ Opcodes.ASM9, typeRef, typePath, start, end, index, descriptor);
88   }
89 
90   /**
91    * Constructs a new {@link LocalVariableAnnotationNode}.
92    *
93    * @param api the ASM API version implemented by this visitor. Must be one of the {@code
94    *     ASM}<i>x</i> values in {@link Opcodes}.
95    * @param typeRef a reference to the annotated type. See {@link org.objectweb.asm.TypeReference}.
96    * @param start the fist instructions corresponding to the continuous ranges that make the scope
97    *     of this local variable (inclusive).
98    * @param end the last instructions corresponding to the continuous ranges that make the scope of
99    *     this local variable (exclusive). This array must have the same size as the 'start' array.
100    * @param index the local variable's index in each range. This array must have the same size as
101    *     the 'start' array.
102    * @param typePath the path to the annotated type argument, wildcard bound, array element type, or
103    *     static inner type within 'typeRef'. May be {@literal null} if the annotation targets
104    *     'typeRef' as a whole.
105    * @param descriptor the class descriptor of the annotation class.
106    */
LocalVariableAnnotationNode( final int api, final int typeRef, final TypePath typePath, final LabelNode[] start, final LabelNode[] end, final int[] index, final String descriptor)107   public LocalVariableAnnotationNode(
108       final int api,
109       final int typeRef,
110       final TypePath typePath,
111       final LabelNode[] start,
112       final LabelNode[] end,
113       final int[] index,
114       final String descriptor) {
115     super(api, typeRef, typePath, descriptor);
116     this.start = Util.asArrayList(start);
117     this.end = Util.asArrayList(end);
118     this.index = Util.asArrayList(index);
119   }
120 
121   /**
122    * Makes the given visitor visit this type annotation.
123    *
124    * @param methodVisitor the visitor that must visit this annotation.
125    * @param visible {@literal true} if the annotation is visible at runtime.
126    */
accept(final MethodVisitor methodVisitor, final boolean visible)127   public void accept(final MethodVisitor methodVisitor, final boolean visible) {
128     Label[] startLabels = new Label[this.start.size()];
129     Label[] endLabels = new Label[this.end.size()];
130     int[] indices = new int[this.index.size()];
131     for (int i = 0, n = startLabels.length; i < n; ++i) {
132       startLabels[i] = this.start.get(i).getLabel();
133       endLabels[i] = this.end.get(i).getLabel();
134       indices[i] = this.index.get(i);
135     }
136     accept(
137         methodVisitor.visitLocalVariableAnnotation(
138             typeRef, typePath, startLabels, endLabels, indices, desc, visible));
139   }
140 }
141