diff options
author | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2022-02-08 06:02:05 +0100 |
---|---|---|
committer | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2022-02-08 06:02:05 +0100 |
commit | 261758b4448b3d48ff2f48926ffcb8ea66121603 (patch) | |
tree | 1996f090ab62b3626fe875406a294e724ad94a12 /src/core/lombok/javac/handlers | |
parent | 8a914b1bf0eaa13178e719431add47b78d4e7277 (diff) | |
download | lombok-261758b4448b3d48ff2f48926ffcb8ea66121603.tar.gz lombok-261758b4448b3d48ff2f48926ffcb8ea66121603.tar.bz2 lombok-261758b4448b3d48ff2f48926ffcb8ea66121603.zip |
[implements #1456] accessors can now be made final via `@Accessors`.
Diffstat (limited to 'src/core/lombok/javac/handlers')
-rw-r--r-- | src/core/lombok/javac/handlers/HandleAccessors.java | 3 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleGetter.java | 16 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleSetter.java | 21 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleWith.java | 13 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleWithBy.java | 11 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/JavacHandlerUtil.java | 96 |
6 files changed, 130 insertions, 30 deletions
diff --git a/src/core/lombok/javac/handlers/HandleAccessors.java b/src/core/lombok/javac/handlers/HandleAccessors.java index 60466d78..ac0ace4f 100644 --- a/src/core/lombok/javac/handlers/HandleAccessors.java +++ b/src/core/lombok/javac/handlers/HandleAccessors.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012-2021 The Project Lombok Authors. + * Copyright (C) 2012-2022 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 @@ -44,5 +44,6 @@ public class HandleAccessors extends JavacAnnotationHandler<Accessors> { handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.ACCESSORS_FLAG_USAGE, "@Accessors"); deleteAnnotationIfNeccessary(annotationNode, Accessors.class); + if (annotation.isMarking()) annotationNode.addWarning("Accessors on its own does nothing. Set at least one parameter"); } } diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 7a7e41f9..86eb9fda 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2021 The Project Lombok Authors. + * Copyright (C) 2009-2022 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 @@ -33,6 +33,7 @@ import java.util.Map; import lombok.AccessLevel; import lombok.ConfigurationKeys; +import lombok.experimental.Accessors; import lombok.experimental.Delegate; import lombok.Getter; import lombok.core.AST.Kind; @@ -169,7 +170,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { return; } - JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get(); + JCVariableDecl fieldDecl = (JCVariableDecl) fieldNode.get(); if (lazy) { if ((fieldDecl.mods.flags & Flags.PRIVATE) == 0 || (fieldDecl.mods.flags & Flags.FINAL) == 0) { @@ -186,14 +187,15 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { } } - String methodName = toGetterName(fieldNode); + AnnotationValues<Accessors> accessors = getAccessorsForField(fieldNode); + String methodName = toGetterName(fieldNode, accessors); if (methodName == null) { source.addWarning("Not generating getter for this field: It does not fit your @Accessors prefix list."); return; } - for (String altName : toAllGetterNames(fieldNode)) { + for (String altName : toAllGetterNames(fieldNode, accessors)) { switch (methodExists(altName, fieldNode, false, 0)) { case EXISTS_BY_LOMBOK: return; @@ -221,8 +223,10 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { // Remember the type; lazy will change it JCExpression methodType = cloneType(treeMaker, 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)); + Name methodName = field.toName(toGetterName(field, accessors)); + boolean makeFinal = shouldMakeFinal(field, accessors); List<JCStatement> statements; JCTree toClearOfMarkers = null; @@ -260,6 +264,7 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { } if (isFieldDeprecated(field)) annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil())); + if (makeFinal) access |= Flags.FINAL; JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source); @@ -270,7 +275,6 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { } } decl.mods.annotations = decl.mods.annotations.appendList(delegates); - if (addSuppressWarningsUnchecked) { ListBuffer<JCExpression> suppressions = new ListBuffer<JCExpression>(); if (!Boolean.FALSE.equals(field.getAst().readConfiguration(ConfigurationKeys.ADD_SUPPRESSWARNINGS_ANNOTATIONS))) { diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index 1b675e8c..04fa8b77 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2021 The Project Lombok Authors. + * Copyright (C) 2009-2022 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 @@ -31,6 +31,7 @@ import lombok.AccessLevel; import lombok.ConfigurationKeys; import lombok.Setter; import lombok.core.AST.Kind; +import lombok.experimental.Accessors; import lombok.core.AnnotationValues; import lombok.javac.Javac; import lombok.javac.JavacAnnotationHandler; @@ -146,8 +147,9 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { return; } + AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(fieldNode); JCVariableDecl fieldDecl = (JCVariableDecl) fieldNode.get(); - String methodName = toSetterName(fieldNode); + String methodName = toSetterName(fieldNode, accessors); if (methodName == null) { fieldNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list."); @@ -159,7 +161,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { return; } - for (String altName : toAllSetterNames(fieldNode)) { + for (String altName : toAllSetterNames(fieldNode, accessors)) { switch (methodExists(altName, fieldNode, false, 1)) { case EXISTS_BY_LOMBOK: return; @@ -184,9 +186,11 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { } public static JCMethodDecl createSetter(long access, JavacNode field, JavacTreeMaker treeMaker, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { - String setterName = toSetterName(field); - boolean returnThis = shouldReturnThis(field); - return createSetter(access, false, field, treeMaker, setterName, null, null, returnThis, source, onMethod, onParam); + AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field); + String setterName = toSetterName(field, accessors); + boolean returnThis = shouldReturnThis(field, accessors); + JCMethodDecl setter = createSetter(access, false, field, treeMaker, setterName, null, null, returnThis, source, onMethod, onParam); + return setter; } public static JCMethodDecl createSetter(long access, boolean deprecate, JavacNode field, JavacTreeMaker treeMaker, String setterName, Name paramName, Name booleanFieldToSet, boolean shouldReturnThis, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) { @@ -198,8 +202,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { returnStatement = treeMaker.Return(treeMaker.Ident(field.toName("this"))); } - JCMethodDecl d = createSetter(access, deprecate, field, treeMaker, setterName, paramName, booleanFieldToSet, returnType, returnStatement, source, onMethod, onParam); - return d; + return createSetter(access, deprecate, field, treeMaker, setterName, paramName, booleanFieldToSet, returnType, returnStatement, source, onMethod, onParam); } public static JCMethodDecl createSetterWithRecv(long access, boolean deprecate, JavacNode field, JavacTreeMaker treeMaker, String setterName, Name paramName, Name booleanFieldToSet, boolean shouldReturnThis, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam, JCVariableDecl recv) { @@ -270,6 +273,8 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil())); } + AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field); + if (shouldMakeFinal(field, accessors)) access |= Flags.FINAL; JCMethodDecl methodDef; if (recv != null && treeMaker.hasMethodDefWithRecvParam()) { methodDef = treeMaker.MethodDefWithRecvParam(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, diff --git a/src/core/lombok/javac/handlers/HandleWith.java b/src/core/lombok/javac/handlers/HandleWith.java index 47f78b1e..c7fa0531 100644 --- a/src/core/lombok/javac/handlers/HandleWith.java +++ b/src/core/lombok/javac/handlers/HandleWith.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012-2021 The Project Lombok Authors. + * Copyright (C) 2012-2022 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 @@ -33,6 +33,7 @@ import lombok.With; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; import lombok.core.configuration.CheckerFrameworkVersion; +import lombok.experimental.Accessors; import lombok.javac.JavacAnnotationHandler; import lombok.javac.JavacNode; import lombok.javac.JavacTreeMaker; @@ -158,8 +159,9 @@ public class HandleWith extends JavacAnnotationHandler<With> { return; } + AnnotationValues<Accessors> accessors = getAccessorsForField(fieldNode); JCVariableDecl fieldDecl = (JCVariableDecl) fieldNode.get(); - String methodName = toWithName(fieldNode); + String methodName = toWithName(fieldNode, accessors); if (methodName == null) { fieldNode.addWarning("Not generating a withX method for this field: It does not fit your @Accessors prefix list."); @@ -187,7 +189,7 @@ public class HandleWith extends JavacAnnotationHandler<With> { return; } - for (String altName : toAllWithNames(fieldNode)) { + for (String altName : toAllWithNames(fieldNode, accessors)) { switch (methodExists(altName, fieldNode, false, 1)) { case EXISTS_BY_LOMBOK: return; @@ -282,7 +284,10 @@ public class HandleWith extends JavacAnnotationHandler<With> { if (isFieldDeprecated(field)) annsOnMethod = annsOnMethod.prepend(maker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil())); - if (makeAbstract) access = access | Flags.ABSTRACT; + if (makeAbstract) access |= Flags.ABSTRACT; + AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field); + boolean makeFinal = shouldMakeFinal(field, accessors); + if (makeFinal) access |= Flags.FINAL; JCMethodDecl decl = recursiveSetGeneratedBy(maker.MethodDef(maker.Modifiers(access, annsOnMethod), methodName, returnType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source); copyJavadoc(field, decl, CopyJavadoc.WITH); diff --git a/src/core/lombok/javac/handlers/HandleWithBy.java b/src/core/lombok/javac/handlers/HandleWithBy.java index 4ba4337e..ff67fd9f 100644 --- a/src/core/lombok/javac/handlers/HandleWithBy.java +++ b/src/core/lombok/javac/handlers/HandleWithBy.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2021 The Project Lombok Authors. + * Copyright (C) 2020-2022 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 @@ -34,6 +34,7 @@ import lombok.core.AST.Kind; import lombok.core.AnnotationValues; import lombok.core.LombokImmutableList; import lombok.core.configuration.CheckerFrameworkVersion; +import lombok.experimental.Accessors; import lombok.experimental.WithBy; import lombok.javac.Javac; import lombok.javac.JavacAnnotationHandler; @@ -158,8 +159,9 @@ public class HandleWithBy extends JavacAnnotationHandler<WithBy> { return; } + AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(fieldNode); JCVariableDecl fieldDecl = (JCVariableDecl) fieldNode.get(); - String methodName = toWithByName(fieldNode); + String methodName = toWithByName(fieldNode, accessors); if (methodName == null) { fieldNode.addWarning("Not generating a withXBy method for this field: It does not fit your @Accessors prefix list."); @@ -181,7 +183,7 @@ public class HandleWithBy extends JavacAnnotationHandler<WithBy> { return; } - for (String altName : toAllWithByNames(fieldNode)) { + for (String altName : toAllWithByNames(fieldNode, accessors)) { switch (methodExists(altName, fieldNode, false, 1)) { case EXISTS_BY_LOMBOK: return; @@ -326,6 +328,9 @@ public class HandleWithBy extends JavacAnnotationHandler<WithBy> { if (isFieldDeprecated(field)) annsOnMethod = annsOnMethod.prepend(maker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil())); if (makeAbstract) access = access | Flags.ABSTRACT; + AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field); + boolean makeFinal = shouldMakeFinal(field, accessors); + if (makeFinal) access |= Flags.FINAL; createRelevantNonNullAnnotation(source, param); JCMethodDecl decl = recursiveSetGeneratedBy(maker.MethodDef(maker.Modifiers(access, annsOnMethod), methodName, returnType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index d3532f79..53a518b4 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2021 The Project Lombok Authors. + * Copyright (C) 2009-2022 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 @@ -573,6 +573,14 @@ public class JavacHandlerUtil { } /** + * Translates the given field into all possible getter names. + * Convenient wrapper around {@link HandlerUtil#toAllGetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static java.util.List<String> toAllGetterNames(JavacNode field, AnnotationValues<Accessors> accessors) { + return HandlerUtil.toAllGetterNames(field.getAst(), accessors, field.getName(), isBoolean(field)); + } + + /** * @return the likely getter name for the stated field. (e.g. private boolean foo; to isFoo). * * Convenient wrapper around {@link HandlerUtil#toGetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. @@ -582,6 +590,15 @@ public class JavacHandlerUtil { } /** + * @return the likely getter name for the stated field. (e.g. private boolean foo; to isFoo). + * + * Convenient wrapper around {@link HandlerUtil#toGetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static String toGetterName(JavacNode field, AnnotationValues<Accessors> accessors) { + return HandlerUtil.toGetterName(field.getAst(), accessors, field.getName(), isBoolean(field)); + } + + /** * Translates the given field into all possible setter names. * Convenient wrapper around {@link HandlerUtil#toAllSetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}. */ @@ -590,6 +607,14 @@ public class JavacHandlerUtil { } /** + * Translates the given field into all possible setter names. + * Convenient wrapper around {@link HandlerUtil#toAllSetterNames(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static java.util.List<String> toAllSetterNames(JavacNode field, AnnotationValues<Accessors> accessors) { + return HandlerUtil.toAllSetterNames(field.getAst(), accessors, field.getName(), isBoolean(field)); + } + + /** * @return the likely setter name for the stated field. (e.g. private boolean foo; to setFoo). * * Convenient wrapper around {@link HandlerUtil#toSetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. @@ -599,6 +624,15 @@ public class JavacHandlerUtil { } /** + * @return the likely setter name for the stated field. (e.g. private boolean foo; to setFoo). + * + * Convenient wrapper around {@link HandlerUtil#toSetterName(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static String toSetterName(JavacNode field, AnnotationValues<Accessors> accessors) { + return HandlerUtil.toSetterName(field.getAst(), accessors, field.getName(), isBoolean(field)); + } + + /** * Translates the given field into all possible with names. * Convenient wrapper around {@link HandlerUtil#toAllWithNames(lombok.core.AnnotationValues, CharSequence, boolean)}. */ @@ -607,6 +641,14 @@ public class JavacHandlerUtil { } /** + * Translates the given field into all possible with names. + * Convenient wrapper around {@link HandlerUtil#toAllWithNames(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static java.util.List<String> toAllWithNames(JavacNode field, AnnotationValues<Accessors> accessors) { + return HandlerUtil.toAllWithNames(field.getAst(), accessors, field.getName(), isBoolean(field)); + } + + /** * Translates the given field into all possible withBy names. * Convenient wrapper around {@link HandlerUtil#toAllWithByNames(lombok.core.AnnotationValues, CharSequence, boolean)}. */ @@ -615,6 +657,14 @@ public class JavacHandlerUtil { } /** + * Translates the given field into all possible withBy names. + * Convenient wrapper around {@link HandlerUtil#toAllWithByNames(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static java.util.List<String> toAllWithByNames(JavacNode field, AnnotationValues<Accessors> accessors) { + return HandlerUtil.toAllWithByNames(field.getAst(), accessors, field.getName(), isBoolean(field)); + } + + /** * @return the likely with name for the stated field. (e.g. private boolean foo; to withFoo). * * Convenient wrapper around {@link HandlerUtil#toWithName(lombok.core.AnnotationValues, CharSequence, boolean)}. @@ -624,6 +674,15 @@ public class JavacHandlerUtil { } /** + * @return the likely with name for the stated field. (e.g. private boolean foo; to withFoo). + * + * Convenient wrapper around {@link HandlerUtil#toWithName(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static String toWithName(JavacNode field, AnnotationValues<Accessors> accessors) { + return HandlerUtil.toWithName(field.getAst(), accessors, field.getName(), isBoolean(field)); + } + + /** * @return the likely withBy name for the stated field. (e.g. private boolean foo; to withFooBy). * * Convenient wrapper around {@link HandlerUtil#toWithByName(lombok.core.AnnotationValues, CharSequence, boolean)}. @@ -633,17 +692,33 @@ public class JavacHandlerUtil { } /** + * @return the likely withBy name for the stated field. (e.g. private boolean foo; to withFooBy). + * + * Convenient wrapper around {@link HandlerUtil#toWithByName(lombok.core.AnnotationValues, CharSequence, boolean)}. + */ + public static String toWithByName(JavacNode field, AnnotationValues<Accessors> accessors) { + return HandlerUtil.toWithByName(field.getAst(), accessors, field.getName(), isBoolean(field)); + } + + /** * When generating a setter, the setter either returns void (beanspec) or Self (fluent). * This method scans for the {@code Accessors} annotation to figure that out. */ - public static boolean shouldReturnThis(JavacNode field) { + public static boolean shouldReturnThis(JavacNode field, AnnotationValues<Accessors> accessors) { if ((((JCVariableDecl) field.get()).mods.flags & Flags.STATIC) != 0) return false; - AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field); - return HandlerUtil.shouldReturnThis0(accessors, field.getAst()); } + /** + * When generating a setter/getter/wither, should it be made final? + */ + public static boolean shouldMakeFinal(JavacNode field, AnnotationValues<Accessors> accessors) { + if ((((JCVariableDecl) field.get()).mods.flags & Flags.STATIC) != 0) return false; + + return HandlerUtil.shouldMakeFinal0(accessors, field.getAst()); + } + public static JCExpression cloneSelfType(JavacNode childOfType) { JavacNode typeNode = childOfType; JavacTreeMaker maker = childOfType.getTreeMaker(); @@ -696,9 +771,12 @@ public class JavacHandlerUtil { } public static AnnotationValues<Accessors> getAccessorsForField(JavacNode field) { + AnnotationValues<Accessors> values = null; + for (JavacNode node : field.down()) { if (annotationTypeMatches(Accessors.class, node)) { - return createAnnotation(Accessors.class, node); + values = createAnnotation(Accessors.class, node); + break; } } @@ -706,13 +784,15 @@ public class JavacHandlerUtil { while (current != null) { for (JavacNode node : current.down()) { if (annotationTypeMatches(Accessors.class, node)) { - return createAnnotation(Accessors.class, node); + AnnotationValues<Accessors> onType = createAnnotation(Accessors.class, node); + values = values == null ? onType : values.integrate(onType); + break; } } current = current.up(); } - return AnnotationValues.of(Accessors.class, field); + return values == null ? AnnotationValues.of(Accessors.class, field) : values; } /** @@ -2185,7 +2265,7 @@ public class JavacHandlerUtil { Javac.setDocComment(cu, n, javadoc); } }); - return shouldReturnThis(node) ? addReturnsThisIfNeeded(out) : out; + return shouldReturnThis(node, JavacHandlerUtil.getAccessorsForField(node)) ? addReturnsThisIfNeeded(out) : out; } } |