为什么在生成javadoc时会出现ClassCastException?

时间:2023-02-04 16:23:00

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

我正在使用ant生成javadocs,但一遍又一遍地得到这个异常 - 为什么?

I'm using JDK version 1.6.0_06.

我正在使用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)

4 个解决方案

#1


54  

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.

看起来这已被报告为Java错误。它似乎是由第三方库(如JUnit)中的注释使用,而不是在javadoc调用中包含带有该注释的jar。

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

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

#2


1  

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

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

Eg: sample.java File

例如:sample.java文件

@ChannelPipeline

Makes changes in

改变了

\\\\@ChannelPipeline

Try to run javadoc command once again. Now Class Cast Exception won't occur

尝试再次运行javadoc命令。现在不会发生类强制转换异常

#3


0  

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

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

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

[javadoc] java.lang.ClassCastException:com.sun.tools.javadoc.ClassDocImpl无法强制转换为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 ..

在我的java编译目标..

<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...

在我的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>

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

所以,我在 标签中添加了classpathref>

#4


0  

There is another way to get a ClassCastException (1.8.0_101 is failing currently):

还有另一种获取ClassCastException的方法(1.8.0_101当前失败):

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

java.lang.ClassCastException:com.sun.tools.javadoc.MethodDocImpl无法强制转换为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:

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

/**
 ** {@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.

该错误依赖于订单。如果javadoc首先处理TestClass1,则抛出ClassCastException。如果javadoc首先处理TestClass2,它将完成正常。该错误报告为JDK-8170444。

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

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

#1


54  

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.

看起来这已被报告为Java错误。它似乎是由第三方库(如JUnit)中的注释使用,而不是在javadoc调用中包含带有该注释的jar。

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

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

#2


1  

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

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

Eg: sample.java File

例如:sample.java文件

@ChannelPipeline

Makes changes in

改变了

\\\\@ChannelPipeline

Try to run javadoc command once again. Now Class Cast Exception won't occur

尝试再次运行javadoc命令。现在不会发生类强制转换异常

#3


0  

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

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

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

[javadoc] java.lang.ClassCastException:com.sun.tools.javadoc.ClassDocImpl无法强制转换为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 ..

在我的java编译目标..

<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...

在我的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>

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

所以,我在 标签中添加了classpathref>

#4


0  

There is another way to get a ClassCastException (1.8.0_101 is failing currently):

还有另一种获取ClassCastException的方法(1.8.0_101当前失败):

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

java.lang.ClassCastException:com.sun.tools.javadoc.MethodDocImpl无法强制转换为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:

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

/**
 ** {@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.

该错误依赖于订单。如果javadoc首先处理TestClass1,则抛出ClassCastException。如果javadoc首先处理TestClass2,它将完成正常。该错误报告为JDK-8170444。

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

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