如何从 ConstraintDescriptor::getAnnotation 获取真正的注释而不是代理?(javax.validation Apache Bval) [英] How do I get the real annotation instead of a proxy from ConstraintDescriptor::getAnnotation? (javax.validation Apache Bval)

查看:55
本文介绍了如何从 ConstraintDescriptor::getAnnotation 获取真正的注释而不是代理?(javax.validation Apache Bval)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取注释的 Class 对象引用,该注释在 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).

在得到一些 ConstraintViolations 后,我将它们传递给以下函数:

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

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

这将返回一个类对象,其 getName()getCanonicalName()getTypeName() 都返回java.lang.反映.代理".

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

奇怪的是,Class 对象的 toString() 方法返回了类 com.sun.proxy.$Proxy10".

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

推荐答案

Java 注解由 Proxy 实现.Proxy 确实是真正的注释.您应该使用 Annotation.annotationType 而不是 Object.getClass 来获取真正的注释类.

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.

这篇关于如何从 ConstraintDescriptor::getAnnotation 获取真正的注释而不是代理?(javax.validation Apache Bval)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆