/
method.go
153 lines (123 loc) · 3.58 KB
/
method.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
package heap
import "github.com/Frederick-S/jvmgo/classfile"
type Method struct {
ClassMember
maxStackSize uint
maxNumberOfLocalVariables uint
code []byte
exceptionTable ExceptionTable
lineNumberTable *classfile.LineNumberTableAttribute
argumentsCount uint
}
func newMethods(class *Class, memberInfos []*classfile.MemberInfo) []*Method {
methods := make([]*Method, len(memberInfos))
for i, memberInfo := range memberInfos {
methods[i] = newMethod(class, memberInfo)
}
return methods
}
func newMethod(class *Class, cfMethod *classfile.MemberInfo) *Method {
method := &Method{}
method.class = class
method.copyMemberInfo(cfMethod)
method.copyAttributes(cfMethod)
methodDescriptor := parseMethodDescriptor(method.descriptor)
method.calculateArgumentsCount(methodDescriptor.parameterTypes)
if method.IsNative() {
method.injectCodeAttribute(methodDescriptor.returnType)
}
return method
}
func (method *Method) copyAttributes(memberInfo *classfile.MemberInfo) {
codeAttribute := memberInfo.GetCodeAttribute()
if codeAttribute != nil {
method.maxStackSize = codeAttribute.GetMaxStackSize()
method.maxNumberOfLocalVariables = codeAttribute.GetMaxNumberOfLocalVariables()
method.code = codeAttribute.GetCode()
method.exceptionTable = newExceptionTable(codeAttribute.GetExceptionTable(), method.class.constantPool)
method.lineNumberTable = codeAttribute.GetLineNumberTableAttribute()
}
}
func (method *Method) calculateArgumentsCount(parameterTypes []string) {
for _, parameterType := range parameterTypes {
if parameterType == "J" || parameterType == "D" {
method.argumentsCount += 2
} else {
method.argumentsCount += 1
}
}
if !method.IsStatic() {
// `this` reference
method.argumentsCount++
}
}
func (method *Method) injectCodeAttribute(returnType string) {
method.maxStackSize = 4
method.maxNumberOfLocalVariables = method.argumentsCount
switch returnType[0] {
case 'V':
// return
method.code = []byte{0xfe, 0xb1}
case 'L', '[':
// areturn
method.code = []byte{0xfe, 0xb0}
case 'D':
// dreturn
method.code = []byte{0xfe, 0xaf}
case 'F':
// freturn
method.code = []byte{0xfe, 0xae}
case 'J':
// lreturn
method.code = []byte{0xfe, 0xad}
default:
// ireturn
method.code = []byte{0xfe, 0xac}
}
}
func (method *Method) IsSynchronized() bool {
return method.accessFlags&ACC_SYNCHRONIZED != 0
}
func (method *Method) IsBridge() bool {
return method.accessFlags&ACC_BRIDGE != 0
}
func (method *Method) IsVarargs() bool {
return method.accessFlags&ACC_VARARGS != 0
}
func (method *Method) IsNative() bool {
return method.accessFlags&ACC_NATIVE != 0
}
func (method *Method) IsAbstract() bool {
return method.accessFlags&ACC_ABSTRACT != 0
}
func (method *Method) IsStrict() bool {
return method.accessFlags&ACC_STRICT != 0
}
func (method *Method) GetMaxStackSize() uint {
return method.maxStackSize
}
func (method *Method) GetMaxNumberOfLocalVariables() uint {
return method.maxNumberOfLocalVariables
}
func (method *Method) GetCode() []byte {
return method.code
}
func (method *Method) GetArgumentsCount() uint {
return method.argumentsCount
}
func (method *Method) FindExceptionHandler(exceptionClass *Class, pc int) int {
exceptionHandler := method.exceptionTable.findExceptionHandler(exceptionClass, pc)
if exceptionHandler != nil {
return exceptionHandler.handlerPC
}
return -1
}
func (method *Method) GetLineNumber(pc int) int {
if method.IsNative() {
return -2
}
if method.lineNumberTable == nil {
return -1
}
return method.lineNumberTable.GetLineNumber(pc)
}