gpt4 book ai didi

java - 使用类的注释构建 TypeDescriptor

转载 作者:行者123 更新时间:2023-12-01 08:57:36 26 4
gpt4 key购买 nike

我使用的 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(类源类型,类目标类型)

Spring 的代码来自 4.2.8.RELEASE

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

最佳答案

在这个问题上 sleep ,我最终最终使用底层类来测试注释的存在。

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

关于java - 使用类的注释构建 TypeDescriptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41941518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com