Skip to content

Commit

Permalink
[fixes #2443] Remove multiline Javadoc tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawi01 authored and rspilker committed Jan 14, 2021
1 parent b8d49a8 commit 2e5ea51
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 13 deletions.
17 changes: 13 additions & 4 deletions src/core/lombok/core/handlers/HandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -761,11 +761,20 @@ public static int defaultEqualsAndHashcodeIncludeRank(String typeName) {
private static final Pattern SECTION_FINDER = Pattern.compile("^\\s*\\**\\s*[-*][-*]+\\s*([GS]ETTER|WITH(?:ER)?)\\s*[-*][-*]+\\s*\\**\\s*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
private static final Pattern LINE_BREAK_FINDER = Pattern.compile("(\\r?\\n)?");

public static String stripLinesWithTagFromJavadoc(String javadoc, String regexpFragment) {
public enum JavadocTag {
PARAM("@param(?:eter)?"),
RETURN("@returns?");

private Pattern pattern;

JavadocTag(String regexpFragment) {
pattern = Pattern.compile("\\s?^[ \\t]*\\**[ \\t]*" + regexpFragment + "(\\S|\\s)*?(?=(\\s^\\s*\\**\\s*@|\\Z))", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
}
}

public static String stripLinesWithTagFromJavadoc(String javadoc, JavadocTag tag) {
if (javadoc == null || javadoc.isEmpty()) return javadoc;
Pattern p = Pattern.compile("^\\s*\\**\\s*" + regexpFragment + "\\s*\\**\\s*$", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(javadoc);
return m.replaceAll("");
return tag.pattern.matcher(javadoc).replaceAll("");
}

public static String stripSectionsFromJavadoc(String javadoc) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2688,7 +2688,7 @@ public static enum CopyJavadoc {
String out = getJavadocSection(javadoc, "GETTER");
final boolean sectionBased = out != null;
if (!sectionBased) {
out = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), "@param(?:eter)?\\s+.*");
out = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), JavadocTag.PARAM);
}
return out;
}
Expand Down Expand Up @@ -2718,7 +2718,7 @@ private static String applySetter(final CompilationUnitDeclaration cu, EclipseNo
String out = getJavadocSection(javadoc, sectionName);
final boolean sectionBased = out != null;
if (!sectionBased) {
out = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), "@returns?\\s+.*");
out = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), JavadocTag.RETURN);
}
return shouldReturnThis(node) ? addReturnsThisIfNeeded(out) : out;
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/lombok/javac/handlers/JavacHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2044,15 +2044,15 @@ public static enum CopyJavadoc {
String out = getJavadocSection(javadoc, "GETTER");
final boolean sectionBased = out != null;
if (!sectionBased) {
out = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), "@param(?:eter)?\\s+.*");
out = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), JavadocTag.PARAM);
}
node.getAst().cleanupTask("javadocfilter-getter", n, new CleanupTask() {
@Override public void cleanup() {
String javadoc = Javac.getDocComment(cu, n);
if (javadoc == null || javadoc.isEmpty()) return;
javadoc = stripSectionsFromJavadoc(javadoc);
if (!sectionBased) {
javadoc = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), "@returns?\\s+.*");
javadoc = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), JavadocTag.RETURN);
}
Javac.setDocComment(cu, n, javadoc);
}
Expand Down Expand Up @@ -2085,15 +2085,15 @@ private static String applySetter(final JCCompilationUnit cu, JavacNode node, St
String out = getJavadocSection(javadoc, sectionName);
final boolean sectionBased = out != null;
if (!sectionBased) {
out = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), "@returns?\\s+.*");
out = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), JavadocTag.RETURN);
}
node.getAst().cleanupTask("javadocfilter-setter", n, new CleanupTask() {
@Override public void cleanup() {
String javadoc = Javac.getDocComment(cu, n);
if (javadoc == null || javadoc.isEmpty()) return;
javadoc = stripSectionsFromJavadoc(javadoc);
if (!sectionBased) {
javadoc = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), "@param(?:eter)?\\s+.*");
javadoc = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), JavadocTag.PARAM);
}
Javac.setDocComment(cu, n, javadoc);
}
Expand Down
2 changes: 0 additions & 2 deletions test/transform/resource/after-delombok/BuilderJavadoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class BuilderJavadoc<T> {
/**
* basic gets only a builder setter.
* @see #getsetwith
*
* @return tag is removed from the setter.
*/
private final int basic;
Expand Down Expand Up @@ -92,7 +91,6 @@ public static <T> BuilderJavadoc.BuilderJavadocBuilder<T> builder() {
}
/**
* getsetwith gets a builder setter, an instance getter and setter, and a wither.
*
* @return tag is moved to the getter.
*/
@java.lang.SuppressWarnings("all")
Expand Down
51 changes: 51 additions & 0 deletions test/transform/resource/after-delombok/JavadocMultiline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class JavadocMultiline {
/**
* This is a list of booleans.
*/
private java.util.List<Boolean> booleans;
/**
* This is a list of booleans.
*/
private java.util.List<Boolean> booleans2;

/**
* This is a list of booleans.
*
* @return A list of booleans to set for this object. This is a Javadoc return that is long
* enough to wrap to multiple lines.
*/
@java.lang.SuppressWarnings("all")
public java.util.List<Boolean> getBooleans() {
return this.booleans;
}

/**
* This is a list of booleans.
*/
@java.lang.SuppressWarnings("all")
public java.util.List<Boolean> getBooleans2() {
return this.booleans2;
}

/**
* This is a list of booleans.
*
* @param booleans A list of booleans to set for this object. This is a Javadoc param that is
* long enough to wrap to multiple lines.
*/
@java.lang.SuppressWarnings("all")
public void setBooleans(final java.util.List<Boolean> booleans) {
this.booleans = booleans;
}

/**
* This is a list of booleans.
*
* @param booleans A list of booleans to set for this object. This is a Javadoc param that is
* long enough to wrap to multiple lines.
*/
@java.lang.SuppressWarnings("all")
public void setBooleans2(final java.util.List<Boolean> booleans2) {
this.booleans2 = booleans2;
}
}
1 change: 0 additions & 1 deletion test/transform/resource/after-ecj/BuilderJavadoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public BuilderJavadocBuilder<T> predefWithJavadoc(final int x) {
}
/**
* getsetwith gets a builder setter, an instance getter and setter, and a wither.
*
* @return tag is moved to the getter.
*/
public @java.lang.SuppressWarnings("all") int getGetsetwith() {
Expand Down
40 changes: 40 additions & 0 deletions test/transform/resource/after-ecj/JavadocMultiline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@lombok.Getter @lombok.Setter class JavadocMultiline {
private java.util.List<Boolean> booleans;
private java.util.List<Boolean> booleans2;
JavadocMultiline() {
super();
}
/**
* This is a list of booleans.
*
* @return A list of booleans to set for this object. This is a Javadoc return that is long
* enough to wrap to multiple lines.
*/
public @java.lang.SuppressWarnings("all") java.util.List<Boolean> getBooleans() {
return this.booleans;
}
/**
* This is a list of booleans.
*/
public @java.lang.SuppressWarnings("all") java.util.List<Boolean> getBooleans2() {
return this.booleans2;
}
/**
* This is a list of booleans.
*
* @param booleans A list of booleans to set for this object. This is a Javadoc param that is
* long enough to wrap to multiple lines.
*/
public @java.lang.SuppressWarnings("all") void setBooleans(final java.util.List<Boolean> booleans) {
this.booleans = booleans;
}
/**
* This is a list of booleans.
*
* @param booleans A list of booleans to set for this object. This is a Javadoc param that is
* long enough to wrap to multiple lines.
*/
public @java.lang.SuppressWarnings("all") void setBooleans2(final java.util.List<Boolean> booleans2) {
this.booleans2 = booleans2;
}
}
22 changes: 22 additions & 0 deletions test/transform/resource/before/JavadocMultiline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@lombok.Getter
@lombok.Setter
class JavadocMultiline {
/**
* This is a list of booleans.
*
* @param booleans A list of booleans to set for this object. This is a Javadoc param that is
* long enough to wrap to multiple lines.
* @return A list of booleans to set for this object. This is a Javadoc return that is long
* enough to wrap to multiple lines.
*/
private java.util.List<Boolean> booleans;


/**
* This is a list of booleans.
*
* @param booleans A list of booleans to set for this object. This is a Javadoc param that is
* long enough to wrap to multiple lines.
*/
private java.util.List<Boolean> booleans2;
}

0 comments on commit 2e5ea51

Please sign in to comment.