简体   繁体   English

Java相当于Mono.Cecil或.NET的反射,可以访问MSIL代码

[英]Java equivalent of Mono.Cecil or .NET's reflection with access to MSIL code

I thought I'd automate error detection in future code, provided some of our code needs to explicitly release connection objects because of what we think is a bug in the Java HTTP API we use (not important which API it is). 我认为我将在未来的代码中自动执行错误检测,前提是我们的一些代码需要显式释放连接对象,因为我们认为这是我们使用的Java HTTP API中的一个错误(不重要的是哪个API)。

The idea was simple: For every method on a class A, if that method makes a call to a method with a return type assignable from Response , that method should also call close() on the return value. 这个想法很简单:对于类A上的每个方法,如果该方法调用具有可从Response分配的返回类型的方法,则该方法还应该对返回值调用close() All this without actually running any code in the class being checked. 所有这些都没有实际运行正在检查的类中的任何代码。

Forgive my rant, but just to say what I don't want: I found http://asm.ow2.org/ , which provided access to the bytecode I needed, however: The API seems tailored for bytecode manipulation (no pure introspection), and it's die-hard based on the visitor pattern in a way that makes it virtually impossible to discover its workings through my IDE's autocomplete, and none of the visit*() methods really nails bytecode semantics with Java objects, but strings mixing method signature and return type signature altogether. 原谅我的咆哮,但只是说我不想要的东西:我找到了http://asm.ow2.org/ ,它提供了我所需的字节码的访问权限,但是:API似乎是为字节码操作而定制的(没有纯粹的内省),它是基于访问者模式的顽固,这种方式几乎不可能通过我的IDE自动完成来发现它的工作原理,并且访问*()方法都没有真正用Java对象指定字节码语义,而是字符串混合方法签名和返回类型签名。 I'm not even sure if the bytecodes have any constant defined somewhere. 我甚至不确定字节码是否在某处定义了任何常量。

I was able to make use of it though, but it was really elegant. 我虽然能够使用它,但它非常优雅。 I wonder what other libraries for Java are out there with a philosophy more similar to Mono.Cecil and .NET's own System.Reflection.MethodBody object? 我想知道其他Java库是否与Mono.Cecil和.NET自己的System.Reflection.MethodBody对象更相似?

Unfortunately, ASM and BCEL are pretty much the gold standard bytecode manipulation libraries. 不幸的是,ASM和BCEL几乎是黄金标准字节码操作库。 If you don't like them, you'll probably have to modify them yourselves or take a different approach. 如果您不喜欢它们,您可能需要自己修改它们或采取不同的方法。

Edit: Soot is another major bytecode manipulation library. 编辑:Soot是另一个主要的字节码操作库。 You could give that a try. 你可以尝试一下。

I'm not too familiar with ASM and BCEL, but I've written my own tools for bytecode manipulation, available on Github. 我对ASM和BCEL不太熟悉,但是我已经编写了自己的字节码操作工具,可以在Github上找到。 Unfortunately, they're A) written in Python, not Java, and B) barely documented. 不幸的是,它们是A)用Python编写的,而不是Java,而B)几乎没有记录。

I think the best approach would be to modify the verifier to add some extra metadata to the type. 我认为最好的方法是修改验证程序,为类型添加一些额外的元数据。 The verifier already does something similar to make sure that objects are properly initialized, so it wouldn't be hard to modify it to ensure that objects are closed, as long the closed status can be expressed statically as part of the type system (ie they're stored only in local variables before closing and not mixed with already closed objects). 验证器已经做了类似的事情以确保对象被正确初始化,因此不难修改它以确保对象被关闭,只要关闭状态可以静态表示为类型系统的一部分(即它们'在关闭之前仅存储在局部变量中,而不与已关闭的对象混合)。

I have been working on a Java decompiler which uses a type/metadata system based on Mono.Cecil. 我一直在研究一个Java反编译器,它使用基于Mono.Cecil的类型/元数据系统。 It might be sufficient for your needs. 它可能足以满足您的需求。 You can find it here . 你可以在这里找到它。

There are a bunch of other .NET inspired APIs in there too, including a reflection API and an implementation of LINQ/DLR expression trees (w/ compiler). 其中还有许多其他.NET启发的API,包括反射API和LINQ / DLR表达式树(带编译器)的实现。

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

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