How do I get the real annotation instead of a proxy from ConstraintDescriptor::getAnnotation? (javax.validation Apache Bval)

二次信任 提交于 2021-02-11 12:04:28

问题


I'm trying to get a Class object reference for the annotation which caused a ConstraintViolation in the javax.validation package (Apache Bval implementation).

After getting some ConstraintViolations, I pass them into the following function:

private Class<?> getConstraintAnnotation(final ConstraintViolation<?> constraintViolation) {
    return constraintViolation
        .getConstraintDescriptor()
        .getAnnotation()
        .getClass();
  }

And this returns a class object whose getName(), getCanonicalName(), and getTypeName() all return "java.lang.reflect.Proxy".

Weirdly enough, the toString() method of the Class object returns back "class com.sun.proxy.$Proxy10".

Is there a way for me to get the real annotation classes and not these proxies? I would ideally like to map the built-in annotations to error codes (without having to overwrite the message every time I use it).


回答1:


Java annotation is implemented by Proxy. The Proxy do be the really annotation. You should use Annotation.annotationType rather than Object.getClass to get the real annotation class.



来源:https://stackoverflow.com/questions/51489664/how-do-i-get-the-real-annotation-instead-of-a-proxy-from-constraintdescriptorg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!