InvokeInstruction.java

  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. package org.apache.bcel.generic;

  18. import java.util.StringTokenizer;

  19. import org.apache.bcel.Const;
  20. import org.apache.bcel.classfile.Constant;
  21. import org.apache.bcel.classfile.ConstantCP;
  22. import org.apache.bcel.classfile.ConstantPool;
  23. import org.apache.bcel.classfile.Utility;

  24. /**
  25.  * Super class for the INVOKExxx family of instructions.
  26.  */
  27. public abstract class InvokeInstruction extends FieldOrMethod implements ExceptionThrower, StackConsumer, StackProducer {

  28.     /**
  29.      * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
  30.      */
  31.     InvokeInstruction() {
  32.     }

  33.     /**
  34.      * @param index to constant pool
  35.      */
  36.     protected InvokeInstruction(final short opcode, final int index) {
  37.         super(opcode, index);
  38.     }

  39.     /**
  40.      * Also works for instructions whose stack effect depends on the constant pool entry they reference.
  41.      *
  42.      * @return Number of words consumed from stack by this instruction
  43.      */
  44.     @Override
  45.     public int consumeStack(final ConstantPoolGen cpg) {
  46.         int sum;
  47.         if (super.getOpcode() == Const.INVOKESTATIC || super.getOpcode() == Const.INVOKEDYNAMIC) {
  48.             sum = 0;
  49.         } else {
  50.             sum = 1; // this reference
  51.         }

  52.         final String signature = getSignature(cpg);
  53.         sum += Type.getArgumentTypesSize(signature);
  54.         return sum;
  55.     }

  56.     /**
  57.      * @return argument types of referenced method.
  58.      */
  59.     public Type[] getArgumentTypes(final ConstantPoolGen cpg) {
  60.         return Type.getArgumentTypes(getSignature(cpg));
  61.     }

  62.     /**
  63.      * This overrides the deprecated version as we know here that the referenced class may legally be an array.
  64.      *
  65.      * @return name of the referenced class/interface
  66.      * @throws IllegalArgumentException if the referenced class is an array (this should not happen)
  67.      */
  68.     @Override
  69.     public String getClassName(final ConstantPoolGen cpg) {
  70.         final ConstantPool cp = cpg.getConstantPool();
  71.         final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
  72.         final String className = cp.getConstantString(cmr.getClassIndex(), Const.CONSTANT_Class);
  73.         return Utility.pathToPackage(className);
  74.     }

  75.     /**
  76.      * @return name of referenced method.
  77.      */
  78.     public String getMethodName(final ConstantPoolGen cpg) {
  79.         return getName(cpg);
  80.     }

  81.     /**
  82.      * @return return type of referenced method.
  83.      */
  84.     public Type getReturnType(final ConstantPoolGen cpg) {
  85.         return Type.getReturnType(getSignature(cpg));
  86.     }

  87.     /**
  88.      * @return return type of referenced method.
  89.      */
  90.     @Override
  91.     public Type getType(final ConstantPoolGen cpg) {
  92.         return getReturnType(cpg);
  93.     }

  94.     /**
  95.      * Also works for instructions whose stack effect depends on the constant pool entry they reference.
  96.      *
  97.      * @return Number of words produced onto stack by this instruction
  98.      */
  99.     @Override
  100.     public int produceStack(final ConstantPoolGen cpg) {
  101.         final String signature = getSignature(cpg);
  102.         return Type.getReturnTypeSize(signature);
  103.     }

  104.     /**
  105.      * @return mnemonic for instruction with symbolic references resolved
  106.      */
  107.     @Override
  108.     public String toString(final ConstantPool cp) {
  109.         final Constant c = cp.getConstant(super.getIndex());
  110.         final StringTokenizer tok = new StringTokenizer(cp.constantToString(c));

  111.         final String opcodeName = Const.getOpcodeName(super.getOpcode());

  112.         final StringBuilder sb = new StringBuilder(opcodeName);
  113.         if (tok.hasMoreTokens()) {
  114.             sb.append(" ");
  115.             sb.append(Utility.packageToPath(tok.nextToken()));
  116.             if (tok.hasMoreTokens()) {
  117.                 sb.append(tok.nextToken());
  118.             }
  119.         }

  120.         return sb.toString();
  121.     }

  122. }