Reading Java.class file

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

  • Methods with annotations
  • Method Parameters with Annotations

The input class file most likely belongs to several types that are not related to the tool class path. This is normal, I don’t need to do anything with them, I just need to read fully qualified type names as strings. I need to get some information from annotations, but they will be in the class path of the tool.

Is there a library that I can use for this purpose? It would be nice if the API was a bit like the reflection API, but this is not necessary.

+4
source share
6 answers

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

+5
source

The javap tool included in the JDK is probably already doing what you need.

+5
source

You need to look at two things. Reflection API and Decompiler

+2
source

I think you could use spring class scan.

 ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); String basePackage = "org/springframework/samples/petclinic"; Set<BeanDefinition> components = provider.findCandidateComponents(basePackage); for (BeanDefinition component : components) { System.out.printf("Component: %s\n", component.getBeanClassName()); } 

for more information you can look at spring docs

+1
source

I use asm to define all methods.

+1
source

If you have access to create the .class file, then the Reflection API should help.

0
source

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


All Articles