-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathReflectionInvokerBuilder.java
More file actions
301 lines (262 loc) · 9.41 KB
/
ReflectionInvokerBuilder.java
File metadata and controls
301 lines (262 loc) · 9.41 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// MessagePack-RPC for Java
//
// Copyright (C) 2010 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.rpc.reflect;
import java.io.IOException;
import java.lang.reflect.*;
import org.msgpack.*;
import org.msgpack.template.*;
import org.msgpack.rpc.*;
import org.msgpack.type.Value;
import org.msgpack.unpacker.Converter;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
public class ReflectionInvokerBuilder extends InvokerBuilder {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ReflectionInvokerBuilder.class);
protected MessagePack messagePack;
public ReflectionInvokerBuilder(MessagePack messagePack){
this.messagePack = messagePack;
}
static abstract class ReflectionArgumentEntry extends ArgumentEntry {
ReflectionArgumentEntry(ArgumentEntry e) {
super(e);
}
public abstract void convert(Object[] params, Value obj) throws MessageTypeException;
public void setNull(Object[] params) {
params[getIndex()] = null;
}
}
static class NullArgumentEntry extends ReflectionArgumentEntry {
NullArgumentEntry(ArgumentEntry e) {
super(e);
}
public void convert(Object[] params, Value obj) throws MessageTypeException { }
}
static class BooleanArgumentEntry extends ReflectionArgumentEntry {
BooleanArgumentEntry(ArgumentEntry e) {
super(e);
}
public void convert(Object[] params, Value obj) throws MessageTypeException {
params[getIndex()] = obj.asBooleanValue().getBoolean();
}
}
static class ByteArgumentEntry extends ReflectionArgumentEntry {
ByteArgumentEntry(ArgumentEntry e) {
super(e);
}
public void convert(Object[] params, Value obj) throws MessageTypeException {
params[getIndex()] = obj.asIntegerValue().getByte();
}
}
static class ShortArgumentEntry extends ReflectionArgumentEntry {
ShortArgumentEntry(ArgumentEntry e) {
super(e);
}
public void convert(Object[] params, Value obj) throws MessageTypeException {
params[getIndex()] = obj.asIntegerValue().getShort();
}
}
static class IntArgumentEntry extends ReflectionArgumentEntry {
IntArgumentEntry(ArgumentEntry e) {
super(e);
}
public void convert(Object[] params, Value obj) throws MessageTypeException {
params[getIndex()] = obj.asIntegerValue().getInt();
}
}
static class LongArgumentEntry extends ReflectionArgumentEntry {
LongArgumentEntry(ArgumentEntry e) {
super(e);
}
public void convert(Object[] params, Value obj) throws MessageTypeException {
params[getIndex()] = obj.asIntegerValue().getLong();
}
}
static class FloatArgumentEntry extends ReflectionArgumentEntry {
FloatArgumentEntry(ArgumentEntry e) {
super(e);
}
public void convert(Object[] params, Value obj) throws MessageTypeException {
params[getIndex()] = obj.asFloatValue().getFloat();
}
}
static class DoubleArgumentEntry extends ReflectionArgumentEntry {
DoubleArgumentEntry(ArgumentEntry e) {
super(e);
}
public void convert(Object[] params, Value obj) throws MessageTypeException {
params[getIndex()] = obj.asFloatValue().getDouble();
}
}
static class ObjectArgumentEntry extends ReflectionArgumentEntry {
private Template template;
private MessagePack messagePack;
ObjectArgumentEntry(MessagePack messagePack,ArgumentEntry e, Template template) {
super(e);
this.template = template;
this.messagePack = messagePack;
}
public void convert(Object[] params, Value obj) throws MessageTypeException {
try {
params[getIndex()] = template.read(new Converter(messagePack,obj),null);//messagePack.convert(obj,template);
} catch (IOException e) {
new MessageTypeException(e);
}
}
}
private static class ReflectionInvoker implements Invoker {
protected Method method;
protected int parameterLength;
protected ReflectionArgumentEntry[] entries;
protected int minimumArrayLength;
boolean async;
public ReflectionInvoker(Method method, ReflectionArgumentEntry[] entries, boolean async) {
this.method = method;
this.parameterLength = method.getParameterTypes().length;
this.entries = entries;
this.async = async;
this.minimumArrayLength = 0;
for(int i=0; i < entries.length; i++) {
ReflectionArgumentEntry e = entries[i];
if(!e.isOptional()){//e.isRequired() || e.isNullable()) {
this.minimumArrayLength = i+1;
}
}
}
public void invoke(Object target, Request request) throws Exception {
Object[] params = new Object[parameterLength];
if(async) {
params[0] = request;
}
// TODO set default values here
try {
Value args = request.getArguments();
Value[] array = args.asArrayValue().getElementArray();
int length = array.length;
if(length < minimumArrayLength) {
throw new MessageTypeException(String.format("Method needs at least %s args.But only %s args are passed",minimumArrayLength,length));
}
int i;
for(i=0; i < minimumArrayLength; i++) {
ReflectionArgumentEntry e = entries[i];
if(!e.isAvailable()) {
continue;
}
Value obj = array[i];
if(obj.isNilValue()) {
if(e.isRequired()) {
// Required + nil => exception
throw new MessageTypeException();
} else if(e.isOptional()) {
// Optional + nil => keep default value
} else { // Nullable
// Nullable + nil => set null
e.setNull(params);
}
} else {
try{
e.convert(params, obj);
}catch(MessageTypeException mte){
logger.error(String.format("Expect Method:%s ArgIndex:%s Type:%s. But passed:%s",request.getMethodName(),i,e.getGenericType(),obj));
throw new MessageTypeException(String.format(
"%sth argument type is %s.But wrong type is sent.",i+1,e.getJavaTypeName())
);
}
}
}
int max = length < entries.length ? length : entries.length;
for(; i < max; i++) {
ReflectionArgumentEntry e = entries[i];
if(!e.isAvailable()) {
continue;
}
Value obj = array[i];
if(obj.isNilValue()) {
// this is Optional field becaue i >= minimumArrayLength
// Optional + nil => keep default value
} else {
try{
e.convert(params, obj);
}catch(MessageTypeException mte){
logger.error(String.format("Expect Method:%s ArgIndex:%s Type:%s. But passed:%s",request.getMethodName(),i,e.getGenericType(),obj));
throw new MessageTypeException(String.format(
"%sth argument type is %s.But wrong type is sent.",i+1,e.getJavaTypeName())
);
}
}
}
// latter entries are all Optional + nil => keep default value
} catch (MessageTypeException e) {
throw e;
} catch (Exception e) {
//e.printStackTrace();
throw e;
}
Object result = null;
try{
result = method.invoke(target, params);
}catch(InvocationTargetException e ){
if(e.getCause() != null && e.getCause() instanceof Exception){
throw (Exception)e.getCause();
}else{
throw e;
}
}
if(!async) {
request.sendResult(result);
}
// TODO exception
}
}
public Invoker buildInvoker(Method targetMethod, ArgumentEntry[] entries, boolean async) {
int mod = targetMethod.getModifiers();
if(!Modifier.isPublic(mod)) {
targetMethod.setAccessible(true);
}
ReflectionArgumentEntry[] res = new ReflectionArgumentEntry[entries.length];
for(int i=0; i < entries.length; i++) {
ArgumentEntry e = entries[i];
Class<?> type = e.getType();
if(!e.isAvailable()) {
res[i] = new NullArgumentEntry(e);
} else if(type.equals(boolean.class)) {
res[i] = new BooleanArgumentEntry(e);
} else if(type.equals(byte.class)) {
res[i] = new ByteArgumentEntry(e);
} else if(type.equals(short.class)) {
res[i] = new ShortArgumentEntry(e);
} else if(type.equals(int.class)) {
res[i] = new IntArgumentEntry(e);
} else if(type.equals(long.class)) {
res[i] = new LongArgumentEntry(e);
} else if(type.equals(float.class)) {
res[i] = new FloatArgumentEntry(e);
} else if(type.equals(double.class)) {
res[i] = new DoubleArgumentEntry(e);
} else {
Type t = e.getGenericType();
Template tmpl = messagePack.lookup(t);
if(tmpl == null){
messagePack.register((Class<?>)t);
tmpl = messagePack.lookup(t);
}
res[i] = new ObjectArgumentEntry(messagePack,e, tmpl);
}
}
return new ReflectionInvoker(targetMethod, res, async);
}
}