快速业务通道

Classworking工具箱: 注释(Annotation)与ASM - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-16
ing</code> method to a class.   */ public class ToStringGenerator extends ClassAdapter {    private final ClassWriter m_writer;    private final String m_internalName;    private final FieldInfo[] m_fields;    public ToStringGenerator(ClassWriter cw, String iname, FieldInfo[] props) {      super(cw);      m_writer = cw;      m_internalName = iname;      m_fields = props;    }    // called at end of class    public void visitEnd() {      // set up to build the toString() method      MethodVisitor mv = m_writer.visitMethod(Opcodes.ACC_PUBLIC,        "toString", "()Ljava/lang/String;", null, null);      mv.visitCode();      // create and initialize StringBuffer instance      mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuffer");      mv.visitInsn(Opcodes.DUP);      mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuffer",        "<init>", "()V");      // start text with class name      String name = m_internalName;      int split = name.lastIndexOf(''/'');      if (split >= 0) {        name = name.substring(split+1);      }      mv.visitLdcInsn(name + ":");      mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer",        "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;");      // loop through all field values to be included      boolean newline = false;      for (int i = 0; i < m_fields.length; i++) {        // check type of field (objects other than Strings need conversion)        FieldInfo prop = m_fields[i];        Type type = prop.getType();        boolean isobj = type.getSort() == Type.OBJECT &&          !type.getClassName().equals("java.lang.String");        // format lead text, with newline for object or after object        String lead = (isobj || newline) ? "\n " : " ";        if (prop.getText().length() > 0) {          lead += prop.getText() + "=";        }        mv.visitLdcInsn(lead);        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL,          "java/lang/StringBuffer", "append",          "(Ljava/lang/String;)Ljava/lang/StringBuffer;");        // load the actual field value and append        mv.visitVarInsn(Opcodes.ALOAD, 0);        mv.visitFieldInsn(Opcodes.GETFIELD, m_internalName,          prop.getField(), type.getDescriptor());        if (isobj) {          // convert objects by calling toString() method          mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL,           

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号