View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.generic;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.classfile.Constant;
25  import org.apache.bcel.classfile.ConstantClass;
26  import org.apache.bcel.classfile.ConstantPool;
27  import org.apache.bcel.classfile.Utility;
28  import org.apache.bcel.util.ByteSequence;
29  
30  /**
31   * Abstract super class for instructions that use an index into the constant pool such as LDC, INVOKEVIRTUAL, etc.
32   *
33   * @see ConstantPoolGen
34   * @see LDC
35   * @see INVOKEVIRTUAL
36   */
37  public abstract class CPInstruction extends Instruction implements TypedInstruction, IndexedInstruction {
38  
39      /**
40       * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
41       */
42      @Deprecated
43      protected int index; // index to constant pool
44  
45      /**
46       * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
47       */
48      CPInstruction() {
49      }
50  
51      /**
52       * Constructs a CPInstruction.
53       *
54       * @param opcode the opcode.
55       * @param index to constant pool.
56       */
57      protected CPInstruction(final short opcode, final int index) {
58          super(opcode, (short) 3);
59          setIndex(index);
60      }
61  
62      /**
63       * Dumps instruction as byte code to stream out.
64       *
65       * @param out Output stream.
66       */
67      @Override
68      public void dump(final DataOutputStream out) throws IOException {
69          out.writeByte(super.getOpcode());
70          out.writeShort(index);
71      }
72  
73      /**
74       * @return index in constant pool referred by this instruction.
75       */
76      @Override
77      public final int getIndex() {
78          return index;
79      }
80  
81      /**
82       * @return type related with this instruction.
83       */
84      @Override
85      public Type getType(final ConstantPoolGen cpg) {
86          final ConstantPool cp = cpg.getConstantPool();
87          String name = cp.getConstantString(index, org.apache.bcel.Const.CONSTANT_Class);
88          if (!name.startsWith("[")) {
89              name = "L" + name + ";";
90          }
91          return Type.getType(name);
92      }
93  
94      /**
95       * Reads needed data (that is, index) from file.
96       *
97       * @param bytes input stream.
98       * @param wide wide prefix?.
99       */
100     @Override
101     protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
102         setIndex(bytes.readUnsignedShort());
103         super.setLength(3);
104     }
105 
106     /**
107      * Sets the index to constant pool.
108      *
109      * @param index in constant pool.
110      */
111     @Override
112     public void setIndex(final int index) { // TODO could be package-protected?
113         if (index < 0) {
114             throw new ClassGenException("Negative index value: " + index);
115         }
116         this.index = index;
117     }
118 
119     /**
120      * Long output format:
121      *
122      * &lt;name of opcode&gt; "["&lt;opcode number&gt;"]" "("&lt;length of instruction&gt;")" "&lt;"&lt; constant pool
123      * index&gt;"&gt;"
124      *
125      * @param verbose long/short format switch.
126      * @return mnemonic for instruction.
127      */
128     @Override
129     public String toString(final boolean verbose) {
130         return super.toString(verbose) + " " + index;
131     }
132 
133     /**
134      * @return mnemonic for instruction with symbolic references resolved.
135      */
136     @Override
137     public String toString(final ConstantPool cp) {
138         final Constant c = cp.getConstant(index);
139         String str = cp.constantToString(c);
140         if (c instanceof ConstantClass) {
141             str = Utility.packageToPath(str);
142         }
143         return org.apache.bcel.Const.getOpcodeName(super.getOpcode()) + " " + str;
144     }
145 }