Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate @return and @param JavaDoc tags for @Getter and @Setter annotations #3209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/core/lombok/core/handlers/HandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ 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)?");
private static final Pattern MAIN_SENTENCE_FINDER = Pattern.compile("\r?\n\\s*?[\r\n]|<p>|^\\s*@");

public enum JavadocTag {
PARAM("@param(?:eter)?"),
Expand All @@ -905,6 +906,18 @@ public enum JavadocTag {
JavadocTag(String regexpFragment) {
pattern = Pattern.compile("\\s?^[ \\t]*\\**[ \\t]*" + regexpFragment + "(\\S|\\s)*?(?=(\\s^\\s*\\**\\s*@|\\Z))", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
}

public boolean isPartOf(String javadoc) {
return pattern.matcher(javadoc).find();
}
}

public static String getMainSentenceOfJavaDoc(String javadoc) {
if (javadoc == null || javadoc.isEmpty()) return javadoc;

String trimmedJavadoc = javadoc.trim();
Matcher matcher = MAIN_SENTENCE_FINDER.matcher(trimmedJavadoc);
return matcher.find() ? trimmedJavadoc.substring(0, matcher.start()).trim() : trimmedJavadoc;
}

public static String stripLinesWithTagFromJavadoc(String javadoc, JavadocTag tag) {
Expand Down
12 changes: 12 additions & 0 deletions src/core/lombok/javac/handlers/JavacHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2205,6 +2205,12 @@ public static enum CopyJavadoc {
if (!sectionBased) {
out = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), JavadocTag.PARAM);
}
if (!JavadocTag.RETURN.isPartOf(out)) {
String mainSentence = getMainSentenceOfJavaDoc(out);
if (mainSentence != null && !mainSentence.isEmpty()) {
out = addJavadocLine(out, "@return " + mainSentence);
}
}
node.getAst().cleanupTask("javadocfilter-getter", n, new CleanupTask() {
@Override public void cleanup() {
String javadoc = Javac.getDocComment(cu, n);
Expand Down Expand Up @@ -2246,6 +2252,12 @@ private static String applySetter(final JCCompilationUnit cu, JavacNode node, St
if (!sectionBased) {
out = stripLinesWithTagFromJavadoc(stripSectionsFromJavadoc(javadoc), JavadocTag.RETURN);
}
if (!JavadocTag.PARAM.isPartOf(out)) {
String mainSentence = getMainSentenceOfJavaDoc(out);
if (mainSentence != null && !mainSentence.isEmpty()) {
out = addJavadocLine(out, "@param " + node.getName() + ' ' + mainSentence);
}
}
node.getAst().cleanupTask("javadocfilter-setter", n, new CleanupTask() {
@Override public void cleanup() {
String javadoc = Javac.getDocComment(cu, n);
Expand Down
111 changes: 111 additions & 0 deletions test/transform/resource/after-delombok/GetterJavaDoc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
class GetterJavaDoc {
int noJavaDoc;
/**
*/
int emptyJavaDoc;
/**
*/
int emptyJavaDocLine;
/**
* Some text
*/
int oneLine;
/**
* Some text
* in a second line
*/
int twoLines;
/**
* Some text
* in a second line
*
* And a second paragraph
*/
int secondParagraph;
/**
* Some text
* in a second line
* <p>
* And a second paragraph
*/
int secondParagraphHtml;
/**
* Some text
*/
int withCustomReturnTag;

@java.lang.SuppressWarnings("all")
public int getNoJavaDoc() {
return this.noJavaDoc;
}

/**
*/
@java.lang.SuppressWarnings("all")
public int getEmptyJavaDoc() {
return this.emptyJavaDoc;
}

/**
*/
@java.lang.SuppressWarnings("all")
public int getEmptyJavaDocLine() {
return this.emptyJavaDocLine;
}

/**
* Some text
* @return Some text
*/
@java.lang.SuppressWarnings("all")
public int getOneLine() {
return this.oneLine;
}

/**
* Some text
* in a second line
* @return Some text
* in a second line
*/
@java.lang.SuppressWarnings("all")
public int getTwoLines() {
return this.twoLines;
}

/**
* Some text
* in a second line
*
* And a second paragraph
* @return Some text
* in a second line
*/
@java.lang.SuppressWarnings("all")
public int getSecondParagraph() {
return this.secondParagraph;
}

/**
* Some text
* in a second line
* <p>
* And a second paragraph
* @return Some text
* in a second line
*/
@java.lang.SuppressWarnings("all")
public int getSecondParagraphHtml() {
return this.secondParagraphHtml;
}

/**
* Some text
*
* @return some custom text
*/
@java.lang.SuppressWarnings("all")
public int getWithCustomReturnTag() {
return this.withCustomReturnTag;
}
}
111 changes: 111 additions & 0 deletions test/transform/resource/after-delombok/SetterJavaDoc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
class SetterJavaDoc {
int noJavaDoc;
/**
*/
int emptyJavaDoc;
/**
*/
int emptyJavaDocLine;
/**
* Some text
*/
int oneLine;
/**
* Some text
* in a second line
*/
int twoLines;
/**
* Some text
* in a second line
*
* And a second paragraph
*/
int secondParagraph;
/**
* Some text
* in a second line
* <p>
* And a second paragraph
*/
int secondParagraphHtml;
/**
* Some text
*/
int withCustomParamTag;

@java.lang.SuppressWarnings("all")
public void setNoJavaDoc(final int noJavaDoc) {
this.noJavaDoc = noJavaDoc;
}

/**
*/
@java.lang.SuppressWarnings("all")
public void setEmptyJavaDoc(final int emptyJavaDoc) {
this.emptyJavaDoc = emptyJavaDoc;
}

/**
*/
@java.lang.SuppressWarnings("all")
public void setEmptyJavaDocLine(final int emptyJavaDocLine) {
this.emptyJavaDocLine = emptyJavaDocLine;
}

/**
* Some text
* @param oneLine Some text
*/
@java.lang.SuppressWarnings("all")
public void setOneLine(final int oneLine) {
this.oneLine = oneLine;
}

/**
* Some text
* in a second line
* @param twoLines Some text
* in a second line
*/
@java.lang.SuppressWarnings("all")
public void setTwoLines(final int twoLines) {
this.twoLines = twoLines;
}

/**
* Some text
* in a second line
*
* And a second paragraph
* @param secondParagraph Some text
* in a second line
*/
@java.lang.SuppressWarnings("all")
public void setSecondParagraph(final int secondParagraph) {
this.secondParagraph = secondParagraph;
}

/**
* Some text
* in a second line
* <p>
* And a second paragraph
* @param secondParagraphHtml Some text
* in a second line
*/
@java.lang.SuppressWarnings("all")
public void setSecondParagraphHtml(final int secondParagraphHtml) {
this.secondParagraphHtml = secondParagraphHtml;
}

/**
* Some text
*
* @param withCustomParamTag some custom text
*/
@java.lang.SuppressWarnings("all")
public void setWithCustomParamTag(final int withCustomParamTag) {
this.withCustomParamTag = withCustomParamTag;
}
}
56 changes: 56 additions & 0 deletions test/transform/resource/before/GetterJavaDoc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import lombok.Getter;

class GetterJavaDoc {
@Getter
int noJavaDoc;

/**
*/
@Getter
int emptyJavaDoc;

/**
*
*/
@Getter
int emptyJavaDocLine;

/**
* Some text
*/
@Getter
int oneLine;

/**
* Some text
* in a second line
*/
@Getter
int twoLines;

/**
* Some text
* in a second line
*
* And a second paragraph
*/
@Getter
int secondParagraph;

/**
* Some text
* in a second line
* <p>
* And a second paragraph
*/
@Getter
int secondParagraphHtml;

/**
* Some text
*
* @return some custom text
*/
@Getter
int withCustomReturnTag;
}
56 changes: 56 additions & 0 deletions test/transform/resource/before/SetterJavaDoc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import lombok.Setter;

class SetterJavaDoc {
@Setter
int noJavaDoc;

/**
*/
@Setter
int emptyJavaDoc;

/**
*
*/
@Setter
int emptyJavaDocLine;

/**
* Some text
*/
@Setter
int oneLine;

/**
* Some text
* in a second line
*/
@Setter
int twoLines;

/**
* Some text
* in a second line
*
* And a second paragraph
*/
@Setter
int secondParagraph;

/**
* Some text
* in a second line
* <p>
* And a second paragraph
*/
@Setter
int secondParagraphHtml;

/**
* Some text
*
* @param withCustomParamTag some custom text
*/
@Setter
int withCustomParamTag;
}