-
Notifications
You must be signed in to change notification settings - Fork 523
/
class.go
156 lines (140 loc) · 3.7 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package heap
import "strings"
import "jvmgo/ch08/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
initStarted bool
}
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) Name() string {
return self.name
}
func (self *Class) ConstantPool() *ConstantPool {
return self.constantPool
}
func (self *Class) Fields() []*Field {
return self.fields
}
func (self *Class) Methods() []*Method {
return self.methods
}
func (self *Class) Loader() *ClassLoader {
return self.loader
}
func (self *Class) SuperClass() *Class {
return self.superClass
}
func (self *Class) StaticVars() Slots {
return self.staticVars
}
func (self *Class) InitStarted() bool {
return self.initStarted
}
func (self *Class) StartInit() {
self.initStarted = true
}
// 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.getMethod("main", "([Ljava/lang/String;)V", true)
}
func (self *Class) GetClinitMethod() *Method {
return self.getMethod("<clinit>", "()V", true)
}
func (self *Class) getMethod(name, descriptor string, isStatic bool) *Method {
for c := self; c != nil; c = c.superClass {
for _, method := range c.methods {
if method.IsStatic() == isStatic &&
method.name == name &&
method.descriptor == descriptor {
return method
}
}
}
return nil
}
func (self *Class) getField(name, descriptor string, isStatic bool) *Field {
for c := self; c != nil; c = c.superClass {
for _, field := range c.fields {
if field.IsStatic() == isStatic &&
field.name == name &&
field.descriptor == descriptor {
return field
}
}
}
return nil
}
func (self *Class) isJlObject() bool {
return self.name == "java/lang/Object"
}
func (self *Class) isJlCloneable() bool {
return self.name == "java/lang/Cloneable"
}
func (self *Class) isJioSerializable() bool {
return self.name == "java/io/Serializable"
}
func (self *Class) NewObject() *Object {
return newObject(self)
}
func (self *Class) ArrayClass() *Class {
arrayClassName := getArrayClassName(self.name)
return self.loader.LoadClass(arrayClassName)
}