diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index 40854a34..07610a0b 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -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 JavacAnnotationHandlernil(), method, List.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); diff --git a/src/core/lombok/javac/handlers/HandleNonNull.java b/src/core/lombok/javac/handlers/HandleNonNull.java index 3b2bcc74..d3649ff7 100644 --- a/src/core/lombok/javac/handlers/HandleNonNull.java +++ b/src/core/lombok/javac/handlers/HandleNonNull.java @@ -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 { JCModifiers mods = maker.Modifiers(toJavacModifier(AccessLevel.PUBLIC) | COMPACT_RECORD_CONSTRUCTOR, List.nil()); JCBlock body = maker.Block(0L, List.nil()); - if (existingCtr == null) { - JCMethodDecl constr = maker.MethodDef(mods, typeNode.toName(""), null, List.nil(), params.toList(), List.nil(), body, null); - return recursiveSetGeneratedBy(constr, source); + JCMethodDecl constuctor = existingCtr; + if (constuctor == null) { + constuctor = maker.MethodDef(mods, typeNode.toName(""), null, List.nil(), params.toList(), List.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 { if (md.name.contentEquals("")) { if ((md.mods.flags & Flags.GENERATEDCONSTR) != 0) { existingCtr = md; - existingCtr.mods.flags = existingCtr.mods.flags & ~Flags.GENERATEDCONSTR; generateConstructor = true; } else { if (!isTolerate(typeNode, md)) { diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index fff1846a..b9b8a719 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -2119,14 +2119,15 @@ public class JavacHandlerUtil { ListBuffer out = new ListBuffer(); 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 copyExpression(T expression, JavacTreeMaker maker) { TreeVisitor visitor = new TreeCopier(maker.getUnderlyingTreeMaker()); - return (JCAnnotation) visitor.visitAnnotation(annotation, null); + return (T) expression.accept(visitor, null); } static List mergeAnnotations(List a, List b) { diff --git a/test/core/src/lombok/RunTestsViaDelombok.java b/test/core/src/lombok/RunTestsViaDelombok.java index 80eb24be..8175fb71 100644 --- a/test/core/src/lombok/RunTestsViaDelombok.java +++ b/test/core/src/lombok/RunTestsViaDelombok.java @@ -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 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 astContext = new ArrayDeque(); - final Deque lombokGeneratedNodes = new ArrayDeque(); 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(); } } }