-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathDescriptorExposer.java
More file actions
231 lines (203 loc) · 7.55 KB
/
DescriptorExposer.java
File metadata and controls
231 lines (203 loc) · 7.55 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package org.python.expose.generate;
import org.objectweb.asm.Type;
import org.python.core.PyDataDescr;
/**
* Generates a class to expose a descriptor on Python type. One of addMethodGetter or addFieldGetter
* must be called, and possibly one of addMethodSetter and addFieldSetter if this is a settable
* descriptor. If this is a deletable descriptor, addMethodDeleter may be called. There is no
* addFieldDeleter since there's no defined behavior to 'delete' a field.
*/
public class DescriptorExposer extends Exposer {
private Type onType, ofType;
private String name;
private String doc;
private String getterMethodName, getterFieldName, setterMethodName, setterFieldName,
deleterMethodName;
/**
* Creates an exposer that will work on type and have <code>descrName</code> as its name in
* the type's dict.
*/
public DescriptorExposer(Type onType, String descrName) {
super(PyDataDescr.class, onType.getClassName() + "$" + descrName + "_descriptor", ASSUPER);
this.onType = onType;
name = descrName;
}
/**
* @return - the name this descriptor will be exposed as in its type's dict
*/
public String getName() {
return name;
}
public void addMethodGetter(String methodName, String desc) {
addMethodGetter(methodName, desc, null);
}
public void addMethodGetter(String methodName, String desc, String doc) {
if(hasGetter()) {
error("Descriptor can only have one getter");
}
if(Type.getArgumentTypes(desc).length > 0) {
error("Getter can't take arguments");
}
setOfType(Type.getReturnType(desc));
getterMethodName = methodName;
this.doc = doc;
}
public void addFieldGetter(String fieldName, Type fieldType) {
addFieldGetter(fieldName, fieldType, null);
}
public void addFieldGetter(String fieldName, Type fieldType, String doc) {
if(hasGetter()) {
error("Can only have one getter for a descriptor");
}
setOfType(fieldType);
getterFieldName = fieldName;
this.doc = doc;
}
public boolean hasGetter() {
return getterMethodName != null || getterFieldName != null;
}
private void setOfType(Type type) {
if(ofType == null) {
ofType = type;
} else if(!ofType.equals(type)) {
error("Types of the getter and setter must agree");
}
}
public void addMethodSetter(String methodName, String desc) {
if(hasSetter()) {
error("Descriptor can only have one setter");
}
Type[] args = Type.getArgumentTypes(desc);
if(args.length > 1) {
error("Setter can only take one argument");
}
setOfType(args[0]);
setterMethodName = methodName;
}
public void addFieldSetter(String fieldName, Type fieldType) {
if(hasSetter()) {
error("Descriptor can only have one setter");
}
setOfType(fieldType);
setterFieldName = fieldName;
}
public boolean hasSetter() {
return setterMethodName != null || setterFieldName != null;
}
public void addMethodDeleter(String methodName, String desc) {
deleterMethodName = methodName;
}
public String toString() {
return "DescriptorExposer[class=" + onType.getClassName() + ", name=" + name + "]";
}
@Override
protected void generate() {
if(!hasGetter()) {
error("A descriptor requires at least a get");
}
generateConstructor();
if(getterMethodName != null) {
generateMethodGetter();
} else {
generateFieldGetter();
}
generateImplement("Get", getterMethodName != null || getterFieldName != null);
if(setterMethodName != null) {
generateMethodSetter();
} else if(setterFieldName != null) {
generateFieldSetter();
}
generateImplement("Set", setterMethodName != null || setterFieldName != null);
if(deleterMethodName != null) {
generateMethodDeleter();
}
generateImplement("Delete", deleterMethodName != null);
}
private void generateConstructor() {
startConstructor();
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn(name);
if(PRIMITIVES.containsKey(ofType)) {
mv.visitLdcInsn(PRIMITIVES.get(ofType));
} else {
mv.visitLdcInsn(ofType);
}
if (doc == null) {
mv.visitInsn(ACONST_NULL);
} else {
mv.visitLdcInsn(doc);
}
superConstructor(STRING, CLASS, STRING);
endConstructor();
}
private void generateMethodGetter() {
startMethod("invokeGet", OBJECT, PYOBJ);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, onType.getInternalName());
call(onType, getterMethodName, ofType);
if(PRIMITIVES.containsKey(ofType) || ofType.equals(STRING)) {
toPy(ofType);
}
endMethod(ARETURN);
}
private void generateFieldGetter() {
startMethod("invokeGet", OBJECT, PYOBJ);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, onType.getInternalName());
mv.visitFieldInsn(GETFIELD,
onType.getInternalName(),
getterFieldName,
ofType.getDescriptor());
if(PRIMITIVES.containsKey(ofType) || ofType.equals(STRING)) {
toPy(ofType);
}
endMethod(ARETURN);
}
private void generateMethodSetter() {
startMethod("invokeSet", VOID, PYOBJ, OBJECT);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, onType.getInternalName());
mv.visitVarInsn(ALOAD, 2);
if(PRIMITIVES.containsKey(ofType)) {
mv.visitTypeInsn(CHECKCAST, PRIMITIVES.get(ofType).getInternalName());
call(PRIMITIVES.get(ofType), ofType.getClassName() + "Value", ofType);
} else {
mv.visitTypeInsn(CHECKCAST, ofType.getInternalName());
}
call(onType, setterMethodName, VOID, ofType);
endMethod(RETURN);
}
private void generateFieldSetter() {
startMethod("invokeSet", VOID, PYOBJ, OBJECT);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, onType.getInternalName());
mv.visitVarInsn(ALOAD, 2);
if(PRIMITIVES.containsKey(ofType)) {
mv.visitTypeInsn(CHECKCAST, PRIMITIVES.get(ofType).getInternalName());
call(PRIMITIVES.get(ofType), ofType.getClassName() + "Value", ofType);
} else {
mv.visitTypeInsn(CHECKCAST, ofType.getInternalName());
}
mv.visitFieldInsn(PUTFIELD,
onType.getInternalName(),
setterFieldName,
ofType.getDescriptor());
endMethod(RETURN);
}
private void generateMethodDeleter() {
startMethod("invokeDelete", VOID, PYOBJ);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, onType.getInternalName());
call(onType, deleterMethodName, VOID);
endMethod(RETURN);
}
private void generateImplement(String setOrDelete, boolean implementsIt) {
startMethod("implementsDescr" + setOrDelete, BOOLEAN);
mv.visitInsn(implementsIt ? ICONST_1 : ICONST_0);
endMethod(IRETURN);
}
private void error(String reason) {
throw new InvalidExposingException(reason + "[class=" + onType.getClassName() + ", name="
+ name + "]");
}
}