-
Notifications
You must be signed in to change notification settings - Fork 529
/
Copy pathclass.go
99 lines (88 loc) · 2.39 KB
/
class.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package heap
import "strings"
import "jvmgo/ch06/classfile"
// name, superClassName and interfaceNames are all binary names(jvms8-4.2.1)
type Class struct {
accessFlags uint16
name string // thisClassName
superClassName string
interfaceNames []string
constantPool *ConstantPool
fields []*Field
methods []*Method
loader *ClassLoader
superClass *Class
interfaces []*Class
instanceSlotCount uint
staticSlotCount uint
staticVars Slots
}
func newClass(cf *classfile.ClassFile) *Class {
class := &Class{}
class.accessFlags = cf.AccessFlags()
class.name = cf.ClassName()
class.superClassName = cf.SuperClassName()
class.interfaceNames = cf.InterfaceNames()
class.constantPool = newConstantPool(class, cf.ConstantPool())
class.fields = newFields(class, cf.Fields())
class.methods = newMethods(class, cf.Methods())
return class
}
func (self *Class) IsPublic() bool {
return 0 != self.accessFlags&ACC_PUBLIC
}
func (self *Class) IsFinal() bool {
return 0 != self.accessFlags&ACC_FINAL
}
func (self *Class) IsSuper() bool {
return 0 != self.accessFlags&ACC_SUPER
}
func (self *Class) IsInterface() bool {
return 0 != self.accessFlags&ACC_INTERFACE
}
func (self *Class) IsAbstract() bool {
return 0 != self.accessFlags&ACC_ABSTRACT
}
func (self *Class) IsSynthetic() bool {
return 0 != self.accessFlags&ACC_SYNTHETIC
}
func (self *Class) IsAnnotation() bool {
return 0 != self.accessFlags&ACC_ANNOTATION
}
func (self *Class) IsEnum() bool {
return 0 != self.accessFlags&ACC_ENUM
}
// getters
func (self *Class) ConstantPool() *ConstantPool {
return self.constantPool
}
func (self *Class) StaticVars() Slots {
return self.staticVars
}
// jvms 5.4.4
func (self *Class) isAccessibleTo(other *Class) bool {
return self.IsPublic() ||
self.getPackageName() == other.getPackageName()
}
func (self *Class) getPackageName() string {
if i := strings.LastIndex(self.name, "/"); i >= 0 {
return self.name[:i]
}
return ""
}
func (self *Class) GetMainMethod() *Method {
return self.getStaticMethod("main", "([Ljava/lang/String;)V")
}
func (self *Class) getStaticMethod(name, descriptor string) *Method {
for _, method := range self.methods {
if method.IsStatic() &&
method.name == name &&
method.descriptor == descriptor {
return method
}
}
return nil
}
func (self *Class) NewObject() *Object {
return newObject(self)
}