[英]Unable to Get Fully-Qualified Type Name from Javadoc Element
Short and sweet: despite the fact that I am asking the Javadoc API to give me the fully-qualified name of an annotation, it is only returning the simple type name. 简短而有趣:尽管我要求Javadoc API为我提供注释的全限定名称,但它仅返回简单的类型名称。
I am writing a Javadoc doclet that relies heavily on the inspection of annotations. 我正在编写一个Javadoc doclet,它在很大程度上依赖于注释的检查。 As such, I have created a utility function that will take an array of
AnnotationDesc
objects and return a Map
that associates the fully-qualified name of the annotation to the AnnotationDesc
object that describes it. 这样,我创建了一个实用程序函数,该函数将使用
AnnotationDesc
对象的数组并返回一个Map
,该Map
将注释的标准名称与描述该注释的AnnotationDesc
对象相关联。 Here are the relevant functions: 以下是相关功能:
public static final Map<String, AnnotationDesc> getAnnotationMap(AnnotationDesc[] notes)
{
if (notes == null)
{
return Collections.emptyMap();
}
return Collections.unmodifiableMap(Arrays.stream(notes).collect(Collectors.toMap(AnnotationUtils::getNoteName, note -> note)));
}
private static String getNoteName(AnnotationDesc note) { return note.annotationType().qualifiedTypeName(); }
For what it's worth, I have also tried using qualifiedName
, which is another method exposed by the return value of the AnnotationDesc.annotationType
method. 对于它的价值,我还尝试使用
qualifiedName
,这是AnnotationDesc.annotationType
方法的返回值公开的另一种方法。
In my integration tests, this is all working great. 在我的集成测试中,这一切都很好。 However, when I push my doclet out to Artifactory, pull it down into another project and try to invoke it by way of a Gradle task, the keys in my map are the simple type names of the annotations.
但是,当我将doclet推送到Artifactory时,将其下拉到另一个项目中并尝试通过Gradle任务调用它,则地图中的键就是注释的简单类型名称。
Here is the definition of my Gradle task: 这是我的Gradle任务的定义:
task myDocletTask(type: Javadoc) {
source = sourceSets.main.allJava
destinationDir = reporting.file("my-doclet-dir")
options.docletpath = configurations.jaxDoclet.files.asType(List)
options.doclet = <redacted - fully qualified type name of custom doclet>
}
I have noticed that if a program element is annotated with a fully-qualified annotation, then the fully-qualified name is actually picked up by the Javadoc API. 我已经注意到,如果程序元素使用完全限定的注释进行注释,则Javadoc API实际上会使用完全限定的名称。 Eg:
@com.package.Annotation
results in the expected behavior, but @Annotation
does not. 如:
@com.package.Annotation
在预期的行为结果,但@Annotation
没有。
Can someone please help me to understand why this is happening and, more importantly, how I can achieve the expected/desired behavior? 有人可以帮我理解为什么会这样,更重要的是,我如何才能实现预期/期望的行为?
The problem was that the annotations were not on the classpath of the doclet at "documentation time". 问题在于注释在“文档编制时间”不在doclet的类路径上。 In order to fix this problem I augmented my Gradle Javadoc task with the following line:
options.classpath = sourceSets.main.runtimeClasspath.asType(List)
. 为了解决此问题,我在Gradle Javadoc任务中增加了以下内容:
options.classpath = sourceSets.main.runtimeClasspath.asType(List)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.