diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2018-09-11 01:41:22 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2018-09-11 01:59:19 +0200 |
commit | d7873f2d21564e8e7f22409fe03681d7dd4c8c1e (patch) | |
tree | 582ac7cf6ef36c69e93503a04b8391950df25f24 /src | |
parent | cc8370ab2d7b3ca15023364c99e53735e62e13d7 (diff) | |
download | lombok-d7873f2d21564e8e7f22409fe03681d7dd4c8c1e.tar.gz lombok-d7873f2d21564e8e7f22409fe03681d7dd4c8c1e.tar.bz2 lombok-d7873f2d21564e8e7f22409fe03681d7dd4c8c1e.zip |
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.
Also some work on the notion of TYPE_USE annotations.
Diffstat (limited to 'src')
18 files changed, 154 insertions, 102 deletions
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<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.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<List<String>> COPYABLE_ANNOTATIONS = new ConfigurationKey<List<String>>("lombok.copyableAnnotations", "Copy these annotations to getters, setters, withers, builder-setters, etc.") {}; + public static final ConfigurationKey<List<TypeName>> COPYABLE_ANNOTATIONS = new ConfigurationKey<List<TypeName>>("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 <strong>be deleted</strong> 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<String, String>(); + 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<String> 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<String> 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<String> names) { + public static Annotation[] findCopyableAnnotations(EclipseNode node) { + AbstractVariableDeclaration avd = (AbstractVariableDeclaration) node.get(); + if (avd.annotations == null) return EMPTY_ANNOTATIONS_ARRAY; List<Annotation> result = new ArrayList<Annotation>(); - if (field.annotations == null) return EMPTY_ANNOTATIONS_ARRAY; - for (Annotation annotation : field.annotations) { + List<TypeName> 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<Builder> { 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<Builder> { 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<Getter> { } public MethodDeclaration createGetter(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, ASTNode source, boolean lazy, List<Annotation> 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<Getter> { } 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<Setter> { 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<Statement> statements = new ArrayList<Statement>(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<Setter> { 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<SuperBuilder> { 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<Wither> { 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<Expression> args = new ArrayList<Expression>(); @@ -278,7 +276,7 @@ public class HandleWither extends EclipseAnnotationHandler<Wither> { method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; List<Statement> statements = new ArrayList<Statement>(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<Wither> { 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<Builder> { 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> 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.singularData = getSingularData(fieldNode); bfd.originalFieldNode = fieldNode; @@ -334,14 +330,10 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { 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 = nonNulls.appendList(nullables).appendList(copyableAnnotations); + bfd.annotations = findCopyableAnnotations(param); 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 1e45d73f..3c434d40 100644 --- a/src/core/lombok/javac/handlers/HandleConstructor.java +++ b/src/core/lombok/javac/handlers/HandleConstructor.java @@ -127,7 +127,7 @@ public class HandleConstructor { //Skip static fields. if ((fieldFlags & Flags.STATIC) != 0) continue; boolean isFinal = (fieldFlags & Flags.FINAL) != 0; - boolean isNonNull = nullMarked && !findAnnotations(child, NON_NULL_PATTERN).isEmpty(); + boolean isNonNull = nullMarked && hasNonNullAnnotations(child); if ((isFinal || isNonNull) && fieldDecl.init == null) fields.append(child); } return fields.toList(); @@ -329,13 +329,11 @@ public class HandleConstructor { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); Name fieldName = removePrefixFromField(fieldNode); Name rawName = field.name; - List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); - List<JCAnnotation> copyableAnnotations = findExactAnnotations(fieldNode, getCopyableAnnotationNames(fieldNode.getAst())); + List<JCAnnotation> copyableAnnotations = findCopyableAnnotations(fieldNode); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext()); - JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables).appendList(copyableAnnotations)), fieldName, field.vartype, null); + JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, copyableAnnotations), fieldName, field.vartype, null); params.append(param); - if (!nonNulls.isEmpty()) { + if (hasNonNullAnnotations(fieldNode)) { JCStatement nullCheck = generateNullCheck(maker, fieldNode, param, source); if (nullCheck != null) nullChecks.append(nullCheck); } @@ -472,10 +470,9 @@ public class HandleConstructor { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); Name fieldName = removePrefixFromField(fieldNode); JCExpression pType = cloneType(maker, field.vartype, source, typeNode.getContext()); - List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); + List<JCAnnotation> copyableAnnotations = findCopyableAnnotations(fieldNode); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext()); - JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables)), fieldName, pType, null); + JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, copyableAnnotations), fieldName, pType, null); params.append(param); args.append(maker.Ident(fieldName)); } diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 04b300a8..7a178f66 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -243,13 +243,9 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> { List<JCExpression> throwsClauses = List.nil(); JCExpression annotationMethodDefaultValue = null; - List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); - List<JCAnnotation> copyableAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(field.getAst())); - + List<JCAnnotation> copyableAnnotations = findCopyableAnnotations(field); List<JCAnnotation> delegates = findDelegatesAndRemoveFromField(field); - - List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod).appendList(nonNulls).appendList(nullables).appendList(copyableAnnotations); + List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod).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 e5e9481d..28f5318d 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -226,17 +226,15 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { JCAssign assign = treeMaker.Assign(fieldRef, treeMaker.Ident(fieldDecl.name)); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); - List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); - List<JCAnnotation> copyableAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(field.getAst())); + List<JCAnnotation> copyableAnnotations = findCopyableAnnotations(field); Name methodName = field.toName(setterName); - List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables).appendList(copyableAnnotations); + List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(copyableAnnotations); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, field.getContext()); JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(flags, annsOnParam), fieldDecl.name, fieldDecl.vartype, null); - if (nonNulls.isEmpty()) { + if (!hasNonNullAnnotations(field)) { statements.append(treeMaker.Exec(assign)); } else { JCStatement nullCheck = generateNullCheck(treeMaker, field, source); diff --git a/src/core/lombok/javac/handlers/HandleSuperBuilder.java b/src/core/lombok/javac/handlers/HandleSuperBuilder.java index b8f572d5..bcb7ee33 100644 --- a/src/core/lombok/javac/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/javac/handlers/HandleSuperBuilder.java @@ -431,8 +431,7 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> { statements.append(assign); } - List<JCAnnotation> nonNulls = findAnnotations(bfd.originalFieldNode, NON_NULL_PATTERN); - if (!nonNulls.isEmpty()) { + if (hasNonNullAnnotations(bfd.originalFieldNode)) { JCStatement nullCheck = generateNullCheck(maker, bfd.originalFieldNode, source); if (nullCheck != null) statements.append(nullCheck); } diff --git a/src/core/lombok/javac/handlers/HandleWither.java b/src/core/lombok/javac/handlers/HandleWither.java index b0e32d61..33c4dec2 100644 --- a/src/core/lombok/javac/handlers/HandleWither.java +++ b/src/core/lombok/javac/handlers/HandleWither.java @@ -222,9 +222,7 @@ public class HandleWither extends JavacAnnotationHandler<Wither> { JCVariableDecl fieldDecl = (JCVariableDecl) field.get(); - List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); - List<JCAnnotation> copyableAnnotations = findExactAnnotations(field, getCopyableAnnotationNames(field.getAst())); + List<JCAnnotation> copyableAnnotations = findCopyableAnnotations(field); Name methodName = field.toName(witherName); @@ -232,7 +230,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(copyableAnnotations); + List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(copyableAnnotations); JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, annsOnParam), fieldDecl.name, fieldDecl.vartype, null); @@ -265,7 +263,7 @@ public class HandleWither extends JavacAnnotationHandler<Wither> { JCConditional conditional = maker.Conditional(identityCheck, maker.Ident(field.toName("this")), newClass); JCReturn returnStatement = maker.Return(conditional); - if (nonNulls.isEmpty()) { + if (!hasNonNullAnnotations(field)) { statements.append(returnStatement); } else { JCStatement nullCheck = generateNullCheck(maker, field, source); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 1cc28072..b1557533 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -49,6 +49,7 @@ import lombok.core.LombokImmutableList; import lombok.core.AnnotationValues.AnnotationValue; import lombok.core.TypeResolver; import lombok.core.configuration.NullCheckExceptionType; +import lombok.core.configuration.TypeName; import lombok.core.handlers.HandlerUtil; import lombok.delombok.LombokOptionsFactory; import lombok.experimental.Accessors; @@ -1335,16 +1336,58 @@ public class JavacHandlerUtil { return result.toList(); } + public static boolean hasNonNullAnnotations(JavacNode node) { + for (JavacNode child : node.down()) { + if (child.getKind() == Kind.ANNOTATION) { + JCAnnotation annotation = (JCAnnotation) child.get(); + for (String nn : NONNULL_ANNOTATIONS) if (typeMatches(nn, node, annotation.annotationType)) return true; + } + } + + return false; + } + /** - * 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 List<JCAnnotation> findExactAnnotations(JavacNode fieldNode, java.util.List<String> names) { + public static List<JCAnnotation> findCopyableAnnotations(JavacNode node) { + JCAnnotation anno = null; + String annoName = null; + for (JavacNode child : node.down()) { + if (child.getKind() == Kind.ANNOTATION) { + if (anno != null) { + annoName = ""; + break; + } + JCAnnotation annotation = (JCAnnotation) child.get(); + annoName = annotation.annotationType.toString(); + anno = annotation; + } + } + + if (annoName == null) return List.nil(); + + java.util.List<TypeName> configuredCopyable = node.getAst().readConfiguration(ConfigurationKeys.COPYABLE_ANNOTATIONS); + + if (!annoName.isEmpty()) { + for (TypeName cn : configuredCopyable) if (typeMatches(cn.toString(), node, anno.annotationType)) return List.of(anno); + for (String bn : BASE_COPYABLE_ANNOTATIONS) if (typeMatches(bn, node, anno.annotationType)) return List.of(anno); + } + ListBuffer<JCAnnotation> result = new ListBuffer<JCAnnotation>(); - for (JavacNode child : fieldNode.down()) { + for (JavacNode child : node.down()) { if (child.getKind() == Kind.ANNOTATION) { JCAnnotation annotation = (JCAnnotation) child.get(); - String annoName = annotation.annotationType.toString(); - if (names.contains(annoName)) result.append(annotation); + boolean match = false; + for (TypeName cn : configuredCopyable) if (typeMatches(cn.toString(), node, annotation.annotationType)) { + result.append(annotation); + match = true; + break; + } + if (!match) for (String bn : BASE_COPYABLE_ANNOTATIONS) if (typeMatches(bn, node, annotation.annotationType)) { + result.append(annotation); + break; + } } } return result.toList(); |