forked from msgpack/msgpack-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultBuildContext.java
More file actions
275 lines (242 loc) · 8.96 KB
/
DefaultBuildContext.java
File metadata and controls
275 lines (242 loc) · 8.96 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//
// MessagePack for Java
//
// Copyright (C) 2009-2011 FURUHASHI Sadayuki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.template.builder;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import org.msgpack.*;
import org.msgpack.packer.Packer;
import org.msgpack.template.*;
import org.msgpack.unpacker.Unpacker;
import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtNewConstructor;
import javassist.NotFoundException;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class DefaultBuildContext extends BuildContext<FieldEntry> {
protected FieldEntry[] entries;
protected Class<?> origClass;
protected String origName;
protected Template<?>[] templates;
public DefaultBuildContext(JavassistTemplateBuilder director) {
super(director);
}
public Template buildTemplate(Class targetClass, FieldEntry[] entries, Template[] templates) {
this.entries = entries;
this.templates = templates;
this.origClass = targetClass;
this.origName = origClass.getName();
return build(origName);
}
protected void setSuperClass() throws CannotCompileException, NotFoundException {
tmplCtClass.setSuperclass(director.getCtClass(
JavassistTemplateBuilder.JavassistTemplate.class.getName()));
}
protected void buildConstructor() throws CannotCompileException,
NotFoundException {
// Constructor(Class targetClass, Template[] templates)
CtConstructor newCtCons = CtNewConstructor.make(
new CtClass[] {
director.getCtClass(Class.class.getName()),
director.getCtClass(Template.class.getName() + "[]")
}, new CtClass[0], tmplCtClass);
tmplCtClass.addConstructor(newCtCons);
}
protected Template buildInstance(Class<?> c) throws NoSuchMethodException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Constructor<?> cons = c.getConstructor(new Class[] { Class.class, Template[].class });
Object tmpl = cons.newInstance(new Object[] { origClass, templates });
return (Template) tmpl;
}
protected void buildMethodInit() {
}
protected String buildWriteMethodBody() {
resetStringBuilder();
buildString("\n{\n");
buildString(" if ($2 == null) {\n");
buildString(" if ($3) {\n");
buildString(" throw new %s(\"Attempted to write null\");\n", MessageTypeException.class.getName());
buildString(" }\n");
buildString(" $1.writeNil();\n");
buildString(" return;\n");
buildString(" }\n");
buildString(" %s _$$_t = (%s) $2;\n", origName, origName);
buildString(" $1.writeArrayBegin(%d);\n", entries.length);
for (int i = 0; i < entries.length; i++) {
FieldEntry e = entries[i];
if (!e.isAvailable()) {
buildString(" $1.writeNil();\n");
continue;
}
DefaultFieldEntry de = (DefaultFieldEntry) e;
boolean isPrivate = Modifier.isPrivate(de.getField().getModifiers());
Class<?> type = de.getType();
if (type.isPrimitive()) { // primitive types
if (!isPrivate) {
buildString(" $1.%s(_$$_t.%s);\n", primitiveWriteName(type), de.getName());
} else {
buildString(" %s.writePrivateField($1, _$$_t, %s.class, \"%s\", templates[%d]);\n",
DefaultBuildContext.class.getName(), de.getField().getDeclaringClass().getName(), de.getName(), i);
}
} else { // reference types
if (!isPrivate) {
buildString(" if (_$$_t.%s == null) {\n", de.getName());
} else {
buildString(" if (%s.readPrivateField(_$$_t, %s.class, \"%s\") == null) {\n",
DefaultBuildContext.class.getName(), de.getField().getDeclaringClass().getName(), de.getName());
}
if (de.isNotNullable()) {
buildString(" throw new %s(\"%s cannot be null by @NotNullable\");\n",
MessageTypeException.class.getName(), de.getName());
} else {
buildString(" $1.writeNil();\n");
}
buildString(" } else {\n");
if (!isPrivate) {
buildString(" templates[%d].write($1, _$$_t.%s);\n", i, de.getName());
} else {
buildString(" %s.writePrivateField($1, _$$_t, %s.class, \"%s\", templates[%d]);\n",
DefaultBuildContext.class.getName(), de.getField().getDeclaringClass().getName(), de.getName(), i);
}
buildString(" }\n");
}
}
buildString(" $1.writeArrayEnd();\n");
buildString("}\n");
return getBuiltString();
}
public static Object readPrivateField(Object target, Class targetClass, String fieldName) {
Field field = null;
try {
field = targetClass.getDeclaredField(fieldName);
field.setAccessible(true);
Object valueReference = field.get(target);
return valueReference;
} catch (Exception e) {
throw new MessageTypeException(e);
} finally {
if (field != null) {
field.setAccessible(false);
}
}
}
public static void writePrivateField(Packer packer, Object target, Class targetClass, String fieldName, Template tmpl) {
Field field = null;
try {
field = targetClass.getDeclaredField(fieldName);
field.setAccessible(true);
Object valueReference = field.get(target);
tmpl.write(packer, valueReference);
} catch (Exception e) {
throw new MessageTypeException(e);
} finally {
if (field != null) {
field.setAccessible(false);
}
}
}
protected String buildReadMethodBody() {
resetStringBuilder();
buildString("\n{\n");
buildString(" if (!$3 && $1.trySkipNil()) {\n");
buildString(" return null;\n");
buildString(" }\n");
buildString(" %s _$$_t;\n", origName);
buildString(" if ($2 == null) {\n");
buildString(" _$$_t = new %s();\n", origName);
buildString(" } else {\n");
buildString(" _$$_t = (%s) $2;\n", origName);
buildString(" }\n");
buildString(" $1.readArrayBegin();\n");
int i;
for (i = 0; i < entries.length; i++) {
FieldEntry e = entries[i];
if (!e.isAvailable()) {
buildString(" $1.skip();\n");
continue;
}
if (e.isOptional()) {
buildString(" if ($1.trySkipNil()) {");
// if Optional and nil, then keep default value
buildString(" } else {\n");
}
DefaultFieldEntry de = (DefaultFieldEntry) e;
boolean isPrivate = Modifier.isPrivate(de.getField().getModifiers());
Class<?> type = de.getType();
if (type.isPrimitive()) {
if (!isPrivate) {
buildString(" _$$_t.%s = $1.%s();\n", de.getName(), primitiveReadName(type));
} else {
buildString(" %s.readPrivateField($1, _$$_t, %s.class, \"%s\", templates[%d]);\n",
DefaultBuildContext.class.getName(), de.getField().getDeclaringClass().getName(), de.getName(), i);
}
} else {
if (!isPrivate) {
buildString(" _$$_t.%s = (%s) this.templates[%d].read($1, _$$_t.%s);\n",
de.getName(), de.getJavaTypeName(), i, de.getName());
} else {
buildString(" %s.readPrivateField($1, _$$_t, %s.class, \"%s\", templates[%d]);\n",
DefaultBuildContext.class.getName(), de.getField().getDeclaringClass().getName(), de.getName(), i);
}
}
if (de.isOptional()) {
buildString(" }\n");
}
}
buildString(" $1.readArrayEnd();\n");
buildString(" return _$$_t;\n");
buildString("}\n");
return getBuiltString();
}
public static void readPrivateField(Unpacker unpacker, Object target, Class targetClass, String fieldName, Template tmpl) {
Field field = null;
try {
field = targetClass.getDeclaredField(fieldName);
field.setAccessible(true);
Object fieldReference = field.get(target);
Object valueReference = tmpl.read(unpacker, fieldReference);
if (valueReference != fieldReference) {
field.set(target, valueReference);
}
} catch (Exception e) {
throw new MessageTypeException(e);
} finally {
if (field != null) {
field.setAccessible(false);
}
}
}
@Override
public void writeTemplate(Class<?> targetClass, FieldEntry[] entries, Template[] templates, String directoryName) {
this.entries = entries;
this.templates = templates;
this.origClass = targetClass;
this.origName = origClass.getName();
write(origName, directoryName);
}
@Override
public Template loadTemplate(Class<?> targetClass, FieldEntry[] entries, Template[] templates) {
this.entries = entries;
this.templates = templates;
this.origClass = targetClass;
this.origName = origClass.getName();
return load(origName);
}
}