-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (67 loc) · 1.94 KB
/
main.go
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
package main
import (
"MiniJVM/src/classfile"
"MiniJVM/src/classpath"
"fmt"
"strings"
)
var CMD *Cmd
func main() {
CMD = parseCmd()
if CMD.versionFlag {
fmt.Println("version 0.0.1")
} else if CMD.helpFlag || CMD.class == "" {
printUsage()
} else {
startJVM(CMD)
}
}
func startJVM(cmd *Cmd) {
classPath := classpath.Parse(cmd.XjreOption, cmd.cpOption)
className := strings.Replace(cmd.class, ".", "/", -1)
cf := loadClass(cmd, classPath, className)
fmt.Println(cmd.class)
printClassInfo(cf)
}
func loadClass(cmd *Cmd, classPath *classpath.Classpath, className string) *classfile.ClassFile {
classData, _, err := classPath.ReadClass(className)
if err != nil {
fmt.Printf("Error finding or loading main class %s\n", cmd.class)
fmt.Printf(err.Error())
panic(err)
}
cf, err := classfile.Parse(classData)
if err != nil {
panic(err)
}
return cf
}
func printClassInfo(cf *classfile.ClassFile) {
fmt.Printf("version: %v.%v\n", cf.MajorVersion(), cf.MinorVersion())
fmt.Printf("constants count: %v\n", len(cf.ConstantPool()))
fmt.Printf("access flags: 0x%x\n", cf.AccessFlags())
fmt.Printf("this class: %v\n", cf.ClassName())
fmt.Printf("super class: %v\n", cf.SuperClassName())
fmt.Printf("interfaces: %v\n", cf.InterfaceNames())
fmt.Printf("fields count: %v\n", len(cf.Fields()))
for _, f := range cf.Fields() {
fmt.Printf(" %s\n", f.Name())
}
fmt.Printf("methods count: %v\n", len(cf.Methods()))
for _, m := range cf.Methods() {
fmt.Printf(" %s\n", m.Name())
}
}
//func startJVM(CMD *Cmd) {
// classPath := classpath.Parse(CMD.XjreOption, CMD.cpOption)
// fmt.Printf("classpath:%s class:%s args:%v\n",
// classPath, CMD.class, CMD.args)
// className := strings.Replace(CMD.class, ".", "/", -1)
// classData, _, err := classPath.ReadClass(className)
// if err != nil {
// fmt.Printf("Error finding or loading main class %s\n", CMD.class)
// fmt.Printf(err.Error())
// return
// }
// fmt.Printf("class data:%v\n", classData)
//}