ASM ByteCode Instrumentation,执行哪种方法

时间:2013-02-06 06:37:49

标签: java-bytecode-asm

我正在使用ASM字节码库来使用pre-main来测试Java类。 我们如何获得执行方法的名称? 提前谢谢......

2 个答案:

答案 0 :(得分:0)

简而言之,相应的name方法调用有一个visit*()参数。您必须制作一个更具体的示例才能获得更详细的答案。

答案 1 :(得分:0)

Attached my Code for your reference

package com.eg.agent;

import java.lang.instrument.Instrumentation;

public class Agent {
 private Agent(Instrumentation instrumentation){
}

public static void premain(String agentArgs, Instrumentation inst)
{
 EgClassFileTransformer egClassFileTransformer = 
       new EgClassFileTransformer (agentArgs , inst);
}

}

package com.eg.agent;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.objectweb.asm.ClassReader;

public class EgClassFileTransformer implements ClassFileTransformer {

    protected String agentArgString = "";
    protected Instrumentation instrumentation;

    public EgClassFileTransformer(String agentArgs, Instrumentation inst){
        agentArgString = agentArgs;
        instrumentation = inst;
        instrumentation.addTransformer(this);
    }

    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, 
            ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException
        {
            //System.out.println("ClassName :"+className);
            InputStream in = loader.getResourceAsStream(className.replace('.', '/') + ".class");
            try{
                ClassReader classReader=new ClassReader(in);
                String superClassName = classReader.getSuperName();
                String[] interfaces = classReader.getInterfaces();
                if(interfaces!=null && interfaces.length > 0){
                    for(int k=0;k<interfaces.length;k++){
                        String inface = interfaces[k];
                        System.out.println(" \t interface :"+inface);
                    }
                }
                //System.out.println("superClassName :"+superClassName);
                ArrayList thisList = new ArrayList();
                thisList.add(superClassName);
                ArrayList superList = printSuperClassNames(superClassName , thisList);
                System.out.println("className :"+className +" ==>"+ " superList :"+superList);
            } catch (IOException e) {
                //e.printStackTrace();
                System.out.println("[EXECEPTION] ..."+className);
            }
            return null;
        }

    public static ArrayList printSuperClassNames(String className, ArrayList list)
    {
        ClassReader cr=null;
        try {
            cr = new ClassReader(className);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String superName = cr.getSuperName();
        //System.out.println("[superName]"+superName);
        if(superName!=null && !superName.equals("java/lang/Object"))
        {
            list.add(superName);
            String superClass = superName.replace('.', '/');
            printSuperClassNames(superClass, list);
        }
        return list;
    }
}