forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSenderEnhancer.java
More file actions
519 lines (475 loc) · 22.5 KB
/
SenderEnhancer.java
File metadata and controls
519 lines (475 loc) · 22.5 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package act.mail.bytecode;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* 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.
* #L%
*/
import act.asm.Label;
import act.asm.MethodVisitor;
import act.asm.Opcodes;
import act.asm.Type;
import act.asm.tree.*;
import act.controller.meta.HandlerParamMetaInfo;
import act.controller.meta.LocalVariableMetaInfo;
import act.mail.MailerContext;
import act.mail.meta.SenderMethodMetaInfo;
import act.util.AsmTypes;
import org.osgl.logging.L;
import org.osgl.logging.Logger;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.concurrent.Future;
public class SenderEnhancer extends MethodVisitor implements Opcodes {
private static final Logger logger = L.get(SenderEnhancer.class);
private SenderMethodMetaInfo info;
private MethodVisitor next;
private int paramIdShift = 0;
public SenderEnhancer(final MethodVisitor mv, SenderMethodMetaInfo meta, final int access, final String name, final String desc, final String signature, final String[] exceptions) {
super(ASM5, new MethodNode(access, name, desc, signature, exceptions));
this.info = meta;
this.next = mv;
}
@Override
public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
if (!"this".equals(name)) {
int paramId = index;
if (!info.isStatic()) {
paramId--;
}
paramId -= paramIdShift;
if (paramId < info.paramCount()) {
HandlerParamMetaInfo param = info.param(paramId);
param.name(name);
if (Type.getType(long.class).equals(param.type()) || Type.getType(double.class).equals(param.type())) {
paramIdShift++;
}
}
LocalVariableMetaInfo local = new LocalVariableMetaInfo(index, name, desc, start, end);
info.addLocal(local);
}
super.visitLocalVariable(name, desc, signature, start, end, index);
}
@Override
public void visitEnd() {
MethodNode mn = (MethodNode) mv;
transform(mn);
mn.accept(next);
super.visitEnd();
}
private void transform(MethodNode mn) {
new Transformer(mn, info).doIt();
}
private static class Transformer {
MethodNode mn;
InsnList instructions;
private SenderMethodMetaInfo meta;
List<Label> lblList = C.newSizedList(20);
Transformer(MethodNode mn, SenderMethodMetaInfo meta) {
this.mn = mn;
this.meta = meta;
this.instructions = mn.instructions;
}
void doIt() {
ListIterator<AbstractInsnNode> itr = instructions.iterator();
Segment cur = null;
while (itr.hasNext()) {
AbstractInsnNode insn = itr.next();
if (insn.getType() == AbstractInsnNode.LABEL) {
cur = new Segment(((LabelNode) insn).getLabel(), meta, instructions, itr, this);
} else if (null != cur) {
if (cur.handle(insn)) {
cur = null;
}
}
}
}
private static abstract class InstructionHandler {
Segment segment;
SenderMethodMetaInfo meta;
InstructionHandler(Segment segment, SenderMethodMetaInfo meta) {
this.segment = segment;
this.meta = meta;
}
protected abstract boolean handle(AbstractInsnNode node);
protected void refreshIteratorNext() {
segment.itr.previous();
segment.itr.next();
}
}
private static class Segment {
Label startLabel;
InsnList instructions;
SenderMethodMetaInfo meta;
ListIterator<AbstractInsnNode> itr;
Transformer trans;
private Map<Integer, InstructionHandler> handlers;
Segment(Label start, SenderMethodMetaInfo meta, InsnList instructions, ListIterator<AbstractInsnNode> itr, Transformer trans) {
E.NPE(meta);
this.startLabel = start;
this.meta = meta;
this.instructions = instructions;
this.itr = itr;
this.trans = trans;
this.handlers = C.Map(
AbstractInsnNode.METHOD_INSN, new InvocationHandler(this, meta)
);
trans.lblList.add(start);
}
protected boolean handle(AbstractInsnNode node) {
InstructionHandler handler = handlers.get(node.getType());
if (null != handler) {
return handler.handle(node);
} else {
return false;
}
}
}
private static class InvocationHandler extends InstructionHandler {
InvocationHandler(Segment segment, SenderMethodMetaInfo meta) {
super(segment, meta);
}
@Override
protected boolean handle(AbstractInsnNode node) {
MethodInsnNode n = (MethodInsnNode) node;
Type type = Type.getMethodType(n.desc);
Type retType = type.getReturnType();
//String method = n.name;
//String owner = Type.getType(n.owner).toString();
if (MailerContext.class.getName().equals(retType.getClassName())) {
return injectCreatingMailerContextCode(n);
} else if (Future.class.getName().equals(retType.getClassName()) && "send".equals(n.name)) {
return injectRenderArgSetCode(n);
}
return false;
}
private boolean injectCreatingMailerContextCode(AbstractInsnNode insnNode) {
AbstractInsnNode node = insnNode.getNext();
InsnList list = new InsnList();
if (node instanceof VarInsnNode) {
VarInsnNode varNode = (VarInsnNode) node;
if (varNode.getOpcode() == ASTORE) {
meta.appCtxLocalVariableTableIndex(varNode.var);
list.add(new TypeInsnNode(NEW, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME));
list.add(new InsnNode(DUP));
list.add(new MethodInsnNode(INVOKESTATIC, AsmTypes.APP_INTERNAL_NAME, "instance", "()" + AsmTypes.APP_DESC, false));
String confId = meta.configId();
if (null == confId) {
confId = "default";
}
list.add(new LdcInsnNode(confId));
String templateContext = meta.templateContext();
if (null != templateContext) {
list.add(new LdcInsnNode(templateContext));
list.add(new MethodInsnNode(INVOKESPECIAL, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME, "<init>", "(Lact/app/App;Ljava/lang/String;Ljava/lang/String;)V", false));
} else {
list.add(new MethodInsnNode(INVOKESPECIAL, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME, "<init>", "(Lact/app/App;Ljava/lang/String;)V", false));
}
segment.instructions.insertBefore(node, list);
segment.instructions.remove(insnNode);
return true;
}
return false;
} else {
list.add(new TypeInsnNode(NEW, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME));
list.add(new InsnNode(DUP));
list.add(new MethodInsnNode(INVOKESTATIC, AsmTypes.APP_INTERNAL_NAME, "instance", "()" + AsmTypes.APP_DESC, false));
String confId = meta.configId();
if (null == confId) {
confId = "default";
}
list.add(new LdcInsnNode(confId));
String templateContext = meta.templateContext();
if (null != templateContext) {
list.add(new LdcInsnNode(templateContext));
list.add(new MethodInsnNode(INVOKESPECIAL, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME, "<init>", "(Lact/app/App;Ljava/lang/String;Ljava/lang/String;)V", false));
} else {
list.add(new MethodInsnNode(INVOKESPECIAL, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME, "<init>", "(Lact/app/App;Ljava/lang/String;)V", false));
}
segment.instructions.insertBefore(node, list);
segment.instructions.remove(insnNode);
node = node.getNext();
while (node.getOpcode() != POP) {
node = node.getNext();
}
int maxLocal = segment.trans.mn.maxLocals;
segment.trans.mn.maxLocals++;
segment.instructions.insertBefore(node, new VarInsnNode(ASTORE, maxLocal));
segment.instructions.remove(node);
meta.appCtxLocalVariableTableIndex(maxLocal);
return true;
}
}
@SuppressWarnings("FallThrough")
private boolean injectRenderArgSetCode(AbstractInsnNode invokeNode) {
if (!segment.meta.hasLocalVariableTable()) {
logger.warn("local variable table info not found. AppContext render args will not be automatically populated");
}
AbstractInsnNode node = invokeNode.getPrevious();
List<LoadInsnInfo> loadInsnInfoList = new ArrayList<>();
String templateLiteral = null;
while (null != node) {
int type = node.getType();
boolean breakWhile = false;
switch (type) {
case AbstractInsnNode.LABEL:
case AbstractInsnNode.FRAME:
node = node.getNext();
breakWhile = true;
break;
case AbstractInsnNode.VAR_INSN:
VarInsnNode n = (VarInsnNode) node;
if (0 == n.var && !segment.meta.isStatic()) {
// skip "this"
break;
}
LoadInsn insn = LoadInsn.of(n.getOpcode());
if (insn.isStoreInsn()) {
break;
}
LoadInsnInfo info = new LoadInsnInfo(insn, n.var);
loadInsnInfoList.add(info);
break;
case AbstractInsnNode.LDC_INSN:
LdcInsnNode ldc = (LdcInsnNode) node;
if (null != templateLiteral) {
logger.warn("Cannot have more than one template path parameter in the render call. Template path[%s] ignored", templateLiteral);
} else if (!(ldc.cst instanceof String)) {
logger.warn("Template path must be strictly String type. Found: %s", ldc.cst);
} else {
templateLiteral = ldc.cst.toString();
}
default:
}
if (breakWhile) {
break;
}
node = node.getPrevious();
}
InsnList list = new InsnList();
int len = loadInsnInfoList.size();
// SetRenderArgs enhancement
int ctxId = meta.appCtxLocalVariableTableIndex();
if (ctxId < 0) {
list.add(new TypeInsnNode(NEW, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME));
list.add(new InsnNode(DUP));
list.add(new MethodInsnNode(INVOKESTATIC, AsmTypes.APP_INTERNAL_NAME, "instance", "()" + AsmTypes.APP_DESC, false));
String confId = meta.configId();
if (null == confId) {
confId = "default";
}
list.add(new LdcInsnNode(confId));
String templateContext = meta.templateContext();
if (null != templateContext) {
list.add(new LdcInsnNode(templateContext));
list.add(new MethodInsnNode(INVOKESPECIAL, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME, "<init>", "(Lact/app/App;Ljava/lang/String;Ljava/lang/String;)V", false));
} else {
list.add(new MethodInsnNode(INVOKESPECIAL, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME, "<init>", "(Lact/app/App;Ljava/lang/String;)V", false));
}
int maxLocal = segment.trans.mn.maxLocals;
list.add(new VarInsnNode(ASTORE, maxLocal));
segment.trans.mn.maxLocals++;
ctxId = maxLocal;
}
list.add(new VarInsnNode(ALOAD, ctxId));
S.Buffer sb = S.newBuffer();
for (int i = 0; i < len; ++i) {
LoadInsnInfo info = loadInsnInfoList.get(i);
info.appendTo(list, segment, sb);
}
LdcInsnNode ldc = new LdcInsnNode(sb.toString());
list.add(ldc);
MethodInsnNode invokeRenderArg = new MethodInsnNode(INVOKEVIRTUAL, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME, RENDER_ARG_NAMES_NM, RENDER_ARG_NAMES_DESC, false);
list.add(invokeRenderArg);
// setTemplatePath enhancement
if (null != templateLiteral) {
LdcInsnNode insnNode = new LdcInsnNode(templateLiteral);
list.add(insnNode);
MethodInsnNode invokeTemplatePath = new MethodInsnNode(INVOKEVIRTUAL, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME, TEMPLATE_LITERAL, TEMPLATE_LITERAL_DESC, false);
list.add(invokeTemplatePath);
} else {
String className = meta.classInfo().className();
String method = meta.name();
list.add(new LdcInsnNode(className));
list.add(new LdcInsnNode(method));
list.add(new MethodInsnNode(INVOKEVIRTUAL, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME, SENDER_PATH_NM, SENDER_LITERAL_DESC, false));
}
InsnNode pop = new InsnNode(POP);
list.add(pop);
list.add(new VarInsnNode(ALOAD, ctxId));
String method = meta.appCtxLocalVariableTableIndex() < 0 ? "doSend" : "doSendWithoutLoadThreadLocal";
MethodInsnNode realSend = new MethodInsnNode(INVOKESTATIC, "act/mail/Mailer$Util", method, "(Lact/mail/MailerContext;)Ljava/util/concurrent/Future;", false);
list.add(realSend);
segment.instructions.insertBefore(node, list);
while (node != invokeNode) {
AbstractInsnNode n0 = node.getNext();
segment.instructions.remove(node);
node = n0;
}
segment.instructions.remove(invokeNode);
return true;
}
}
private static final int _I = 'I';
private static final int _Z = 'Z';
private static final int _S = 'S';
private static final int _B = 'B';
private static final int _C = 'C';
private enum LoadInsn {
I(ILOAD) {
void appendTo(InsnList list, int varIndex, String type) {
super.appendTo(list, varIndex, type);
String owner, desc;
switch (type.hashCode()) {
case _I:
owner = "java/lang/Integer";
desc = "(I)Ljava/lang/Integer;";
break;
case _Z:
owner = "java/lang/Boolean";
desc = "(Z)Ljava/lang/Boolean;";
break;
case _S:
owner = "java/lang/Short";
desc = "(S)Ljava/lang/Short";
break;
case _B:
owner = "java/lang/Byte";
desc = "(B)Ljava/lang/Byte;";
break;
case _C:
owner = "java/lang/Character";
desc = "(C)Ljava/lang/Character;";
break;
default:
throw E.unexpected("int var type not recognized: %s", type);
}
MethodInsnNode method = new MethodInsnNode(INVOKESTATIC, owner, "valueOf", desc, false);
list.add(method);
}
}, L(LLOAD) {
@Override
void appendTo(InsnList list, int varIndex, String type) {
super.appendTo(list, varIndex, type);
MethodInsnNode method = new MethodInsnNode(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
list.add(method);
}
}, F(FLOAD) {
@Override
void appendTo(InsnList list, int varIndex, String type) {
super.appendTo(list, varIndex, type);
MethodInsnNode method = new MethodInsnNode(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;", false);
list.add(method);
}
}, D(DLOAD) {
@Override
void appendTo(InsnList list, int varIndex, String type) {
super.appendTo(list, varIndex, type);
MethodInsnNode method = new MethodInsnNode(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false);
list.add(method);
}
}, A(ALOAD), Store(-1) {
@Override
void appendTo(InsnList list, int varIndex, String type) {
throw E.unsupport();
}
};
private int opcode;
LoadInsn(int opcode) {
this.opcode = opcode;
}
static LoadInsn of(int opcode) {
switch (opcode) {
case ILOAD:
return I;
case LLOAD:
return L;
case FLOAD:
return F;
case DLOAD:
return D;
case ALOAD:
return A;
default:
return Store;
}
}
boolean isStoreInsn() {
return this == Store;
}
void appendTo(InsnList list, int varIndex, String type) {
VarInsnNode load = new VarInsnNode(opcode, varIndex);
list.add(load);
}
}
private static class LoadInsnInfo {
LoadInsn insn;
int index;
LoadInsnInfo(LoadInsn insn, int index) {
this.insn = insn;
this.index = index;
}
void appendTo(InsnList list, Segment segment, S.Buffer paramNames) {
LocalVariableMetaInfo var = var(segment);
if (null == var) return;
LdcInsnNode ldc = new LdcInsnNode(var.name());
list.add(ldc);
insn.appendTo(list, index, var.type());
MethodInsnNode invokeRenderArg = new MethodInsnNode(INVOKEVIRTUAL, AsmTypes.MAILER_CONTEXT_INTERNAL_NAME, RENDER_NM, RENDER_DESC, false);
list.add(invokeRenderArg);
if (paramNames.length() != 0) {
paramNames.append(',');
}
paramNames.append(var.name());
}
LocalVariableMetaInfo var(Segment segment) {
Label lbl = segment.startLabel;
int pos = -1;
List<Label> lblList = segment.trans.lblList;
while (null != lbl) {
LocalVariableMetaInfo var = segment.meta.localVariable(index, lbl);
if (null != var) return var;
if (-1 == pos) {
pos = lblList.indexOf(lbl);
if (pos <= 0) {
return null;
}
}
lbl = lblList.get(--pos);
}
return null;
}
@Override
public String toString() {
return S.fmt("%sLoad %s", insn, index);
}
}
}
private static final String GET_MAILER_CTX_DESC = "()" + AsmTypes.MAILER_CONTEXT_DESC;
private static final String RENDER_NM = "renderArg";
private static final String RENDER_DESC = AsmTypes.methodDesc(MailerContext.class, String.class, Object.class);
private static final String TEMPLATE_LITERAL = "templateLiteral";
private static final String SENDER_PATH_NM = "senderPath";
private static final String TEMPLATE_LITERAL_DESC = AsmTypes.methodDesc(MailerContext.class, String.class);
private static final String SENDER_LITERAL_DESC = AsmTypes.methodDesc(MailerContext.class, String.class, String.class);
private static final String RENDER_ARG_NAMES_NM = "__appRenderArgNames";
private static final String RENDER_ARG_NAMES_DESC = AsmTypes.methodDesc(MailerContext.class, String.class);
}