简体   繁体   English

字节码操纵模式

[英]Bytecode manipulation patterns

What legitimate uses are there for bytecode manipulation and how people implement those bytecode manipulation based solutions in practice? 字节码操作的合法用途是什么以及人们如何在实践中实现基于字节码操作的解决方案?

Update: I should have made it more clear that this question really is about what patterns and techniques people use to make their code fly with the help of bytecode manipulation. 更新:我应该更清楚地说明这个问题确实是关于人们使用什么模式和技术来使代码在字节码操作的帮助下飞行。

Something like aspect oriented programming that was already mentioned or building proxy objects on the fly and similar techniques. 像已经提到的面向方面编程或动态构建代理对象和类似技术的东西。

Bytecode manipulation lets you implement arbitrarily complex (and interesting) program transformations, such as: 字节码操作允许您实现任意复杂(和有趣)的程序转换,例如:

  • entry/exit logging code for selected functions 所选功能的进入/退出记录代码
  • security transformations that stub out access to certain API's 安全转换,可以访问某些API
  • API substitution for, eg, running code in a test harness. API替换,例如,在测试工具中运行代码。

The scope is endless; 范围无穷无尽; this is just a small sampling. 这只是一个小样本。

As for how this is typically done, start here . 至于如何做到这一点,从这里开始。

So, one can read bytecode to implement an interpreter / JVM. 因此,可以读取字节码来实现解释器/ JVM。 One can write / generate bytecode when implementing a Java compiler or a compiler for another language that will target the JVM (eg Scala and Jython ). 在实现Java编译器或针对JVM的另一种语言的编译器(例如ScalaJython )时,可以编写/生成字节码。 You might perform bytecode manipulation to optimize bytecode (if you want to produce and market a bytecode optimizer or you need it as an internal tool to give your company's code an edge over the competition). 您可以执行字节码操作来优化字节码(如果您想生产和销售字节码优化器,或者您需要它作为内部工具来使您公司的代码在竞争中占据优势)。 In a similar vein, you might manipulate bytecode in order to obfuscate it prior to distribution. 与此类似,您可以操作字节码,以便在分发之前对其进行模糊处理。 You might also perform bytecode manipulation for aspect-oriented programming ; 您也可以为面向方面的编程执行字节码操作; for example, you might want to insert hooks (maybe for timing or logging purposes or for some other reason), and if it were simpler or less expensive to manipulate the bytecode than to edit all the source files (such as might be the case if the source code is unavailable or from many different sources, not all of which may be under one's control or for which it might be expensive and time-consuming to convince those teams to add such hooks), this might be a case where it would make sense to insert the modifications to the final bytecode output rather than to attempt to modify the original code (which might require upstreaming or maintaining a separate fork, or purchasing the source code from a third party that supplies only the bytecode). 例如,您可能希望插入挂钩(可能用于计时或日志记录或出于某些其他原因),并且如果操作字节码比编辑所有源文件更简单或更便宜(例如可能是这种情况)源代码不可用或来自许多不同的来源,并非所有这些都可能在一个人的控制之下,或者说服那些团队添加这样的钩子可能是昂贵和耗时的),这可能是它会使将修改插入最终字节码输出而不是尝试修改原始代码(可能需要上游或维护单独的fork,或从仅提供字节码的第三方购买源代码)。

You can manipulate bytecode yourself, although there are many existing open source libraries and frameworks to do it, including BCEL and ASM to name just a couple. 您可以自己操作字节码,尽管有许多现有的开源库和框架可以做到,包括BCELASM ,仅举几例。

There are papers Patterns of Aspect-Oriented Design (PDF) and Aspect-Oriented Design Principles: Lessons from Object-Oriented Design (PDF) which describe some patterns for AOP/bytecode manipulation. 有论文的面向方面设计模式 (PDF)和面向方面的设计原则:面向对象设计的教训 (PDF)描述了AOP /字节码操作的一些模式。

Personally I have used bytecode manipulation with ASM in one framework to generate some boilerplate code for classes which use that framework. 我个人在一个框架中使用了ASM的字节码操作来为使用该框架的类生成一些样板代码。 The framework requires custom equals() and hashCode() methods for client code, so I generate those by hooking in a Java Agent which modifies the bytecode as the ClassLoader loads the classes. 框架需要为客户端代码定制equals()和hashCode()方法,因此我通过挂钩Java代理生成这些方法 ,该代理在ClassLoader加载类时修改字节码。 I have also many times used CGLIB to produce dynamic proxies (if that counts as AOP). 我也多次使用CGLIB来生成动态代理(如果算作AOP)。

One use for bytecode manipulation is in aspect oriented programming . 字节码操作的一个用途是面向方面编程 In Java, you can use AspectJ for this. 在Java中,您可以使用AspectJ

Some frameworks such as BEA KODO (Implementation of the Java Data Objects specification) use bytecode manipulation to "enhance" Plain Old Java Objects and add the persistence logic, based on an XML description. 诸如BEA KODO(Java数据对象规范的实现)之类的一些框架使用字节码操作来“增强”普通旧Java对象并基于XML描述添加持久性逻辑。

Thus, database mapping information is then automatically generated on the bytecode. 因此,然后在字节码上自动生成数据库映射信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM