0

I have the following code that works nicely if the project is in my classpath locally and I do a getClass().getClassLoader(). But when I try to load dynamically from an external jar it fails.

It fails to load MyType, which is the type on the method parameter like this:

@MyAnnotation("FOO.BAR")
public Object echo(@Valid MyType param) throws Exception {
    return...;
}

This code will fail with "org.reflections.ReflectionsException: could not get type for name MyType":

myLoader = new URLClassLoader(new URL[]{new URL("file:///"+jarfile)}); 
myLoaders.Add(myLoader);
ConfigurationBuilder builder = new ConfigurationBuilder()
    .addClassLoaders(loaders)
    .setScanners(new MethodAnnotationsScanner())
    .setUrls(ClasspathHelper.forPackage(myPackage, myLoaders));
Reflections reflections = new Reflections(builder);
return  reflections.getMethodsAnnotatedWith(MyAnnotation.class);

I believe the problem lies in the class loader. Is there another way to load a jar such that it picks up all the types?

grinder22
  • 551
  • 4
  • 17

1 Answers1

0

I think I just solved it by using forClassLoader instead of forPackage.

grinder22
  • 551
  • 4
  • 17
  • I don't know if something changed or if I got the worst false positive ever, but it is not working now even with the forClassLoader. At this point I'm stumped and thinking this is a limitation of google's reflection library. – grinder22 Sep 11 '15 at 13:36
  • Scratch that. I am convinced they changed the 0.9.9-RC on maven central and broke me. I just updated to 0.9.10 and it is working again! – grinder22 Sep 11 '15 at 15:14