src/share/classes/jdk/internal/org/objectweb/asm/tree/AbstractInsnNode.java

Print this page




  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 package jdk.internal.org.objectweb.asm.tree;
  60 

  61 import java.util.List;
  62 import java.util.Map;
  63 
  64 import jdk.internal.org.objectweb.asm.MethodVisitor;
  65 
  66 /**
  67  * A node that represents a bytecode instruction. <i>An instruction can appear
  68  * at most once in at most one {@link InsnList} at a time</i>.
  69  *
  70  * @author Eric Bruneton
  71  */
  72 public abstract class AbstractInsnNode {
  73 
  74     /**
  75      * The type of {@link InsnNode} instructions.
  76      */
  77     public static final int INSN = 0;
  78 
  79     /**
  80      * The type of {@link IntInsnNode} instructions.


 140      * The type of {@link MultiANewArrayInsnNode} instructions.
 141      */
 142     public static final int MULTIANEWARRAY_INSN = 13;
 143 
 144     /**
 145      * The type of {@link FrameNode} "instructions".
 146      */
 147     public static final int FRAME = 14;
 148 
 149     /**
 150      * The type of {@link LineNumberNode} "instructions".
 151      */
 152     public static final int LINE = 15;
 153 
 154     /**
 155      * The opcode of this instruction.
 156      */
 157     protected int opcode;
 158 
 159     /**






















 160      * Previous instruction in the list to which this instruction belongs.
 161      */
 162     AbstractInsnNode prev;
 163 
 164     /**
 165      * Next instruction in the list to which this instruction belongs.
 166      */
 167     AbstractInsnNode next;
 168 
 169     /**
 170      * Index of this instruction in the list to which it belongs. The value of
 171      * this field is correct only when {@link InsnList#cache} is not null. A
 172      * value of -1 indicates that this instruction does not belong to any
 173      * {@link InsnList}.
 174      */
 175     int index;
 176 
 177     /**
 178      * Constructs a new {@link AbstractInsnNode}.
 179      *
 180      * @param opcode the opcode of the instruction to be constructed.

 181      */
 182     protected AbstractInsnNode(final int opcode) {
 183         this.opcode = opcode;
 184         this.index = -1;
 185     }
 186 
 187     /**
 188      * Returns the opcode of this instruction.
 189      *
 190      * @return the opcode of this instruction.
 191      */
 192     public int getOpcode() {
 193         return opcode;
 194     }
 195 
 196     /**
 197      * Returns the type of this instruction.
 198      *
 199      * @return the type of this instruction, i.e. one the constants defined in
 200      *         this class.


 209      *         belongs, if any. May be <tt>null</tt>.
 210      */
 211     public AbstractInsnNode getPrevious() {
 212         return prev;
 213     }
 214 
 215     /**
 216      * Returns the next instruction in the list to which this instruction
 217      * belongs, if any.
 218      *
 219      * @return the next instruction in the list to which this instruction
 220      *         belongs, if any. May be <tt>null</tt>.
 221      */
 222     public AbstractInsnNode getNext() {
 223         return next;
 224     }
 225 
 226     /**
 227      * Makes the given code visitor visit this instruction.
 228      *
 229      * @param cv a code visitor.

 230      */
 231     public abstract void accept(final MethodVisitor cv);
 232 
 233     /**























 234      * Returns a copy of this instruction.
 235      *
 236      * @param labels a map from LabelNodes to cloned LabelNodes.

 237      * @return a copy of this instruction. The returned instruction does not
 238      *         belong to any {@link InsnList}.
 239      */
 240     public abstract AbstractInsnNode clone(final Map<LabelNode, LabelNode> labels);

 241 
 242     /**
 243      * Returns the clone of the given label.
 244      *
 245      * @param label a label.
 246      * @param map a map from LabelNodes to cloned LabelNodes.


 247      * @return the clone of the given label.
 248      */
 249     static LabelNode clone(final LabelNode label, final Map<LabelNode, LabelNode> map) {

 250         return map.get(label);
 251     }
 252 
 253     /**
 254      * Returns the clones of the given labels.
 255      *
 256      * @param labels a list of labels.
 257      * @param map a map from LabelNodes to cloned LabelNodes.


 258      * @return the clones of the given labels.
 259      */
 260     static LabelNode[] clone(final List<LabelNode> labels, final Map<LabelNode, LabelNode> map) {

 261         LabelNode[] clones = new LabelNode[labels.size()];
 262         for (int i = 0; i < clones.length; ++i) {
 263             clones[i] = map.get(labels.get(i));
 264         }
 265         return clones;
 266     }
































 267 }


  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 package jdk.internal.org.objectweb.asm.tree;
  60 
  61 import java.util.ArrayList;
  62 import java.util.List;
  63 import java.util.Map;
  64 
  65 import jdk.internal.org.objectweb.asm.MethodVisitor;
  66 
  67 /**
  68  * A node that represents a bytecode instruction. <i>An instruction can appear
  69  * at most once in at most one {@link InsnList} at a time</i>.
  70  *
  71  * @author Eric Bruneton
  72  */
  73 public abstract class AbstractInsnNode {
  74 
  75     /**
  76      * The type of {@link InsnNode} instructions.
  77      */
  78     public static final int INSN = 0;
  79 
  80     /**
  81      * The type of {@link IntInsnNode} instructions.


 141      * The type of {@link MultiANewArrayInsnNode} instructions.
 142      */
 143     public static final int MULTIANEWARRAY_INSN = 13;
 144 
 145     /**
 146      * The type of {@link FrameNode} "instructions".
 147      */
 148     public static final int FRAME = 14;
 149 
 150     /**
 151      * The type of {@link LineNumberNode} "instructions".
 152      */
 153     public static final int LINE = 15;
 154 
 155     /**
 156      * The opcode of this instruction.
 157      */
 158     protected int opcode;
 159 
 160     /**
 161      * The runtime visible type annotations of this instruction. This field is
 162      * only used for real instructions (i.e. not for labels, frames, or line
 163      * number nodes). This list is a list of {@link TypeAnnotationNode} objects.
 164      * May be <tt>null</tt>.
 165      *
 166      * @associates jdk.internal.org.objectweb.asm.tree.TypeAnnotationNode
 167      * @label visible
 168      */
 169     public List<TypeAnnotationNode> visibleTypeAnnotations;
 170 
 171     /**
 172      * The runtime invisible type annotations of this instruction. This field is
 173      * only used for real instructions (i.e. not for labels, frames, or line
 174      * number nodes). This list is a list of {@link TypeAnnotationNode} objects.
 175      * May be <tt>null</tt>.
 176      *
 177      * @associates jdk.internal.org.objectweb.asm.tree.TypeAnnotationNode
 178      * @label invisible
 179      */
 180     public List<TypeAnnotationNode> invisibleTypeAnnotations;
 181 
 182     /**
 183      * Previous instruction in the list to which this instruction belongs.
 184      */
 185     AbstractInsnNode prev;
 186 
 187     /**
 188      * Next instruction in the list to which this instruction belongs.
 189      */
 190     AbstractInsnNode next;
 191 
 192     /**
 193      * Index of this instruction in the list to which it belongs. The value of
 194      * this field is correct only when {@link InsnList#cache} is not null. A
 195      * value of -1 indicates that this instruction does not belong to any
 196      * {@link InsnList}.
 197      */
 198     int index;
 199 
 200     /**
 201      * Constructs a new {@link AbstractInsnNode}.
 202      *
 203      * @param opcode
 204      *            the opcode of the instruction to be constructed.
 205      */
 206     protected AbstractInsnNode(final int opcode) {
 207         this.opcode = opcode;
 208         this.index = -1;
 209     }
 210 
 211     /**
 212      * Returns the opcode of this instruction.
 213      *
 214      * @return the opcode of this instruction.
 215      */
 216     public int getOpcode() {
 217         return opcode;
 218     }
 219 
 220     /**
 221      * Returns the type of this instruction.
 222      *
 223      * @return the type of this instruction, i.e. one the constants defined in
 224      *         this class.


 233      *         belongs, if any. May be <tt>null</tt>.
 234      */
 235     public AbstractInsnNode getPrevious() {
 236         return prev;
 237     }
 238 
 239     /**
 240      * Returns the next instruction in the list to which this instruction
 241      * belongs, if any.
 242      *
 243      * @return the next instruction in the list to which this instruction
 244      *         belongs, if any. May be <tt>null</tt>.
 245      */
 246     public AbstractInsnNode getNext() {
 247         return next;
 248     }
 249 
 250     /**
 251      * Makes the given code visitor visit this instruction.
 252      *
 253      * @param cv
 254      *            a code visitor.
 255      */
 256     public abstract void accept(final MethodVisitor cv);
 257 
 258     /**
 259      * Makes the given visitor visit the annotations of this instruction.
 260      *
 261      * @param mv
 262      *            a method visitor.
 263      */
 264     protected final void acceptAnnotations(final MethodVisitor mv) {
 265         int n = visibleTypeAnnotations == null ? 0 : visibleTypeAnnotations
 266                 .size();
 267         for (int i = 0; i < n; ++i) {
 268             TypeAnnotationNode an = visibleTypeAnnotations.get(i);
 269             an.accept(mv.visitInsnAnnotation(an.typeRef, an.typePath, an.desc,
 270                     true));
 271         }
 272         n = invisibleTypeAnnotations == null ? 0 : invisibleTypeAnnotations
 273                 .size();
 274         for (int i = 0; i < n; ++i) {
 275             TypeAnnotationNode an = invisibleTypeAnnotations.get(i);
 276             an.accept(mv.visitInsnAnnotation(an.typeRef, an.typePath, an.desc,
 277                     false));
 278         }
 279     }
 280 
 281     /**
 282      * Returns a copy of this instruction.
 283      *
 284      * @param labels
 285      *            a map from LabelNodes to cloned LabelNodes.
 286      * @return a copy of this instruction. The returned instruction does not
 287      *         belong to any {@link InsnList}.
 288      */
 289     public abstract AbstractInsnNode clone(
 290             final Map<LabelNode, LabelNode> labels);
 291 
 292     /**
 293      * Returns the clone of the given label.
 294      *
 295      * @param label
 296      *            a label.
 297      * @param map
 298      *            a map from LabelNodes to cloned LabelNodes.
 299      * @return the clone of the given label.
 300      */
 301     static LabelNode clone(final LabelNode label,
 302             final Map<LabelNode, LabelNode> map) {
 303         return map.get(label);
 304     }
 305 
 306     /**
 307      * Returns the clones of the given labels.
 308      *
 309      * @param labels
 310      *            a list of labels.
 311      * @param map
 312      *            a map from LabelNodes to cloned LabelNodes.
 313      * @return the clones of the given labels.
 314      */
 315     static LabelNode[] clone(final List<LabelNode> labels,
 316             final Map<LabelNode, LabelNode> map) {
 317         LabelNode[] clones = new LabelNode[labels.size()];
 318         for (int i = 0; i < clones.length; ++i) {
 319             clones[i] = map.get(labels.get(i));
 320         }
 321         return clones;
 322     }
 323 
 324     /**
 325      * Clones the annotations of the given instruction into this instruction.
 326      *
 327      * @param insn
 328      *            the source instruction.
 329      * @return this instruction.
 330      */
 331     protected final AbstractInsnNode cloneAnnotations(
 332             final AbstractInsnNode insn) {
 333         if (insn.visibleTypeAnnotations != null) {
 334             this.visibleTypeAnnotations = new ArrayList<TypeAnnotationNode>();
 335             for (int i = 0; i < insn.visibleTypeAnnotations.size(); ++i) {
 336                 TypeAnnotationNode src = insn.visibleTypeAnnotations.get(i);
 337                 TypeAnnotationNode ann = new TypeAnnotationNode(src.typeRef,
 338                         src.typePath, src.desc);
 339                 src.accept(ann);
 340                 this.visibleTypeAnnotations.add(ann);
 341             }
 342         }
 343         if (insn.invisibleTypeAnnotations != null) {
 344             this.invisibleTypeAnnotations = new ArrayList<TypeAnnotationNode>();
 345             for (int i = 0; i < insn.invisibleTypeAnnotations.size(); ++i) {
 346                 TypeAnnotationNode src = insn.invisibleTypeAnnotations.get(i);
 347                 TypeAnnotationNode ann = new TypeAnnotationNode(src.typeRef,
 348                         src.typePath, src.desc);
 349                 src.accept(ann);
 350                 this.invisibleTypeAnnotations.add(ann);
 351             }
 352         }
 353         return this;
 354     }
 355 }