【问题标题】:Build TypeDescriptor with annotations for Class使用 Class 的注释构建 TypeDescriptor
【发布时间】:2017-06-15 22:35:50
【问题描述】:

我正在使用一个应该与

匹配的 ConditionalConverter
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
  return sourceType.hasAnnotation(Document.class);
}

但在这里,sourceType.hasAnnotation(Document.class) 会返回 false,即使提供的类是例如:

@Document
class Person {}

如果我看看后面是什么,从 Class 创建 TypeDescriptor 没有注意注释:

public static TypeDescriptor valueOf(Class<?> type) {
    if (type == null) {
        type = Object.class;
    }
    TypeDescriptor desc = commonTypesCache.get(type);
    return (desc != null ? desc : new TypeDescriptor(ResolvableType.forClass(type), null, null));
}

有关更多信息,对转换器的调用是通过 org.springframework.core.convert.ConversionService.canConvert(Class sourceType, Class targetType)

来自 Spring 的代码来自 4.2.8.RELEASE

不知道如何让我的转换器工作。

【问题讨论】:

    标签: java spring


    【解决方案1】:

    在这个问题上睡着了,我终于用底层的Class来测试注解的存在了。

    @Override
    public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
      return sourceType.getObjectType().isAnnotationPresent(Document.class);
    }
    

    【讨论】:

    • 睡在问题上总是积极的;)
    猜你喜欢
    • 1970-01-01
    • 2021-11-11
    • 2012-10-20
    • 1970-01-01
    • 2010-11-10
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多