Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a Java .class file

I want to write a tool which generates some code using a compiled .class file as input. Specifically, I want to read from this class file:

  • Methods, with annotations
  • Method parameters, with annotations

The input class file will likely refer to several types that are not in the tool's classpath. This is ok, I don't need to do anything with them, just need to read fully qualified type names as strings. I do need to get some information from the annotations, but they will be in the tool's classpath.

Is there a library which I can use for this purpose? It would be nice if the API was a bit like the reflection API, but it doesn't have to be.

like image 789
Bart van Heukelom Avatar asked Jan 26 '11 13:01

Bart van Heukelom


1 Answers

ASM http://asm.ow2.org/ allows you to read a class from a file/input stream without class loading it. It allows you to see annotations which are not loaded at runtime. It can also be used to modify the class byte code/annotations/method etc.

like image 191
Peter Lawrey Avatar answered Oct 19 '22 23:10

Peter Lawrey