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.util.StringTokenizer;
22
23 import org.apache.bcel.Const;
24 import org.apache.bcel.classfile.Constant;
25 import org.apache.bcel.classfile.ConstantCP;
26 import org.apache.bcel.classfile.ConstantPool;
27 import org.apache.bcel.classfile.Utility;
28
29 /**
30 * Superclass for the INVOKExxx family of instructions.
31 */
32 public abstract class InvokeInstruction extends FieldOrMethod implements ExceptionThrower, StackConsumer, StackProducer {
33
34 /**
35 * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
36 */
37 InvokeInstruction() {
38 }
39
40 /**
41 * Constructs an InvokeInstruction.
42 *
43 * @param opcode The opcode.
44 * @param index to constant pool.
45 */
46 protected InvokeInstruction(final short opcode, final int index) {
47 super(opcode, index);
48 }
49
50 /**
51 * Also works for instructions whose stack effect depends on the constant pool entry they reference.
52 *
53 * @return Number of words consumed from stack by this instruction.
54 */
55 @Override
56 public int consumeStack(final ConstantPoolGen cpg) {
57 int sum;
58 if (super.getOpcode() == Const.INVOKESTATIC || super.getOpcode() == Const.INVOKEDYNAMIC) {
59 sum = 0;
60 } else {
61 sum = 1; // this reference
62 }
63
64 final String signature = getSignature(cpg);
65 sum += Type.getArgumentTypesSize(signature);
66 return sum;
67 }
68
69 /**
70 * Gets the argument types of referenced method.
71 *
72 * @param cpg The constant pool generator.
73 * @return argument types of referenced method.
74 */
75 public Type[] getArgumentTypes(final ConstantPoolGen cpg) {
76 return Type.getArgumentTypes(getSignature(cpg));
77 }
78
79 /**
80 * Gets the name of the referenced class or interface.
81 * <p>
82 * This overrides the deprecated version as we know here that the referenced class may legally be an array.
83 * </p>
84 *
85 * @return name of the referenced class/interface.
86 * @throws IllegalArgumentException Thrown if the referenced class is an array (this should not happen)
87 */
88 @Override
89 public String getClassName(final ConstantPoolGen cpg) {
90 final ConstantPool cp = cpg.getConstantPool();
91 final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
92 final String className = cp.getConstantString(cmr.getClassIndex(), Const.CONSTANT_Class);
93 return Utility.pathToPackage(className);
94 }
95
96 /**
97 * Gets the name of referenced method.
98 *
99 * @param cpg The constant pool generator.
100 * @return name of referenced method.
101 */
102 public String getMethodName(final ConstantPoolGen cpg) {
103 return getName(cpg);
104 }
105
106 /**
107 * Gets the return type of referenced method.
108 *
109 * @param cpg The constant pool generator.
110 * @return return type of referenced method.
111 */
112 public Type getReturnType(final ConstantPoolGen cpg) {
113 return Type.getReturnType(getSignature(cpg));
114 }
115
116 /**
117 * Gets the return type of referenced method.
118 *
119 * @param cpg The constant pool generator.
120 * @return return type of referenced method.
121 */
122 @Override
123 public Type getType(final ConstantPoolGen cpg) {
124 return getReturnType(cpg);
125 }
126
127 /**
128 * Also works for instructions whose stack effect depends on the constant pool entry they reference.
129 *
130 * @return Number of words produced onto stack by this instruction.
131 */
132 @Override
133 public int produceStack(final ConstantPoolGen cpg) {
134 final String signature = getSignature(cpg);
135 return Type.getReturnTypeSize(signature);
136 }
137
138 /**
139 * @return mnemonic for instruction with symbolic references resolved.
140 */
141 @Override
142 public String toString(final ConstantPool cp) {
143 final Constant c = cp.getConstant(super.getIndex());
144 final StringTokenizer tok = new StringTokenizer(cp.constantToString(c));
145
146 final String opcodeName = Const.getOpcodeName(super.getOpcode());
147
148 final StringBuilder sb = new StringBuilder(opcodeName);
149 if (tok.hasMoreTokens()) {
150 sb.append(" ");
151 sb.append(Utility.packageToPath(tok.nextToken()));
152 if (tok.hasMoreTokens()) {
153 sb.append(tok.nextToken());
154 }
155 }
156
157 return sb.toString();
158 }
159
160 }
161