diff options
author | Rawi01 <Rawi01@users.noreply.github.com> | 2023-03-12 09:05:24 +0100 |
---|---|---|
committer | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2023-03-22 12:33:59 +0100 |
commit | 8c0081a806a3c772f29be5c038024a5a941c4be6 (patch) | |
tree | b9120f8f99941e0eeecdcc8563241074747a65e8 /src/core/lombok/javac/handlers | |
parent | 3b968fe57c3f816a27e5d57f1bf6060d6e4c4b4e (diff) | |
download | lombok-8c0081a806a3c772f29be5c038024a5a941c4be6.tar.gz lombok-8c0081a806a3c772f29be5c038024a5a941c4be6.tar.bz2 lombok-8c0081a806a3c772f29be5c038024a5a941c4be6.zip |
[fixes #3364] Generate JCTree clones to prevent duplicate nodes
Diffstat (limited to 'src/core/lombok/javac/handlers')
7 files changed, 34 insertions, 61 deletions
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<Builder> { List<JCAnnotation> annsOnMethod = job.checkerFramework.generateSideEffectFree() ? List.of(maker.Annotation(genTypeRef(job.builderType, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE), List.<JCExpression>nil())) : List.<JCAnnotation>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.<JCTypeParameter>nil(), recv, List.<JCVariableDecl>nil(), thrownExceptions, body, null); + methodDef = maker.MethodDefWithRecvParam(maker.Modifiers(toJavacModifier(job.accessInners), annsOnMethod), job.toName(job.buildMethodName), returnTypeCopy, List.<JCTypeParameter>nil(), recv, List.<JCVariableDecl>nil(), thrownExceptions, body, null); } else { - methodDef = maker.MethodDef(maker.Modifiers(toJavacModifier(job.accessInners), annsOnMethod), job.toName(job.buildMethodName), returnType, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), thrownExceptions, body, null); + methodDef = maker.MethodDef(maker.Modifiers(toJavacModifier(job.accessInners), annsOnMethod), job.toName(job.buildMethodName), returnTypeCopy, List.<JCTypeParameter>nil(), List.<JCVariableDecl>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<JCAnnotation> 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 JavacAnnotationHandler<EqualsAndHas injectMethod(typeNode, equalsMethod); if (needsCanEqual && canEqualExists == MemberExistsResult.NOT_EXISTS) { - JCMethodDecl canEqualMethod = createCanEqual(typeNode, source, onParam); + JCMethodDecl canEqualMethod = createCanEqual(typeNode, source, copyAnnotations(onParam)); injectMethod(typeNode, canEqualMethod); } @@ -240,10 +240,10 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas /* if (this.$hashCodeCache != 0) return this.$hashCodeCache; */ { if (cacheHashCode) { - JCIdent receiver = maker.Ident(typeNode.toName("this")); - JCFieldAccess cacheHashCodeFieldAccess = maker.Select(receiver, typeNode.toName(HASH_CODE_CACHE_NAME)); - JCExpression cacheNotZero = maker.Binary(CTC_NOT_EQUAL, cacheHashCodeFieldAccess, maker.Literal(CTC_INT, 0)); - statements.append(maker.If(cacheNotZero, maker.Return(cacheHashCodeFieldAccess), null)); + JCFieldAccess hashCodeCacheFieldAccess = createHashCodeCacheFieldAccess(typeNode, maker); + JCExpression cacheNotZero = maker.Binary(CTC_NOT_EQUAL, hashCodeCacheFieldAccess, maker.Literal(CTC_INT, 0)); + hashCodeCacheFieldAccess = createHashCodeCacheFieldAccess(typeNode, maker); + statements.append(maker.If(cacheNotZero, maker.Return(hashCodeCacheFieldAccess), null)); } } @@ -345,7 +345,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas statements.append(maker.If(maker.Binary(CTC_EQUAL, maker.Ident(resultName), maker.Literal(CTC_INT, 0)), maker.Exec(maker.Assign(maker.Ident(resultName), genJavaLangTypeRef(typeNode, "Integer", "MIN_VALUE"))), null)); - JCFieldAccess cacheHashCodeFieldAccess = maker.Select(maker.Ident(typeNode.toName("this")), typeNode.toName(HASH_CODE_CACHE_NAME)); + JCFieldAccess cacheHashCodeFieldAccess = createHashCodeCacheFieldAccess(typeNode, maker); statements.append(maker.Exec(maker.Assign(cacheHashCodeFieldAccess, maker.Ident(resultName)))); } } @@ -359,6 +359,12 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>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<Getter> { 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> 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<Getter> { ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); 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<Getter> { 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<Getter> { /* private final java.util.concurrent.atomic.AtomicReference<Object> fieldName = new java.util.concurrent.atomic.AtomicReference<Object>(); */ { field.vartype = recursiveSetGeneratedBy( maker.TypeApply(chainDotsString(fieldNode, AR), List.<JCExpression>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<Getter> { return maker.Exec(maker.Apply(NIL_EXPRESSION, maker.Select(receiver, source.toName("set")), List.<JCExpression>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.<JCExpression>nil(), method, List.<JCExpression>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<SuperBuilder> { 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<SuperBuilder> { // 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<JCAnnotation> annsOnParamType = List.nil(); @@ -644,6 +645,7 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> { // 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.<JCTypeParameter>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<JCAnnotation> 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<TypeName> 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<JCAnnotation> result = new ListBuffer<JCAnnotation>(); 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<JCAnnotation> findAnnotationsInList(JavacNode node, java.util.List<String> 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<JCAnnotation> result = new ListBuffer<JCAnnotation>(); for (JavacNode child : node.down()) { if (child.getKind() == Kind.ANNOTATION) { @@ -1788,7 +1747,7 @@ public class JavacHandlerUtil { } } } - return result.toList(); + return copyAnnotations(result.toList()); } /** |