mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-13 06:15:41 +00:00
[fixes #3805] Create copies for expressions
This commit is contained in:
1 parent
47cb06ef2c
commit
4404ef9bb2
5 files changed
+27
-53
No files matched your search
@@ -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
|
||||
@@ -185,7 +185,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -43,7 +43,6 @@ import com.sun.tools.javac.tree.JCTree.JCAnnotation;
|
||||
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;
|
||||
@@ -156,11 +155,9 @@ public class HandleLog {
|
||||
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);
|
||||
|
||||
@@ -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
|
||||
@@ -94,17 +94,17 @@ public class HandleNonNull extends JavacAnnotationHandler<NonNull> {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,7 +132,6 @@ public class HandleNonNull extends JavacAnnotationHandler<NonNull> {
|
||||
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)) {
|
||||
|
||||
@@ -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
|
||||
@@ -2115,14 +2115,15 @@ public class JavacHandlerUtil {
|
||||
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) {
|
||||
|
||||
@@ -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
|
||||
@@ -117,6 +117,7 @@ public class RunTestsViaDelombok extends AbstractRunTests {
|
||||
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);
|
||||
}
|
||||
@@ -150,30 +151,12 @@ public class RunTestsViaDelombok extends AbstractRunTests {
|
||||
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;
|
||||
@@ -198,12 +181,9 @@ public class RunTestsViaDelombok extends AbstractRunTests {
|
||||
|
||||
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));
|
||||
@@ -212,10 +192,7 @@ public class RunTestsViaDelombok extends AbstractRunTests {
|
||||
try {
|
||||
super.scan(tree);
|
||||
} finally {
|
||||
JCTree _tree = astContext.pop();
|
||||
if (!lombokGeneratedNodes.isEmpty() && lombokGeneratedNodes.peek().equals(_tree)) {
|
||||
lombokGeneratedNodes.pop();
|
||||
}
|
||||
astContext.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user