简体   繁体   English

无法从Javadoc元素获取完全限定的类型名称

[英]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.

相关问题 JavaDoc提供完全限定的类名...如何缩写? - JavaDoc giving fully-qualified class name… how to abbreviate? Eclipse API:仅使用文件的名称字符串从IJavaProject获取IFile或标准名称 - Eclipse API: Get IFile or fully-qualified name from IJavaProject using only the file's name string 如何阻止Javadoc使用完全限定名称? - How to stop Javadoc from using fully qualified name? 无法使用完全限定名称从CMD启动Java文件 - Unable to launch java file from CMD using Fully Qualified Name 为什么在子类中没有完全限定名称的情况下可以访问静态方法? - Why can a static method be accessed without a fully-qualified name in a subclass? 如何从IResource对象获取完全限定的类名? - How to get fully qualified class name from IResource object? 如何获取java类的完全限定名称 - how get the fully qualified name of the java class 如何获取AST中节点的完全限定名称? - how to get the fully qualified name of node in an AST? 从完全限定名称获取类 - Getting the class from its fully qualified name 如何在Java中获取与CallableStatement.registerOutParameter一起使用的标准SQL类型名称 - how to get fully qualified SQL type name in java to be used with CallableStatement.registerOutParameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM