1

I am trying to use the getDocComment method to grab some javadoc from a variable declaration, however when there is an annotation between the comment and element then getDocComment returns null

For example:

/**
 * A description of varA
 */
private SomeObject varA;

Then I get my output correctly as: * `varA` (`SomeObject`) - A description of varA

However, if I have an annotation on the variable it does not get the description (debugger shows null).

/**
 * A description of varA
 */
@NotNull
private SomeObject varA;

Then I get my output incorrectly as: * `varA` (`SomeObject`) -

Just want to add some additional context as to why I suspect it is the annotation that is breaking this. The documentation for getDocComment() is as follows:

Returns the text of the documentation ("Javadoc") comment of an element.
A documentation comment of an element is a comment that begins with "/**", ends with a separate "*/", and immediately precedes the element, ignoring white space. Therefore, a documentation comment contains at least three "*" characters. The text returned for the documentation comment is a processed form of the comment as it appears in source code. The leading "/**" and trailing "*/" are removed. For lines of the comment starting after the initial "/**", leading white space characters are discarded as are any consecutive "*" characters appearing after the white space or starting the line. The processed lines are then concatenated together (including line terminators) and returned.

Is there a way around this or an alternate method I'm not seeing?

Tom Delebo
  • 21
  • 2
  • A runnable example would be likely to get more people interested in examining this. – tgdavies Dec 22 '22 at 04:10
  • @tgdavies Unfortunately, I can't. It's part of a much larger pipeline processing all the APIs, methods, fields, etc and I have no way to break it down into a smaller piece and the whole is way to big to post on here. I am referring to this method from the JDK just so it's clear that it's not a custom method I wrote: https://docs.oracle.com/javase/9/docs/api/javax/lang/model/util/Elements.html#getDocComment-javax.lang.model.element.Element- – Tom Delebo Dec 22 '22 at 06:56
  • I don't know whether this will help or not, but you have written `@NotNull` in the wrong place in your code. A type annotation such as `@NotNull` should be written immediately before the type, as in `private @NotNull SomeObject varA;` which is a private variable of type `@NotNull SomeObject`. – mernst Dec 22 '22 at 16:53

0 Answers0