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

ClassCastException when trying to use custom DocletEnvironment implementation

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      openjdk version "11.0.2" 2019-01-15
      OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
      OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      When trying to customize the JavaDoc generation process by wrapping the original DocletEnvironment implementation, the JavaDoc generation fails with a ClassCastException, because the JDK attempts to cast to the "exact" type of the DocletEnvironment instance.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Write a custom doclet that extends the StandardDoclet and uses a delegate for DocletEnvironment and then use the doclet during JavaDoc generation.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The doclet works and no classcastexception is raised.
      ACTUAL -
      The Workarounds class attempts to cast the class to the default DocEnvImpl type and fails with a ClassCastException.

      ---------- BEGIN SOURCE ----------
      package bug.demo;

      import java.util.Set;

      import javax.lang.model.SourceVersion;
      import javax.lang.model.element.Element;
      import javax.lang.model.element.TypeElement;
      import javax.lang.model.util.Elements;
      import javax.lang.model.util.Types;
      import javax.tools.JavaFileManager;
      import javax.tools.JavaFileObject.Kind;

      import com.sun.source.util.DocTrees;

      import jdk.javadoc.doclet.DocletEnvironment;
      import jdk.javadoc.doclet.StandardDoclet;

      public class NewDoclet extends StandardDoclet {

          @Override
          public boolean run(DocletEnvironment docEnv) {
              return super.run(wrap(docEnv));
          }

          public DocletEnvironment wrap(DocletEnvironment env) {
              return new DocletEnvironment() {
                  @Override
                  public Set<? extends Element> getSpecifiedElements() {
                      return env.getSpecifiedElements();
                  }

                  @Override
                  public Set<? extends Element> getIncludedElements() {
                      return env.getIncludedElements();
                  }

                  @Override
                  public DocTrees getDocTrees() {
                      return env.getDocTrees();
                  }

                  @Override
                  public Elements getElementUtils() {
                      return env.getElementUtils();
                  }

                  @Override
                  public Types getTypeUtils() {
                      return env.getTypeUtils();
                  }

                  @Override
                  public boolean isIncluded(Element element) {
                      return env.isIncluded(element);
                  }

                  @Override
                  public boolean isSelected(Element element) {
                      return env.isSelected(element);
                  }

                  @Override
                  public JavaFileManager getJavaFileManager() {
                      return env.getJavaFileManager();
                  }

                  @Override
                  public SourceVersion getSourceVersion() {
                      return env.getSourceVersion();
                  }

                  @Override
                  public ModuleMode getModuleMode() {
                      return env.getModuleMode();
                  }

                  @Override
                  public Kind getFileKind(TypeElement type) {
                      return env.getFileKind(type);
                  }
              };
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Extend the unsupported DocEnvImpl type.

      FREQUENCY : always


            hannesw Hannes Wallnoefer
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: