URLClassLoader loads Annotation as com.sun.$Proxy$27

前端 未结 3 1994
小鲜肉
小鲜肉 2021-01-06 08:27

I\'m trying to dynamically load a java class. The basic idea is, that a jar contains modules which get loaded dynamically at runtime. This is how I do it (I know it\'s hacky

3条回答
  •  孤独总比滥情好
    2021-01-06 08:46

    I had the same problem when trying to create a ant task for code generation based on a declarative approach using annotations. I found that the documentation of the Proxy - Object states that instanceof should resolve it, but this didn't work fopr me neither. I finally got a around with

    Annotation[] annotations = classObj.getAnnotations();

        for(int i = 0;i < annotations.length;i++) {
            Class[] interfaces = annotations[i].getClass().getInterfaces();
    
            for(int i2 = 0; i2 < interfaces.length;i2++) {
                System.out.println("interface:" + interfaces[i2].getName());
            }
    

    giving my the name of the original annotation, so comparing this name to the annotations classname will give you the desired result.

提交回复
热议问题