dnault / therapi-runtime-javadoc

Read Javadoc comments at run time.
Apache License 2.0
117 stars 19 forks source link

Can't get Javadoc for nested class in default package #35

Closed jhameier closed 4 years ago

jhameier commented 4 years ago

Inner classes in the default package as the JavaReader fails to the return a valid comment from the javadoc.

Attaching a stand alone example

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>JavadocRuntimeReader</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>8</source>
          <target>8</target>
          <annotationProcessorPaths>
            <annotationProcessorPath>
              <groupId>com.github.therapi</groupId>
              <artifactId>therapi-runtime-javadoc-scribe</artifactId>
              <version>0.9.0</version>
            </annotationProcessorPath>
          </annotationProcessorPaths>
        </configuration>

      </plugin>
    </plugins>
  </build>
  <dependencies>
    <!-- Annotation processor -->
<!--    <dependency>-->
<!--      <groupId>com.github.therapi</groupId>-->
<!--      <artifactId>therapi-runtime-javadoc-scribe</artifactId>-->
<!--      <version>0.9.0</version>-->
<!--      <scope>provided</scope>-->
<!--    </dependency>-->

    <!-- Runtime library -->
    <dependency>
      <groupId>com.github.therapi</groupId>
      <artifactId>therapi-runtime-javadoc</artifactId>
      <version>0.9.0</version>
    </dependency>
  </dependencies>
</project>

Example class reader

import com.github.therapi.runtimejavadoc.ClassJavadoc;
import com.github.therapi.runtimejavadoc.CommentFormatter;
import com.github.therapi.runtimejavadoc.MethodJavadoc;
import com.github.therapi.runtimejavadoc.RuntimeJavadoc;

public class JavadocReader {
  private static final CommentFormatter FORMATTER = new CommentFormatter();

  public static void main(String[] args) {
    printClassJavadocClass(TestClass.class);
  }

  private static void printClassJavadocClass(Class<?> clazz) {
    ClassJavadoc classDoc = RuntimeJavadoc.getJavadoc(clazz);
    if (classDoc.isEmpty()) {
      System.out.println("Failed to return javadoc");
      return;
    }
    System.out.println(FORMATTER.format(classDoc.getComment()));
    StringBuilder sb = new StringBuilder();
    for (MethodJavadoc methodDoc : classDoc.getMethods()) {
      sb.append(methodDoc.getName())
          .append(methodDoc.getParamTypes())
          .append(FORMATTER.format(methodDoc.getComment()))
          .append("  returns ")
          .append(FORMATTER.format(methodDoc.getReturns()))
          .append(System.lineSeparator());
    }
    System.out.println("\t" + sb.toString());
  }

  /**
   * This is a test class level javadoc.
   */
  static class TestClass {

    /**
     * This is a test method level javadoc.
     */
    public void testMethod() {}
  }
}

Originally posted by @jhameier in https://github.com/dnault/therapi-runtime-javadoc/issues/34#issuecomment-660267335

jhameier commented 4 years ago

So further testing shows that if the reader is in a package it does read the javadoc of the inner class

jhameier commented 4 years ago

As a side note, I also confirmed that using either the annotation processor paths tag or the dependency tag in the pom both work!

dnault commented 4 years ago

Thanks for reporting this. Fixed in 0.10.0, which is now up on Maven Central.

jhameier commented 4 years ago

Thanks. Tracking down another issue with JsonJavadocReader throwing AssertionError; as soon as I can determine what is actually causing the issue is I will open a new ticket.