0

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

A Frayed Knot
  • 476
  • 5
  • 20

1 Answers1

2

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.

Dean Xu
  • 4,438
  • 1
  • 17
  • 44