Skip to content

Commit

Permalink
[fixes projectlombok#3709] Improve extraction of javadoc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawi01 committed Aug 20, 2024
1 parent eb4722c commit f19a7e8
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
Expand Down Expand Up @@ -2797,6 +2799,9 @@ public static Annotation[][] getRecordFieldAnnotations(TypeDeclaration typeDecla
return annotations;
}

private static final Pattern JAVADOC_PATTERN = Pattern.compile("^\\s*\\/\\*\\*((?:\\S|\\s)*?)\\*\\/", Pattern.MULTILINE);
private static final Pattern LEADING_ASTERISKS_PATTERN = Pattern.compile("(?m)^\\s*\\* ?");

public static String getDocComment(EclipseNode eclipseNode) {
if (eclipseNode.getAst().getSource() == null) return null;

Expand All @@ -2816,11 +2821,11 @@ public static String getDocComment(EclipseNode eclipseNode) {
if (start != -1 && end != -1) {
char[] rawContent = CharOperation.subarray(eclipseNode.getAst().getSource(), start, end);
String rawContentString = new String(rawContent);
int startIndex = rawContentString.indexOf("/**");
int endIndex = rawContentString.indexOf("*/");
if (startIndex != -1 && endIndex != -1) {
Matcher javadocMatcher = JAVADOC_PATTERN.matcher(rawContentString);
if (javadocMatcher.find()) {
String javadoc = javadocMatcher.group(1);
/* Remove all leading asterisks */
return rawContentString.substring(startIndex + 3, endIndex).replaceAll("(?m)^\\s*\\* ?", "").trim();
return LEADING_ASTERISKS_PATTERN.matcher(javadoc).replaceAll("").trim();
}
}
return null;
Expand Down
72 changes: 72 additions & 0 deletions test/transform/resource/after-delombok/WeirdJavadoc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
public class WeirdJavadoc {
// Comment
/* Weird comment /** */
/* Weird comment /** */
/**
* This is the real comment
* @param test Copy this
*/
WeirdJavadoc(String test) {
}
// Comment
/* Weird comment /** */
/* Weird comment /** */
/**
* This is the real comment
*/
private String test;
@java.lang.SuppressWarnings("all")
@lombok.Generated
public static class WeirdJavadocBuilder {
@java.lang.SuppressWarnings("all")
@lombok.Generated
private String test;
@java.lang.SuppressWarnings("all")
@lombok.Generated
WeirdJavadocBuilder() {
}
/**
* @param test Copy this
* @return {@code this}.
*/
@java.lang.SuppressWarnings("all")
@lombok.Generated
public WeirdJavadoc.WeirdJavadocBuilder test(final String test) {
this.test = test;
return this;
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public WeirdJavadoc build() {
return new WeirdJavadoc(this.test);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
@lombok.Generated
public java.lang.String toString() {
return "WeirdJavadoc.WeirdJavadocBuilder(test=" + this.test + ")";
}
}
@java.lang.SuppressWarnings("all")
@lombok.Generated
public static WeirdJavadoc.WeirdJavadocBuilder builder() {
return new WeirdJavadoc.WeirdJavadocBuilder();
}
/**
* This is the real comment
*/
@java.lang.SuppressWarnings("all")
@lombok.Generated
public String getTest() {
return this.test;
}
/**
* This is the real comment
* @param test Copy this
*/
@java.lang.SuppressWarnings("all")
@lombok.Generated
public void setTest(final String test) {
this.test = test;
}
}
45 changes: 45 additions & 0 deletions test/transform/resource/after-ecj/WeirdJavadoc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
public class WeirdJavadoc {
public static @java.lang.SuppressWarnings("all") @lombok.Generated class WeirdJavadocBuilder {
private @java.lang.SuppressWarnings("all") @lombok.Generated String test;
@java.lang.SuppressWarnings("all") @lombok.Generated WeirdJavadocBuilder() {
super();
}
/**
* @param test Copy this
* @return {@code this}.
*/
public @java.lang.SuppressWarnings("all") @lombok.Generated WeirdJavadoc.WeirdJavadocBuilder test(final String test) {
this.test = test;
return this;
}
public @java.lang.SuppressWarnings("all") @lombok.Generated WeirdJavadoc build() {
return new WeirdJavadoc(this.test);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") @lombok.Generated java.lang.String toString() {
return (("WeirdJavadoc.WeirdJavadocBuilder(test=" + this.test) + ")");
}
}
private @Getter @Setter String test;
@Builder WeirdJavadoc(String test) {
super();
}
public static @java.lang.SuppressWarnings("all") @lombok.Generated WeirdJavadoc.WeirdJavadocBuilder builder() {
return new WeirdJavadoc.WeirdJavadocBuilder();
}
/**
* This is the real comment
*/
public @java.lang.SuppressWarnings("all") @lombok.Generated String getTest() {
return this.test;
}
/**
* This is the real comment
* @param test Copy this
*/
public @java.lang.SuppressWarnings("all") @lombok.Generated void setTest(final String test) {
this.test = test;
}
}
28 changes: 28 additions & 0 deletions test/transform/resource/before/WeirdJavadoc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

public class WeirdJavadoc {
// Comment
/* Weird comment /** */
/**
* This is the real comment
* @param test Copy this
*/
/* Weird comment /** */
@Builder
WeirdJavadoc(String test) {

}

// Comment
/* Weird comment /** */
/**
* This is the real comment
* @param test Copy this
*/
/* Weird comment /** */
@Getter
@Setter
private String test;
}

0 comments on commit f19a7e8

Please sign in to comment.