4

I must print on a standard output the comment write over a Java class. How can I do so?

eg.

/**
 * Comment to write
 * @version 1.1
 */
public class WriteMyComment {
    //something...
}
3
  • 6
    What is your input? If you're reading a compiled class file, then you cannot do it, because comments are not included into the compiled class. If you're reading a source .java file, then just parse the text and get your comments. Commented Feb 14, 2012 at 11:02
  • I have a production server that load some library with the same name. I want to know the version of a class that it is stored on its header comment. Commented Feb 14, 2012 at 13:19
  • If you don't have source files and you didn't write the library, then you're pretty much out of luck. You can try retrieving the annotations, as indicated by answers below, but if the developer of the library didn't use them, then you don't have that info either. Commented Feb 14, 2012 at 13:24

3 Answers 3

3

I'd rather use annotations for whatever you need to do. I'm not sure is it possible to has access to source comments on 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());
        }

    }
Sign up to request clarification or add additional context in comments.

Comments

1

I used QDox for a similiar task and as far as I remember, I was able to handle the comments too.

Comments

0

Comments are not available in the java class files, so you can't use them if you don't have the source files. If the comments mean anything for you application, use annotations, they are meant to be used by applications.

Otherwise you'll have to reside to tools that scan the source code for comments, perhaps like javadoc or tools mentioned in other answers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.