java ASM 分析类
java ASM 分析类
yan5845hao 发表于11个月前
java ASM 分析类
  • 发表于 11个月前
  • 阅读 6
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

实现ClassVisitor接口

package com.uwo.resources.asm.test;

import org.objectweb.asm.*;

/**
 * Created by yanhao on 2017/5/15.
 */
public class UwoClassVisitor implements ClassVisitor{

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

    public void visitOuterClass(String owner, String name, String desc) {
        System.out.println("visitOuterClass");
    }

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

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

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

    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
        System.out.println("Annotation===========start==========");
        System.out.println("Annotation desc = " + desc);
        System.out.println("Annotation visible = " + visible);
        System.out.println("Annotation===========end==========");
        return null;
    }

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

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

    public void visitEnd() {
        System.out.println("visitEnd");
    }
}

执行代码

try {
    ClassReader cr = new ClassReader(Uwo.class.getName());
    cr.accept(new UwoClassVisitor(), 0);
} catch (IOException e) {
    e.printStackTrace();
}

执行结果

visit
visitSource
Field===========start==========
Field access = 4
Field name = name
Field desc = Ljava/lang/String;
Field signature = null
Field value = null
Field===========end==========
Field===========start==========
Field access = 4
Field name = sex
Field desc = Ljava/lang/Integer;
Field signature = null
Field value = null
Field===========end==========
Method===========start==========
Method access = 1
Method name = <init>
Method desc = (Ljava/lang/String;)V
Method signature = null
Method exceptions = null
Method===========end==========
Method===========start==========
Method access = 1
Method name = hello
Method desc = (Ljava/lang/String;)Ljava/lang/String;
Method signature = null
Method exceptions = null
Method===========end==========
Method===========start==========
Method access = 1
Method name = run
Method desc = ()V
Method signature = null
Method exceptions = null
Method===========end==========
visitEnd
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 6
博文 102
码字总数 20669
×
yan5845hao
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: