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 super class for instructions that use an index into the constant pool such as LDC, INVOKEVIRTUAL, etc.
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 * @return index in constant pool referred by this instruction.
76 */
77 @Override
78 public final int getIndex() {
79 return index;
80 }
81
82 /**
83 * @return type related with this instruction.
84 */
85 @Override
86 public Type getType(final ConstantPoolGen cpg) {
87 final ConstantPool cp = cpg.getConstantPool();
88 String name = cp.getConstantString(index, Const.CONSTANT_Class);
89 if (!name.startsWith("[")) {
90 name = "L" + name + ";";
91 }
92 return Type.getType(name);
93 }
94
95 /**
96 * Reads needed data (that is, index) from file.
97 *
98 * @param bytes input stream.
99 * @param wide wide prefix?.
100 */
101 @Override
102 protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
103 setIndex(bytes.readUnsignedShort());
104 super.setLength(3);
105 }
106
107 /**
108 * Sets the index to constant pool.
109 *
110 * @param index in constant pool.
111 * @throws ClassGenException Thrown if index is out of bounds.
112 */
113 @Override
114 public void setIndex(final int index) { // TODO could be package-protected?
115 if (!isNonNegativeUShort(index)) {
116 throw new ClassGenException("Illegal index: " + index);
117 }
118 this.index = index;
119 }
120
121 /**
122 * Long output format:
123 *
124 * <name of opcode> "["<opcode number>"]" "("<length of instruction>")" "<"< constant pool
125 * index>">"
126 *
127 * @param verbose long/short format switch.
128 * @return mnemonic for instruction.
129 */
130 @Override
131 public String toString(final boolean verbose) {
132 return super.toString(verbose) + " " + index;
133 }
134
135 /**
136 * @return mnemonic for instruction with symbolic references resolved.
137 */
138 @Override
139 public String toString(final ConstantPool cp) {
140 final Constant c = cp.getConstant(index);
141 String str = cp.constantToString(c);
142 if (c instanceof ConstantClass) {
143 str = Utility.packageToPath(str);
144 }
145 return Const.getOpcodeName(super.getOpcode()) + " " + str;
146 }
147 }