1 /* 2 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package com.sun.org.apache.bcel.internal.generic; 22 23 import java.util.StringTokenizer; 24 25 import com.sun.org.apache.bcel.internal.Const; 26 import com.sun.org.apache.bcel.internal.classfile.Constant; 27 import com.sun.org.apache.bcel.internal.classfile.ConstantCP; 28 import com.sun.org.apache.bcel.internal.classfile.ConstantPool; 29 30 /** 31 * Super class for the INVOKExxx family of instructions. 32 * 33 * @version $Id$ 34 * @LastModified: Jun 2019 35 */ 36 public abstract class InvokeInstruction extends FieldOrMethod implements ExceptionThrower, 37 StackConsumer, StackProducer { 38 39 /** 40 * Empty constructor needed for Instruction.readInstruction. 41 * Not to be used otherwise. 42 */ 43 InvokeInstruction() { 44 } 45 46 47 /** 48 * @param index to constant pool 49 */ 50 protected InvokeInstruction(final short opcode, final int index) { 51 super(opcode, index); 52 } 53 54 55 /** 56 * @return mnemonic for instruction with symbolic references resolved 57 */ 58 @Override 59 public String toString( final ConstantPool cp ) { 60 final Constant c = cp.getConstant(super.getIndex()); 61 final StringTokenizer tok = new StringTokenizer(cp.constantToString(c)); 62 return Const.getOpcodeName(super.getOpcode()) + " " + tok.nextToken().replace('.', '/') 63 + tok.nextToken(); 64 } 65 66 67 /** 68 * Also works for instructions whose stack effect depends on the 69 * constant pool entry they reference. 70 * @return Number of words consumed from stack by this instruction 71 */ 72 @Override 73 public int consumeStack( final ConstantPoolGen cpg ) { 74 int sum; 75 if ((super.getOpcode() == Const.INVOKESTATIC) || (super.getOpcode() == Const.INVOKEDYNAMIC)) { 76 sum = 0; 77 } else { 78 sum = 1; // this reference 79 } 80 81 final String signature = getSignature(cpg); 82 sum += Type.getArgumentTypesSize(signature); 83 return sum; | 1 /* 2 * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package com.sun.org.apache.bcel.internal.generic; 22 23 import java.util.StringTokenizer; 24 25 import com.sun.org.apache.bcel.internal.Const; 26 import com.sun.org.apache.bcel.internal.classfile.Constant; 27 import com.sun.org.apache.bcel.internal.classfile.ConstantCP; 28 import com.sun.org.apache.bcel.internal.classfile.ConstantPool; 29 30 /** 31 * Super class for the INVOKExxx family of instructions. 32 * 33 * @LastModified: Jan 2020 34 */ 35 public abstract class InvokeInstruction extends FieldOrMethod implements ExceptionThrower, 36 StackConsumer, StackProducer { 37 38 /** 39 * Empty constructor needed for Instruction.readInstruction. 40 * Not to be used otherwise. 41 */ 42 InvokeInstruction() { 43 } 44 45 46 /** 47 * @param index to constant pool 48 */ 49 protected InvokeInstruction(final short opcode, final int index) { 50 super(opcode, index); 51 } 52 53 54 /** 55 * @return mnemonic for instruction with symbolic references resolved 56 */ 57 @Override 58 public String toString( final ConstantPool cp ) { 59 final Constant c = cp.getConstant(super.getIndex()); 60 final StringTokenizer tok = new StringTokenizer(cp.constantToString(c)); 61 62 final String opcodeName = Const.getOpcodeName(super.getOpcode()); 63 64 final StringBuilder sb = new StringBuilder(opcodeName); 65 if (tok.hasMoreTokens()) { 66 sb.append(" "); 67 sb.append(tok.nextToken().replace('.', '/')); 68 if (tok.hasMoreTokens()) { 69 sb.append(tok.nextToken()); 70 } 71 } 72 73 return sb.toString(); 74 } 75 76 77 /** 78 * Also works for instructions whose stack effect depends on the 79 * constant pool entry they reference. 80 * @return Number of words consumed from stack by this instruction 81 */ 82 @Override 83 public int consumeStack( final ConstantPoolGen cpg ) { 84 int sum; 85 if ((super.getOpcode() == Const.INVOKESTATIC) || (super.getOpcode() == Const.INVOKEDYNAMIC)) { 86 sum = 0; 87 } else { 88 sum = 1; // this reference 89 } 90 91 final String signature = getSignature(cpg); 92 sum += Type.getArgumentTypesSize(signature); 93 return sum; |