Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8170447

Javadoc crashes with ClassCastException when it finds an @link to an annotation method in documentaion text

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.8.0_101"
      Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.10586]

      A DESCRIPTION OF THE PROBLEM :
      When a class references a annotation method in documentation, like this:

      /**
       * {@link Generated#value()}
       */

      and another class uses this annotation, and if the documentation text is processed first by javadoc, then the usage of the annotation will throw a ClassCastException. This is the result of processing the documentation text links. The code in com.sun.tools.javadoc.ClassDocImpl.searchMethod() will only expect method references and will always call env.getMethodDoc() and never env.getAnnotationTypeElementDoc(). After visiting the documentation text and the link, env.methodMap will contain a MethodDocImpl for a annotation method. The next time javadoc encounters the annotation itself in code, it will call env.getAnnotationTypeElementDoc() which will try to cast the MethodDocImpl to AnnotationTypeElementDocImpl.

      The order of appearance is important to trigger the bug. If javadoc encounters the annotation usage in code first, the annotation method will be stored correctly as AnnotationTypeElementDocImpl in methodMap.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      create a class org.somepackage.TestClass:

      package org.somepackage;
      import javax.annotation.Generated;
      /**
       * {@link Generated#value()}
       */
      public class TestClass {
      }


      Create a class org.somepackage2.TestClass2:

      package org.somepackage2;
      import javax.annotation.Generated;
      public class TestClass2 {
      @Generated("sometext")
      public void someMethod() {
      }
      }

      Run javadoc like this:

      javadoc -sourcepath . org.somepackage org.somepackage2

      and you will see the exception. Run it like this:

      javadoc -sourcepath . org.somepackage2 org.somepackage

      and it will complete fine.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      javadoc should complete without errors in both cases
      ACTUAL -
      ClassCastException in one of both cases

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.ClassCastException: com.sun.tools.javadoc.MethodDocImpl cannot be cast to com.sun.tools.javadoc.AnnotationTypeElementDocImpl
              at com.sun.tools.javadoc.DocEnv.getAnnotationTypeElementDoc(DocEnv.java:721)
              at com.sun.tools.javadoc.AnnotationDescImpl$ElementValuePairImpl.element(AnnotationDescImpl.java:156)
              at com.sun.tools.doclets.formats.html.HtmlDocletWriter.addAnnotations(HtmlDocletWriter.java:2050)
              at com.sun.tools.doclets.formats.html.HtmlDocletWriter.getAnnotations(HtmlDocletWriter.java:2012)
              at com.sun.tools.doclets.formats.html.HtmlDocletWriter.getAnnotations(HtmlDocletWriter.java:1925)
              at com.sun.tools.doclets.formats.html.HtmlDocletWriter.addAnnotationInfo(HtmlDocletWriter.java:1901)
              at com.sun.tools.doclets.formats.html.HtmlDocletWriter.addAnnotationInfo(HtmlDocletWriter.java:1874)
              at com.sun.tools.doclets.formats.html.AbstractExecutableMemberWriter.addParameters(AbstractExecutableMemberWriter.java:207)
              at com.sun.tools.doclets.formats.html.AbstractExecutableMemberWriter.addParameters(AbstractExecutableMemberWriter.java:176)
              at com.sun.tools.doclets.formats.html.MethodWriterImpl.getSignature(MethodWriterImpl.java:134)
              at com.sun.tools.doclets.internal.toolkit.builders.MethodBuilder.buildSignature(MethodBuilder.java:185)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.invokeMethod(AbstractBuilder.java:180)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.build(AbstractBuilder.java:135)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractMemberBuilder.build(AbstractMemberBuilder.java:78)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.buildChildren(AbstractBuilder.java:161)
              at com.sun.tools.doclets.internal.toolkit.builders.MethodBuilder.buildMethodDoc(MethodBuilder.java:168)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.invokeMethod(AbstractBuilder.java:180)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.build(AbstractBuilder.java:135)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractMemberBuilder.build(AbstractMemberBuilder.java:78)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.buildChildren(AbstractBuilder.java:161)
              at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.buildMethodDetails(ClassBuilder.java:416)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.invokeMethod(AbstractBuilder.java:180)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.build(AbstractBuilder.java:135)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.buildChildren(AbstractBuilder.java:161)
              at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.buildMemberDetails(ClassBuilder.java:356)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.invokeMethod(AbstractBuilder.java:180)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.build(AbstractBuilder.java:135)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.buildChildren(AbstractBuilder.java:161)
              at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.buildClassDoc(ClassBuilder.java:148)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.invokeMethod(AbstractBuilder.java:180)
              at com.sun.tools.doclets.internal.toolkit.builders.AbstractBuilder.build(AbstractBuilder.java:135)
              at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.build(ClassBuilder.java:120)
              at com.sun.tools.doclets.formats.html.HtmlDoclet.generateClassFiles(HtmlDoclet.java:189)
              at com.sun.tools.doclets.internal.toolkit.AbstractDoclet.generateClassFiles(AbstractDoclet.java:192)
              at com.sun.tools.doclets.internal.toolkit.AbstractDoclet.startGeneration(AbstractDoclet.java:137)
              at com.sun.tools.doclets.internal.toolkit.AbstractDoclet.start(AbstractDoclet.java:82)
              at com.sun.tools.doclets.formats.html.HtmlDoclet.start(HtmlDoclet.java:80)
              at com.sun.tools.doclets.standard.Standard.start(Standard.java:39)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
              at com.sun.tools.javadoc.DocletInvoker.invoke(DocletInvoker.java:310)
              at com.sun.tools.javadoc.DocletInvoker.start(DocletInvoker.java:189)
              at com.sun.tools.javadoc.Start.parseAndExecute(Start.java:366)
              at com.sun.tools.javadoc.Start.begin(Start.java:219)
              at com.sun.tools.javadoc.Start.begin(Start.java:205)
              at com.sun.tools.javadoc.Main.execute(Main.java:64)
              at com.sun.tools.javadoc.Main.main(Main.java:54)

      REPRODUCIBILITY :
      This bug can be reproduced rarely.

      ---------- BEGIN SOURCE ----------
      Provided in "Steps to Reproduce"
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use actually the annotation in the class in which the @link appears or remove the @link from the documentation.


            ksrini Kumar Srinivasan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: