How can I get Java comments by code?

I have to print in the standard output a comment written over a Java class. How can i do this?

eg.

/** * Comment to write * @version 1.1 */ public class WriteMyComment { //something... } 
+4
source share
3 answers

I would rather use annotations for what you need. I'm not sure it is possible to have access to the original comments at runtime.

MyAnnotation.java

 @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) public @interface MyAnnotation { String value(); } 

Test.java

 @MyAnnotation("Some notes here") public class Test { public static void main(String[] args) { System.out.println(Test.class.getAnnotation(MyAnnotation.class).value()); } } 
+3
source

I used QDox for a similar task, and as far as I remember, I could also process comments.

+1
source

Comments are not available in java class files, so you cannot use them if you do not have source files. If comments mean anything to your application, use annotations; they are intended to be used by applications.

Otherwise, you will have to live in tools that scan source code for comments, perhaps like javadoc or tools mentioned in other answers.

0
source

Source: https://habr.com/ru/post/1396381/


All Articles