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-2011 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 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.
  81      */
  82     public static final int INT_INSN = 1;
  83 
  84     /**
  85      * The type of {@link VarInsnNode} instructions.
  86      */
  87     public static final int VAR_INSN = 2;
  88 
  89     /**
  90      * The type of {@link TypeInsnNode} instructions.
  91      */
  92     public static final int TYPE_INSN = 3;
  93 
  94     /**
  95      * The type of {@link FieldInsnNode} instructions.
  96      */
  97     public static final int FIELD_INSN = 4;
  98 
  99     /**
 100      * The type of {@link MethodInsnNode} instructions.
 101      */
 102     public static final int METHOD_INSN = 5;
 103 
 104     /**
 105      * The type of {@link InvokeDynamicInsnNode} instructions.
 106      */
 107     public static final int INVOKE_DYNAMIC_INSN = 6;
 108 
 109     /**
 110      * The type of {@link JumpInsnNode} instructions.
 111      */
 112     public static final int JUMP_INSN = 7;
 113 
 114     /**
 115      * The type of {@link LabelNode} "instructions".
 116      */
 117     public static final int LABEL = 8;
 118 
 119     /**
 120      * The type of {@link LdcInsnNode} instructions.
 121      */
 122     public static final int LDC_INSN = 9;
 123 
 124     /**
 125      * The type of {@link IincInsnNode} instructions.
 126      */
 127     public static final int IINC_INSN = 10;
 128 
 129     /**
 130      * The type of {@link TableSwitchInsnNode} instructions.
 131      */
 132     public static final int TABLESWITCH_INSN = 11;
 133 
 134     /**
 135      * The type of {@link LookupSwitchInsnNode} instructions.
 136      */
 137     public static final int LOOKUPSWITCH_INSN = 12;
 138 
 139     /**
 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.
 201      */
 202     public abstract int getType();
 203 
 204     /**
 205      * Returns the previous instruction in the list to which this instruction
 206      * belongs, if any.
 207      *
 208      * @return the previous instruction in the list to which this instruction
 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 }