sdaschner / jaxrs-analyzer

Creates REST documentation for JAX-RS projects
Apache License 2.0
319 stars 101 forks source link

Loading static values from system classloader #135

Open koebbingd opened 6 years ago

koebbingd commented 6 years ago

The backtrace states that an analyzed class can not be found. This error is generated by InstructionBuilder.getStaticValue:

   field = Class.forName(containingClass.replace('/', '.')).getDeclaredField(name);

When I replace this line of code with

   final String className = containingClass.replace('/', '.');
   final Class<?> clazz = ContextClassReader.getClassLoader().loadClass(className);
   field = clazz.getDeclaredField(name);

I get rid of the error, but I wonder if the intended functionality is still given.

sdaschner commented 6 years ago

Yes, the problem is that this can't be changed atm, since the Maven plugin has two different classloaders (with their set of class instances). So yes, it has quite a few side effects... This needs some further refactoring on the overall class loading first.

asafm commented 6 years ago

Hey @sdaschner - we met at JCrete. We bumped into your project and try using it to generate Swagger files out of existing JAX-RS Resource classes. We ran this issue here. I started looking at the source code to understand, couldn't figure out something and I will be glad for any pointers to save me time:

  1. You mentioned Maven plugin has different class-loaders. I thought that all the Maven plugin needs to provide are two strings: (1) project class paths (i.e. resource classes packages), (2) class paths (i.e. list of jars). Once you have that, you can easily instantiate a JaxRs Analyzer and run. If that is so, why the reliance on Maven class loaders?

  2. I saw in the code that ContextClassLoader is static. Any reason why static, and instantiate it when you create the Project Analyzer?