Java binary (.class) file format parser?

只愿长相守 提交于 2019-12-12 11:23:01

问题


I am looking for a tool (or chain of tools) that can parse a .class files to a Java object. Something like :

JavaClass parsed = myTool.parse("/some_folder/SomeClassFile.class");

The parsed object would have methods like :

List<JavaMethod> methods = parsed.getMethods();
List<JavaInterface> interfaces = parsed.getImplementedInterfaces();
List<JavaMethod> calls = someMethod.getCalls();

My constraints are :

  1. The aim is to parse all classes of a project, including jars, so performance does matter.
  2. The parser has to parse Java 8 .class files
  3. Java is my main language so it's better for me if it's in Java.

Of course I can do some coding, so for example if the output is xml it's ok.

So here are the options I have found so far - none being satisfying as it is :

  1. BCEL or ASM look like a weapon of choice, but they also look like they're not maintained anymore. Besides, they're a little overkill for my purpose.
  2. Elipse AST would work, but from what I saw it's only for source files, and I need to parse binary files (jars...)
  3. A grammar for a parsing engine like antlr, yacc, bison... would work, but I have yet to find a reference grammar for .class files ! Besides, antlr is for text files, and I don't know of another reference Java parser engine (as I said, I would like a Java tool better).
  4. I first thought a class loader would do the job, and that I could just load the class and then use reflection. But actually I realized I need stuff that the Java reflection api doesn't provide, like getting calls from a particular method.

So here is where I am now ! Thanks in advance for your help !


回答1:


I'd recommend ASM. From what I've seen, it's by far the most popular Java bytecode library, and yes, it is still maintained. At time of writing, it looks like the most recent change was 41 days ago. So it's not constantly churning but it's not like it's abandoned either. And with such a commonly used library, support should be easy to find.




回答2:


I made some test for JBBP to parse java class, take a look at it, may be it will be useful for you https://github.com/raydac/java-binary-block-parser/blob/master/src/test/java/com/igormaznitsa/jbbp/it/ClassParsingTest.java



来源:https://stackoverflow.com/questions/28776205/java-binary-class-file-format-parser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!