From 7a575e1e5d1c78fa5be1deca7fc308bd9eb390dd Mon Sep 17 00:00:00 2001 From: Werner Dietl Date: Mon, 28 May 2018 13:32:49 -0400 Subject: Copy all field annotations to the corresponding builder parameter. Maybe some filtering is necessary and should be added later. --- src/core/lombok/javac/handlers/HandleBuilder.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/core') diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index 1a471029..335dee1a 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -85,6 +85,7 @@ public class HandleBuilder extends JavacAnnotationHandler { } private static class BuilderFieldData { + List annotations; JCExpression type; Name rawName; Name name; @@ -151,6 +152,7 @@ public class HandleBuilder extends JavacAnnotationHandler { BuilderFieldData bfd = new BuilderFieldData(); bfd.rawName = fd.name; bfd.name = removePrefixFromField(fieldNode); + bfd.annotations = fd.mods.annotations; bfd.type = fd.vartype; bfd.singularData = getSingularData(fieldNode); bfd.originalFieldNode = fieldNode; @@ -329,6 +331,7 @@ public class HandleBuilder extends JavacAnnotationHandler { JCVariableDecl raw = (JCVariableDecl) param.get(); bfd.name = raw.name; bfd.rawName = raw.name; + bfd.annotations = raw.mods.annotations; bfd.type = raw.vartype; bfd.singularData = getSingularData(param); bfd.originalFieldNode = param; @@ -678,13 +681,13 @@ public class HandleBuilder extends JavacAnnotationHandler { public void makeSetterMethodsForBuilder(JavacNode builderType, BuilderFieldData fieldNode, JavacNode source, boolean fluent, boolean chain) { boolean deprecate = isFieldDeprecated(fieldNode.originalFieldNode); if (fieldNode.singularData == null || fieldNode.singularData.getSingularizer() == null) { - makeSimpleSetterMethodForBuilder(builderType, deprecate, fieldNode.createdFields.get(0), fieldNode.nameOfSetFlag, source, fluent, chain); + makeSimpleSetterMethodForBuilder(builderType, deprecate, fieldNode.createdFields.get(0), fieldNode.nameOfSetFlag, source, fluent, chain, fieldNode.annotations); } else { fieldNode.singularData.getSingularizer().generateMethods(fieldNode.singularData, deprecate, builderType, source.get(), fluent, chain); } } - private void makeSimpleSetterMethodForBuilder(JavacNode builderType, boolean deprecate, JavacNode fieldNode, Name nameOfSetFlag, JavacNode source, boolean fluent, boolean chain) { + private void makeSimpleSetterMethodForBuilder(JavacNode builderType, boolean deprecate, JavacNode fieldNode, Name nameOfSetFlag, JavacNode source, boolean fluent, boolean chain, List annosOnParam) { Name fieldName = ((JCVariableDecl) fieldNode.get()).name; for (JavacNode child : builderType.down()) { @@ -698,7 +701,7 @@ public class HandleBuilder extends JavacAnnotationHandler { JavacTreeMaker maker = fieldNode.getTreeMaker(); - JCMethodDecl newMethod = HandleSetter.createSetter(Flags.PUBLIC, deprecate, fieldNode, maker, setterName, nameOfSetFlag, chain, source, List.nil(), List.nil()); + JCMethodDecl newMethod = HandleSetter.createSetter(Flags.PUBLIC, deprecate, fieldNode, maker, setterName, nameOfSetFlag, chain, source, List.nil(), annosOnParam); injectMethod(builderType, newMethod); } -- cgit From 79bfcc4f7ae4bcd61f7f942bfefb940c77927b71 Mon Sep 17 00:00:00 2001 From: Werner Dietl Date: Mon, 27 Aug 2018 18:36:13 -0400 Subject: Add configuration key, handle whereever NULLABLE is handled, support Eclipse. --- src/core/lombok/ConfigurationKeys.java | 8 ++++++++ src/core/lombok/core/handlers/HandlerUtil.java | 5 +++++ src/core/lombok/eclipse/handlers/HandleBuilder.java | 12 +++++++++--- src/core/lombok/eclipse/handlers/HandleConstructor.java | 3 ++- src/core/lombok/eclipse/handlers/HandleGetter.java | 1 + src/core/lombok/eclipse/handlers/HandleSetter.java | 3 ++- src/core/lombok/eclipse/handlers/HandleWither.java | 3 ++- src/core/lombok/javac/handlers/HandleBuilder.java | 7 ++++++- src/core/lombok/javac/handlers/HandleConstructor.java | 3 ++- src/core/lombok/javac/handlers/HandleGetter.java | 3 ++- src/core/lombok/javac/handlers/HandleSetter.java | 3 ++- src/core/lombok/javac/handlers/HandleWither.java | 3 ++- src/core/lombok/javac/handlers/JavacHandlerUtil.java | 17 +++++++++++++++++ 13 files changed, 60 insertions(+), 11 deletions(-) (limited to 'src/core') diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java index 184ded27..beb0d1e0 100644 --- a/src/core/lombok/ConfigurationKeys.java +++ b/src/core/lombok/ConfigurationKeys.java @@ -563,4 +563,12 @@ public class ConfigurationKeys { * If set to {@code true}, no further {@code lombok.config} files will be checked. */ public static final ConfigurationKey STOP_BUBBLING = new ConfigurationKey("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>. + * + * Copy these annotations to getters, setters, etc. + */ + public static final ConfigurationKey> COPY_ANNOTATIONS = new ConfigurationKey>("lombok.copyAnnotations", "Copy these annotations to getters, setters, etc.") {}; + } diff --git a/src/core/lombok/core/handlers/HandlerUtil.java b/src/core/lombok/core/handlers/HandlerUtil.java index 0d64c550..48c48c20 100644 --- a/src/core/lombok/core/handlers/HandlerUtil.java +++ b/src/core/lombok/core/handlers/HandlerUtil.java @@ -230,6 +230,11 @@ 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 copyAnnotationNames(AST ast) { + return ast.readConfiguration(ConfigurationKeys.COPY_ANNOTATIONS); + } + /** * Generates a getter name from a given field name. * diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index 4d20f052..77c29634 100644 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -106,6 +106,7 @@ public class HandleBuilder extends EclipseAnnotationHandler { } private static class BuilderFieldData { + Annotation[] annotations; TypeReference type; char[] rawName; char[] name; @@ -199,9 +200,14 @@ public class HandleBuilder extends EclipseAnnotationHandler { EclipseNode isDefault = findAnnotation(Builder.Default.class, fieldNode); boolean isFinal = ((fd.modifiers & ClassFileConstants.AccFinal) != 0) || (valuePresent && !hasAnnotation(NonFinal.class, fieldNode)); + Annotation[] nonNulls = findAnnotations(fd, NON_NULL_PATTERN); + Annotation[] nullables = findAnnotations(fd, NULLABLE_PATTERN); + Annotation[] copyAnnotations = findExactAnnotations(fd, copyAnnotationNames(fieldNode.getAst())); + BuilderFieldData bfd = new BuilderFieldData(); bfd.rawName = fieldNode.getName().toCharArray(); bfd.name = removePrefixFromField(fieldNode); + bfd.annotations = copyAnnotations(fd, nonNulls, nullables, copyAnnotations); bfd.type = fd.type; bfd.singularData = getSingularData(fieldNode, ast); bfd.originalFieldNode = fieldNode; @@ -744,13 +750,13 @@ public class HandleBuilder extends EclipseAnnotationHandler { public void makeSetterMethodsForBuilder(EclipseNode builderType, BuilderFieldData bfd, EclipseNode sourceNode, boolean fluent, boolean chain) { boolean deprecate = isFieldDeprecated(bfd.originalFieldNode); if (bfd.singularData == null || bfd.singularData.getSingularizer() == null) { - makeSimpleSetterMethodForBuilder(builderType, deprecate, bfd.createdFields.get(0), bfd.nameOfSetFlag, sourceNode, fluent, chain); + makeSimpleSetterMethodForBuilder(builderType, deprecate, bfd.createdFields.get(0), bfd.nameOfSetFlag, sourceNode, fluent, chain, bfd.annotations); } else { bfd.singularData.getSingularizer().generateMethods(bfd.singularData, deprecate, builderType, fluent, chain); } } - private void makeSimpleSetterMethodForBuilder(EclipseNode builderType, boolean deprecate, EclipseNode fieldNode, char[] nameOfSetFlag, EclipseNode sourceNode, boolean fluent, boolean chain) { + private void makeSimpleSetterMethodForBuilder(EclipseNode builderType, boolean deprecate, EclipseNode fieldNode, char[] nameOfSetFlag, EclipseNode sourceNode, boolean fluent, boolean chain, Annotation[] annotations) { TypeDeclaration td = (TypeDeclaration) builderType.get(); AbstractMethodDeclaration[] existing = td.methods; if (existing == null) existing = EMPTY; @@ -767,7 +773,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(), Collections.emptyList()); + sourceNode, Collections.emptyList(), Arrays.asList(annotations)); injectMethod(builderType, setter); } diff --git a/src/core/lombok/eclipse/handlers/HandleConstructor.java b/src/core/lombok/eclipse/handlers/HandleConstructor.java index 6c7b4caf..cb9c2b4b 100644 --- a/src/core/lombok/eclipse/handlers/HandleConstructor.java +++ b/src/core/lombok/eclipse/handlers/HandleConstructor.java @@ -405,11 +405,12 @@ 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())); if (nonNulls.length != 0) { Statement nullCheck = generateNullCheck(parameter, sourceNode); if (nullCheck != null) nullChecks.add(nullCheck); } - parameter.annotations = copyAnnotations(source, nonNulls, nullables); + parameter.annotations = copyAnnotations(source, nonNulls, nullables, copyAnnotations); params.add(parameter); } diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java index d0c2cc23..45ddb2cc 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/handlers/HandleGetter.java @@ -274,6 +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())), findDelegatesAndMarkAsHandled(fieldNode), deprecated); } diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java index d4df0deb..bd34b313 100644 --- a/src/core/lombok/eclipse/handlers/HandleSetter.java +++ b/src/core/lombok/eclipse/handlers/HandleSetter.java @@ -238,6 +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())); List statements = new ArrayList(5); if (nonNulls.length == 0) { statements.add(assignment); @@ -255,7 +256,7 @@ public class HandleSetter extends EclipseAnnotationHandler { statements.add(returnStatement); } method.statements = statements.toArray(new Statement[0]); - param.annotations = copyAnnotations(source, nonNulls, nullables, onParam.toArray(new Annotation[0])); + param.annotations = copyAnnotations(source, nonNulls, nullables, copyAnnotations, 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 c035fc26..e9831ce1 100644 --- a/src/core/lombok/eclipse/handlers/HandleWither.java +++ b/src/core/lombok/eclipse/handlers/HandleWither.java @@ -242,6 +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())); if (!makeAbstract) { List args = new ArrayList(); @@ -285,7 +286,7 @@ public class HandleWither extends EclipseAnnotationHandler { method.statements = statements.toArray(new Statement[0]); } - param.annotations = copyAnnotations(source, nonNulls, nullables, onParam.toArray(new Annotation[0])); + param.annotations = copyAnnotations(source, nonNulls, nullables, copyAnnotations, 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 335dee1a..75bfb01c 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -149,10 +149,15 @@ public class HandleBuilder extends JavacAnnotationHandler { 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 nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); + List nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); + List copyAnnotations = findExactAnnotations(fieldNode, copyAnnotationNames(fieldNode.getAst())); + BuilderFieldData bfd = new BuilderFieldData(); bfd.rawName = fd.name; bfd.name = removePrefixFromField(fieldNode); - bfd.annotations = fd.mods.annotations; + bfd.annotations = nonNulls.appendList(nullables).appendList(copyAnnotations); bfd.type = fd.vartype; bfd.singularData = getSingularData(fieldNode); bfd.originalFieldNode = fieldNode; diff --git a/src/core/lombok/javac/handlers/HandleConstructor.java b/src/core/lombok/javac/handlers/HandleConstructor.java index 76caaffe..b0fa3d29 100644 --- a/src/core/lombok/javac/handlers/HandleConstructor.java +++ b/src/core/lombok/javac/handlers/HandleConstructor.java @@ -331,8 +331,9 @@ public class HandleConstructor { Name rawName = field.name; List nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); List nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); + List copyAnnotations = findExactAnnotations(fieldNode, copyAnnotationNames(fieldNode.getAst())); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext()); - JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables)), fieldName, field.vartype, null); + JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables).appendList(copyAnnotations)), 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 4fc6155c..6e4b2685 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -245,10 +245,11 @@ public class HandleGetter extends JavacAnnotationHandler { List nonNulls = findAnnotations(field, NON_NULL_PATTERN); List nullables = findAnnotations(field, NULLABLE_PATTERN); + List copyAnnotations = findExactAnnotations(field, copyAnnotationNames(field.getAst())); List delegates = findDelegatesAndRemoveFromField(field); - List annsOnMethod = copyAnnotations(onMethod).appendList(nonNulls).appendList(nullables); + List annsOnMethod = copyAnnotations(onMethod).appendList(nonNulls).appendList(nullables).appendList(copyAnnotations); if (isFieldDeprecated(field)) { annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.nil())); } diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index 0ddaa7d7..631ea193 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -228,9 +228,10 @@ public class HandleSetter extends JavacAnnotationHandler { ListBuffer statements = new ListBuffer(); List nonNulls = findAnnotations(field, NON_NULL_PATTERN); List nullables = findAnnotations(field, NULLABLE_PATTERN); + List copyAnnotations = findExactAnnotations(field, copyAnnotationNames(field.getAst())); Name methodName = field.toName(setterName); - List annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables); + List annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables).appendList(copyAnnotations); 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 87f3c16a..e6fc2d02 100644 --- a/src/core/lombok/javac/handlers/HandleWither.java +++ b/src/core/lombok/javac/handlers/HandleWither.java @@ -224,6 +224,7 @@ public class HandleWither extends JavacAnnotationHandler { List nonNulls = findAnnotations(field, NON_NULL_PATTERN); List nullables = findAnnotations(field, NULLABLE_PATTERN); + List copyAnnotations = findExactAnnotations(field, copyAnnotationNames(field.getAst())); Name methodName = field.toName(witherName); @@ -231,7 +232,7 @@ public class HandleWither extends JavacAnnotationHandler { JCBlock methodBody = null; long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, field.getContext()); - List annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables); + List annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables).appendList(copyAnnotations); 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 92c642d4..cb729183 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -1335,6 +1335,23 @@ public class JavacHandlerUtil { return result.toList(); } + /** + * Searches the given field node for annotations and returns each one that matches the provided list of names. + */ + public static List findExactAnnotations(JavacNode fieldNode, java.util.List names) { + ListBuffer result = new ListBuffer(); + 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); + } + } + } + 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. -- cgit From fc35839c793dfe2e3294c0f526dcc4cb5741533b Mon Sep 17 00:00:00 2001 From: Werner Dietl Date: Mon, 27 Aug 2018 18:56:16 -0400 Subject: Copy annotations in Eclipse HandleBuilder. --- src/core/lombok/eclipse/handlers/HandleBuilder.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src/core') diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index 77c29634..167d1ba0 100644 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -382,6 +382,7 @@ public class HandleBuilder extends EclipseAnnotationHandler { Argument arg = (Argument) param.get(); bfd.rawName = arg.name; bfd.name = arg.name; + bfd.annotations = arg.annotations; bfd.type = arg.type; bfd.singularData = getSingularData(param, ast); bfd.originalFieldNode = param; -- cgit From 65fc9897b3c2183cef305a71cf6b064fb9756253 Mon Sep 17 00:00:00 2001 From: Werner Dietl Date: Thu, 30 Aug 2018 23:17:29 -0400 Subject: Handle null annotation array correctly. --- src/core/lombok/eclipse/handlers/HandleBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core') diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index 167d1ba0..3e373a00 100644 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -774,7 +774,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(), Arrays.asList(annotations)); + sourceNode, Collections.emptyList(), annotations != null ? Arrays.asList(annotations) : Collections.emptyList()); injectMethod(builderType, setter); } -- cgit 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 --- src/core/lombok/ConfigurationKeys.java | 6 +++--- src/core/lombok/core/handlers/HandlerUtil.java | 6 +++--- .../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 ++-- src/core/lombok/javac/handlers/HandleBuilder.java | 15 ++++++++++----- src/core/lombok/javac/handlers/HandleConstructor.java | 4 ++-- src/core/lombok/javac/handlers/HandleGetter.java | 4 ++-- src/core/lombok/javac/handlers/HandleSetter.java | 4 ++-- src/core/lombok/javac/handlers/HandleWither.java | 4 ++-- src/core/lombok/javac/handlers/JavacHandlerUtil.java | 8 +++----- 14 files changed, 60 insertions(+), 34 deletions(-) (limited to 'src/core') 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 STOP_BUBBLING = new ConfigurationKey("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> COPY_ANNOTATIONS = new ConfigurationKey>("lombok.copyAnnotations", "Copy these annotations to getters, setters, etc.") {}; + public static final ConfigurationKey> COPYABLE_ANNOTATIONS = new ConfigurationKey>("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 copyAnnotationNames(AST ast) { - return ast.readConfiguration(ConfigurationKeys.COPY_ANNOTATIONS); + /** Returns the configuration value for ConfigurationKeys.COPYABLE_ANNOTATIONS. */ + public static List 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 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; 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 { 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 nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); List nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); - List copyAnnotations = findExactAnnotations(fieldNode, copyAnnotationNames(fieldNode.getAst())); - + List 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 { for (JavacNode param : fillParametersFrom.down()) { if (param.getKind() != Kind.ARGUMENT) continue; BuilderFieldData bfd = new BuilderFieldData(); + + List nonNulls = findAnnotations(param, NON_NULL_PATTERN); + List nullables = findAnnotations(param, NULLABLE_PATTERN); + List 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 nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); List nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); - List copyAnnotations = findExactAnnotations(fieldNode, copyAnnotationNames(fieldNode.getAst())); + List 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 { List nonNulls = findAnnotations(field, NON_NULL_PATTERN); List nullables = findAnnotations(field, NULLABLE_PATTERN); - List copyAnnotations = findExactAnnotations(field, copyAnnotationNames(field.getAst())); + List copyableAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(field.getAst())); List delegates = findDelegatesAndRemoveFromField(field); - List annsOnMethod = copyAnnotations(onMethod).appendList(nonNulls).appendList(nullables).appendList(copyAnnotations); + List annsOnMethod = copyAnnotations(onMethod).appendList(nonNulls).appendList(nullables).appendList(copyableAnnotations); if (isFieldDeprecated(field)) { annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.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 { ListBuffer statements = new ListBuffer(); List nonNulls = findAnnotations(field, NON_NULL_PATTERN); List nullables = findAnnotations(field, NULLABLE_PATTERN); - List copyAnnotations = findExactAnnotations(field, copyAnnotationNames(field.getAst())); + List copyableAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(field.getAst())); Name methodName = field.toName(setterName); - List annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables).appendList(copyAnnotations); + List 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 { List nonNulls = findAnnotations(field, NON_NULL_PATTERN); List nullables = findAnnotations(field, NULLABLE_PATTERN); - List copyAnnotations = findExactAnnotations(field, copyAnnotationNames(field.getAst())); + List copyableAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(field.getAst())); Name methodName = field.toName(witherName); @@ -232,7 +232,7 @@ public class HandleWither extends JavacAnnotationHandler { JCBlock methodBody = null; long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, field.getContext()); - List annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables).appendList(copyAnnotations); + List 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. -- cgit From d7873f2d21564e8e7f22409fe03681d7dd4c8c1e Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 11 Sep 2018 01:41:22 +0200 Subject: Replaced the notion of ‘nullable’ and ‘nonnull’ get copied to ‘any ‘copyable’ annotations get copied’, with ‘copyable’ defined as a specific FQN-style list of well-known nullity-indicating annotations, plus whatever you configured in lombok.config. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also some work on the notion of TYPE_USE annotations. --- src/core/lombok/ConfigurationKeys.java | 5 +- src/core/lombok/NonNull.java | 2 +- src/core/lombok/core/TypeLibrary.java | 6 +++ src/core/lombok/core/handlers/HandlerUtil.java | 39 +++++++++++----- .../eclipse/handlers/EclipseHandlerUtil.java | 36 ++++++++++++--- .../lombok/eclipse/handlers/HandleBuilder.java | 12 ++--- .../lombok/eclipse/handlers/HandleConstructor.java | 12 ++--- src/core/lombok/eclipse/handlers/HandleGetter.java | 12 ++--- src/core/lombok/eclipse/handlers/HandleSetter.java | 8 ++-- .../eclipse/handlers/HandleSuperBuilder.java | 9 ++-- src/core/lombok/eclipse/handlers/HandleWither.java | 8 ++-- src/core/lombok/javac/handlers/HandleBuilder.java | 12 +---- .../lombok/javac/handlers/HandleConstructor.java | 15 +++--- src/core/lombok/javac/handlers/HandleGetter.java | 8 +--- src/core/lombok/javac/handlers/HandleSetter.java | 8 ++-- .../lombok/javac/handlers/HandleSuperBuilder.java | 3 +- src/core/lombok/javac/handlers/HandleWither.java | 8 ++-- .../lombok/javac/handlers/JavacHandlerUtil.java | 53 ++++++++++++++++++++-- 18 files changed, 154 insertions(+), 102 deletions(-) (limited to 'src/core') diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java index 89748f60..2cca27fa 100644 --- a/src/core/lombok/ConfigurationKeys.java +++ b/src/core/lombok/ConfigurationKeys.java @@ -27,6 +27,7 @@ import lombok.core.configuration.CallSuperType; import lombok.core.configuration.ConfigurationKey; import lombok.core.configuration.FlagUsageType; import lombok.core.configuration.NullCheckExceptionType; +import lombok.core.configuration.TypeName; /** * A container class containing all lombok configuration keys that do not belong to a specific annotation. @@ -565,10 +566,10 @@ public class ConfigurationKeys { public static final ConfigurationKey STOP_BUBBLING = new ConfigurationKey("config.stopBubbling", "Tell the configuration system it should stop looking for other configuration files (default: false).") {}; /** - * lombok configuration: {@code lombok.copyableAnnotations} += <String: fully-qualified annotation class name>. + * lombok configuration: {@code lombok.copyableAnnotations} += <TypeName: fully-qualified annotation class name>. * * Copy these annotations to getters, setters, withers, builder-setters, etc. */ - public static final ConfigurationKey> COPYABLE_ANNOTATIONS = new ConfigurationKey>("lombok.copyableAnnotations", "Copy these annotations to getters, setters, withers, builder-setters, etc.") {}; + public static final ConfigurationKey> COPYABLE_ANNOTATIONS = new ConfigurationKey>("lombok.copyableAnnotations", "Copy these annotations to getters, setters, withers, builder-setters, etc.") {}; } diff --git a/src/core/lombok/NonNull.java b/src/core/lombok/NonNull.java index 58538583..caf6ed05 100644 --- a/src/core/lombok/NonNull.java +++ b/src/core/lombok/NonNull.java @@ -41,7 +41,7 @@ import java.lang.annotation.Target; * this annotation will be deleted from the lombok package. If the need to update an import statement scares * you, you should use your own annotation named {@code @NonNull} instead of this one. */ -@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE}) +@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE}) @Retention(RetentionPolicy.CLASS) @Documented public @interface NonNull { diff --git a/src/core/lombok/core/TypeLibrary.java b/src/core/lombok/core/TypeLibrary.java index cdaf7a70..ceaf5f90 100644 --- a/src/core/lombok/core/TypeLibrary.java +++ b/src/core/lombok/core/TypeLibrary.java @@ -45,6 +45,12 @@ public class TypeLibrary { qualified = null; } + public TypeLibrary(TypeLibrary parent) { + unqualifiedToQualifiedMap = new HashMap(); + unqualified = null; + qualified = null; + } + public void lock() { this.locked = true; } diff --git a/src/core/lombok/core/handlers/HandlerUtil.java b/src/core/lombok/core/handlers/HandlerUtil.java index 1694e305..296b70b4 100644 --- a/src/core/lombok/core/handlers/HandlerUtil.java +++ b/src/core/lombok/core/handlers/HandlerUtil.java @@ -27,7 +27,6 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.regex.Pattern; import lombok.AllArgsConstructor; import lombok.ConfigurationKeys; @@ -77,6 +76,33 @@ public class HandlerUtil { return 43; } + public static final List NONNULL_ANNOTATIONS, BASE_COPYABLE_ANNOTATIONS; + static { + NONNULL_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(new String[] { + "lombok.NonNull", + "javax.annotation.Nonnull", + "edu.umd.cs.findbugs.annotations.NonNull", + "org.jetbrains.annotations.NotNull", + "android.support.annotation.NonNull", + "org.eclipse.jdt.annotation.NonNull", + })); + BASE_COPYABLE_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(new String[] { + "lombok.NonNull", + "javax.annotation.Nonnull", + "edu.umd.cs.findbugs.annotations.NonNull", + "org.jetbrains.annotations.NotNull", + "android.support.annotation.NonNull", + "org.eclipse.jdt.annotation.NonNull", + "javax.annotation.Nullable", + "javax.annotation.CheckForNull", + "edu.umd.cs.findbugs.annotations.UnknownNullness", + "edu.umd.cs.findbugs.annotations.Nullable", + "org.jetbrains.annotations.Nullable", + "android.support.annotation.Nullable", + "org.eclipse.jdt.annotation.Nullable", + })); + } + /** Checks if the given name is a valid identifier. * * If it is, this returns {@code true} and does nothing else. @@ -222,19 +248,8 @@ public class HandlerUtil { constructor and the implied starts-out-as-null state that goes with it is in fact mandatory' which happens with javax.validation.constraints.NotNull. Various problems with spring have also been reported. See issue #287, issue #271, and issue #43. */ - /** Matches the simple part of any annotation that lombok considers as indicative of NonNull status. */ - public static final Pattern NON_NULL_PATTERN = Pattern.compile("^(?:nonnull)$", Pattern.CASE_INSENSITIVE); - - /** Matches the simple part of any annotation that lombok considers as indicative of Nullable status. */ - public static final Pattern NULLABLE_PATTERN = Pattern.compile("^(?:nullable|checkfornull)$", Pattern.CASE_INSENSITIVE); - public static final String DEFAULT_EXCEPTION_FOR_NON_NULL = "java.lang.NullPointerException"; - /** Returns the configuration value for ConfigurationKeys.COPYABLE_ANNOTATIONS. */ - public static List getCopyableAnnotationNames(AST ast) { - return ast.readConfiguration(ConfigurationKeys.COPYABLE_ANNOTATIONS); - } - /** * Generates a getter name from a given field name. * diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index e7a58de3..a972c1fe 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -48,6 +48,7 @@ import lombok.core.AnnotationValues; import lombok.core.AnnotationValues.AnnotationValue; import lombok.core.TypeResolver; import lombok.core.configuration.NullCheckExceptionType; +import lombok.core.configuration.TypeName; import lombok.core.debug.ProblemReporter; import lombok.core.handlers.HandlerUtil; import lombok.eclipse.Eclipse; @@ -672,19 +673,42 @@ public class EclipseHandlerUtil { } } + public static boolean hasNonNullAnnotations(EclipseNode node) { + AbstractVariableDeclaration avd = (AbstractVariableDeclaration) node.get(); + if (avd.annotations == null) return false; + for (Annotation annotation : avd.annotations) { + TypeReference typeRef = annotation.type; + if (typeRef != null && typeRef.getTypeName() != null) { + for (String bn : NONNULL_ANNOTATIONS) if (typeMatches(bn, node, typeRef)) return true; + } + } + return false; + } + 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. + * Searches the given field node for annotations and returns each one that is 'copyable' (either via configuration or from the base list). */ - public static Annotation[] findExactAnnotations(AbstractVariableDeclaration field, List names) { + public static Annotation[] findCopyableAnnotations(EclipseNode node) { + AbstractVariableDeclaration avd = (AbstractVariableDeclaration) node.get(); + if (avd.annotations == null) return EMPTY_ANNOTATIONS_ARRAY; List result = new ArrayList(); - if (field.annotations == null) return EMPTY_ANNOTATIONS_ARRAY; - for (Annotation annotation : field.annotations) { + List configuredCopyable = node.getAst().readConfiguration(ConfigurationKeys.COPYABLE_ANNOTATIONS); + + for (Annotation annotation : avd.annotations) { TypeReference typeRef = annotation.type; + boolean match = false; if (typeRef != null && typeRef.getTypeName() != null) { - String annoName = toQualifiedName(typeRef.getTypeName()); - if (names.contains(annoName)) result.add(annotation); + for (TypeName cn : configuredCopyable) if (typeMatches(cn.toString(), node, typeRef)) { + result.add(annotation); + match = true; + break; + } + if (!match) for (String bn : BASE_COPYABLE_ANNOTATIONS) if (typeMatches(bn, node, typeRef)) { + result.add(annotation); + break; + } } } return result.toArray(EMPTY_ANNOTATIONS_ARRAY); diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index f05896ab..280afc26 100644 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -200,14 +200,12 @@ public class HandleBuilder extends EclipseAnnotationHandler { EclipseNode isDefault = findAnnotation(Builder.Default.class, fieldNode); boolean isFinal = ((fd.modifiers & ClassFileConstants.AccFinal) != 0) || (valuePresent && !hasAnnotation(NonFinal.class, fieldNode)); - Annotation[] nonNulls = findAnnotations(fd, NON_NULL_PATTERN); - Annotation[] nullables = findAnnotations(fd, NULLABLE_PATTERN); - Annotation[] copyAnnotations = findExactAnnotations(fd, getCopyableAnnotationNames(fieldNode.getAst())); + Annotation[] copyableAnnotations = findCopyableAnnotations(fieldNode); BuilderFieldData bfd = new BuilderFieldData(); bfd.rawName = fieldNode.getName().toCharArray(); bfd.name = removePrefixFromField(fieldNode); - bfd.annotations = copyAnnotations(fd, nonNulls, nullables, copyAnnotations); + bfd.annotations = copyAnnotations(fd, copyableAnnotations); bfd.type = fd.type; bfd.singularData = getSingularData(fieldNode, ast); bfd.originalFieldNode = fieldNode; @@ -381,13 +379,11 @@ public class HandleBuilder extends EclipseAnnotationHandler { 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())); + Annotation[] copyableAnnotations = findCopyableAnnotations(param); bfd.rawName = arg.name; bfd.name = arg.name; - bfd.annotations = copyAnnotations(arg, nonNulls, nullables, copyAnnotations); + bfd.annotations = copyAnnotations(arg, copyableAnnotations); bfd.type = arg.type; bfd.singularData = getSingularData(param, ast); bfd.originalFieldNode = param; diff --git a/src/core/lombok/eclipse/handlers/HandleConstructor.java b/src/core/lombok/eclipse/handlers/HandleConstructor.java index 3d947a73..cb07115a 100644 --- a/src/core/lombok/eclipse/handlers/HandleConstructor.java +++ b/src/core/lombok/eclipse/handlers/HandleConstructor.java @@ -139,7 +139,7 @@ public class HandleConstructor { FieldDeclaration fieldDecl = (FieldDeclaration) child.get(); if (!filterField(fieldDecl)) continue; boolean isFinal = (fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0; - boolean isNonNull = nullMarked && findAnnotations(fieldDecl, NON_NULL_PATTERN).length != 0; + boolean isNonNull = nullMarked && hasNonNullAnnotations(child); if ((isFinal || isNonNull) && fieldDecl.initialization == null) fields.add(child); } return fields; @@ -403,14 +403,12 @@ public class HandleConstructor { assigns.add(assignment); long fieldPos = (((long) field.sourceStart) << 32) | field.sourceEnd; 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, getCopyableAnnotationNames(fieldNode.getAst())); - if (nonNulls.length != 0) { + Annotation[] copyableAnnotations = findCopyableAnnotations(fieldNode); + if (hasNonNullAnnotations(fieldNode)) { Statement nullCheck = generateNullCheck(parameter, sourceNode); if (nullCheck != null) nullChecks.add(nullCheck); } - parameter.annotations = copyAnnotations(source, nonNulls, nullables, copyAnnotations); + parameter.annotations = copyAnnotations(source, copyableAnnotations); params.add(parameter); } @@ -547,7 +545,7 @@ public class HandleConstructor { assigns.add(nameRef); Argument parameter = new Argument(field.name, fieldPos, copyType(field.type, source), Modifier.FINAL); - parameter.annotations = copyAnnotations(source, findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN)); + parameter.annotations = copyAnnotations(source, findCopyableAnnotations(fieldNode)); params.add(parameter); } diff --git a/src/core/lombok/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java index 58af8c1e..7d3fe62f 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/handlers/HandleGetter.java @@ -236,8 +236,6 @@ public class HandleGetter extends EclipseAnnotationHandler { } public MethodDeclaration createGetter(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, ASTNode source, boolean lazy, List onMethod) { - FieldDeclaration field = (FieldDeclaration) fieldNode.get(); - // Remember the type; lazy will change it; TypeReference returnType = copyType(((FieldDeclaration) fieldNode.get()).type, source); @@ -271,12 +269,10 @@ public class HandleGetter extends EclipseAnnotationHandler { } method.annotations = copyAnnotations(source, - onMethod.toArray(new Annotation[0]), - findAnnotations(field, NON_NULL_PATTERN), - findAnnotations(field, NULLABLE_PATTERN), - findExactAnnotations(field, getCopyableAnnotationNames(fieldNode.getAst())), - findDelegatesAndMarkAsHandled(fieldNode), - deprecated); + onMethod.toArray(new Annotation[0]), + findCopyableAnnotations(fieldNode), + findDelegatesAndMarkAsHandled(fieldNode), + deprecated); } method.traverse(new SetGeneratedByVisitor(source), parent.scope); diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java index ca81fef7..529a7d19 100644 --- a/src/core/lombok/eclipse/handlers/HandleSetter.java +++ b/src/core/lombok/eclipse/handlers/HandleSetter.java @@ -236,11 +236,9 @@ public class HandleSetter extends EclipseAnnotationHandler { method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart; method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; - Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); - Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); - Annotation[] copyableAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(fieldNode.getAst())); + Annotation[] copyableAnnotations = findCopyableAnnotations(fieldNode); List statements = new ArrayList(5); - if (nonNulls.length == 0) { + if (!hasNonNullAnnotations(fieldNode)) { statements.add(assignment); } else { Statement nullCheck = generateNullCheck(field, sourceNode); @@ -256,7 +254,7 @@ public class HandleSetter extends EclipseAnnotationHandler { statements.add(returnStatement); } method.statements = statements.toArray(new Statement[0]); - param.annotations = copyAnnotations(source, nonNulls, nullables, copyableAnnotations, onParam.toArray(new Annotation[0])); + param.annotations = copyAnnotations(source, copyableAnnotations, onParam.toArray(new Annotation[0])); method.traverse(new SetGeneratedByVisitor(source), parent.scope); return method; diff --git a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java index 3c07ac55..559cca20 100644 --- a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java @@ -493,12 +493,9 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { statements.add(assignment); } - Annotation[] nonNulls = findAnnotations((FieldDeclaration)fieldNode.originalFieldNode.get(), NON_NULL_PATTERN); - if (nonNulls.length != 0) { - Statement nullCheck = generateNullCheck((FieldDeclaration)fieldNode.originalFieldNode.get(), sourceNode); - if (nullCheck != null) { - statements.add(nullCheck); - } + if (hasNonNullAnnotations(fieldNode.originalFieldNode)) { + Statement nullCheck = generateNullCheck((FieldDeclaration) fieldNode.originalFieldNode.get(), sourceNode); + if (nullCheck != null) statements.add(nullCheck); } } diff --git a/src/core/lombok/eclipse/handlers/HandleWither.java b/src/core/lombok/eclipse/handlers/HandleWither.java index 11032e9c..a99789a6 100644 --- a/src/core/lombok/eclipse/handlers/HandleWither.java +++ b/src/core/lombok/eclipse/handlers/HandleWither.java @@ -240,9 +240,7 @@ public class HandleWither extends EclipseAnnotationHandler { method.typeParameters = null; method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; - Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); - Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); - Annotation[] copyableAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(fieldNode.getAst())); + Annotation[] copyableAnnotations = findCopyableAnnotations(fieldNode); if (!makeAbstract) { List args = new ArrayList(); @@ -278,7 +276,7 @@ public class HandleWither extends EclipseAnnotationHandler { method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; List statements = new ArrayList(5); - if (nonNulls.length > 0) { + if (hasNonNullAnnotations(fieldNode)) { Statement nullCheck = generateNullCheck(field, sourceNode); if (nullCheck != null) statements.add(nullCheck); } @@ -286,7 +284,7 @@ public class HandleWither extends EclipseAnnotationHandler { method.statements = statements.toArray(new Statement[0]); } - param.annotations = copyAnnotations(source, nonNulls, nullables, copyableAnnotations, onParam.toArray(new Annotation[0])); + param.annotations = copyAnnotations(source, 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 7577eeb2..8170898b 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -150,14 +150,10 @@ public class HandleBuilder extends JavacAnnotationHandler { JavacNode isDefault = findAnnotation(Builder.Default.class, fieldNode, false); boolean isFinal = (fd.mods.flags & Flags.FINAL) != 0 || (valuePresent && !hasAnnotation(NonFinal.class, fieldNode)); - List nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); - List nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); - List copyableAnnotations = findExactAnnotations(fieldNode, getCopyableAnnotationNames(fieldNode.getAst())); - BuilderFieldData bfd = new BuilderFieldData(); bfd.rawName = fd.name; bfd.name = removePrefixFromField(fieldNode); - bfd.annotations = nonNulls.appendList(nullables).appendList(copyableAnnotations); + bfd.annotations = findCopyableAnnotations(fieldNode); bfd.type = fd.vartype; bfd.sin