forked from msgpack/msgpack-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionBeansTemplateBuilder.java
More file actions
211 lines (191 loc) · 6.21 KB
/
ReflectionBeansTemplateBuilder.java
File metadata and controls
211 lines (191 loc) · 6.21 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
//
// 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.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import org.msgpack.annotation.Ignore;
import org.msgpack.annotation.Index;
import org.msgpack.annotation.NotNullable;
import org.msgpack.annotation.Optional;
import org.msgpack.packer.Packer;
import org.msgpack.template.FieldOption;
import org.msgpack.template.Template;
import org.msgpack.template.TemplateRegistry;
import org.msgpack.unpacker.Unpacker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Class for building java reflection template builder for java beans class.
*
* @author takeshita
*
*/
@SuppressWarnings({ "rawtypes" })
public class ReflectionBeansTemplateBuilder extends ReflectionTemplateBuilder {
private static Logger LOG = LoggerFactory.getLogger(ReflectionBeansTemplateBuilder.class);
static class ReflectionBeansFieldTemplate extends ReflectionFieldTemplate {
ReflectionBeansFieldTemplate(final FieldEntry entry) {
super(entry);
}
@Override
public void write(Packer packer, Object v, boolean required) throws IOException {
packer.write(v);
}
@Override
public Object read(Unpacker unpacker, Object to, boolean required) throws IOException {
Object o = unpacker.read(entry.getType());
entry.set(to, o);
return o;
}
}
public ReflectionBeansTemplateBuilder(TemplateRegistry registry) {
super(registry);
}
@Override
public boolean matchType(Type targetType, boolean hasAnnotation) {
Class<?> targetClass = (Class<?>) targetType;
boolean matched = matchAtBeansClassTemplateBuilder(targetClass, hasAnnotation);
if (matched) {
LOG.debug("matched type: " + targetClass.getName());
}
return matched;
}
@Override
protected ReflectionFieldTemplate[] toTemplates(FieldEntry[] entries) {
ReflectionFieldTemplate[] tmpls = new ReflectionFieldTemplate[entries.length];
for (int i = 0; i < entries.length; i++) {
FieldEntry e = entries[i];
Class<?> type = e.getType();
if (type.isPrimitive()) {
tmpls[i] = new ReflectionBeansFieldTemplate(e);
} else {
Template tmpl = registry.lookup(e.getGenericType());
tmpls[i] = new FieldTemplateImpl(e, tmpl);
}
}
return tmpls;
}
@Override
public FieldEntry[] toFieldEntries(Class<?> targetClass, FieldOption implicitOption) {
BeanInfo desc;
try {
desc = Introspector.getBeanInfo(targetClass);
} catch (IntrospectionException e1) {
throw new TemplateBuildException("Class must be java beans class:" + targetClass.getName());
}
PropertyDescriptor[] props = desc.getPropertyDescriptors();
ArrayList<PropertyDescriptor> list = new ArrayList<PropertyDescriptor>();
for (int i = 0; i < props.length; i++) {
PropertyDescriptor pd = props[i];
if (!isIgnoreProperty(pd)) {
list.add(pd);
}
}
props = new PropertyDescriptor[list.size()];
list.toArray(props);
BeansFieldEntry[] entries = new BeansFieldEntry[props.length];
for (int i = 0; i < props.length; i++) {
PropertyDescriptor p = props[i];
int index = getPropertyIndex(p);
if (index >= 0) {
if (entries[index] != null) {
throw new TemplateBuildException("duplicated index: "
+ index);
}
if (index >= entries.length) {
throw new TemplateBuildException("invalid index: " + index);
}
entries[index] = new BeansFieldEntry(p);
props[index] = null;
}
}
int insertIndex = 0;
for (int i = 0; i < props.length; i++) {
PropertyDescriptor p = props[i];
if (p != null) {
while (entries[insertIndex] != null) {
insertIndex++;
}
entries[insertIndex] = new BeansFieldEntry(p);
}
}
for (int i = 0; i < entries.length; i++) {
BeansFieldEntry e = entries[i];
FieldOption op = getPropertyOption(e, implicitOption);
e.setOption(op);
}
return entries;
}
private FieldOption getPropertyOption(BeansFieldEntry e, FieldOption implicitOption) {
FieldOption forGetter = getMethodOption(e.getPropertyDescriptor().getReadMethod());
if (forGetter != FieldOption.DEFAULT) {
return forGetter;
}
FieldOption forSetter = getMethodOption(e.getPropertyDescriptor().getWriteMethod());
if (forSetter != FieldOption.DEFAULT) {
return forSetter;
} else {
return implicitOption;
}
}
private FieldOption getMethodOption(Method method) {
if (isAnnotated(method, Ignore.class)) {
return FieldOption.IGNORE;
} else if (isAnnotated(method, Optional.class)) {
return FieldOption.OPTIONAL;
} else if (isAnnotated(method, NotNullable.class)) {
return FieldOption.NOTNULLABLE;
}
return FieldOption.DEFAULT;
}
private int getPropertyIndex(PropertyDescriptor desc) {
int getterIndex = getMethodIndex(desc.getReadMethod());
if (getterIndex >= 0) {
return getterIndex;
}
int setterIndex = getMethodIndex(desc.getWriteMethod());
return setterIndex;
}
private int getMethodIndex(Method method) {
Index a = method.getAnnotation(Index.class);
if (a == null) {
return -1;
} else {
return a.value();
}
}
private boolean isIgnoreProperty(PropertyDescriptor desc) {
if (desc == null) {
return true;
}
Method getter = desc.getReadMethod();
Method setter = desc.getWriteMethod();
return getter == null || setter == null
|| !Modifier.isPublic(getter.getModifiers())
|| !Modifier.isPublic(setter.getModifiers())
|| isAnnotated(getter, Ignore.class)
|| isAnnotated(setter, Ignore.class);
}
}