diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2018-09-04 01:47:59 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2018-09-11 01:59:19 +0200 |
commit | cc8370ab2d7b3ca15023364c99e53735e62e13d7 (patch) | |
tree | 984b7fa824760fb2040067500aaacc859ed68821 /src/core/lombok | |
parent | cd8434feee79c6e119de0a254e071c6c49a8938e (diff) | |
download | lombok-cc8370ab2d7b3ca15023364c99e53735e62e13d7.tar.gz lombok-cc8370ab2d7b3ca15023364c99e53735e62e13d7.tar.bz2 lombok-cc8370ab2d7b3ca15023364c99e53735e62e13d7.zip |
code review and fixes for the ‘copyable annotations’ setting concept.
Diffstat (limited to 'src/core/lombok')
14 files changed, 60 insertions, 34 deletions
diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java index beb0d1e0..89748f60 100644 --- a/src/core/lombok/ConfigurationKeys.java +++ b/src/core/lombok/ConfigurationKeys.java @@ -565,10 +565,10 @@ public class ConfigurationKeys { public static final ConfigurationKey<Boolean> STOP_BUBBLING = new ConfigurationKey<Boolean>("config.stopBubbling", "Tell the configuration system it should stop looking for other configuration files (default: false).") {}; /** - * lombok configuration: {@code lombok.copyAnnotations} += <String: fully-qualified annotation class name>. + * lombok configuration: {@code lombok.copyableAnnotations} += <String: fully-qualified annotation class name>. * - * Copy these annotations to getters, setters, etc. + * Copy these annotations to getters, setters, withers, builder-setters, etc. */ - public static final ConfigurationKey<List<String>> COPY_ANNOTATIONS = new ConfigurationKey<List<String>>("lombok.copyAnnotations", "Copy these annotations to getters, setters, etc.") {}; + public static final ConfigurationKey<List<String>> COPYABLE_ANNOTATIONS = new ConfigurationKey<List<String>>("lombok.copyableAnnotations", "Copy these annotations to getters, setters, withers, builder-setters, etc.") {}; } diff --git a/src/core/lombok/core/handlers/HandlerUtil.java b/src/core/lombok/core/handlers/HandlerUtil.java index 48c48c20..1694e305 100644 --- a/src/core/lombok/core/handlers/HandlerUtil.java +++ b/src/core/lombok/core/handlers/HandlerUtil.java @@ -230,9 +230,9 @@ public class HandlerUtil { public static final String DEFAULT_EXCEPTION_FOR_NON_NULL = "java.lang.NullPointerException"; - /** Returns the configuration value for ConfigurationKeys.COPY_ANNOTATIONS. */ - public static List<String> copyAnnotationNames(AST<?, ?, ?> ast) { - return ast.readConfiguration(ConfigurationKeys.COPY_ANNOTATIONS); + /** Returns the configuration value for ConfigurationKeys.COPYABLE_ANNOTATIONS. */ + public static List<String> getCopyableAnnotationNames(AST<?, ?, ?> ast) { + return ast.readConfiguration(ConfigurationKeys.COPYABLE_ANNOTATIONS); } /** diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 1e29764a..e7a58de3 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -672,6 +672,24 @@ public class EclipseHandlerUtil { } } + private static final Annotation[] EMPTY_ANNOTATIONS_ARRAY = new Annotation[0]; + + /** + * Searches the given field node for annotations and returns each one that matches the provided list of names. + */ + public static Annotation[] findExactAnnotations(AbstractVariableDeclaration field, List<String> names) { + List<Annotation> result = new ArrayList<Annotation>(); + if (field.annotations == null) return EMPTY_ANNOTATIONS_ARRAY; + for (Annotation annotation : field.annotations) { + TypeReference typeRef = annotation.type; + if (typeRef != null && typeRef.getTypeName() != null) { + String annoName = toQualifiedName(typeRef.getTypeName()); + if (names.contains(annoName)) result.add(annotation); + } + } + return result.toArray(EMPTY_ANNOTATIONS_ARRAY); + } + /** * Checks if the provided annotation type is likely to be the intended type for the given annotation node. * diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index 3e373a00..f05896ab 100644 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -202,8 +202,8 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> { Annotation[] nonNulls = findAnnotations(fd, NON_NULL_PATTERN); Annotation[] nullables = findAnnotations(fd, NULLABLE_PATTERN); - Annotation[] copyAnnotations = findExactAnnotations(fd, copyAnnotationNames(fieldNode.getAst())); - + Annotation[] copyAnnotations = findExactAnnotations(fd, getCopyableAnnotationNames(fieldNode.getAst())); + BuilderFieldData bfd = new BuilderFieldData(); bfd.rawName = fieldNode.getName().toCharArray(); bfd.name = removePrefixFromField(fieldNode); @@ -380,9 +380,14 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> { if (param.getKind() != Kind.ARGUMENT) continue; BuilderFieldData bfd = new BuilderFieldData(); Argument arg = (Argument) param.get(); + + Annotation[] nonNulls = findAnnotations(arg, NON_NULL_PATTERN); + Annotation[] nullables = findAnnotations(arg, NULLABLE_PATTERN); + Annotation[] copyAnnotations = findExactAnnotations(arg, getCopyableAnnotationNames(param.getAst())); + bfd.rawName = arg.name; bfd.name = arg.name; - bfd.annotations = arg.annotations; + bfd.annotations = copyAnnotations(arg, nonNulls, nullables, copyAnnotations); bfd.type = arg.type; bfd.singularData = getSingularData(param, ast); bfd.originalFieldNode = param; @@ -774,7 +779,7 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> { String setterName = fluent ? fieldNode.getName() : HandlerUtil.buildAccessorName("set", fieldNode.getName()); MethodDeclaration setter = HandleSetter.createSetter(td, deprecate, fieldNode, setterName, nameOfSetFlag, chain, ClassFileConstants.AccPublic, - sourceNode, Collections.<Annotation>emptyList(), annotations != null ? Arrays.asList(annotations) : Collections.<Annotation>emptyList()); + sourceNode, Collections.<Annotation>emptyList(), annotations != null ? Arrays.asList(copyAnnotations(sourceNode.get(), annotations)) : Collections.<Annotation>emptyList()); injectMethod(builderType, setter); } diff --git a/src/core/lombok/eclipse/handlers/HandleConstructor.java b/src/core/lombok/eclipse/handlers/HandleConstructor.java index cb9c2b4b..3d947a73 100644 --- a/src/core/lombok/eclipse/handlers/HandleConstructor.java +++ b/src/core/lombok/eclipse/handlers/HandleConstructor.java @@ -405,7 +405,7 @@ public class HandleConstructor { Argument parameter = new Argument(fieldName, fieldPos, copyType(field.type, source), Modifier.FINAL); Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); - Annotation[] copyAnnotations = findExactAnnotations(field, copyAnnotationNames(fieldNode.getAst())); + Annotation[] copyAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(fieldNode.getAst())); if (nonNulls.length != 0) { Statement nullCheck = generateNullCheck(parameter, sourceNode); if (nullCheck != null) nullChecks.add(nullCheck); diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java index 45ddb2cc..58af8c1e 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/handlers/HandleGetter.java @@ -274,7 +274,7 @@ public class HandleGetter extends EclipseAnnotationHandler<Getter> { onMethod.toArray(new Annotation[0]), findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN), - findExactAnnotations(field, copyAnnotationNames(fieldNode.getAst())), + findExactAnnotations(field, getCopyableAnnotationNames(fieldNode.getAst())), findDelegatesAndMarkAsHandled(fieldNode), deprecated); } diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java index bd34b313..ca81fef7 100644 --- a/src/core/lombok/eclipse/handlers/HandleSetter.java +++ b/src/core/lombok/eclipse/handlers/HandleSetter.java @@ -238,7 +238,7 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); - Annotation[] copyAnnotations = findExactAnnotations(field, copyAnnotationNames(fieldNode.getAst())); + Annotation[] copyableAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(fieldNode.getAst())); List<Statement> statements = new ArrayList<Statement>(5); if (nonNulls.length == 0) { statements.add(assignment); @@ -256,7 +256,7 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { statements.add(returnStatement); } method.statements = statements.toArray(new Statement[0]); - param.annotations = copyAnnotations(source, nonNulls, nullables, copyAnnotations, onParam.toArray(new Annotation[0])); + param.annotations = copyAnnotations(source, nonNulls, nullables, copyableAnnotations, onParam.toArray(new Annotation[0])); method.traverse(new SetGeneratedByVisitor(source), parent.scope); return method; diff --git a/src/core/lombok/eclipse/handlers/HandleWither.java b/src/core/lombok/eclipse/handlers/HandleWither.java index e9831ce1..11032e9c 100644 --- a/src/core/lombok/eclipse/handlers/HandleWither.java +++ b/src/core/lombok/eclipse/handlers/HandleWither.java @@ -242,7 +242,7 @@ public class HandleWither extends EclipseAnnotationHandler<Wither> { Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); - Annotation[] copyAnnotations = findExactAnnotations(field, copyAnnotationNames(fieldNode.getAst())); + Annotation[] copyableAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(fieldNode.getAst())); if (!makeAbstract) { List<Expression> args = new ArrayList<Expression>(); @@ -286,7 +286,7 @@ public class HandleWither extends EclipseAnnotationHandler<Wither> { method.statements = statements.toArray(new Statement[0]); } - param.annotations = copyAnnotations(source, nonNulls, nullables, copyAnnotations, onParam.toArray(new Annotation[0])); + param.annotations = copyAnnotations(source, nonNulls, nullables, copyableAnnotations, onParam.toArray(new Annotation[0])); method.traverse(new SetGeneratedByVisitor(source), parent.scope); return method; diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index 75bfb01c..7577eeb2 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -149,15 +149,15 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { JCVariableDecl fd = (JCVariableDecl) fieldNode.get(); JavacNode isDefault = findAnnotation(Builder.Default.class, fieldNode, false); boolean isFinal = (fd.mods.flags & Flags.FINAL) != 0 || (valuePresent && !hasAnnotation(NonFinal.class, fieldNode)); - + List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); - List<JCAnnotation> copyAnnotations = findExactAnnotations(fieldNode, copyAnnotationNames(fieldNode.getAst())); - + List<JCAnnotation> copyableAnnotations = findExactAnnotations(fieldNode, getCopyableAnnotationNames(fieldNode.getAst())); + BuilderFieldData bfd = new BuilderFieldData(); bfd.rawName = fd.name; bfd.name = removePrefixFromField(fieldNode); - bfd.annotations = nonNulls.appendList(nullables).appendList(copyAnnotations); + bfd.annotations = nonNulls.appendList(nullables).appendList(copyableAnnotations); bfd.type = fd.vartype; bfd.singularData = getSingularData(fieldNode); bfd.originalFieldNode = fieldNode; @@ -333,10 +333,15 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { for (JavacNode param : fillParametersFrom.down()) { if (param.getKind() != Kind.ARGUMENT) continue; BuilderFieldData bfd = new BuilderFieldData(); + + List<JCAnnotation> nonNulls = findAnnotations(param, NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(param, NULLABLE_PATTERN); + List<JCAnnotation> copyableAnnotations = findExactAnnotations(param, getCopyableAnnotationNames(param.getAst())); + JCVariableDecl raw = (JCVariableDecl) param.get(); bfd.name = raw.name; bfd.rawName = raw.name; - bfd.annotations = raw.mods.annotations; + bfd.annotations = nonNulls.appendList(nullables).appendList(copyableAnnotations); bfd.type = raw.vartype; bfd.singularData = getSingularData(param); bfd.originalFieldNode = param; diff --git a/src/core/lombok/javac/handlers/HandleConstructor.java b/src/core/lombok/javac/handlers/HandleConstructor.java index b0fa3d29..1e45d73f 100644 --- a/src/core/lombok/javac/handlers/HandleConstructor.java +++ b/src/core/lombok/javac/handlers/HandleConstructor.java @@ -331,9 +331,9 @@ public class HandleConstructor { Name rawName = field.name; List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); - List<JCAnnotation> copyAnnotations = findExactAnnotations(fieldNode, copyAnnotationNames(fieldNode.getAst())); + List<JCAnnotation> copyableAnnotations = findExactAnnotations(fieldNode, getCopyableAnnotationNames(fieldNode.getAst())); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext()); - JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables).appendList(copyAnnotations)), fieldName, field.vartype, null); + JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables).appendList(copyableAnnotations)), fieldName, field.vartype, null); params.append(param); if (!nonNulls.isEmpty()) { JCStatement nullCheck = generateNullCheck(maker, fieldNode, param, source); diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 6e4b2685..04b300a8 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -245,11 +245,11 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); - List<JCAnnotation> copyAnnotations = findExactAnnotations(field, copyAnnotationNames(field.getAst())); + List<JCAnnotation> copyableAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(field.getAst())); List<JCAnnotation> delegates = findDelegatesAndRemoveFromField(field); - List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod).appendList(nonNulls).appendList(nullables).appendList(copyAnnotations); + List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod).appendList(nonNulls).appendList(nullables).appendList(copyableAnnotations); if (isFieldDeprecated(field)) { annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil())); } diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index 631ea193..e5e9481d 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -228,10 +228,10 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); - List<JCAnnotation> copyAnnotations = findExactAnnotations(field, copyAnnotationNames(field.getAst())); + List<JCAnnotation> copyableAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(field.getAst())); Name methodName = field.toName(setterName); - List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables).appendList(copyAnnotations); + List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables).appendList(copyableAnnotations); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, field.getContext()); JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(flags, annsOnParam), fieldDecl.name, fieldDecl.vartype, null); diff --git a/src/core/lombok/javac/handlers/HandleWither.java b/src/core/lombok/javac/handlers/HandleWither.java index e6fc2d02..b0e32d61 100644 --- a/src/core/lombok/javac/handlers/HandleWither.java +++ b/src/core/lombok/javac/handlers/HandleWither.java @@ -224,7 +224,7 @@ public class HandleWither extends JavacAnnotationHandler<Wither> { List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); - List<JCAnnotation> copyAnnotations = findExactAnnotations(field, copyAnnotationNames(field.getAst())); + List<JCAnnotation> copyableAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(field.getAst())); Name methodName = field.toName(witherName); @@ -232,7 +232,7 @@ public class HandleWither extends JavacAnnotationHandler<Wither> { JCBlock methodBody = null; long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, field.getContext()); - List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables).appendList(copyAnnotations); + List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables).appendList(copyableAnnotations); JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, annsOnParam), fieldDecl.name, fieldDecl.vartype, null); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index cb729183..1cc28072 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -1343,15 +1343,13 @@ public class JavacHandlerUtil { for (JavacNode child : fieldNode.down()) { if (child.getKind() == Kind.ANNOTATION) { JCAnnotation annotation = (JCAnnotation) child.get(); - String annoname = annotation.annotationType.toString(); - if (names.contains(annoname)) { - result.append(annotation); - } + String annoName = annotation.annotationType.toString(); + if (names.contains(annoName)) result.append(annotation); } } return result.toList(); } - + /** * Generates a new statement that checks if the given variable is null, and if so, throws a configured exception with the * variable name as message. |