From 8c0081a806a3c772f29be5c038024a5a941c4be6 Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Sun, 12 Mar 2023 09:05:24 +0100 Subject: [fixes #3364] Generate JCTree clones to prevent duplicate nodes --- src/core/lombok/javac/handlers/HandleBuilder.java | 5 ++- .../lombok/javac/handlers/HandleConstructor.java | 2 +- .../javac/handlers/HandleEqualsAndHashCode.java | 18 ++++++--- src/core/lombok/javac/handlers/HandleGetter.java | 12 +++--- src/core/lombok/javac/handlers/HandleLog.java | 9 ++++- .../lombok/javac/handlers/HandleSuperBuilder.java | 4 +- .../lombok/javac/handlers/JavacHandlerUtil.java | 45 +--------------------- 7 files changed, 34 insertions(+), 61 deletions(-) (limited to 'src/core/lombok/javac') diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index ed7d2844..34b4f004 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -796,10 +796,11 @@ public class HandleBuilder extends JavacAnnotationHandler { List annsOnMethod = job.checkerFramework.generateSideEffectFree() ? List.of(maker.Annotation(genTypeRef(job.builderType, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.nil())) : List.nil(); JCVariableDecl recv = generateReceiver(job); JCMethodDecl methodDef; + JCExpression returnTypeCopy = cloneType(maker, returnType, job.sourceNode); if (recv != null && maker.hasMethodDefWithRecvParam()) { - methodDef = maker.MethodDefWithRecvParam(maker.Modifiers(toJavacModifier(job.accessInners), annsOnMethod), job.toName(job.buildMethodName), returnType, List.nil(), recv, List.nil(), thrownExceptions, body, null); + methodDef = maker.MethodDefWithRecvParam(maker.Modifiers(toJavacModifier(job.accessInners), annsOnMethod), job.toName(job.buildMethodName), returnTypeCopy, List.nil(), recv, List.nil(), thrownExceptions, body, null); } else { - methodDef = maker.MethodDef(maker.Modifiers(toJavacModifier(job.accessInners), annsOnMethod), job.toName(job.buildMethodName), returnType, List.nil(), List.nil(), thrownExceptions, body, null); + methodDef = maker.MethodDef(maker.Modifiers(toJavacModifier(job.accessInners), annsOnMethod), job.toName(job.buildMethodName), returnTypeCopy, List.nil(), List.nil(), thrownExceptions, body, null); } if (staticName == null) createRelevantNonNullAnnotation(job.builderType, methodDef); return methodDef; diff --git a/src/core/lombok/javac/handlers/HandleConstructor.java b/src/core/lombok/javac/handlers/HandleConstructor.java index dc70e2ce..6a5dad1b 100644 --- a/src/core/lombok/javac/handlers/HandleConstructor.java +++ b/src/core/lombok/javac/handlers/HandleConstructor.java @@ -440,7 +440,7 @@ public class HandleConstructor { if (!type.typarams.isEmpty()) { for (JCTypeParameter param : type.typarams) { - typeParams.append(maker.TypeParameter(param.name, param.bounds)); + typeParams.append(maker.TypeParameter(param.name, cloneTypes(maker, param.bounds, source))); } } List annsOnReturnType = List.nil(); diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index dace3521..3b408145 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -191,7 +191,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandlernil(), List.nil(), List.nil(), body, null), source); } + private JCFieldAccess createHashCodeCacheFieldAccess(JavacNode typeNode, JavacTreeMaker maker) { + JCIdent receiver = maker.Ident(typeNode.toName("this")); + JCFieldAccess cacheHashCodeFieldAccess = maker.Select(receiver, typeNode.toName(HASH_CODE_CACHE_NAME)); + return cacheHashCodeFieldAccess; + } + public JCExpressionStatement createResultCalculation(JavacNode typeNode, JCExpression expr) { /* result = result * PRIME + expr; */ JavacTreeMaker maker = typeNode.getTreeMaker(); diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 86eb9fda..3f7fbd20 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -222,7 +222,7 @@ public class HandleGetter extends JavacAnnotationHandler { JCVariableDecl fieldNode = (JCVariableDecl) field.get(); // Remember the type; lazy will change it - JCExpression methodType = cloneType(treeMaker, copyType(treeMaker, fieldNode), source); + JCExpression methodType = copyType(treeMaker, fieldNode, source); AnnotationValues accessors = JavacHandlerUtil.getAccessorsForField(field); // Generate the methodName; lazy will change the field type Name methodName = field.toName(toGetterName(field, accessors)); @@ -359,7 +359,7 @@ public class HandleGetter extends JavacAnnotationHandler { ListBuffer statements = new ListBuffer(); JCVariableDecl field = (JCVariableDecl) fieldNode.get(); - JCExpression copyOfRawFieldType = copyType(maker, field); + JCExpression copyOfRawFieldType = copyType(maker, field, source); JCExpression copyOfBoxedFieldType = null; field.type = null; boolean isPrimitive = false; @@ -371,7 +371,7 @@ public class HandleGetter extends JavacAnnotationHandler { copyOfBoxedFieldType = genJavaLangTypeRef(fieldNode, boxed); } } - if (copyOfBoxedFieldType == null) copyOfBoxedFieldType = copyType(maker, field); + if (copyOfBoxedFieldType == null) copyOfBoxedFieldType = copyType(maker, field, source); Name valueName = fieldNode.toName("value"); Name actualValueName = fieldNode.toName("actualValue"); @@ -446,7 +446,7 @@ public class HandleGetter extends JavacAnnotationHandler { /* private final java.util.concurrent.atomic.AtomicReference fieldName = new java.util.concurrent.atomic.AtomicReference(); */ { field.vartype = recursiveSetGeneratedBy( maker.TypeApply(chainDotsString(fieldNode, AR), List.of(genJavaLangTypeRef(fieldNode, "Object"))), source); - field.init = recursiveSetGeneratedBy(maker.NewClass(null, NIL_EXPRESSION, copyType(maker, field), NIL_EXPRESSION, null), source); + field.init = recursiveSetGeneratedBy(maker.NewClass(null, NIL_EXPRESSION, copyType(maker, field, source), NIL_EXPRESSION, null), source); } return statements.toList(); @@ -462,7 +462,7 @@ public class HandleGetter extends JavacAnnotationHandler { return maker.Exec(maker.Apply(NIL_EXPRESSION, maker.Select(receiver, source.toName("set")), List.of(value))); } - public JCExpression copyType(JavacTreeMaker treeMaker, JCVariableDecl fieldNode) { - return fieldNode.type != null ? treeMaker.Type(fieldNode.type) : fieldNode.vartype; + public JCExpression copyType(JavacTreeMaker treeMaker, JCVariableDecl fieldNode, JavacNode source) { + return fieldNode.type != null ? treeMaker.Type(fieldNode.type) : cloneType(treeMaker, fieldNode.vartype, source); } } diff --git a/src/core/lombok/javac/handlers/HandleLog.java b/src/core/lombok/javac/handlers/HandleLog.java index 957f5581..f5eefaa1 100644 --- a/src/core/lombok/javac/handlers/HandleLog.java +++ b/src/core/lombok/javac/handlers/HandleLog.java @@ -43,6 +43,7 @@ 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; @@ -148,14 +149,18 @@ public class HandleLog { LogFactoryParameter parameter = parameters.get(i); switch (parameter) { case TYPE: - expressions[i] = loggingType; + expressions[i] = cloneType(maker, loggingType, typeNode); break; case NAME: JCExpression method = maker.Select(loggingType, typeNode.toName("getName")); expressions[i] = maker.Apply(List.nil(), method, List.nil()); break; case TOPIC: - expressions[i] = (JCExpression) loggerTopic.clone(); + if (loggerTopic instanceof JCLiteral) { + expressions[i] = maker.Literal(((JCLiteral) loggerTopic).value); + } else { + expressions[i] = cloneType(maker, loggerTopic, typeNode); + } break; case NULL: expressions[i] = maker.Literal(CTC_BOT, null); diff --git a/src/core/lombok/javac/handlers/HandleSuperBuilder.java b/src/core/lombok/javac/handlers/HandleSuperBuilder.java index 913f838c..1ebbccc8 100644 --- a/src/core/lombok/javac/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/javac/handlers/HandleSuperBuilder.java @@ -260,7 +260,7 @@ public class HandleSuperBuilder extends JavacAnnotationHandler { Name superclassName = ((JCFieldAccess) extendsClause).getIdentifier(); String builderClassNameTemplate = BuilderJob.getBuilderClassNameTemplate(annotationNode, null); String superclassBuilderClassName = job.replaceBuilderClassName(superclassName.toString(), builderClassNameTemplate); - superclassBuilderClass = parent.getTreeMaker().Select((JCFieldAccess) extendsClause, parent.toName(superclassBuilderClassName)); + superclassBuilderClass = parent.getTreeMaker().Select(cloneType(maker, (JCFieldAccess) extendsClause, annotationNode), parent.toName(superclassBuilderClassName)); } else if (extendsClause != null) { String builderClassNameTemplate = BuilderJob.getBuilderClassNameTemplate(annotationNode, null); String superclassBuilderClassName = job.replaceBuilderClassName(extendsClause.toString(), builderClassNameTemplate); @@ -607,6 +607,7 @@ public class HandleSuperBuilder extends JavacAnnotationHandler { // Now add the . JCWildcard wildcard = maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null); typeParameterNames.append(wildcard); + wildcard = maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null); typeParameterNames.append(wildcard); // And return type annotations. List annsOnParamType = List.nil(); @@ -644,6 +645,7 @@ public class HandleSuperBuilder extends JavacAnnotationHandler { // Now add the . JCWildcard wildcard = maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null); typeParameterNames.append(wildcard); + wildcard = maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null); typeParameterNames.append(wildcard); JCTypeApply returnType = maker.TypeApply(namePlusTypeParamsToTypeReference(maker, job.parentType, job.toName(job.builderAbstractClassName), false, List.nil()), typeParameterNames.toList()); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index ac947581..9c6e0f84 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -1695,29 +1695,8 @@ public class JavacHandlerUtil { * Searches the given field node for annotations and returns each one that is 'copyable' (either via configuration or from the base list). */ public static List findCopyableAnnotations(JavacNode node) { - JCAnnotation anno = null; - String annoName = null; - for (JavacNode child : node.down()) { - if (child.getKind() == Kind.ANNOTATION) { - if (anno != null) { - annoName = ""; - break; - } - JCAnnotation annotation = (JCAnnotation) child.get(); - annoName = annotation.annotationType.toString(); - anno = annotation; - } - } - - if (annoName == null) return List.nil(); - java.util.List configuredCopyable = node.getAst().readConfiguration(ConfigurationKeys.COPYABLE_ANNOTATIONS); - if (!annoName.isEmpty()) { - for (TypeName cn : configuredCopyable) if (cn != null && typeMatches(cn.toString(), node, annoName)) return List.of(anno); - for (String bn : BASE_COPYABLE_ANNOTATIONS) if (typeMatches(bn, node, annoName)) return List.of(anno); - } - ListBuffer result = new ListBuffer(); for (JavacNode child : node.down()) { if (child.getKind() == Kind.ANNOTATION) { @@ -1735,7 +1714,7 @@ public class JavacHandlerUtil { } } } - return result.toList(); + return copyAnnotations(result.toList()); } /** @@ -1756,26 +1735,6 @@ public class JavacHandlerUtil { * Searches the given field node for annotations that are in the given list, and returns those. */ private static List findAnnotationsInList(JavacNode node, java.util.List annotationsToFind) { - JCAnnotation anno = null; - String annoName = null; - for (JavacNode child : node.down()) { - if (child.getKind() == Kind.ANNOTATION) { - if (anno != null) { - annoName = ""; - break; - } - JCAnnotation annotation = (JCAnnotation) child.get(); - annoName = annotation.annotationType.toString(); - anno = annotation; - } - } - - if (annoName == null) return List.nil(); - - if (!annoName.isEmpty()) { - for (String bn : annotationsToFind) if (typeMatches(bn, node, annoName)) return List.of(anno); - } - ListBuffer result = new ListBuffer(); for (JavacNode child : node.down()) { if (child.getKind() == Kind.ANNOTATION) { @@ -1788,7 +1747,7 @@ public class JavacHandlerUtil { } } } - return result.toList(); + return copyAnnotations(result.toList()); } /** -- cgit