google reflections not resolving my type parameters on annotated methods

720 views Asked by At

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?

1

There are 1 answers

2
grinder22 On

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