Java binary (.class) file parser?

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

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

The analyzed object would have such methods as:

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

My limitations:

  • The goal is to analyze all classes of the project, including banks, so it is important to complete .
  • The parser should parse Java 8..class files
  • Java is my main language, so I feel better if it is in Java.

Of course, I can do some encoding, for example, if the xml output is normal.

So, here are the options that I have found so far - none of them satisfy:

  • BCEL or ASM look like a weapon of choice, but they also look like they are no longer supported. They are also a bit overloaded for my purpose.
  • Elipse AST will work, but from the fact that I saw it only for the source files, and I need to parse the binaries (jars ...)
  • Grammar for a parser like antlr, yacc, bison ... will work, but I have yet to find reference grammar for .class files! Also, antlr for text files, and I don't know of any other Java parser referencing mechanism (as I said, I would like a better Java tool).
  • At first I thought that the class loader would complete the task and that I could just load the class and then use reflection. But in fact, I realized that I need things that Java api reflection does not provide, for example, receiving calls from a specific method.

So that's where I am now! Thanks in advance for your help!

+3
source share
2 answers

I would recommend ASM. From what I saw, this is by far the most popular Java bytecode library, and yes, it is still supported. At the time of this writing, it looked like the last change was 41 days ago. Thus, he does not constantly shake, but does not like that he also refused. And with such a widely used library, support should be easy to find.

+2
source

I did some tests for JBBP to parse a java class, take a look at it, maybe 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

0
source

Source: https://habr.com/ru/post/1234544/


All Articles