ASM4 读取java类注解

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();
}


转载于:https://my.oschina.net/wjgood/blog/61808

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值