问题
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