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.Const;
25  import org.apache.bcel.classfile.Constant;
26  import org.apache.bcel.classfile.ConstantClass;
27  import org.apache.bcel.classfile.ConstantPool;
28  import org.apache.bcel.classfile.Utility;
29  import org.apache.bcel.util.ByteSequence;
30  
31  /**
32   * Abstract superclass for instructions that use an index into the constant pool such as LDC, INVOKEVIRTUAL, and so on.
33   *
34   * @see ConstantPoolGen
35   * @see LDC
36   * @see INVOKEVIRTUAL
37   */
38  public abstract class CPInstruction extends Instruction implements TypedInstruction, IndexedInstruction {
39  
40      /**
41       * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
42       */
43      @Deprecated
44      protected int index; // index to constant pool
45  
46      /**
47       * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
48       */
49      CPInstruction() {
50      }
51  
52      /**
53       * Constructs a CPInstruction.
54       *
55       * @param opcode The opcode.
56       * @param index to constant pool.
57       */
58      protected CPInstruction(final short opcode, final int index) {
59          super(opcode, (short) 3);
60          setIndex(index);
61      }
62  
63      /**
64       * Dumps instruction as byte code to stream out.
65       *
66       * @param out Output stream.
67       */
68      @Override
69      public void dump(final DataOutputStream out) throws IOException {
70          out.writeByte(super.getOpcode());
71          out.writeShort(index);
72      }
73  
74      /**
75       * Gets the constant pool index referred to by this instruction.
76       *
77       * @return index in constant pool referred by this instruction.
78       */
79      @Override
80      public final int getIndex() {
81          return index;
82      }
83  
84      /**
85       * Gets the type associated with this instruction.
86       *
87       * @return type related with this instruction.
88       */
89      @Override
90      public Type getType(final ConstantPoolGen cpg) {
91          final ConstantPool cp = cpg.getConstantPool();
92          String name = cp.getConstantString(index, Const.CONSTANT_Class);
93          if (!name.startsWith("[")) {
94              name = "L" + name + ";";
95          }
96          return Type.getType(name);
97      }
98  
99      /**
100      * Reads needed data (that is, index) from file.
101      *
102      * @param bytes input stream.
103      * @param wide wide prefix?.
104      */
105     @Override
106     protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
107         setIndex(bytes.readUnsignedShort());
108         super.setLength(3);
109     }
110 
111     /**
112      * Sets the index to constant pool.
113      *
114      * @param index in constant pool.
115      * @throws ClassGenException Thrown if index is out of bounds.
116      */
117     @Override
118     public void setIndex(final int index) { // TODO could be package-protected?
119         if (!isNonNegativeUShort(index)) {
120             throw new ClassGenException("Illegal index: " + index);
121         }
122         this.index = index;
123     }
124 
125     /**
126      * Long output format:
127      *
128      * <name of opcode> "["<opcode number>"]" "("<length of instruction>")" "<"< constant pool
129      * index>">"
130      *
131      * @param verbose long/short format switch.
132      * @return mnemonic for instruction.
133      */
134     @Override
135     public String toString(final boolean verbose) {
136         return super.toString(verbose) + " " + index;
137     }
138 
139     /**
140      * @return mnemonic for instruction with symbolic references resolved.
141      */
142     @Override
143     public String toString(final ConstantPool cp) {
144         final Constant c = cp.getConstant(index);
145         String str = cp.constantToString(c);
146         if (c instanceof ConstantClass) {
147             str = Utility.packageToPath(str);
148         }
149         return Const.getOpcodeName(super.getOpcode()) + " " + str;
150     }
151 }