return merge(node, 0, context);
}
private String merge(INode node, int pop, Context context) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Writer out = new TemplateWriter(bos, "GBK");
ClassWriter cw = new ClassWriter(COMPUTE_FRAMES | COMPUTE_MAXS);
MethodVisitor mw;
String name = "test";
cw.visit(V1_5, ACC_PUBLIC, name, null, "java/lang/Object",
new String[] { IParser.NAME });
mw = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mw.visitVarInsn(ALOAD, THIS);
mw.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mw.visitInsn(RETURN);
mw.visitMaxs(0, 0);
mw.visitEnd();
// 定义类的merge方法
mw = cw.visitMethod(ACC_PUBLIC, "merge", "(L" + Context.NAME
+ ";Ljava/io/Writer;)V", null, null);
mw.visitVarInsn(ALOAD, CONTEXT);
mw.visitMethodInsn(INVOKEVIRTUAL, Context.NAME, "getTemplate", "()L"
+ Template.NAME + ";");
mw.visitVarInsn(ASTORE, TEMPLATE);
node.parse(mw, LOCAL_START, null);
while (pop-- > 0) {
mw.visitInsn(POP);
}
mw.visitInsn(RETURN);
mw.visitMaxs(0, 0);
mw.visitEnd();
cw.visitEnd();
byte[] code = cw.toByteArray();
try {
IParser parser = (IParser) DynamicClassLoader.getClass(name, code)
.newInstance();
parser.merge(context, out);
out.flush();
return new String(bos.toByteArray());
} catch (Exception e) {
throw new RuntimeException("不能实例化Parser对象");
}
}