我是靠谱客的博主 积极银耳汤,最近开发中收集的这篇文章主要介绍ClassVisitor 类分析,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

读取的类文件

1.hello/asm/bean/User.java

2.hello/asm/bean/Column.java

3.hello/asm/bean/Table.java

package hello.asm.bean;

@Table(name="BASE_USER")
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.CLASS)
public @interface Table {
    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(ElementType.FIELD)
@Retention(RetentionPolicy.CLASS)
public @interface Column {
    String name();
}

package hello.asm.my;

import java.io.IOException;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;

public class ClassPrinter extends ClassVisitor {
	
	public ClassPrinter(int api) {
		super(api);
	}

	public void visit(int version, int access, String name, String signature,String superName, String[] interfaces) {
	    System.out.println("visit,version:"+version+",access:"+access+",name:"+name+",signature:"+signature+",superName:"+superName
	    		+",interfaces:"+interfaces);
	}

	public void visitSource(String source, String debug) {
	    System.out.println("visitSource,source:"+source + ",debug:"+debug);
	}

	public void visitOuterClass(String owner, String name, String desc) {
	    System.out.println("visitOuterClass,owner:"+owner + ",name:"+name+",desc:"+desc);
	}
	
	public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
	    	System.out.println("visitAnnotation,desc:"+desc + ",visible:"+visible);
		return null;
	}

	public void visitAttribute(Attribute attr) {
	    System.out.println("visitAttribute,attr:"+attr);
	}

	public void visitInnerClass(String name, String outerName,
			String innerName, int access) {
	    System.out.println("visitInnerClass,name:"+name+",outerName:"+outerName+",innerName:"+innerName+",access:"+access);
	}

	public FieldVisitor visitField(int access, String name, String desc,
			String signature, Object value) {
	    	System.out.println("visitField,access:"+access+",name:"+name+",desc:"+desc+",signature:"+signature
	    		+",value:"+value);
		return null;
	}

	public MethodVisitor visitMethod(int access, String name, String desc,
			String signature, String[] exceptions) {
	    System.out.println("visitMethod,access:"+access+",name:"+name+",desc:"+desc+",signature:"+signature
	    		+",exceptions:"+exceptions);
		return null;
	}

	public void visitEnd() {
		System.out.println("}");
	}
	
	
	public static void main(String[] args) throws IOException {
		ClassPrinter cp = new ClassPrinter(0);
		ClassReader cr = new ClassReader("hello/asm/bean/User");
		cr.accept(cp, 0);
	}
}

执行的结果如下:

visit,version:50,access:33,name:hello/asm/bean/User,signature:null,superName:java/lang/Object,interfaces:[Ljava.lang.String;@ca0b6

visitSource,source:User.java,debug:null

visitAnnotation,desc:Lhello/asm/bean/Table;,visible:false

visitField,access:2,name:name,desc:Ljava/lang/String;,signature:null,value:null

visitField,access:2,name:id,desc:Ljava/lang/String;,signature:null,value:null

visitMethod,access:1,name:<init>,desc:(Ljava/lang/String;Ljava/lang/String;)V,signature:null,exceptions:null

visitMethod,access:1,name:<init>,desc:(Ljava/lang/String;)V,signature:null,exceptions:null

visitMethod,access:2,name:<init>,desc:()V,signature:null,exceptions:null

visitMethod,access:1,name:getName,desc:()Ljava/lang/String;,signature:null,exceptions:null

visitMethod,access:1,name:setName,desc:(Ljava/lang/String;)V,signature:null,exceptions:null

visitMethod,access:1,name:getId,desc:()Ljava/lang/String;,signature:null,exceptions:null

visitMethod,access:1,name:setId,desc:(Ljava/lang/String;)V,signature:null,exceptions:null

visitMethod,access:1,name:getUser,desc:()V,signature:null,exceptions:null

}


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

最后

以上就是积极银耳汤为你收集整理的ClassVisitor 类分析的全部内容,希望文章能够帮你解决ClassVisitor 类分析所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部