Java javassist 方法调用

标签 java methods code-injection javassist javaagents

因此,我将 java 代理与 javassist 结合使用,目的是将一些与监控相关的小代码注入(inject)到不同类中的不同方法中。

我的java代理代码:

public class ConverterAgent implements ClassFileTransformer {

public static void premain(String args, Instrumentation instrumentation){
    System.out.println(">>>>>>>>>> Intializing Java agent <<<<<<<<<<");
    ConverterAgent transformer = new ConverterAgent();
    instrumentation.addTransformer(transformer);

}

public static void agentmain(String args, Instrumentation instrumentation){
    System.out.println(">>>>>>>>>> Intializing Java agent <<<<<<<<<<");
    ConverterAgent transformer=new ConverterAgent();
    instrumentation.addTransformer(transformer); 
}


@Override
public byte[] transform(final ClassLoader loader, 
        String className, 
        Class<?> classBeingRedefined, 
        ProtectionDomain protectionDoman, 
        byte[] classFileBuffer)
                throws IllegalClassFormatException {



//javassist code goes here


return classFileBuffer;

}

}

我的 javassist 注入(inject)看起来像这样:

if ("className1".equals(className)){

//code

}


 if ("className2".equals(className)){

//same code as in first class

}


if ("className3".equals(className)){

//same code as in first and second class

}

所以我多次注入(inject)完全相同的代码,我想优化我的流程并为每次注入(inject)调用一个方法,这样我就不必一遍又一遍地复制相同的代码。但这就是我遇到问题的地方,我应该使用什么方法类型以及除了类和方法名称之外它还需要什么参数。

最佳答案

在您的转换方法中,您正在获取类字节码,然后返回经过修改的"new"类字节码。

这意味着您肯定要返回的是包含转换方法所需信息的字节[]

所以你的方法应该是这样的:

public class DynamicTransformer implements ClassFileTransformer {

    public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined,
    ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {

        byte[] byteCode = classfileBuffer;

        // into the transformer will arrive every class loaded so you filter 
        // to match only what you need
        if (className.equals("com/full/path/to/be/instrumented/className1") ||
            className.equals("com/full/path/to/be/instrumented/className2") ||
            className.equals("com/full/path/to/be/instrumented/className3") ) {

            byteCode = myMethodThatTransform(className, byteCode);
        }

        return byteCode;
    }


    public byte[] myMethodThatTransform(String className, byte[] byteCode){\
        try {
            // retrive default Javassist class pool
            ClassPool cp = ClassPool.getDefault();
            // get from the class pool our class with this qualified name
            CtClass cc = cp.get(className);
            // get all the methods of the retrieved class
            CtMethod[] methods = cc.getDeclaredMethods()
            for(CtMethod meth : methods) {
                // The instrumentation code to be returned and injected
                final StringBuffer buffer = new StringBuffer();
                String name = meth.getName();
                // just print into the buffer a log for example
                buffer.append("System.out.println(\"Method " + name + " executed\" );");
                meth.insertBefore(buffer.toString())
            }
            // create the byteclode of the class
            byteCode = cc.toBytecode();
            // remove the CtClass from the ClassPool
            cc.detach();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return byteCode;
    }
}

关于Java javassist 方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42621130/

相关文章:

java - curl 错误 : could not resolve host, java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1

java - 有什么好的免费工具可以用来调查 Java 中无意的对象保留?

java - 测试Java代码时"Cannot find symbol"

python - 为什么我们不能 **unsplat 'self' 到一个方法中?

c# - 如何在文本框为空时输入默认值

visual-studio-code - 如何在 vscode 中正确注入(inject)语法扩展(所以它有效)?

javascript - 使用 Objective-C 的 JavaScript 注入(inject)方法

java - Spring 无法自动注入(inject)构造函数参数

java - 在java中使用一个方法内生成的本体到另一个方法

dll中的子应用程序中的c++事件循环