首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用类的注释构建TypeDescriptor

使用类的注释构建TypeDescriptor
EN

Stack Overflow用户
提问于 2017-01-31 01:07:08
回答 1查看 260关注 0票数 0

我使用的ConditionalConverter应该与

代码语言:javascript
复制
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
  return sourceType.hasAnnotation(Document.class);
}

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

代码语言:javascript
复制
@Document
class Person {}

如果我看看背后是什么,从类中创建的TypeDescriptor没有注意到注释:

代码语言:javascript
复制
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的targetType类调用转换器。)

Spring中的代码来自4.2.8.RELEASE

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-31 18:13:42

考虑到这个问题,我最终使用底层的类来测试注解的存在。

代码语言:javascript
复制
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
  return sourceType.getObjectType().isAnnotationPresent(Document.class);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41941518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档