为什么我在生成 javadoc 时收到 ClassCastException?

发布于 2024-07-04 23:19:13 字数 5113 浏览 5 评论 0 原文

我正在使用 ant 生成 javadoc,但一遍又一遍地出现此异常 - 为什么?

我使用的是 JDK 版本 1.6.0_06

[javadoc] java.lang.ClassCastException: com.sun.tools.javadoc.ClassDocImpl cannot be cast to com.sun.javadoc.AnnotationTypeDoc
  [javadoc]     at com.sun.tools.javadoc.AnnotationDescImpl.annotationType(AnnotationDescImpl.java:46)
  [javadoc]     at com.sun.tools.doclets.formats.html.HtmlDocletWriter.getAnnotations(HtmlDocletWriter.java:1739)
  [javadoc]     at com.sun.tools.doclets.formats.html.HtmlDocletWriter.writeAnnotationInfo(HtmlDocletWriter.java:1713)
  [javadoc]     at com.sun.tools.doclets.formats.html.HtmlDocletWriter.writeAnnotationInfo(HtmlDocletWriter.java:1702)
  [javadoc]     at com.sun.tools.doclets.formats.html.HtmlDocletWriter.writeAnnotationInfo(HtmlDocletWriter.java:1681)
  [javadoc]     at com.sun.tools.doclets.formats.html.FieldWriterImpl.writeSignature(FieldWriterImpl.java:130)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.FieldBuilder.buildSignature(FieldBuilder.java:184)
  [javadoc]     at sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source)
  [javadoc]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  [javadoc]     at java.lang.reflect.Method.invoke(Method.java:597)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.FieldBuilder.invokeMethod(FieldBuilder.java:114)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.build(AbstractBuilder.java:90)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.AbstractMemberBuilder.build(AbstractMemberBuilder.java:56)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.FieldBuilder.buildFieldDoc(FieldBuilder.java:158)
  [javadoc]     at sun.reflect.GeneratedMethodAccessor51.invoke(Unknown Source)
  [javadoc]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  [javadoc]     at java.lang.reflect.Method.invoke(Method.java:597)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.FieldBuilder.invokeMethod(FieldBuilder.java:114)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.build(AbstractBuilder.java:90)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.AbstractMemberBuilder.build(AbstractMemberBuilder.java:56)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.buildFieldDetails(ClassBuilder.java:301)
  [javadoc]     at sun.reflect.GeneratedMethodAccessor43.invoke(Unknown Source)
  [javadoc]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  [javadoc]     at java.lang.reflect.Method.invoke(Method.java:597)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.invokeMethod(ClassBuilder.java:101)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.build(AbstractBuilder.java:90)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.buildClassDoc(ClassBuilder.java:124)
  [javadoc]     at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source)
  [javadoc]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  [javadoc]     at java.lang.reflect.Method.invoke(Method.java:597)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.invokeMethod(ClassBuilder.java:101)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.build(AbstractBuilder.java:90)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.build(ClassBuilder.java:108)
  [javadoc]     at com.sun.tools.doclets.formats.html.HtmlDoclet.generateClassFiles(HtmlDoclet.java:155)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.AbstractDoclet.generateClassFiles(AbstractDoclet.java:164)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.AbstractDoclet.startGeneration(AbstractDoclet.java:106)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.AbstractDoclet.start(AbstractDoclet.java:64)
  [javadoc]     at com.sun.tools.doclets.formats.html.HtmlDoclet.start(HtmlDoclet.java:42)
  [javadoc]     at com.sun.tools.doclets.standard.Standard.start(Standard.java:23)
  [javadoc]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  [javadoc]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  [javadoc]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  [javadoc]     at java.lang.reflect.Method.invoke(Method.java:597)
  [javadoc]     at com.sun.tools.javadoc.DocletInvoker.invoke(DocletInvoker.java:215)
  [javadoc]     at com.sun.tools.javadoc.DocletInvoker.start(DocletInvoker.java:91)
  [javadoc]     at com.sun.tools.javadoc.Start.parseAndExecute(Start.java:340)
  [javadoc]     at com.sun.tools.javadoc.Start.begin(Start.java:128)
  [javadoc]     at com.sun.tools.javadoc.Main.execute(Main.java:41)
  [javadoc]     at com.sun.tools.javadoc.Main.main(Main.java:31)

I'm using ant to generate javadocs, but get this exception over and over - why?

I'm using JDK version 1.6.0_06.

[javadoc] java.lang.ClassCastException: com.sun.tools.javadoc.ClassDocImpl cannot be cast to com.sun.javadoc.AnnotationTypeDoc
  [javadoc]     at com.sun.tools.javadoc.AnnotationDescImpl.annotationType(AnnotationDescImpl.java:46)
  [javadoc]     at com.sun.tools.doclets.formats.html.HtmlDocletWriter.getAnnotations(HtmlDocletWriter.java:1739)
  [javadoc]     at com.sun.tools.doclets.formats.html.HtmlDocletWriter.writeAnnotationInfo(HtmlDocletWriter.java:1713)
  [javadoc]     at com.sun.tools.doclets.formats.html.HtmlDocletWriter.writeAnnotationInfo(HtmlDocletWriter.java:1702)
  [javadoc]     at com.sun.tools.doclets.formats.html.HtmlDocletWriter.writeAnnotationInfo(HtmlDocletWriter.java:1681)
  [javadoc]     at com.sun.tools.doclets.formats.html.FieldWriterImpl.writeSignature(FieldWriterImpl.java:130)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.FieldBuilder.buildSignature(FieldBuilder.java:184)
  [javadoc]     at sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source)
  [javadoc]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  [javadoc]     at java.lang.reflect.Method.invoke(Method.java:597)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.FieldBuilder.invokeMethod(FieldBuilder.java:114)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.build(AbstractBuilder.java:90)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.AbstractMemberBuilder.build(AbstractMemberBuilder.java:56)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.FieldBuilder.buildFieldDoc(FieldBuilder.java:158)
  [javadoc]     at sun.reflect.GeneratedMethodAccessor51.invoke(Unknown Source)
  [javadoc]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  [javadoc]     at java.lang.reflect.Method.invoke(Method.java:597)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.FieldBuilder.invokeMethod(FieldBuilder.java:114)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.build(AbstractBuilder.java:90)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.AbstractMemberBuilder.build(AbstractMemberBuilder.java:56)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.buildFieldDetails(ClassBuilder.java:301)
  [javadoc]     at sun.reflect.GeneratedMethodAccessor43.invoke(Unknown Source)
  [javadoc]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  [javadoc]     at java.lang.reflect.Method.invoke(Method.java:597)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.invokeMethod(ClassBuilder.java:101)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.build(AbstractBuilder.java:90)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.buildClassDoc(ClassBuilder.java:124)
  [javadoc]     at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source)
  [javadoc]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  [javadoc]     at java.lang.reflect.Method.invoke(Method.java:597)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.invokeMethod(ClassBuilder.java:101)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.build(AbstractBuilder.java:90)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.build(ClassBuilder.java:108)
  [javadoc]     at com.sun.tools.doclets.formats.html.HtmlDoclet.generateClassFiles(HtmlDoclet.java:155)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.AbstractDoclet.generateClassFiles(AbstractDoclet.java:164)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.AbstractDoclet.startGeneration(AbstractDoclet.java:106)
  [javadoc]     at com.sun.tools.doclets.internal.toolkit.AbstractDoclet.start(AbstractDoclet.java:64)
  [javadoc]     at com.sun.tools.doclets.formats.html.HtmlDoclet.start(HtmlDoclet.java:42)
  [javadoc]     at com.sun.tools.doclets.standard.Standard.start(Standard.java:23)
  [javadoc]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  [javadoc]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  [javadoc]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  [javadoc]     at java.lang.reflect.Method.invoke(Method.java:597)
  [javadoc]     at com.sun.tools.javadoc.DocletInvoker.invoke(DocletInvoker.java:215)
  [javadoc]     at com.sun.tools.javadoc.DocletInvoker.start(DocletInvoker.java:91)
  [javadoc]     at com.sun.tools.javadoc.Start.parseAndExecute(Start.java:340)
  [javadoc]     at com.sun.tools.javadoc.Start.begin(Start.java:128)
  [javadoc]     at com.sun.tools.javadoc.Main.execute(Main.java:41)
  [javadoc]     at com.sun.tools.javadoc.Main.main(Main.java:31)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与他有关 2024-07-11 23:19:13

看起来这已被报告为 Java bug。 它似乎是由于使用来自第 3 方库(如 JUnit)的注释并且在 javadoc 调用中不包含带有该注释的 jar 引起的。

如果是这种情况,只需使用 javadoc 上的 -classpath 选项并包含额外的 jar 文件。

It looks like this has been reported as a Java bug. It appears to be caused by using annotations from a 3rd party library (like JUnit) and not including the jar with that annotation in the javadoc invocation.

If that is the case, just use the -classpath option on javadoc and include the extra jar files.

顾北清歌寒 2024-07-11 23:19:13

在 Java 版本 5 到 8 中,还有另一种方法可以获取 ClassCastException

java.lang.ClassCastException:com.sun.tools.javadoc.MethodDocImpl 无法转换为 com.sun。 tools.javadoc.AnnotationTypeElementDocImpl

当 javadoc 在处理代码中第一次使用的相同注释之前遇到对 javadoc 文本中注释方法的引用时,就会发生这种情况。 采取这两类:

/**
 ** {@link javax.annotation.Generated#value()}
 */
public class TestClass1 {}


@Generated("sometext")
public class TestClass2 {}

错误是依赖于顺序的。 如果 javadoc 首先处理 TestClass1,则将抛出 ClassCastException。 如果 javadoc 首先处理 TestClass2,它将正常完成。 该错误被报告为 JDK-8170444,并已解决为“无法修复”。 Java 9 中不再存在该错误。

作为解决方法,不要链接到文档文本中的注释方法。

There is another way to get a ClassCastException in versions of Java from 5 through 8:

java.lang.ClassCastException: com.sun.tools.javadoc.MethodDocImpl cannot be cast to com.sun.tools.javadoc.AnnotationTypeElementDocImpl

It will happen when javadoc encounters a reference to a annotation method in javadoc text before processing the same annotation for the first time used in code. Take these two classes:

/**
 ** {@link javax.annotation.Generated#value()}
 */
public class TestClass1 {}


@Generated("sometext")
public class TestClass2 {}

The bug is order dependent. If javadoc processes TestClass1 first, the ClassCastException will be thrown. If javadoc processes TestClass2 first, it will complete fine. The bug is reported as JDK-8170444, and was resolved as "Won't Fix". The bug is no longer present in Java 9.

As a workaround, don't link to annotation methods in your documentation text.

沉鱼一梦 2024-07-11 23:19:13

我对这个问题有一些想法,但这并不是确切的解决方案。

如果您在注释之前给出单个注释行 // 并尝试再次运行 javadoc。 这个问题将解决

例如:sample.java文件

@ChannelPipeline

中进行更改

//@ChannelPipeline

在尝试再次运行javadoc命令 。 现在不会发生 ClassCastException 了。

I have some idea regarding this problem but this not exact solution to get.

If you give single comment line // before annotation and try to run the javadoc once again. This problem will solve

Eg: sample.java file

@ChannelPipeline

Makes changes in

//@ChannelPipeline

Try to run javadoc command once again. Now ClassCastException won't occur.

梦旅人picnic 2024-07-11 23:19:13

我也遇到这个问题了。 我可以正确编译,没有任何错误或警告。 但是当我生成 javadoc 时,出现以下错误。

[javadoc] java.lang.ClassCastException:com.sun.tools.javadoc.ClassDocImpl 无法转换为 com.sun.javadoc.AnnotationTypeDoc

这是我的类路径加载第三方库...

<path id="build.classpath">
<fileset dir=".">
    <include name="libs/*.jar" />
</fileset>

在我的 java 编译目标..

<target name="compile" depends="clean, makedir">
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath">
  <compilerarg value="-Xlint:unchecked"/>
</javac>

在我的 javadoc 目标...

<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}" 
    failonerror="no"
    author="true"
    version="true"
    windowtitle="${Name} API"
    doctitle="${Name}"
    bottom="Copyright © 2014 ColayHIlls.com . All Rights Reserved.">
   <fileset dir="${src.dir}">
            <include name="main/java/com/colayhills/jpcenter/business/service/**" />
   </fileset>
</javadoc>
<echo message="java docs has been generated!"/>
</target>

因此,我向 标记添加了 classpathref="build.classpath" 选项。 现在对我来说很好。

I got this problem too. I can compile properly without any errors or warnings. But when I generating javadoc , I got below error.

[javadoc] java.lang.ClassCastException: com.sun.tools.javadoc.ClassDocImpl cannot be cast to com.sun.javadoc.AnnotationTypeDoc

Here is my classpath loading for my third-party-libs ...

<path id="build.classpath">
<fileset dir=".">
    <include name="libs/*.jar" />
</fileset>

At my java compile target ..

<target name="compile" depends="clean, makedir">
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath">
  <compilerarg value="-Xlint:unchecked"/>
</javac>

And at my javadoc target...

<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}" 
    failonerror="no"
    author="true"
    version="true"
    windowtitle="${Name} API"
    doctitle="${Name}"
    bottom="Copyright © 2014 ColayHIlls.com . All Rights Reserved.">
   <fileset dir="${src.dir}">
            <include name="main/java/com/colayhills/jpcenter/business/service/**" />
   </fileset>
</javadoc>
<echo message="java docs has been generated!"/>
</target>

So , I added classpathref="build.classpath" option to <javadoc tag. Now It is fine for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文