package hello.asm.my;
import hello.asm.bean.User;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
public class Hello extends ClassLoader implements Opcodes{
public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, IOException {
String n = User.class.getName();
ClassReader cr = new ClassReader(n);
final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassNode cn = new ClassNode();
cr.accept(cn, ASM4);
final List<AnnotationNode> nlist = cn.visibleAnnotations;
cr.accept(new ClassVisitor(ASM4,cw) {
@Override
public AnnotationVisitor visitAnnotation(String desc,
boolean visible) {
AnnotationNode an = new AnnotationNode(desc);
nlist.add(an);
return an;
}
}, 2);
for (AnnotationNode annotationNode : nlist) {
List vl = annotationNode.values;
System.out.println(vl.get(0).toString() + vl.get(1));
}
}
}
package hello.asm.bean;
@Table(name="BASE_USER")
@Encode("utf-8")
public class User {
@Column(name = "name")
private String name = "loowj";
@Column(name = "id")
private String id = "001";
/**
* 构造函数1
*/
public User(String id,String name) {
}
public User(String name){
}
private User(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void getUser(){
System.out.println(this.id + " " +this.name);
}
}
package hello.asm.bean;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
public String name();
}
package hello.asm.bean;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.TYPE,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Encode {
String value();
}