View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.bcel.generic;
19  
20  import java.util.StringTokenizer;
21  
22  import org.apache.bcel.Const;
23  import org.apache.bcel.classfile.Constant;
24  import org.apache.bcel.classfile.ConstantCP;
25  import org.apache.bcel.classfile.ConstantPool;
26  
27  /**
28   * Super class for the INVOKExxx family of instructions.
29   *
30   */
31  public abstract class InvokeInstruction extends FieldOrMethod implements ExceptionThrower,
32          StackConsumer, StackProducer {
33  
34      /**
35       * Empty constructor needed for Instruction.readInstruction.
36       * Not to be used otherwise.
37       */
38      InvokeInstruction() {
39      }
40  
41  
42      /**
43       * @param index to constant pool
44       */
45      protected InvokeInstruction(final short opcode, final int index) {
46          super(opcode, index);
47      }
48  
49  
50      /**
51       * @return mnemonic for instruction with symbolic references resolved
52       */
53      @Override
54      public String toString( final ConstantPool cp ) {
55          final Constant c = cp.getConstant(super.getIndex());
56          final StringTokenizer tok = new StringTokenizer(cp.constantToString(c));
57  
58          final String opcodeName = Const.getOpcodeName(super.getOpcode());
59  
60          final StringBuilder sb = new StringBuilder(opcodeName);
61          if (tok.hasMoreTokens()) {
62              sb.append(" ");
63              sb.append(tok.nextToken().replace('.', '/'));
64              if (tok.hasMoreTokens()) {
65                  sb.append(tok.nextToken());
66              }
67          }
68  
69          return sb.toString();
70      }
71  
72  
73      /**
74       * Also works for instructions whose stack effect depends on the
75       * constant pool entry they reference.
76       * @return Number of words consumed from stack by this instruction
77       */
78      @Override
79      public int consumeStack( final ConstantPoolGen cpg ) {
80          int sum;
81          if ((super.getOpcode() == Const.INVOKESTATIC) || (super.getOpcode() == Const.INVOKEDYNAMIC)) {
82              sum = 0;
83          } else {
84              sum = 1; // this reference
85          }
86  
87          final String signature = getSignature(cpg);
88          sum += Type.getArgumentTypesSize(signature);
89          return sum;
90      }
91  
92  
93      /**
94       * Also works for instructions whose stack effect depends on the
95       * constant pool entry they reference.
96       * @return Number of words produced onto stack by this instruction
97       */
98      @Override
99      public int produceStack( final ConstantPoolGen cpg ) {
100         final String signature = getSignature(cpg);
101         return Type.getReturnTypeSize(signature);
102     }
103 
104     /**
105      * This overrides the deprecated version as we know here that the referenced class
106      * may legally be an array.
107      *
108      * @return name of the referenced class/interface
109      * @throws IllegalArgumentException if the referenced class is an array (this should not happen)
110      */
111     @Override
112     public String getClassName( final ConstantPoolGen cpg ) {
113         final ConstantPool cp = cpg.getConstantPool();
114         final ConstantCP./../../../org/apache/bcel/classfile/ConstantCP.html#ConstantCP">ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
115         final String className = cp.getConstantString(cmr.getClassIndex(), Const.CONSTANT_Class);
116         return className.replace('/', '.');
117     }
118 
119     /** @return return type of referenced method.
120      */
121     @Override
122     public Type getType( final ConstantPoolGen cpg ) {
123         return getReturnType(cpg);
124     }
125 
126 
127     /** @return name of referenced method.
128      */
129     public String getMethodName( final ConstantPoolGen cpg ) {
130         return getName(cpg);
131     }
132 
133 
134     /** @return return type of referenced method.
135      */
136     public Type getReturnType( final ConstantPoolGen cpg ) {
137         return Type.getReturnType(getSignature(cpg));
138     }
139 
140 
141     /** @return argument types of referenced method.
142      */
143     public Type[] getArgumentTypes( final ConstantPoolGen cpg ) {
144         return Type.getArgumentTypes(getSignature(cpg));
145     }
146 
147 }