4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.generic; 23 24 import com.sun.org.apache.bcel.internal.classfile.ConstantPool; 25 import com.sun.org.apache.bcel.internal.Constants; 26 import com.sun.org.apache.bcel.internal.ExceptionConstants; 27 28 import java.io.*; 29 import com.sun.org.apache.bcel.internal.util.ByteSequence; 30 31 /** 32 * INVOKEINTERFACE - Invoke interface method 33 * <PRE>Stack: ..., objectref, [arg1, [arg2 ...]] -> ...</PRE> 34 * 35 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 36 */ 37 public final class INVOKEINTERFACE extends InvokeInstruction { 38 private int nargs; // Number of arguments on stack (number of stack slots), called "count" in vmspec2 39 40 /** 41 * Empty constructor needed for the Class.newInstance() statement in 42 * Instruction.readInstruction(). Not to be used otherwise. 43 */ 44 INVOKEINTERFACE() {} 45 46 public INVOKEINTERFACE(int index, int nargs) { 47 super(Constants.INVOKEINTERFACE, index); 48 length = 5; 49 50 if(nargs < 1) 51 throw new ClassGenException("Number of arguments must be > 0 " + nargs); 52 53 this.nargs = nargs; 54 } 55 56 /** 57 * Dump instruction as byte code to stream out. 58 * @param out Output stream 59 */ 60 public void dump(DataOutputStream out) throws IOException { 61 out.writeByte(opcode); 62 out.writeShort(index); 63 out.writeByte(nargs); 64 out.writeByte(0); 65 } 66 67 /** 68 * The <B>count</B> argument according to the Java Language Specification, 69 * Second Edition. 70 */ 71 public int getCount() { return nargs; } 72 73 /** 74 * Read needed data (i.e., index) from file. 75 */ 76 protected void initFromFile(ByteSequence bytes, boolean wide) 77 throws IOException 78 { 79 super.initFromFile(bytes, wide); 80 81 length = 5; 82 nargs = bytes.readUnsignedByte(); 83 bytes.readByte(); // Skip 0 byte 84 } 85 86 /** 87 * @return mnemonic for instruction with symbolic references resolved 88 */ 89 public String toString(ConstantPool cp) { 90 return super.toString(cp) + " " + nargs; 91 } 92 93 public int consumeStack(ConstantPoolGen cpg) { // nargs is given in byte-code 94 return nargs; // nargs includes this reference 95 } 96 97 public Class[] getExceptions() { 98 Class[] cs = new Class[4 + ExceptionConstants.EXCS_INTERFACE_METHOD_RESOLUTION.length]; 99 100 System.arraycopy(ExceptionConstants.EXCS_INTERFACE_METHOD_RESOLUTION, 0, 101 cs, 0, ExceptionConstants.EXCS_INTERFACE_METHOD_RESOLUTION.length); 102 103 cs[ExceptionConstants.EXCS_INTERFACE_METHOD_RESOLUTION.length+3] = ExceptionConstants.INCOMPATIBLE_CLASS_CHANGE_ERROR; 104 cs[ExceptionConstants.EXCS_INTERFACE_METHOD_RESOLUTION.length+2] = ExceptionConstants.ILLEGAL_ACCESS_ERROR; 105 cs[ExceptionConstants.EXCS_INTERFACE_METHOD_RESOLUTION.length+1] = ExceptionConstants.ABSTRACT_METHOD_ERROR; 106 cs[ExceptionConstants.EXCS_INTERFACE_METHOD_RESOLUTION.length] = ExceptionConstants.UNSATISFIED_LINK_ERROR; 107 108 return cs; 109 } 110 111 /** 112 * Call corresponding visitor method(s). The order is: 113 * Call visitor methods of implemented interfaces first, then 114 * call methods according to the class hierarchy in descending order, 115 * i.e., the most specific visitXXX() call comes last. 116 * 117 * @param v Visitor object 118 */ 119 public void accept(Visitor v) { 120 v.visitExceptionThrower(this); 121 v.visitTypedInstruction(this); 122 v.visitStackConsumer(this); 123 v.visitStackProducer(this); 124 v.visitLoadClass(this); 125 v.visitCPInstruction(this); 126 v.visitFieldOrMethod(this); 127 v.visitInvokeInstruction(this); 128 v.visitINVOKEINTERFACE(this); 129 } 130 } | 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.generic; 23 24 import java.io.DataOutputStream; 25 import java.io.IOException; 26 27 import com.sun.org.apache.bcel.internal.Const; 28 import com.sun.org.apache.bcel.internal.ExceptionConst; 29 import com.sun.org.apache.bcel.internal.classfile.ConstantPool; 30 import com.sun.org.apache.bcel.internal.util.ByteSequence; 31 32 /** 33 * INVOKEINTERFACE - Invoke interface method 34 * <PRE>Stack: ..., objectref, [arg1, [arg2 ...]] -> ...</PRE> 35 * 36 * @version $Id: INVOKEINTERFACE.java 1747278 2016-06-07 17:28:43Z britter $ 37 * @see 38 * <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokeinterface"> 39 * The invokeinterface instruction in The Java Virtual Machine Specification</a> 40 */ 41 public final class INVOKEINTERFACE extends InvokeInstruction { 42 43 private int nargs; // Number of arguments on stack (number of stack slots), called "count" in vmspec2 44 45 46 /** 47 * Empty constructor needed for the Class.newInstance() statement in 48 * Instruction.readInstruction(). Not to be used otherwise. 49 */ 50 INVOKEINTERFACE() { 51 } 52 53 54 public INVOKEINTERFACE(final int index, final int nargs) { 55 super(Const.INVOKEINTERFACE, index); 56 super.setLength(5); 57 if (nargs < 1) { 58 throw new ClassGenException("Number of arguments must be > 0 " + nargs); 59 } 60 this.nargs = nargs; 61 } 62 63 64 /** 65 * Dump instruction as byte code to stream out. 66 * @param out Output stream 67 */ 68 @Override 69 public void dump( final DataOutputStream out ) throws IOException { 70 out.writeByte(super.getOpcode()); 71 out.writeShort(super.getIndex()); 72 out.writeByte(nargs); 73 out.writeByte(0); 74 } 75 76 77 /** 78 * The <B>count</B> argument according to the Java Language Specification, 79 * Second Edition. 80 */ 81 public int getCount() { 82 return nargs; 83 } 84 85 86 /** 87 * Read needed data (i.e., index) from file. 88 */ 89 @Override 90 protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException { 91 super.initFromFile(bytes, wide); 92 super.setLength(5); 93 nargs = bytes.readUnsignedByte(); 94 bytes.readByte(); // Skip 0 byte 95 } 96 97 98 /** 99 * @return mnemonic for instruction with symbolic references resolved 100 */ 101 @Override 102 public String toString( final ConstantPool cp ) { 103 return super.toString(cp) + " " + nargs; 104 } 105 106 107 @Override 108 public int consumeStack( final ConstantPoolGen cpg ) { // nargs is given in byte-code 109 return nargs; // nargs includes this reference 110 } 111 112 113 @Override 114 public Class<?>[] getExceptions() { 115 return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION, 116 ExceptionConst.UNSATISFIED_LINK_ERROR, 117 ExceptionConst.ABSTRACT_METHOD_ERROR, 118 ExceptionConst.ILLEGAL_ACCESS_ERROR, 119 ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR); 120 } 121 122 123 /** 124 * Call corresponding visitor method(s). The order is: 125 * Call visitor methods of implemented interfaces first, then 126 * call methods according to the class hierarchy in descending order, 127 * i.e., the most specific visitXXX() call comes last. 128 * 129 * @param v Visitor object 130 */ 131 @Override 132 public void accept( final Visitor v ) { 133 v.visitExceptionThrower(this); 134 v.visitTypedInstruction(this); 135 v.visitStackConsumer(this); 136 v.visitStackProducer(this); 137 v.visitLoadClass(this); 138 v.visitCPInstruction(this); 139 v.visitFieldOrMethod(this); 140 v.visitInvokeInstruction(this); 141 v.visitINVOKEINTERFACE(this); 142 } 143 } |