mirror of
https://github.com/FranzHaidnor/haidnorJVM.git
synced 2026-03-25 07:33:47 +08:00
update INVOKEINTERFACE 支持接口继承
This commit is contained in:
@@ -22,9 +22,7 @@ import org.apache.bcel.classfile.Utility;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
|
||||||
* 调用对象实例方法,根据对象的实际类型进行分派(虚方法分派),支持多态
|
|
||||||
*/
|
|
||||||
public class INVOKEINTERFACE extends Instruction {
|
public class INVOKEINTERFACE extends Instruction {
|
||||||
|
|
||||||
private final int constantMethodrefIndex;
|
private final int constantMethodrefIndex;
|
||||||
@@ -132,13 +130,25 @@ public class INVOKEINTERFACE extends Instruction {
|
|||||||
|
|
||||||
private static org.apache.bcel.classfile.Method getMethodFromInterface(JavaClass javaClass, String methodSignature, String methodName) throws ClassNotFoundException {
|
private static org.apache.bcel.classfile.Method getMethodFromInterface(JavaClass javaClass, String methodSignature, String methodName) throws ClassNotFoundException {
|
||||||
JavaClass[] interfaces = javaClass.getInterfaces();
|
JavaClass[] interfaces = javaClass.getInterfaces();
|
||||||
for (JavaClass anInterface : interfaces) {
|
if (interfaces.length == 0) {
|
||||||
for (org.apache.bcel.classfile.Method method : anInterface.getMethods()) {
|
for (org.apache.bcel.classfile.Method method : javaClass.getMethods()) {
|
||||||
if (method.getSignature().equals(methodSignature) && method.getName().equals(methodName)) {
|
if (method.getSignature().equals(methodSignature) && method.getName().equals(methodName)) {
|
||||||
return method;
|
return method;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (JavaClass interfaceJavaClass : interfaces) {
|
||||||
|
for (org.apache.bcel.classfile.Method method : interfaceJavaClass.getMethods()) {
|
||||||
|
if (method.getSignature().equals(methodSignature) && method.getName().equals(methodName)) {
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (interfaceJavaClass.getInterfaces() != null) {
|
||||||
|
for (JavaClass classInterface : interfaceJavaClass.getInterfaces()) {
|
||||||
|
return getMethodFromInterface(classInterface, methodSignature, methodName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,6 @@ package haidnor.jvm.test.clazz;
|
|||||||
/**
|
/**
|
||||||
* 生物
|
* 生物
|
||||||
*/
|
*/
|
||||||
public interface Organism {
|
public interface Organism extends Organism0 {
|
||||||
|
|
||||||
default void die() {
|
|
||||||
System.out.println("Organism die");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/test/java/haidnor/jvm/test/clazz/Organism0.java
Normal file
12
src/test/java/haidnor/jvm/test/clazz/Organism0.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package haidnor.jvm.test.clazz;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生物
|
||||||
|
*/
|
||||||
|
public interface Organism0 {
|
||||||
|
|
||||||
|
default void die() {
|
||||||
|
System.out.println("Organism die 0");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,14 +2,14 @@ package haidnor.jvm.test.demo;
|
|||||||
|
|
||||||
|
|
||||||
import haidnor.jvm.test.clazz.Human;
|
import haidnor.jvm.test.clazz.Human;
|
||||||
import haidnor.jvm.test.clazz.Organism;
|
import haidnor.jvm.test.clazz.Organism0;
|
||||||
import haidnor.jvm.test.clazz.Student;
|
import haidnor.jvm.test.clazz.Student;
|
||||||
|
|
||||||
public class Demo8 {
|
public class Demo8 {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Human organism1 = new Human();
|
Human organism1 = new Human();
|
||||||
Organism organism = new Student();
|
Organism0 organism = new Student();
|
||||||
organism.die();
|
organism.die();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user