Skip to content

Commit

Permalink
[issue #2730] Javadoc generated by lombok in javac now gets its posit…
Browse files Browse the repository at this point in the history
…ion set.

This may fix 'IllegalArgumentException' errors when using google errorprone.
  • Loading branch information
rzwitserloot committed Feb 4, 2021
1 parent 3d3a4c3 commit adf3095
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/delombok/lombok/delombok/DocCommentIntegrator.java
Expand Up @@ -89,7 +89,7 @@ public List<CommentInfo> integrate(List<CommentInfo> comments, JCCompilationUnit
((Map<JCTree, String>) map_).put(node, docCommentContent);
return true;
} else if (Javac.instanceOfDocCommentTable(map_)) {
CommentAttacher_8.attach(node, docCommentContent, map_);
CommentAttacher_8.attach(node, docCommentContent, cmt.pos, map_);
return true;
}

Expand All @@ -98,15 +98,15 @@ public List<CommentInfo> integrate(List<CommentInfo> comments, JCCompilationUnit

/* Container for code which will cause class loader exceptions on javac below 8. By being in a separate class, we avoid the problem. */
private static class CommentAttacher_8 {
static void attach(final JCTree node, String docCommentContent, Object map_) {
static void attach(final JCTree node, String docCommentContent, final int pos, Object map_) {
final String docCommentContent_ = docCommentContent;
((DocCommentTable) map_).putComment(node, new Comment() {
@Override public String getText() {
return docCommentContent_;
}

@Override public int getSourcePos(int index) {
return -1;
return pos + index;
}

@Override public CommentStyle getStyle() {
Expand Down
19 changes: 18 additions & 1 deletion src/utils/lombok/javac/Javac.java
Expand Up @@ -279,6 +279,17 @@ public static String getDocComment(JCCompilationUnit cu, JCTree node) {
return null;
}

/**
* Checks if the javadoc comment associated with {@code tree} has a position set.
*
* Returns true if there is no javadoc comment on the node, or it has position (position isn't -1).
*/
public static boolean validateDocComment(JCCompilationUnit cu, JCTree tree) {
Object dc = getDocComments(cu);
if (!instanceOfDocCommentTable(dc)) return true;
return JavadocOps_8.validateJavadoc(dc, tree);
}

@SuppressWarnings("unchecked")
public static void setDocComment(JCCompilationUnit cu, JCTree node, String javadoc) {
if (javadoc == null) return;
Expand All @@ -302,6 +313,12 @@ static String getJavadoc(Object dc, JCTree node) {
return javadoc.getText();
}

public static boolean validateJavadoc(Object dc, JCTree node) {
DocCommentTable dct = (DocCommentTable) dc;
Comment javadoc = dct.getComment(node);
return javadoc == null || javadoc.getText() == null || javadoc.getSourcePos(0) >= 0;
}

static void setJavadoc(Object dc, JCTree node, String javadoc) {
DocCommentTable dct = (DocCommentTable) dc;
Comment newCmt = createJavadocComment(javadoc, node);
Expand All @@ -315,7 +332,7 @@ private static Comment createJavadocComment(final String text, final JCTree fiel
}

@Override public int getSourcePos(int index) {
return -1;
return field == null ? -1 : field.getStartPosition();
}

@Override public CommentStyle getStyle() {
Expand Down
4 changes: 4 additions & 0 deletions test/core/src/lombok/RunTestsViaDelombok.java
Expand Up @@ -99,6 +99,10 @@ public static class ValidatePositionProcessor extends TreeProcessor {
try {
if (tree instanceof JCModifiers) return;

if (!Javac.validateDocComment(unit, tree)) {
fail("Start position of doc comment (" + Javac.getDocComment(unit, tree) + ") of " + tree + " not set");
}

if (tree.pos == -1) {
fail("Start position of " + tree + " not set");
}
Expand Down

0 comments on commit adf3095

Please sign in to comment.