/
TestException12.java
84 lines (75 loc) · 2.69 KB
/
TestException12.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package test;
import Utils.Cmd;
import classpath.ClassPath;
import instructions.InstructionFactory;
import instructions.base.BytecodeReader;
import instructions.base.Instruction;
import runtimedata.Zframe;
import runtimedata.Zthread;
import runtimedata.heap.*;
import znative.RegisterCenter;
import java.util.Scanner;
/**
* @author zachaxy
* @date 18/1/3
*/
public class TestException12 {
public static void main(String[] args) {
System.out.println("the same as testInterpreter06!");
Scanner in = new Scanner(System.in);
// java -cp /Users/zachaxy/TestClassFiles TestException12
String cmdLine = in.nextLine();
Cmd cmd = new Cmd(cmdLine);
RegisterCenter.init();
ClassPath classPath = new ClassPath(cmd.getXJreOption(), cmd.getCpOption());
ZclassLoader classLoader = new ZclassLoader(classPath);
Zclass testClass = classLoader.loadClass(cmd.getClazz());
Zmethod testMethod = testClass.getMethod("test", "()V");
//初始化栈帧
Zthread thread = new Zthread();
Zframe frame = thread.createFrame(testMethod);
Zframe outFrame = frame;
thread.pushFrame(frame);
BytecodeReader reader = new BytecodeReader();
while (true) {
frame = thread.getCurrentFrame();
int pc = frame.getNextPC();
thread.setPc(pc);
//decode
reader.reset(frame.getMethod().getCode(), pc);
int opCode = reader.readUint8();
//解析指令,创建指令,然后根据不同的指令执行不同的操作
try {
Instruction instruction = InstructionFactory.createInstruction(opCode);
instruction.fetchOperands(reader);
frame.setNextPC(reader.getPc());
instruction.execute(frame);
if (frame == outFrame) {
System.out.println("current instruction: " + pc + ": " + instruction.getClass().getSimpleName());
}
if (thread.isStackEmpty()) {
if (!frame.getOperandStack().isEmpty()) {
System.out.println("return: " + frame.getOperandStack().popInt());
}
break;
}
} catch (Exception e) {
e.printStackTrace();
//return;
}
}
int i = outFrame.getLocalVars().getInt(0);
System.out.println(i);
}
public static void test() {
int i = 100;
try {
f1();
} catch (NumberFormatException e) {
i = 200;
}
}
public static void f1() {
throw new IndexOutOfBoundsException();
}
}