Skip to content

Commit

Permalink
Issues with lombok
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza authored and mickaelistria committed Feb 17, 2025
1 parent af41e85 commit 0d05286
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -965,8 +965,8 @@ protected com.sun.tools.javac.parser.Tokens.Comment processComment(int pos, int

static void addCommentsToUnit(Collection<Comment> comments, CompilationUnit res) {
List<Comment> before = res.getCommentList() == null ? new ArrayList<>() : new ArrayList<>(res.getCommentList());
comments.stream().filter(comment -> comment.getStartPosition() >= 0 && JavacCompilationUnitResolver.noCommentAt(res, comment.getStartPosition()))
.forEach(before::add);
comments.stream().filter(comment -> comment.getStartPosition() >= 0 && !generated(comment) && JavacCompilationUnitResolver.noCommentAt(res, comment.getStartPosition()))
.forEach(before::add);
before.sort(Comparator.comparingInt(Comment::getStartPosition));
res.setCommentTable(before.toArray(Comment[]::new));
}
Expand All @@ -979,6 +979,18 @@ private static boolean noCommentAt(CompilationUnit unit, int pos) {
.allMatch(other -> pos < other.getStartPosition() || pos >= other.getStartPosition() + other.getLength());
}

private static boolean generated(Comment comment) {
ASTNode parentNode = comment.getParent();
if (parentNode instanceof MethodDeclaration md) {
for (Object modifier: md.modifiers()) {
if (modifier instanceof MarkerAnnotation ma) {
return "lombok.Generated".equals(ma.getTypeName().getFullyQualifiedName());
}
}
}
return false;
}

private static class BindingBuilder extends ASTVisitor {
public Map<String, IBinding> bindingMap = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.BoundKind;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Type.PackageType;
import com.sun.tools.javac.parser.ParserFactory;
import com.sun.tools.javac.parser.Tokens.Comment;
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
Expand Down Expand Up @@ -891,7 +892,7 @@ private MethodDeclaration convertMethodDecl(JCMethodDecl javac, ASTNode parent)
String methodDeclName = getMethodDeclName(javac, parent, parent instanceof RecordDeclaration);
boolean methodDeclNameMatchesInit = Objects.equals(methodDeclName, Names.instance(this.context).init.toString());
boolean javacNameMatchesInit = javacName.equals("<init>");
boolean javacNameMatchesError = javacName.equals(ERROR);
boolean javacNameMatchesError = javacName.endsWith(ERROR); // lombok creates set<error>
boolean javacNameMatchesInitAndMethodNameMatchesTypeName = javacNameMatchesInit && methodDeclName.equals(getNodeName(parent));
boolean isConstructor = methodDeclNameMatchesInit || javacNameMatchesInitAndMethodNameMatchesTypeName;
res.setConstructor(isConstructor);
Expand Down Expand Up @@ -979,7 +980,8 @@ private MethodDeclaration convertMethodDecl(JCMethodDecl javac, ASTNode parent)
}
}

if (javac.getBody() != null
boolean generatedByLombok = javac.getModifiers() != null && javac.getModifiers().toString().contains("@lombok.Generated");
if (!generatedByLombok && javac.getBody() != null
&& javac.getBody().endpos > javac.getBody().getStartPosition()) { // otherwise, it's probably generated by lombok
boolean fillBlock = shouldFillBlock(javac, this.focalPoint);
if( fillBlock ) {
Expand Down Expand Up @@ -2789,7 +2791,12 @@ Type convertToType(JCTree javac) {
if (javac instanceof JCIdent ident) {
Name name = convertName(ident.name);
int len = FAKE_IDENTIFIER.equals(name.toString()) ? 0 : ident.name.length();
name.setSourceRange(ident.getStartPosition(), len);
int startPosition = ident.getStartPosition();
if (startPosition < 0 && ident.type instanceof PackageType) {
ILog.get().info("negative start position " + startPosition); // generated by lombok
startPosition = 0;
}
name.setSourceRange(startPosition, len);
SimpleType res = this.ast.newSimpleType(name);
commonSettings(res, ident);
commonSettings(name, ident);
Expand Down Expand Up @@ -2975,7 +2982,12 @@ Type convertToType(JCTree javac) {
// returning null could result in upstream errors, so return a fake type
var res = this.ast.newSimpleType(this.ast.newSimpleName(FAKE_IDENTIFIER));
if (javac instanceof JCErroneous err) {
res.setSourceRange(err.getStartPosition(), 0);
int startPosition = err.getStartPosition();
if (startPosition < 0) {
ILog.get().info("negative start position " + startPosition);
startPosition = 0;
}
res.setSourceRange(startPosition, 0);
}
res.setFlags(ASTNode.RECOVERED);
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,11 @@ public Entry<Object, Integer> memberValue(Expression dom) {
return new SimpleEntry<>(null, IMemberValuePair.K_UNKNOWN);
}
if (dom instanceof StringLiteral stringValue) {
return new SimpleEntry<>(stringValue.getLiteralValue(), IMemberValuePair.K_STRING);
try {
return new SimpleEntry<>(stringValue.getLiteralValue(), IMemberValuePair.K_STRING);
} catch (IllegalArgumentException e) {
// lombok oddity, let's ignore
}
}
if (dom instanceof BooleanLiteral booleanValue) {
return new SimpleEntry<>(booleanValue.booleanValue(), IMemberValuePair.K_BOOLEAN);
Expand Down

0 comments on commit 0d05286

Please sign in to comment.