dnault / therapi-runtime-javadoc

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

Each time i try to get java doc , It returns Empty java doc, I am using this Code #34

Closed chikochavda closed 4 years ago

chikochavda commented 4 years ago

`package com.ps.apps.utility.docgenerator;

import java.io.IOException; import java.util.Collections; import java.util.List; import java.util.Map;

import com.github.therapi.runtimejavadoc.ClassJavadoc; import com.github.therapi.runtimejavadoc.CommentFormatter; import com.github.therapi.runtimejavadoc.MethodJavadoc; import com.github.therapi.runtimejavadoc.OtherJavadoc; import com.github.therapi.runtimejavadoc.RuntimeJavadoc; import com.github.therapi.runtimejavadoc.SeeAlsoJavadoc; import com.github.therapi.runtimejavadoc.ThrowsJavadoc; import com.github.therapi.runtimejavadoc.*; import com.github.therapi.runtimejavadoc.ParamJavadoc;

public class DocExtractorUtil { // formatters are reusable and thread-safe private static final CommentFormatter formatter = new CommentFormatter();

public static DocumentationHolder getDocumentation(String fullyQualifiedClassName) throws IOException {
    DocumentationHolder documentationHolder = new DocumentationHolder();
    ClassJavadoc classDoc = RuntimeJavadoc.getJavadoc(fullyQualifiedClassName);
    documentationHolder.setClassName(classDoc.getName());
    documentationHolder.setClassDocumenation(format(classDoc.getComment()));

    List<String> classTags = Collections.emptyList();
    // @see tags
    for (SeeAlsoJavadoc see : classDoc.getSeeAlso()) {
        classTags.add("See also: " + see.getLink());
    }

    documentationHolder.setTags(classTags);

    Map<String, String> classMiscellaneousJavadoc = Collections.emptyMap();

    // miscellaneous and custom javadoc tags (@author, etc.)
    for (OtherJavadoc other : classDoc.getOther()) {
        classMiscellaneousJavadoc.put(other.getName() , format(other.getComment()));
    }
    documentationHolder.setMiscellaneousJavadoc(classMiscellaneousJavadoc);

    List<MethodDocumentations> methods = Collections.emptyList();   

    for (MethodJavadoc methodDoc : classDoc.getMethods()) {
        MethodDocumentations methodDocumentations = new MethodDocumentations();
        methodDocumentations.setName(methodDoc.getName());
        methodDocumentations.setParamTypes(methodDoc.getParamTypes());
        methodDocumentations.setComment(format(methodDoc.getComment()));
        methodDocumentations.setReturnType(format(methodDoc.getReturns()));
        List<String> tags = Collections.emptyList();
        for (SeeAlsoJavadoc see : methodDoc.getSeeAlso()) {
            tags.add("  See also: " + see.getLink());
        }            
        methodDocumentations.setTags(tags);

        Map<String, String> miscellaneousJavadoc = Collections.emptyMap();
        for (OtherJavadoc other : methodDoc.getOther()) {
            miscellaneousJavadoc.put(other.getName(), format(other.getComment()));
        }
        methodDocumentations.setMiscellaneousJavadoc(miscellaneousJavadoc);

        Map<String, String> paramJavaDoc = Collections.emptyMap();
        for (ParamJavadoc paramDoc : methodDoc.getParams()) {
            paramJavaDoc.put(paramDoc.getName(),  format(paramDoc.getComment()));
        }           
        methodDocumentations.setParams(paramJavaDoc);

        Map<String, String> throwsDocMap = Collections.emptyMap();

        for (ThrowsJavadoc throwsDoc : methodDoc.getThrows()) {
            paramJavaDoc.put(throwsDoc.getName(),  format(throwsDoc.getComment()));
        }
        methodDocumentations.setThrowsDoc(throwsDocMap);

        methods.add(methodDocumentations);
    }
    documentationHolder.setMethods(methods);
    return documentationHolder;
}

private static String format(Comment c) {
    return formatter.format(c);
}

}`

I debugged the internal code and seems like i am missing "__Javadoc.json" file which supposed to be genrated on it's own or do i have to perform any specific task ? I am using maven. I do understand that 'therapi-runtime-javadoc-scribe' is used for baking docs but it's not working?

I want to know, If i should configure something in pom.xml or do i need to use any annotation on class that need bake _Javadoc.json file

dnault commented 4 years ago

It sounds like the therapi-runtime-javadoc-scribe annotation processor is not executing.

If this error is occurring in an IDE, you might need to configure your project to enable annotation processing.

If the error occurs when you project is built by Maven, you might have to include the annotation processor in a different way. Instead of declaring therapi-runtime-javadoc-scribe as a provided dependency, you can declare it as an annotationProcessorPath in the configuration of the Maven compile plugin.

Here's a nice answer from StackOverflow: https://stackoverflow.com/questions/14322904/maven-3-how-to-add-annotation-processor-dependency

And here's what that plugin definition might look like for the therapi-runtime-javadoc-scribe processor:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
        <annotationProcessorPaths>
            <annotationProcessorPath>
              <groupId>com.github.therapi</groupId>
              <artifactId>therapi-runtime-javadoc-scribe</artifactId>
              <version>0.9.0</version>
            </annotationProcessorPath>
        </annotationProcessorPaths>
    </configuration>
</plugin>
chikochavda commented 4 years ago

Hi thanks for the help,

I checked out link you provided and tried to configure it but it didn't work, I was thinking may be i can strip down code to generate json file

Here is my code for for pom.xml Code was not formatted when direcctly posted here, so i used txt file. I think my issue is with annotation process class which used in plugin, May be i am wrong.

In alternate solution i have created custom annotation to hold metadata but i want to use this solution since this will reduce my effort in writing documentation in my custom annotation. Looking forward to your reply. pom.txt

dnault commented 4 years ago
  1. Are you compiling your project with GraalVM?

  2. Can you provide an Minimal Reproducible Example I could use to diagnose the problem? (Like, a self-contained project that I could compile and run).

chikochavda commented 4 years ago

I am using GraalVM in my project for templating programmable js to java conversion, I will need some time to product Reproducible example for my project, I will get right on it to and provide you github link. Thanks

Update :

I have added my code here

https://github.com/chikochavda/therapi-runtime-javadoc-scribe-base-example-demo.git

jhameier commented 4 years ago

I'm having a similar issue. Tried both as a standard maven dependency and in the compiler-plugin as well.

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() {}
  }
}
dnault commented 4 years ago

Hi @jhameier, thanks for the example code! It looks like the annotation processor does the wrong thing for a nested class whose enclosing class lives in the default package.

Can you confirm it works if you either move JavadocReader into a package, or make TestClass a top-level class?

dnault commented 4 years ago

Hi @chikochavda, sorry for the delayed response; I didn't notice you updated your comment with a link to a GitHub repo. Thanks for that link! It looks like spring-boot-maven-plugin doesn't support configuring annotation processors? At least I wasn't able to figure it out this evening. Instead you can add the annotation processor as a "provided" dependency, like:

<dependency>
    <groupId>com.github.therapi</groupId>
    <artifactId>therapi-runtime-javadoc-scribe</artifactId>
    <version>0.9.0</version>
    <scope>provided</scope>
</dependency>

I realize this isn't ideal, but hopefully this will get you going until we figure out how to get the Spring Boot plugin to work with custom annotation processors.

jhameier commented 4 years ago

Okay so that does work. Would you like me to move my question and open a new ticket for inner classes?

dnault commented 4 years ago

@jhameier Yes, please. That would be helpful.

chikochavda commented 4 years ago

Hi Dnault, I seems like i had to run package command before i get my application up, it seemed to work, I was able to make it work, Finally it's integrated in my app. Thanks for your help :)