Skip to content

Commit 25f9cde

Browse files
committed
playground: fix issue in throw render result enhancement and put renderargs enhancement
1 parent f2c16da commit 25f9cde

3 files changed

Lines changed: 51 additions & 34 deletions

File tree

src/test/java/playground/ActionMethodEnhancer.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,14 @@ private void injectRenderArgSetCode(AbstractInsnNode invokeNode) {
154154
boolean breakWhile = false;
155155
switch (type) {
156156
case LABEL:
157+
case FRAME:
158+
node = node.getNext();
157159
breakWhile = true;
158160
break;
159161
case VAR_INSN:
160162
VarInsnNode n = (VarInsnNode)node;
161163
if (0 == n.var && !segment.meta.isStatic()) {
164+
// skip "this"
162165
break;
163166
}
164167
LoadInsn insn = LoadInsn.of(n.getOpcode());
@@ -175,6 +178,9 @@ private void injectRenderArgSetCode(AbstractInsnNode invokeNode) {
175178
}
176179
InsnList list = new InsnList();
177180
int len = loadInsnInfoList.size();
181+
if (len == 0) {
182+
return;
183+
}
178184
int appCtxIdx = appCtxIndex();
179185
if (appCtxIdx < 0) {
180186
MethodInsnNode getAppCtx = new MethodInsnNode(INVOKESTATIC, APP_CONTEXT, "get", "()Lorg/osgl/mvc/server/AppContext;", false);
@@ -215,6 +221,8 @@ private void injectThrowCode(AbstractInsnNode invokeNode) {
215221
break;
216222
case LINE:
217223
curLine = ((LineNumberNode)next).line;
224+
next = next.getNext();
225+
break;
218226
case JUMP_INSN:
219227
AbstractInsnNode tmp = next.getNext();
220228
instructions.remove(next);
@@ -226,6 +234,10 @@ private void injectThrowCode(AbstractInsnNode invokeNode) {
226234
tmp = next.getNext();
227235
instructions.remove(next);
228236
next = tmp;
237+
tmp = next.getPrevious();
238+
if (tmp.getType() == LINE) {
239+
instructions.remove(tmp);
240+
}
229241
break;
230242
}
231243
case FRAME:

src/test/java/playground/C1.java

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,34 @@
99
import org.osgl.mvc.result.Result;
1010
import org.osgl.mvc.server.AppConfig;
1111
import org.osgl.mvc.server.AppContext;
12+
import org.osgl.util.S;
1213

1314
public class C1 extends CBase {
1415

15-
// private static boolean cond1() {
16-
// return _.random(true, false);
17-
// }
18-
//
19-
// @Action(value = "/", methods = {})
20-
// public void root(String id, String email) {
21-
// String name = "root";
22-
// if (cond1()) {
23-
// int i = 5;
24-
// boolean b = cond1();render(id, email,
25-
// name, b, i);
26-
// //System.out.println("abc");
27-
// } else {
28-
// if (cond1()) {
29-
// int i = 0;
30-
// throw renderStatic("abc.html", id, i);
31-
// } else if (cond1()) {
32-
// notFound("not found: %s", 404);
33-
// } else {
34-
// String reason = "abc";
35-
// int code = 5, code2 = 3, code4 = 2;
36-
// badRequest(reason, -1, code, code2, code4);
37-
// }
38-
// }
39-
// }
16+
private static boolean cond1() {
17+
return _.random(true, false);
18+
}
19+
20+
@Action(value = "/", methods = {})
21+
public void root(String id, String email, boolean b) {
22+
String name = "root";
23+
if (b) {
24+
int i = 5;
25+
render(id, email, name, b, i);
26+
//System.out.println("abc");
27+
} else {
28+
if (S.empty(id)) {
29+
int i = 0;
30+
throw renderStatic("abc.html", id, i);
31+
} else if (S.empty(email)) {
32+
notFound("not found: %s", 404);
33+
} else {
34+
String reason = "abc";
35+
int code = 5, code2 = 3, code4 = 2;
36+
badRequest(reason, -1, code, code2, code4);
37+
}
38+
}
39+
}
4040
//
4141
// @GetAction("/do_anno")
4242
// private void doAnno(@Param("svc_id") String svcId, int age, @Param("map") String map) {
@@ -52,8 +52,11 @@ public class C1 extends CBase {
5252
// }
5353

5454
@GetAction("/doIt")
55-
public static void doIt(String id, String email, AppContext ctx) {
56-
int i = 0, j = 1; Boolean b = false;
55+
public static void doIt(@Param("acc_id") String id, String email, boolean b, AppContext ctx) {
56+
int i = 0, j = 1;
57+
if (b) {
58+
ok();
59+
}
5760
renderStatic("", i, j, b, id, email);
5861
}
5962
//

src/test/java/playground/Main.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import org.osgl.mvc.server.asm.ClassVisitor;
1212
import org.osgl.mvc.server.asm.ClassWriter;
1313
import org.osgl.mvc.server.asm.util.TraceClassVisitor;
14+
import org.osgl.util.IO;
1415

15-
import java.io.InputStream;
16-
import java.io.PrintWriter;
16+
import java.io.*;
1717
import java.lang.reflect.InvocationTargetException;
1818
import java.lang.reflect.Method;
1919

@@ -29,7 +29,7 @@ public class Main extends ClassLoader {
2929
@Override
3030
protected synchronized Class<?> loadClass(final String name,
3131
final boolean resolve) throws ClassNotFoundException {
32-
if (name.startsWith("java.") || name.startsWith("org.osgl.")) {
32+
if (!name.equals("playground.C1")) {
3333
return super.loadClass(name, resolve);
3434
}
3535

@@ -45,13 +45,15 @@ protected synchronized Class<?> loadClass(final String name,
4545
ClassReader cr = new ClassReader(is);
4646
ClassWriter cw = new ClassWriter(0);
4747
ClassVisitor cv = new ControllerClassVisitor(cw);
48-
ClassVisitor tv = new TraceClassVisitor(cv, new PrintWriter(System.out));
4948
cr.accept(cv, 0);
5049
b = cw.toByteArray();
50+
OutputStream os1 = new FileOutputStream("t:\\tmp\\4.class");
51+
IO.write(b, os1);
5152
System.out.println("------------ TRANSFORMED -----------");
5253
cr = new ClassReader(b);
5354
cw = new ClassWriter(0);
54-
tv = new TraceClassVisitor(cw, new PrintWriter(System.out));
55+
OutputStream os2 = new FileOutputStream("t:\\tmp\\4.java");
56+
ClassVisitor tv = new TraceClassVisitor(cw, new PrintWriter(os2));
5557
cr.accept(tv, 0);
5658
} catch (Exception e) {
5759
throw new ClassNotFoundException(name, e);
@@ -66,14 +68,14 @@ public static void main(final String args[]) throws Throwable {
6668
ClassLoader loader = new Main();
6769
String s = args.length == 0 ? "playground.C1" : args[0];
6870
Class<C1> c = (Class<C1>)loader.loadClass(s);
69-
Method m = c.getMethod("doIt", String.class, String.class, AppContext.class);
71+
Method m = c.getMethod("root", String.class, String.class, boolean.class/*, AppContext.class*/);
7072
AppConfig cfg = mock(AppConfig.class);
7173
H.Request req = mock(H.Request.class);
7274
H.Response resp = mock(H.Response.class);
7375
AppContext.init(cfg, req, resp);
7476
AppContext ctx = AppContext.get();
7577
try {
76-
m.invoke(c.newInstance(), "id_0", "[email protected]", ctx);
78+
m.invoke(c.newInstance(), "id_0", "[email protected]", false/*, ctx*/);
7779
System.out.println("Render failed");
7880
} catch (Result r) {
7981
System.out.println("Result rendered: ");

0 commit comments

Comments
 (0)