From cc8370ab2d7b3ca15023364c99e53735e62e13d7 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 4 Sep 2018 01:47:59 +0200 Subject: code review and fixes for the ‘copyable annotations’ setting concept. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lombok/eclipse/handlers/EclipseHandlerUtil.java | 18 ++++++++++++++++++ src/core/lombok/eclipse/handlers/HandleBuilder.java | 13 +++++++++---- .../lombok/eclipse/handlers/HandleConstructor.java | 2 +- src/core/lombok/eclipse/handlers/HandleGetter.java | 2 +- src/core/lombok/eclipse/handlers/HandleSetter.java | 4 ++-- src/core/lombok/eclipse/handlers/HandleWither.java | 4 ++-- 6 files changed, 33 insertions(+), 10 deletions(-) (limited to 'src/core/lombok/eclipse') 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 names) { + List result = new ArrayList(); + 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 { 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 { 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 { String setterName = fluent ? fieldNode.getName() : HandlerUtil.buildAccessorName("set", fieldNode.getName()); MethodDeclaration setter = HandleSetter.createSetter(td, deprecate, fieldNode, setterName, nameOfSetFlag, chain, ClassFileConstants.AccPublic, - sourceNode, Collections.emptyList(), annotations != null ? Arrays.asList(annotations) : Collections.emptyList()); + sourceNode, Collections.emptyList(), annotations != null ? Arrays.asList(copyAnnotations(sourceNode.get(), annotations)) : Collections.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 { 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 { 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 statements = new ArrayList(5); if (nonNulls.length == 0) { statements.add(assignment); @@ -256,7 +256,7 @@ public class HandleSetter extends EclipseAnnotationHandler { 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 { 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 args = new ArrayList(); @@ -286,7 +286,7 @@ public class HandleWither extends EclipseAnnotationHandler { 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; -- cgit