0

What is the best way to find all classes with a specific annotation in a JavaEE deployment? I work with a WildFly ApplicationServer and have created my own annotation. At the start of my deployment I would like to find all classes with my annotation and cache them for later access. What do I have to do to protect resources?

Net_Hans
  • 115
  • 12

1 Answers1

0

First of all Wildfly has modular class loading structure. In Wildfly doc:

Since JBoss AS 7, Class loading is considerably different to previous versions of JBoss AS. Class loading is based on the JBoss Modules project. Instead of the more familiar hierarchical class loading environment, WildFly's class loading is based on modules that have to define explicit dependencies on other modules. Deployments in WildFly are also modules, and do not have access to classes that are defined in jars in the application server unless an explicit dependency on those classes is defined.

As described above, if you have more .ear or .war package, they cannot see classes within each other unless explicit dependency defined. Each module can see its own class therefore each module should load and cache its own classes. You can not load all classes with a single class and single method.

With the following code you can see all loaded classes in the module.

Field f = ClassLoader.class.getDeclaredField("classes");
f.setAccessible(true);

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Vector<Class> classes =  (Vector<Class>) f.get(classLoader);
Turac
  • 667
  • 1
  • 4
  • 19
  • Thank you for your answer. does the Wildfly not provide mechanisms such as filter or provider, which gives me back a list with the elements I am looking for? how do the modules like jax-rs do with their annotations? – Net_Hans Aug 10 '19 at 16:07
  • Good point. That's what I thought. I searched in google and Wildfly documentation but I didn't find anything related this topic. – Turac Aug 10 '19 at 16:44
  • Possible duplicate of https://stackoverflow.com/questions/12021543/java-loading-annotated-classes – TacheDeChoco Aug 17 '19 at 14:47