2

I would like to display some text before service class run. This text could be HTML, generated from class' javadoc. Is it possible to access/generate it from class itself?

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

2 Answers2

1

Now with Java 8 you have a whole interface to play with comments and documentation. Check this package com.sun.source.doctree

enissay
  • 300
  • 3
  • 13
0

You need access to the source code files to be able to read comments. If you don't have access (which is most likely the case if you want to read the comments at runtime) you will have to use another solution, like providing the text as a string or resource file.

It's possible to get comments using the Java Compiler API. See: How to access comments from the java compiler tree api generated ast?. The problem here is you still need to know where the source code files are located on the system, if they are even there at all.

Another solution is to use an annotation processor. It has the advantage that it provides you with an abstract representation of the source code without the need to manually read files. You would need to identify the class you want to read the comments from and then use Element.getDocComment.

Community
  • 1
  • 1
kapex
  • 28,903
  • 6
  • 107
  • 121