forked from msgpack/msgpack-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeansBuildContext.java
More file actions
188 lines (155 loc) · 5.6 KB
/
BeansBuildContext.java
File metadata and controls
188 lines (155 loc) · 5.6 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
//
// 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.InvocationTargetException;
import org.msgpack.*;
import org.msgpack.template.*;
import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtNewConstructor;
import javassist.NotFoundException;
@SuppressWarnings("rawtypes")
public class BeansBuildContext extends BuildContext<BeansFieldEntry> {
protected BeansFieldEntry[] entries;
protected Class<?> origClass;
protected String origName;
protected Template<?>[] templates;
public BeansBuildContext(JavassistTemplateBuilder director) {
super(director);
}
public Template buildTemplate(Class<?> targetClass, BeansFieldEntry[] 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() {
}
@Override
protected String buildWriteMethodBody() {
resetStringBuilder();
buildString("{");
buildString("if($2 == null) {");
buildString(" if($3) {");
buildString(" throw new %s(\"Attempted to write null\");", MessageTypeException.class.getName());
buildString(" }");
buildString(" $1.writeNil();");
buildString(" return;");
buildString("}");
buildString("%s _$$_t = (%s)$2;", origName, origName);
buildString("$1.writeArrayBegin(%d);", entries.length);
for (int i = 0; i < entries.length; i++) {
BeansFieldEntry e = entries[i];
if (!e.isAvailable()) {
buildString("$1.writeNil();");
continue;
}
Class<?> type = e.getType();
if (type.isPrimitive()) {
buildString("$1.%s(_$$_t.%s());", primitiveWriteName(type), e.getGetterName());
} else {
buildString("if(_$$_t.%s() == null) {", e.getGetterName());
if (e.isNotNullable()) {
buildString("throw new %s();", MessageTypeException.class.getName());
} else {
buildString("$1.writeNil();");
}
buildString("} else {");
buildString(" this.templates[%d].write($1, _$$_t.%s());", i, e.getGetterName());
buildString("}");
}
}
buildString("$1.writeArrayEnd();");
buildString("}");
return getBuiltString();
}
@Override
protected String buildReadMethodBody() {
resetStringBuilder();
buildString("{ ");
buildString("if(!$3 && $1.trySkipNil()) {");
buildString(" return null;");
buildString("}");
buildString("%s _$$_t;", origName);
buildString("if($2 == null) {");
buildString(" _$$_t = new %s();", origName);
buildString("} else {");
buildString(" _$$_t = (%s)$2;", origName);
buildString("}");
buildString("$1.readArrayBegin();");
for(int i=0; i < entries.length; i++) {
BeansFieldEntry e = entries[i];
if (!e.isAvailable()) {
buildString("$1.skip();"); // TODO #MN
continue;
}
if (e.isOptional()) {
buildString("if($1.trySkipNil()) {");
buildString("_$$_t.%s(null);", e.getSetterName());
buildString("} else {");
}
Class<?> type = e.getType();
if (type.isPrimitive()) {
buildString("_$$_t.%s( $1.%s() );", e.getSetterName(), primitiveReadName(type));
} else {
buildString("_$$_t.%s( (%s)this.templates[%d].read($1, _$$_t.%s()) );",
e.getSetterName(), e.getJavaTypeName(), i, e.getGetterName());
}
if (e.isOptional()) {
buildString("}");
}
}
buildString("$1.readArrayEnd();");
buildString("return _$$_t;");
buildString("}");
return getBuiltString();
}
@Override
public void writeTemplate(Class<?> targetClass, BeansFieldEntry[] entries, Template[] templates, String directoryName) {
throw new UnsupportedOperationException(targetClass.getName());
}
@Override
public Template loadTemplate(Class<?> targetClass, BeansFieldEntry[] entries, Template[] templates) {
return null;
}
}