Skip to content

Commit

Permalink
[fixes projectlombok#3805] Create copies for expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawi01 committed Feb 25, 2025
1 parent 47cb06e commit 4404ef9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2021 The Project Lombok Authors.
* Copyright (C) 2009-2025 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -185,7 +185,7 @@ public void generateMethods(JavacNode typeNode, JavacNode source, java.util.List
}
}

JCMethodDecl equalsMethod = createEquals(typeNode, members, callSuper, fieldAccess, needsCanEqual, source, onParam);
JCMethodDecl equalsMethod = createEquals(typeNode, members, callSuper, fieldAccess, needsCanEqual, source, copyAnnotations(onParam, typeNode.getTreeMaker()));

injectMethod(typeNode, equalsMethod);

Expand Down
11 changes: 4 additions & 7 deletions src/core/lombok/javac/handlers/HandleLog.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2010-2021 The Project Lombok Authors.
* Copyright (C) 2010-2025 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -43,7 +43,6 @@
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCLiteral;
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;
Expand Down Expand Up @@ -156,11 +155,9 @@ private static JCExpression[] createFactoryParameters(JavacNode typeNode, JCFiel
expressions[i] = maker.Apply(List.<JCExpression>nil(), method, List.<JCExpression>nil());
break;
case TOPIC:
if (loggerTopic instanceof JCLiteral) {
expressions[i] = maker.Literal(((JCLiteral) loggerTopic).value);
} else {
expressions[i] = cloneType(maker, loggerTopic, typeNode);
}
JCExpression topicExpression = copyExpression(loggerTopic, maker);
recursiveSetGeneratedBy(topicExpression, typeNode);
expressions[i] = topicExpression;
break;
case NULL:
expressions[i] = maker.Literal(CTC_BOT, null);
Expand Down
21 changes: 10 additions & 11 deletions src/core/lombok/javac/handlers/HandleNonNull.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2013-2021 The Project Lombok Authors.
* Copyright (C) 2013-2025 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -94,17 +94,17 @@ private JCMethodDecl createRecordArgslessConstructor(JavacNode typeNode, JavacNo

JCModifiers mods = maker.Modifiers(toJavacModifier(AccessLevel.PUBLIC) | COMPACT_RECORD_CONSTRUCTOR, List.<JCAnnotation>nil());
JCBlock body = maker.Block(0L, List.<JCStatement>nil());
if (existingCtr == null) {
JCMethodDecl constr = maker.MethodDef(mods, typeNode.toName("<init>"), null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(), body, null);
return recursiveSetGeneratedBy(constr, source);
JCMethodDecl constuctor = existingCtr;
if (constuctor == null) {
constuctor = maker.MethodDef(mods, typeNode.toName("<init>"), null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(), body, null);
} else {
existingCtr.mods = mods;
existingCtr.body = body;
existingCtr = recursiveSetGeneratedBy(existingCtr, source);
addSuppressWarningsAll(existingCtr.mods, typeNode, typeNode.getNodeFor(getGeneratedBy(existingCtr)), typeNode.getContext());
addGenerated(existingCtr.mods, typeNode, typeNode.getNodeFor(getGeneratedBy(existingCtr)), typeNode.getContext());
return existingCtr;
constuctor.mods = mods;
constuctor.body = body;
}
recursiveSetGeneratedBy(constuctor, source);
addSuppressWarningsAll(constuctor.mods, typeNode, source, typeNode.getContext());
addGenerated(constuctor.mods, typeNode, source, typeNode.getContext());
return constuctor;
}

/**
Expand Down Expand Up @@ -132,7 +132,6 @@ private List<JCMethodDecl> addCompactConstructorIfNeeded(JavacNode typeNode, Jav
if (md.name.contentEquals("<init>")) {
if ((md.mods.flags & Flags.GENERATEDCONSTR) != 0) {
existingCtr = md;
existingCtr.mods.flags = existingCtr.mods.flags & ~Flags.GENERATEDCONSTR;
generateConstructor = true;
} else {
if (!isTolerate(typeNode, md)) {
Expand Down
9 changes: 5 additions & 4 deletions src/core/lombok/javac/handlers/JavacHandlerUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2022 The Project Lombok Authors.
* Copyright (C) 2009-2025 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -2115,14 +2115,15 @@ static List<JCAnnotation> copyAnnotations(List<? extends JCExpression> in, Javac
ListBuffer<JCAnnotation> out = new ListBuffer<JCAnnotation>();
for (JCExpression expr : in) {
if (!(expr instanceof JCAnnotation)) continue;
out.append(copyAnnotation((JCAnnotation) expr, maker));
out.append(copyExpression((JCAnnotation) expr, maker));
}
return out.toList();
}

static JCAnnotation copyAnnotation(JCAnnotation annotation, JavacTreeMaker maker) {
@SuppressWarnings("unchecked")
static <T extends JCExpression> T copyExpression(T expression, JavacTreeMaker maker) {
TreeVisitor<JCTree, Void> visitor = new TreeCopier<Void>(maker.getUnderlyingTreeMaker());
return (JCAnnotation) visitor.visitAnnotation(annotation, null);
return (T) expression.accept(visitor, null);
}

static List<JCAnnotation> mergeAnnotations(List<JCAnnotation> a, List<JCAnnotation> b) {
Expand Down
35 changes: 6 additions & 29 deletions test/core/src/lombok/RunTestsViaDelombok.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2024 The Project Lombok Authors.
* Copyright (C) 2009-2025 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -117,6 +117,7 @@ public static class NodePositionMapper extends TreeProcessor {
unit.accept(new TreeScanner() {
@Override public void scan(JCTree tree) {
if (tree == null) return;
if (tree instanceof JCMethodDecl && (((JCMethodDecl) tree).mods.flags & Flags.GENERATEDCONSTR) != 0) return;
if (tree.pos >= 0) {
nodePositions.put(tree, tree.pos);
}
Expand Down Expand Up @@ -150,30 +151,12 @@ private String craftFailMsg(String problematicNode, Deque<JCTree> astContext) {
return msg.append("\n-------").toString();
}

private boolean isLombokGenerated(JCTree tree) {
List<JCAnnotation> annotations = com.sun.tools.javac.util.List.nil();
if (tree instanceof JCMethodDecl) {
annotations = ((JCMethodDecl) tree).mods.annotations;
}
if (tree instanceof JCVariableDecl) {
annotations = ((JCVariableDecl) tree).mods.annotations;
}
for (JCAnnotation annotation: annotations) {
if ("lombok.Generated".equals(annotation.getAnnotationType().toString())) {
return true;
}
}
return false;
}

@Override void processCompilationUnit(final JCCompilationUnit unit) {
final Deque<JCTree> astContext = new ArrayDeque<JCTree>();
final Deque<JCTree> lombokGeneratedNodes = new ArrayDeque<JCTree>();
unit.accept(new TreeScanner() {
@Override public void scan(JCTree tree) {
if (tree == null) return;
if (tree instanceof JCMethodDecl && (((JCMethodDecl) tree).mods.flags & Flags.GENERATEDCONSTR) != 0) return;
if (isLombokGenerated(tree)) lombokGeneratedNodes.add(tree);
astContext.push(tree);
try {
if (tree instanceof JCModifiers) return;
Expand All @@ -198,12 +181,9 @@ private boolean isLombokGenerated(JCTree tree) {

if (check && tree.pos == -1) fail(craftFailMsg("Start position of node not set: ", astContext));

// Ignore ast position validation on lombok generated nodes.
if (lombokGeneratedNodes.isEmpty()) {
Integer expectedPos = nodePositionMapper.nodePositions.get(tree);
if (expectedPos != null && !expectedPos.equals(tree.pos)) {
fail(craftFailMsg(String.format("Expected node position %d, actual node position %d: ", expectedPos, tree.pos), astContext));
}
Integer expectedPos = nodePositionMapper.nodePositions.get(tree);
if (expectedPos != null && !expectedPos.equals(tree.pos)) {
fail(craftFailMsg(String.format("Expected node position %d, actual node position %d: ", expectedPos, tree.pos), astContext));
}
if (check && Javac.getEndPosition(tree, unit) == -1) {
fail(craftFailMsg("End position of node not set: ", astContext));
Expand All @@ -212,10 +192,7 @@ private boolean isLombokGenerated(JCTree tree) {
try {
super.scan(tree);
} finally {
JCTree _tree = astContext.pop();
if (!lombokGeneratedNodes.isEmpty() && lombokGeneratedNodes.peek().equals(_tree)) {
lombokGeneratedNodes.pop();
}
astContext.pop();
}
}
}
Expand Down

0 comments on commit 4404ef9

Please sign in to comment.