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.
This commit is contained in:
Reinier Zwitserloot committed 2018-09-11 01:59:19 +02:00
1 parent cc8370ab2d
commit d7873f2d21
21 files changed
+384 -105

No files matched your search

+3 -2
View File
@@ -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} += &lt;String: fully-qualified annotation class name&gt;.
* lombok configuration: {@code lombok.copyableAnnotations} += &lt;TypeName: fully-qualified annotation class name&gt;.
*
* 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.") {};
}
+1 -1
View File
@@ -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 {
+6
View File
@@ -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;
}
+27 -12
View File
@@ -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.
*
@@ -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);
@@ -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;
@@ -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);
}
@@ -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);
@@ -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;
@@ -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);
}
}
@@ -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;
@@ -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;
@@ -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));
}
@@ -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()));
}
@@ -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);
@@ -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);
}
@@ -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);
@@ -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();
}
/**
* Searches the given field node for annotations and returns each one that matches the provided list of names.
*/
public static List<JCAnnotation> findExactAnnotations(JavacNode fieldNode, java.util.List<String> names) {
ListBuffer<JCAnnotation> result = new ListBuffer<JCAnnotation>();
for (JavacNode child : fieldNode.down()) {
public static boolean hasNonNullAnnotations(JavacNode node) {
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);
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 is 'copyable' (either via configuration or from the base list).
*/
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 : node.down()) {
if (child.getKind() == Kind.ANNOTATION) {
JCAnnotation annotation = (JCAnnotation) child.get();
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();
@@ -0,0 +1,110 @@
import java.util.Set;
import java.util.Map;
import lombok.NonNull;
class BuilderSingularAnnotatedTypes {
private Set<@NonNull String> foos;
private Map<@NonNull String, @NonNull Integer> bars;
@java.lang.SuppressWarnings("all")
BuilderSingularAnnotatedTypes(final Set<@NonNull String> foos, final Map<@NonNull String, @NonNull Integer> bars) {
this.foos = foos;
this.bars = bars;
}
@java.lang.SuppressWarnings("all")
public static class BuilderSingularAnnotatedTypesBuilder {
@java.lang.SuppressWarnings("all")
private java.util.ArrayList<@NonNull String> foos;
@java.lang.SuppressWarnings("all")
private java.util.ArrayList<@NonNull String> bars$key;
@java.lang.SuppressWarnings("all")
private java.util.ArrayList<@NonNull Integer> bars$value;
@java.lang.SuppressWarnings("all")
BuilderSingularAnnotatedTypesBuilder() {
}
@java.lang.SuppressWarnings("all")
public BuilderSingularAnnotatedTypesBuilder foo(final @NonNull String foo) {
if (this.foos == null) this.foos = new java.util.ArrayList<@NonNull String>();
this.foos.add(foo);
return this;
}
@java.lang.SuppressWarnings("all")
public BuilderSingularAnnotatedTypesBuilder foos(final java.util.Collection<? extends @NonNull String> foos) {
if (this.foos == null) this.foos = new java.util.ArrayList<@NonNull String>();
this.foos.addAll(foos);
return this;
}
@java.lang.SuppressWarnings("all")
public BuilderSingularAnnotatedTypesBuilder clearFoos() {
if (this.foos != null) this.foos.clear();
return this;
}
@java.lang.SuppressWarnings("all")
public BuilderSingularAnnotatedTypesBuilder bar(final @NonNull String barKey, final @NonNull Integer barValue) {
if (this.bars$key == null) {
this.bars$key = new java.util.ArrayList<@NonNull String>();
this.bars$value = new java.util.ArrayList<@NonNull Integer>();
}
this.bars$key.add(barKey);
this.bars$value.add(barValue);
return this;
}
@java.lang.SuppressWarnings("all")
public BuilderSingularAnnotatedTypesBuilder bars(final java.util.Map<? extends @NonNull String, ? extends @NonNull Integer> bars) {
if (this.bars$key == null) {
this.bars$key = new java.util.ArrayList<@NonNull String>();
this.bars$value = new java.util.ArrayList<@NonNull Integer>();
}
for (final java.util.Map.Entry<? extends @NonNull String, ? extends @NonNull Integer> $lombokEntry : bars.entrySet()) {
this.bars$key.add($lombokEntry.getKey());
this.bars$value.add($lombokEntry.getValue());
}
return this;
}
@java.lang.SuppressWarnings("all")
public BuilderSingularAnnotatedTypesBuilder clearBars() {
if (this.bars$key != null) {
this.bars$key.clear();
this.bars$value.clear();
}
return this;
}
@java.lang.SuppressWarnings("all")
public BuilderSingularAnnotatedTypes build() {
java.util.Set<@NonNull String> foos;
switch (this.foos == null ? 0 : this.foos.size()) {
case 0:
foos = java.util.Collections.emptySet();
break;
case 1:
foos = java.util.Collections.singleton(this.foos.get(0));
break;
default:
foos = new java.util.LinkedHashSet<@NonNull String>(this.foos.size() < 1073741824 ? 1 + this.foos.size() + (this.foos.size() - 3) / 3 : java.lang.Integer.MAX_VALUE);
foos.addAll(this.foos);
foos = java.util.Collections.unmodifiableSet(foos);
}
java.util.Map<@NonNull String, @NonNull Integer> bars;
switch (this.bars$key == null ? 0 : this.bars$key.size()) {
case 0:
bars = java.util.Collections.emptyMap();
break;
case 1:
bars = java.util.Collections.singletonMap(this.bars$key.get(0), this.bars$value.get(0));
break;
default:
bars = new java.util.LinkedHashMap<@NonNull String, @NonNull Integer>(this.bars$key.size() < 1073741824 ? 1 + this.bars$key.size() + (this.bars$key.size() - 3) / 3 : java.lang.Integer.MAX_VALUE);
for (int $i = 0; $i < this.bars$key.size(); $i++) bars.put(this.bars$key.get($i), (@NonNull Integer) this.bars$value.get($i));
bars = java.util.Collections.unmodifiableMap(bars);
}
return new BuilderSingularAnnotatedTypes(foos, bars);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "BuilderSingularAnnotatedTypes.BuilderSingularAnnotatedTypesBuilder(foos=" + this.foos + ", bars$key=" + this.bars$key + ", bars$value=" + this.bars$value + ")";
}
}
@java.lang.SuppressWarnings("all")
public static BuilderSingularAnnotatedTypesBuilder builder() {
return new BuilderSingularAnnotatedTypesBuilder();
}
}
@@ -0,0 +1,105 @@
import java.util.Set;
import java.util.Map;
import lombok.NonNull;
import lombok.Singular;
@lombok.Builder class BuilderSingularAnnotatedTypes {
public static @java.lang.SuppressWarnings("all") class BuilderSingularAnnotatedTypesBuilder {
private @java.lang.SuppressWarnings("all") java.util.ArrayList<@NonNull String> foos;
private @java.lang.SuppressWarnings("all") java.util.ArrayList<@NonNull String> bars$key;
private @java.lang.SuppressWarnings("all") java.util.ArrayList<@NonNull Integer> bars$value;
@java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder() {
super();
}
public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder foo(@NonNull String foo) {
if ((this.foos == null))
this.foos = new java.util.ArrayList<@NonNull String>();
this.foos.add(foo);
return this;
}
public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder foos(java.util.Collection<? extends @NonNull String> foos) {
if ((this.foos == null))
this.foos = new java.util.ArrayList<@NonNull String>();
this.foos.addAll(foos);
return this;
}
public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder clearFoos() {
if ((this.foos != null))
this.foos.clear();
return this;
}
public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder bar(@NonNull String barKey, @NonNull Integer barValue) {
if ((this.bars$key == null))
{
this.bars$key = new java.util.ArrayList<@NonNull String>();
this.bars$value = new java.util.ArrayList<@NonNull Integer>();
}
this.bars$key.add(barKey);
this.bars$value.add(barValue);
return this;
}
public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder bars(java.util.Map<? extends @NonNull String, ? extends @NonNull Integer> bars) {
if ((this.bars$key == null))
{
this.bars$key = new java.util.ArrayList<@NonNull String>();
this.bars$value = new java.util.ArrayList<@NonNull Integer>();
}
for (java.util.Map.Entry<? extends @NonNull String, ? extends @NonNull Integer> $lombokEntry : bars.entrySet())
{
this.bars$key.add($lombokEntry.getKey());
this.bars$value.add($lombokEntry.getValue());
}
return this;
}
public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder clearBars() {
if ((this.bars$key != null))
{
this.bars$key.clear();
this.bars$value.clear();
}
return this;
}
public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypes build() {
java.util.Set<@NonNull String> foos;
switch (((this.foos == null) ? 0 : this.foos.size())) {
case 0 :
foos = java.util.Collections.emptySet();
break;
case 1 :
foos = java.util.Collections.singleton(this.foos.get(0));
break;
default :
foos = new java.util.LinkedHashSet<@NonNull String>(((this.foos.size() < 0x40000000) ? ((1 + this.foos.size()) + ((this.foos.size() - 3) / 3)) : java.lang.Integer.MAX_VALUE));
foos.addAll(this.foos);
foos = java.util.Collections.unmodifiableSet(foos);
}
java.util.Map<@NonNull String, @NonNull Integer> bars;
switch (((this.bars$key == null) ? 0 : this.bars$key.size())) {
case 0 :
bars = java.util.Collections.emptyMap();
break;
case 1 :
bars = java.util.Collections.singletonMap(this.bars$key.get(0), this.bars$value.get(0));
break;
default :
bars = new java.util.LinkedHashMap<@NonNull String, @NonNull Integer>(((this.bars$key.size() < 0x40000000) ? ((1 + this.bars$key.size()) + ((this.bars$key.size() - 3) / 3)) : java.lang.Integer.MAX_VALUE));
for (int $i = 0;; ($i < this.bars$key.size()); $i ++)
bars.put(this.bars$key.get($i), this.bars$value.get($i));
bars = java.util.Collections.unmodifiableMap(bars);
}
return new BuilderSingularAnnotatedTypes(foos, bars);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
return (((((("BuilderSingularAnnotatedTypes.BuilderSingularAnnotatedTypesBuilder(foos=" + this.foos) + ", bars$key=") + this.bars$key) + ", bars$value=") + this.bars$value) + ")");
}
}
private @Singular Set<@NonNull String> foos;
private @Singular Map<@NonNull String, @NonNull Integer> bars;
@java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypes(final Set<@NonNull String> foos, final Map<@NonNull String, @NonNull Integer> bars) {
super();
this.foos = foos;
this.bars = bars;
}
public static @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder builder() {
return new BuilderSingularAnnotatedTypesBuilder();
}
}
@@ -0,0 +1,12 @@
//VERSION 8:
import java.util.Set;
import java.util.Map;
import lombok.NonNull;
import lombok.Singular;
@lombok.Builder
class BuilderSingularAnnotatedTypes {
@Singular private Set<@NonNull String> foos;
@Singular private Map<@NonNull String, @NonNull Integer> bars;
}