什么是Java字节码注入? [英] What is Java bytecode injection?

查看:1206
本文介绍了什么是Java字节码注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java字节码注入究竟是什么以及为什么要使用它?

What exactly is Java bytecode injection and why would one use it?

推荐答案

Java代码编译成字节码( Foo.java - >> Foo.class )。字节码注入在运行时修改 Foo.class ,以便在加载和运行之前将代码注入其中。

Java code compiles into bytecode (Foo.java ->> Foo.class). Bytecode injection is modifying Foo.classat runtime to inject code into it right before its loaded and run.

想象一下我想知道方法的次数

Imagine a scenario where I want to find out how many times method

public void bar();

Foo.class 中调用。我可以使用 java.lang.instrument 编写代理,在类加载期间拦截 Foo.class ,使用<修改它a href =http://asm.ow2.org/ =noreferrer> ASM ,以便 bar()调用 com.amir.agent.incrementCount()关于方法输入。

is invoked in Foo.class. I could write an agent using java.lang.instrument that intercepts Foo.class during class load, modifies it using ASM so that bar() calls com.amir.agent.incrementCount() on method entry.

现在我可以运行我的程序:

Now I can run my program:

$java -javagent:MyAgent Foo  //assuming Foo has a main() defined

并且在运行时, Foo.class 不仅会正常工作,还会完成我在 MyAgent 每次调用 bar()

and at runtime, Foo.class will not only do its normal work, it will also do work I defined in MyAgent each time bar() is invoked.

For从头开始编写代理的指针,启动此处

For a pointer on writing an agent from scratch, start here.

这篇关于什么是Java字节码注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆