001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License. 
016     *
017     */
018    package org.apache.bcel.generic;
019    
020    import java.util.StringTokenizer;
021    import org.apache.bcel.Constants;
022    import org.apache.bcel.classfile.Constant;
023    import org.apache.bcel.classfile.ConstantPool;
024    
025    /**
026     * Super class for the INVOKExxx family of instructions.
027     *
028     * @version $Id: InvokeInstruction.java 1152072 2011-07-29 01:54:05Z dbrosius $
029     * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
030     */
031    public abstract class InvokeInstruction extends FieldOrMethod implements ExceptionThrower,
032            StackConsumer, StackProducer {
033    
034        private static final long serialVersionUID = 6089031137856650442L;
035    
036    
037        /**
038         * Empty constructor needed for the Class.newInstance() statement in
039         * Instruction.readInstruction(). Not to be used otherwise.
040         */
041        InvokeInstruction() {
042        }
043    
044    
045        /**
046         * @param index to constant pool
047         */
048        protected InvokeInstruction(short opcode, int index) {
049            super(opcode, index);
050        }
051    
052    
053        /**
054         * @return mnemonic for instruction with symbolic references resolved
055         */
056        @Override
057        public String toString( ConstantPool cp ) {
058            Constant c = cp.getConstant(index);
059            StringTokenizer tok = new StringTokenizer(cp.constantToString(c));
060            return Constants.OPCODE_NAMES[opcode] + " " + tok.nextToken().replace('.', '/')
061                    + tok.nextToken();
062        }
063    
064    
065        /**
066         * Also works for instructions whose stack effect depends on the
067         * constant pool entry they reference.
068         * @return Number of words consumed from stack by this instruction
069         */
070        @Override
071        public int consumeStack( ConstantPoolGen cpg ) {
072            int sum;
073            if (opcode == Constants.INVOKESTATIC) {
074                sum = 0;
075            } else {
076                sum = 1; // this reference
077            }
078            
079            String signature = getSignature(cpg);
080            sum += Type.getArgumentTypesSize(signature);
081            return sum;
082        }
083    
084    
085        /**
086         * Also works for instructions whose stack effect depends on the
087         * constant pool entry they reference.
088         * @return Number of words produced onto stack by this instruction
089         */
090        @Override
091        public int produceStack( ConstantPoolGen cpg ) {
092            String signature = getSignature(cpg);
093            return Type.getReturnTypeSize(signature);
094        }
095    
096    
097        /** @return return type of referenced method.
098         */
099        @Override
100        public Type getType( ConstantPoolGen cpg ) {
101            return getReturnType(cpg);
102        }
103    
104    
105        /** @return name of referenced method.
106         */
107        public String getMethodName( ConstantPoolGen cpg ) {
108            return getName(cpg);
109        }
110    
111    
112        /** @return return type of referenced method.
113         */
114        public Type getReturnType( ConstantPoolGen cpg ) {
115            return Type.getReturnType(getSignature(cpg));
116        }
117    
118    
119        /** @return argument types of referenced method.
120         */
121        public Type[] getArgumentTypes( ConstantPoolGen cpg ) {
122            return Type.getArgumentTypes(getSignature(cpg));
123        }
124    }