1 30 import org.objectweb.asm.Attribute; 31 import org.objectweb.asm.FieldVisitor; 32 import org.objectweb.asm.ClassAdapter; 33 import org.objectweb.asm.ClassReader; 34 import org.objectweb.asm.ClassVisitor; 35 import org.objectweb.asm.ClassWriter; 36 import org.objectweb.asm.MethodVisitor; 37 import org.objectweb.asm.Opcodes; 38 import org.objectweb.asm.Label; 39 import org.objectweb.asm.ByteVector; 40 import org.objectweb.asm.util.TraceClassVisitor; 41 42 import java.io.FileOutputStream ; 43 import java.io.PrintWriter ; 44 45 48 public class Attributes extends ClassLoader { 49 50 public static void main(final String args[]) throws Exception { 51 ClassReader cr = new ClassReader("CommentAttribute"); 53 ClassWriter cw = new ClassWriter(0); 54 ClassVisitor cv = new AddCommentClassAdapter(cw); 55 cr.accept(cv, new Attribute[] { new CommentAttribute("") }, 0); 56 byte[] b = cw.toByteArray(); 57 58 FileOutputStream fos = new FileOutputStream ("CommentAttribute.class.new"); 60 fos.write(b); 61 fos.close(); 62 63 cr = new ClassReader(b); 65 cv = new TraceClassVisitor(new PrintWriter (System.out)); 66 cr.accept(cv, new Attribute[] { new CommentAttribute("") }, 0); 67 } 68 } 69 70 class AddCommentClassAdapter extends ClassAdapter implements Opcodes { 71 72 public AddCommentClassAdapter(final ClassVisitor cv) { 73 super(cv); 74 } 75 76 public void visit( 77 final int version, 78 final int access, 79 final String name, 80 final String signature, 81 final String superName, 82 final String [] interfaces) 83 { 84 super.visit(version, access, name, signature, superName, interfaces); 85 visitAttribute(new CommentAttribute("this is a class comment")); 86 } 87 88 public FieldVisitor visitField( 89 final int access, 90 final String name, 91 final String desc, 92 final String signature, 93 final Object value) 94 { 95 FieldVisitor fv = super.visitField(access, name, desc, signature, value); 96 fv.visitAttribute(new CommentAttribute("this is a field comment")); 97 return fv; 98 } 99 100 public MethodVisitor visitMethod( 101 final int access, 102 final String name, 103 final String desc, 104 final String signature, 105 final String [] exceptions) 106 { 107 MethodVisitor mv = cv.visitMethod(access, 108 name, 109 desc, 110 signature, 111 exceptions); 112 if (mv != null) { 113 mv.visitAttribute(new CommentAttribute("this is a method comment")); 114 } 115 return mv; 116 } 117 } 118 119 class CommentAttribute extends Attribute { 120 121 private String comment; 122 123 public CommentAttribute(final String comment) { 124 super("Comment"); 125 this.comment = comment; 126 } 127 128 public String getComment() { 129 return comment; 130 } 131 132 public boolean isUnknown() { 133 return false; 134 } 135 136 protected Attribute read( 137 final ClassReader cr, 138 final int off, 139 final int len, 140 final char[] buf, 141 final int codeOff, 142 final Label[] labels) 143 { 144 return new CommentAttribute(cr.readUTF8(off, buf)); 145 } 146 147 protected ByteVector write( 148 final ClassWriter cw, 149 final byte[] code, 150 final int len, 151 final int maxStack, 152 final int maxLocals) 153 { 154 return new ByteVector().putShort(cw.newUTF8(comment)); 155 } 156 } 157 | Popular Tags |