A Frayed Knot
A Frayed Knot

Reputation: 469

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

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).

Upvotes: 0

Views: 1242

Answers (1)

Dean Xu
Dean Xu

Reputation: 4691

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.

Upvotes: 2

Related Questions