From c42bfbae39990b365a5f05eb23895da6203023bc Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 31 Jan 2020 06:33:33 +0100 Subject: [issue #2221] simplified configuration for `@Singular`-generated plural form nullchecks. --- doc/changelog.markdown | 2 +- src/core/lombok/ConfigurationKeys.java | 14 +--- src/core/lombok/Singular.java | 67 +-------------- .../core/configuration/NullCheckExceptionType.java | 5 +- .../eclipse/handlers/EclipseHandlerUtil.java | 10 +-- .../eclipse/handlers/EclipseSingularsRecipes.java | 52 +++--------- .../lombok/eclipse/handlers/HandleBuilder.java | 15 +--- .../lombok/eclipse/handlers/HandleConstructor.java | 2 +- .../lombok/eclipse/handlers/HandleNonNull.java | 2 +- src/core/lombok/eclipse/handlers/HandleSetter.java | 2 +- .../eclipse/handlers/HandleSuperBuilder.java | 6 +- src/core/lombok/eclipse/handlers/HandleWith.java | 2 +- src/core/lombok/javac/handlers/HandleBuilder.java | 16 +--- src/core/lombok/javac/handlers/HandleSetter.java | 2 +- .../lombok/javac/handlers/HandleSuperBuilder.java | 4 +- .../lombok/javac/handlers/JavacHandlerUtil.java | 8 +- .../javac/handlers/JavacSingularsRecipes.java | 48 +++++------ .../after-delombok/BuilderDefaultsWarnings.java | 4 +- .../BuilderSingularAnnotatedTypes.java | 8 +- ...lderSingularAnnotatedTypesWithSetterPrefix.java | 8 +- .../BuilderSingularGuavaListsSets.java | 20 +++-- .../after-delombok/BuilderSingularGuavaMaps.java | 12 ++- .../after-delombok/BuilderSingularLists.java | 12 ++- .../after-delombok/BuilderSingularMaps.java | 16 +++- .../BuilderSingularMapsWithSetterPrefix.java | 57 ++++++------- .../after-delombok/BuilderSingularNoAuto.java | 12 ++- .../BuilderSingularNoAutoWithSetterPrefix.java | 12 ++- .../BuilderSingularNullBehavior1.java | 91 +++------------------ .../BuilderSingularNullBehavior2.java | 52 ++---------- .../BuilderSingularRedirectToGuava.java | 12 ++- .../after-delombok/BuilderSingularSets.java | 16 +++- .../BuilderSingularSetsWithSetterPrefix.java | 16 +++- .../BuilderSingularToBuilderWithNull.java | 4 +- ...rSingularToBuilderWithNullWithSetterPrefix.java | 4 +- .../BuilderSingularWildcardListsWithToBuilder.java | 8 +- .../BuilderSingularWithPrefixes.java | 4 +- ...uilderSingularWithPrefixesWithSetterPrefix.java | 4 +- .../after-delombok/BuilderWithDeprecated.java | 8 +- .../after-delombok/BuilderWithToBuilder.java | 4 +- .../after-delombok/CheckerFrameworkBuilder.java | 4 +- .../CheckerFrameworkSuperBuilder.java | 4 +- .../resource/after-delombok/SuperBuilderBasic.java | 4 +- .../after-delombok/SuperBuilderBasicToBuilder.java | 4 +- .../SuperBuilderSingularAnnotatedTypes.java | 8 +- .../SuperBuilderWithCustomBuilderMethod.java | 4 +- .../after-delombok/SuperBuilderWithGenerics.java | 4 +- .../after-delombok/SuperBuilderWithGenerics2.java | 4 +- .../SuperBuilderWithGenericsAndToBuilder.java | 4 +- .../after-delombok/SuperBuilderWithPrefixes.java | 4 +- .../after-ecj/BuilderDefaultsWarnings.java | 4 +- .../after-ecj/BuilderSingularAnnotatedTypes.java | 8 +- ...lderSingularAnnotatedTypesWithSetterPrefix.java | 8 +- .../after-ecj/BuilderSingularGuavaListsSets.java | 20 +++-- .../after-ecj/BuilderSingularGuavaMaps.java | 12 ++- .../resource/after-ecj/BuilderSingularLists.java | 12 ++- .../resource/after-ecj/BuilderSingularMaps.java | 16 +++- .../BuilderSingularMapsWithSetterPrefix.java | 71 ++++++++-------- .../resource/after-ecj/BuilderSingularNoAuto.java | 12 ++- .../BuilderSingularNoAutoWithSetterPrefix.java | 12 ++- .../after-ecj/BuilderSingularNullBehavior1.java | 94 ++++------------------ .../after-ecj/BuilderSingularNullBehavior2.java | 49 ++--------- .../after-ecj/BuilderSingularRedirectToGuava.java | 12 ++- .../resource/after-ecj/BuilderSingularSets.java | 16 +++- .../BuilderSingularSetsWithSetterPrefix.java | 16 +++- .../BuilderSingularToBuilderWithNull.java | 4 +- ...rSingularToBuilderWithNullWithSetterPrefix.java | 4 +- .../BuilderSingularWildcardListsWithToBuilder.java | 8 +- .../after-ecj/BuilderSingularWithPrefixes.java | 4 +- ...uilderSingularWithPrefixesWithSetterPrefix.java | 4 +- .../resource/after-ecj/BuilderWithDeprecated.java | 8 +- .../resource/after-ecj/BuilderWithToBuilder.java | 4 +- .../after-ecj/CheckerFrameworkBuilder.java | 4 +- .../after-ecj/CheckerFrameworkSuperBuilder.java | 4 +- .../resource/after-ecj/SuperBuilderBasic.java | 4 +- .../after-ecj/SuperBuilderBasicToBuilder.java | 4 +- .../SuperBuilderSingularAnnotatedTypes.java | 8 +- .../SuperBuilderWithCustomBuilderMethod.java | 4 +- .../after-ecj/SuperBuilderWithGenerics.java | 4 +- .../after-ecj/SuperBuilderWithGenerics2.java | 4 +- .../SuperBuilderWithGenericsAndToBuilder.java | 4 +- .../after-ecj/SuperBuilderWithPrefixes.java | 4 +- .../BuilderSingularMapsWithSetterPrefix.java | 3 +- .../before/BuilderSingularNullBehavior1.java | 7 +- .../before/BuilderSingularNullBehavior2.java | 7 +- website/templates/features/Builder.html | 16 ++-- 85 files changed, 517 insertions(+), 643 deletions(-) diff --git a/doc/changelog.markdown b/doc/changelog.markdown index 1a549866..f1980523 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -6,7 +6,7 @@ Lombok Changelog * PLATFORM: Support for JDK14 (including `pattern match` instanceof expressions). * FEATURE: In [`lombok.config`](https://projectlombok.org/features/configuration) it is possible to import other config files, even from a `.zip` or `.jar`. * FEATURE: You can now configure a builder's 'setter' prefixes via `@Builder(setterPrefix = "set")` for example. We discourage doing this, but if some library you use requires them, have at it. [Pull Request #2174](https://github.com/rzwitserloot/lombok/pull/2174], [Issue #1805](https://github.com/rzwitserloot/lombok/issues/1805). -* FEATURE: If you use `@Builder`'s `@Singular`, a plural form is also generated, which has the effect of adding all elements in the passed collection. If you pass a null reference, this would result in a message-less `NullPointerException`. Now, it results in that exception but with a useful message attached, and you can choose other behaviors as well via a parameter on the `@Singular` annotation and via `lombok.config`; you can even choose treat them as empty collections; this can be useful when deserializing (e.g. Jackson JSON) and JPA/Hibernate code. [Issue #2221](https://github.com/rzwitserloot/lombok/issues/2221]. [singular documentation](https://projectlombok.org/features/Builder). +* FEATURE: If you use `@Builder`'s `@Singular`, a plural form is also generated, which has the effect of adding all elements in the passed collection. If you pass a null reference, this would result in a message-less `NullPointerException`. Now, it results in that exception but with a useful message attached (uses the same config as `@NonNull`), or alternatively via a parameter on `@Singular`, you can choose to ignore such a call (add nothing, return immediately); this can be useful when deserializing (e.g. Jackson JSON) and JPA/Hibernate code. [Issue #2221](https://github.com/rzwitserloot/lombok/issues/2221]. [singular documentation](https://projectlombok.org/features/Builder). * FEATURE: Tired of being unable to use `@javax.annotation.ParametersAreNonnullByDefault` or `@org.eclipse.jdt.annotation.NonNullByDefault` because then the equals method that lombok generates isn't valid? Fret no more; lombok can now add nullity annotations where relevant. Set the flavour of nullity annotation you prefer in your `lombok.config`. Applies to the return value of `toString` and `withX` methods, and the parameter of `equals`, `canEqual`, and the plural form of `@Singular` marked fields for builder classes. [Issue #788](https://github.com/rzwitserloot/lombok/issues/788) * BUGFIX: `lombok.experimental.Wither` has been deprecated (it has been renamed to `lombok.With`). However, the intent is that lombok still handles the old annotation in case you haven't updated your lombok dep yet. However, only a star import on `lombok.experimental.*` worked; an explicit one would cause lombok to not generate any with method. [Issue #2235](https://github.com/rzwitserloot/lombok/issues/2235) * BUGFIX: Referring to an inner class inside the generics on a class marked with `@SuperBuilder` would cause the error `wrong number of type arguments; required 3` [Issue #2262](https://github.com/rzwitserloot/lombok/issues/2262); fixed by github user [`@Lekanich`](https://github.com/rzwitserloot/lombok/issues/2262) - thank you! diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java index 86cb4650..49613fc4 100644 --- a/src/core/lombok/ConfigurationKeys.java +++ b/src/core/lombok/ConfigurationKeys.java @@ -23,7 +23,6 @@ package lombok; import java.util.List; -import lombok.Singular.NullCollectionBehavior; import lombok.core.configuration.CallSuperType; import lombok.core.configuration.CheckerFrameworkVersion; import lombok.core.configuration.ConfigurationKey; @@ -317,18 +316,6 @@ public class ConfigurationKeys { */ public static final ConfigurationKey SINGULAR_AUTO = new ConfigurationKey("lombok.singular.auto", "If true (default): Automatically singularize the assumed-to-be-plural name of your variable/parameter when using @Singular.") {}; - /** - * lombok configuration: {@code lombok.singular.nullCollections} = one of: [{@code NullPointerException}, {@code IllegalArgumentException}, {@code JDK}, {@code Guava}, or {@code ignore}]. - * - * Lombok generates a 'plural form' method, which takes in a collection and will add each element in it to the {@code @Singular} marked target. What should happen if {@code null} is passed instead of a collection instance?
    - *
  • If the chosen configuration is {@code NullPointerException} (the default), or {@code IllegalArgumentException}, that exception type is a thrown, with as message field-name must not be null.
  • - *
  • If the chosen configuration is {@code JDK}, a call to {@code java.util.Objects.requireNonNull} is generated with the fieldname passed along (which throws {@code NullPointerException}).
  • - *
  • If the chosen configuration is {@code Guava}, a call to {@code com.google.common.base.Preconditions.checkNotNull} is generated with the fieldname passed along (which throws {@code NullPointerException}).
  • - *
  • If the chosen configuration is {@code Ignore}, then no exception occurs, and the call does nothing; it acts as if an empty collection was passed.
  • - *
- */ - public static final ConfigurationKey SINGULAR_NULL_COLLECTIONS = new ConfigurationKey("lombok.singular.nullCollections", "Lombok generates a method to add all elements in a collection when using @Singular. What should happen if a null ref is passed? default: throw NullPointerException.") {}; - // ##### Standalones ##### // ----- Cleanup ----- @@ -360,6 +347,7 @@ public class ConfigurationKeys { *
  • If the chosen configuration is {@code JDK}, a call to {@code java.util.Objects.requireNonNull} is generated with the fieldname passed along (which throws {@code NullPointerException}).
  • *
  • If the chosen configuration is {@code Guava}, a call to {@code com.google.common.base.Preconditions.checkNotNull} is generated with the fieldname passed along (which throws {@code NullPointerException}).
  • * + * NB: The chosen nullcheck style is also used by {@code @Builder}'s {@code @Singular} annotation to check any collections passed to a plural-form method. */ public static final ConfigurationKey NON_NULL_EXCEPTION_TYPE = new ConfigurationKey("lombok.nonNull.exceptionType", "The type of the exception to throw if a passed-in argument is null (Default: NullPointerException).") {}; diff --git a/src/core/lombok/Singular.java b/src/core/lombok/Singular.java index f8cf6853..2ceaad58 100644 --- a/src/core/lombok/Singular.java +++ b/src/core/lombok/Singular.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015-2017 The Project Lombok Authors. + * Copyright (C) 2015-2020 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -27,10 +27,6 @@ import static java.lang.annotation.RetentionPolicy.*; import java.lang.annotation.Retention; import java.lang.annotation.Target; -import lombok.core.LombokImmutableList; -import lombok.core.configuration.ExampleValueString; -import lombok.core.configuration.NullCheckExceptionType; - /** * The singular annotation is used together with {@code @Builder} to create single element 'add' methods in the builder for collections. */ @@ -40,63 +36,6 @@ public @interface Singular { /** @return The singular name of this field. If it's a normal english plural, lombok will figure it out automatically. Otherwise, this parameter is mandatory. */ String value() default ""; - NullCollectionBehavior nullBehavior() default NullCollectionBehavior.NULL_POINTER_EXCEPTION; - - @ExampleValueString("[NullPointerException | IllegalArgumentException | JDK | Guava | Ignore]") - public enum NullCollectionBehavior { - ILLEGAL_ARGUMENT_EXCEPTION { - @Override public String getExceptionType() { - return NullCheckExceptionType.ILLEGAL_ARGUMENT_EXCEPTION.getExceptionType(); - } - - @Override public LombokImmutableList getMethod() { - return NullCheckExceptionType.ILLEGAL_ARGUMENT_EXCEPTION.getMethod(); - } - }, - NULL_POINTER_EXCEPTION { - @Override public String getExceptionType() { - return NullCheckExceptionType.NULL_POINTER_EXCEPTION.getExceptionType(); - } - - @Override public LombokImmutableList getMethod() { - return NullCheckExceptionType.NULL_POINTER_EXCEPTION.getMethod(); - } - }, - JDK { - @Override public String getExceptionType() { - return NullCheckExceptionType.JDK.getExceptionType(); - } - - @Override public LombokImmutableList getMethod() { - return NullCheckExceptionType.JDK.getMethod(); - } - }, - GUAVA { - @Override public String getExceptionType() { - return NullCheckExceptionType.GUAVA.getExceptionType(); - } - - @Override public LombokImmutableList getMethod() { - return NullCheckExceptionType.GUAVA.getMethod(); - } - }, - IGNORE { - @Override public String getExceptionType() { - return null; - } - - @Override public LombokImmutableList getMethod() { - return null; - } - }; - - - public String toExceptionMessage(String fieldName) { - return fieldName + " cannot be null"; - } - - public abstract String getExceptionType(); - - public abstract LombokImmutableList getMethod(); - } + /** @return If true, the plural variant (which takes a collection and adds each element inside) will treat {@code null} as an empty collection, i.e. do nothing. If {@code false) (the default), it is null checked as if annotated with {@code @lombok.NonNull}. */ + boolean ignoreNullCollections() default false; } diff --git a/src/core/lombok/core/configuration/NullCheckExceptionType.java b/src/core/lombok/core/configuration/NullCheckExceptionType.java index 4632916c..2c1b7ce6 100644 --- a/src/core/lombok/core/configuration/NullCheckExceptionType.java +++ b/src/core/lombok/core/configuration/NullCheckExceptionType.java @@ -74,8 +74,9 @@ public enum NullCheckExceptionType { private static final LombokImmutableList METHOD_JDK = LombokImmutableList.of("java", "util", "Objects", "requireNonNull"); private static final LombokImmutableList METHOD_GUAVA = LombokImmutableList.of("com", "google", "common", "base", "Preconditions", "checkNotNull"); - public String toExceptionMessage(String fieldName) { - return fieldName + " is marked non-null but is null"; + public String toExceptionMessage(String fieldName, String customMessage) { + if (customMessage == null) return fieldName + " is marked non-null but is null"; + return customMessage.replace("%s", fieldName); } public abstract String getExceptionType(); diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index bd0ad23b..bdb88299 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -1992,7 +1992,7 @@ public class EclipseHandlerUtil { * Generates a new statement that checks if the given local variable is null, and if so, throws a specified exception with the * variable name as message. */ - public static Statement generateNullCheck(TypeReference type, char[] variable, EclipseNode sourceNode) { + public static Statement generateNullCheck(TypeReference type, char[] variable, EclipseNode sourceNode, String customMessage) { NullCheckExceptionType exceptionType = sourceNode.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE); if (exceptionType == null) exceptionType = NullCheckExceptionType.NULL_POINTER_EXCEPTION; @@ -2001,11 +2001,11 @@ public class EclipseHandlerUtil { int pS = source.sourceStart, pE = source.sourceEnd; long p = (long) pS << 32 | pE; - if (isPrimitive(type)) return null; + if (type != null && isPrimitive(type)) return null; SingleNameReference varName = new SingleNameReference(variable, p); setGeneratedBy(varName, source); - StringLiteral message = new StringLiteral(exceptionType.toExceptionMessage(new String(variable)).toCharArray(), pS, pE, 0); + StringLiteral message = new StringLiteral(exceptionType.toExceptionMessage(new String(variable), customMessage).toCharArray(), pS, pE, 0); setGeneratedBy(message, source); LombokImmutableList method = exceptionType.getMethod(); @@ -2071,8 +2071,8 @@ public class EclipseHandlerUtil { * * @param exName The name of the exception to throw; normally {@code java.lang.NullPointerException}. */ - public static Statement generateNullCheck(AbstractVariableDeclaration variable, EclipseNode sourceNode) { - return generateNullCheck(variable.type, variable.name, sourceNode); + public static Statement generateNullCheck(AbstractVariableDeclaration variable, EclipseNode sourceNode, String customMessage) { + return generateNullCheck(variable.type, variable.name, sourceNode, customMessage); } /** diff --git a/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java b/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java index cbbd4cc8..85243ec1 100755 --- a/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java +++ b/src/core/lombok/eclipse/handlers/EclipseSingularsRecipes.java @@ -33,7 +33,6 @@ import java.util.Map; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; -import org.eclipse.jdt.internal.compiler.ast.AllocationExpression; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.Argument; import org.eclipse.jdt.internal.compiler.ast.Block; @@ -49,16 +48,13 @@ import org.eclipse.jdt.internal.compiler.ast.NullLiteral; import org.eclipse.jdt.internal.compiler.ast.OperatorIds; import org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference; import org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference; -import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference; import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference; import org.eclipse.jdt.internal.compiler.ast.Reference; import org.eclipse.jdt.internal.compiler.ast.ReturnStatement; import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference; import org.eclipse.jdt.internal.compiler.ast.Statement; -import org.eclipse.jdt.internal.compiler.ast.StringLiteral; import org.eclipse.jdt.internal.compiler.ast.ThisReference; -import org.eclipse.jdt.internal.compiler.ast.ThrowStatement; import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.eclipse.jdt.internal.compiler.ast.Wildcard; import org.eclipse.jdt.internal.compiler.lookup.ClassScope; @@ -67,12 +63,10 @@ import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; import org.eclipse.jdt.internal.compiler.lookup.TypeIds; import lombok.AccessLevel; -import lombok.Singular.NullCollectionBehavior; import lombok.core.LombokImmutableList; import lombok.core.SpiLoadUtil; import lombok.core.TypeLibrary; import lombok.core.configuration.CheckerFrameworkVersion; -import lombok.eclipse.Eclipse; import lombok.eclipse.EclipseNode; public class EclipseSingularsRecipes { @@ -135,14 +129,14 @@ public class EclipseSingularsRecipes { private final List typeArgs; private final String targetFqn; private final EclipseSingularizer singularizer; - private final NullCollectionBehavior nullCollectionBehavior; + private final boolean ignoreNullCollections; private final ASTNode source; - public SingularData(EclipseNode annotation, char[] singularName, char[] pluralName, List typeArgs, String targetFqn, EclipseSingularizer singularizer, ASTNode source, NullCollectionBehavior nullCollectionBehavior) { - this(annotation, singularName, pluralName, typeArgs, targetFqn, singularizer, source, nullCollectionBehavior, new char[0]); + public SingularData(EclipseNode annotation, char[] singularName, char[] pluralName, List typeArgs, String targetFqn, EclipseSingularizer singularizer, ASTNode source, boolean ignoreNullCollections) { + this(annotation, singularName, pluralName, typeArgs, targetFqn, singularizer, source, ignoreNullCollections, new char[0]); } - public SingularData(EclipseNode annotation, char[] singularName, char[] pluralName, List typeArgs, String targetFqn, EclipseSingularizer singularizer, ASTNode source, NullCollectionBehavior nullCollectionBehavior, char[] setterPrefix) { + public SingularData(EclipseNode annotation, char[] singularName, char[] pluralName, List typeArgs, String targetFqn, EclipseSingularizer singularizer, ASTNode source, boolean ignoreNullCollections, char[] setterPrefix) { this.annotation = annotation; this.singularName = singularName; this.pluralName = pluralName; @@ -150,7 +144,7 @@ public class EclipseSingularsRecipes { this.targetFqn = targetFqn; this.singularizer = singularizer; this.source = source; - this.nullCollectionBehavior = nullCollectionBehavior; + this.ignoreNullCollections = ignoreNullCollections; this.setterPrefix = setterPrefix; } @@ -198,8 +192,8 @@ public class EclipseSingularsRecipes { return singularizer; } - public NullCollectionBehavior getNullCollectionBehavior() { - return nullCollectionBehavior; + public boolean isIgnoreNullCollections() { + return ignoreNullCollections; } public String getTargetSimpleType() { @@ -439,9 +433,9 @@ public class EclipseSingularsRecipes { } protected void nullBehaviorize(EclipseNode typeNode, SingularData data, List statements, Argument arg) { - NullCollectionBehavior behavior = data.getNullCollectionBehavior(); + boolean ignoreNullCollections = data.isIgnoreNullCollections(); - if (behavior == NullCollectionBehavior.IGNORE) { + if (ignoreNullCollections) { Expression isNotNull = new EqualExpression(new SingleNameReference(data.getPluralName(), 0L), new NullLiteral(0, 0), OperatorIds.NOT_EQUAL); Block b = new Block(0); b.statements = statements.toArray(new Statement[statements.size()]); @@ -452,32 +446,8 @@ public class EclipseSingularsRecipes { } EclipseHandlerUtil.createRelevantNonNullAnnotation(typeNode, arg); - - String exceptionTypeStr = behavior.getExceptionType(); - StringLiteral message = new StringLiteral(behavior.toExceptionMessage(new String(data.getPluralName())).toCharArray(), 0, 0, 0); - if (exceptionTypeStr != null) { - Expression isNull = new EqualExpression(new SingleNameReference(data.getPluralName(), 0L), new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL); - int partCount = 1; - for (int i = 0; i < exceptionTypeStr.length(); i++) if (exceptionTypeStr.charAt(i) == '.') partCount++; - long[] ps = new long[partCount]; - Arrays.fill(ps, 0L); - AllocationExpression alloc = new AllocationExpression(); - alloc.type = new QualifiedTypeReference(Eclipse.fromQualifiedName(exceptionTypeStr), ps); - alloc.arguments = new Expression[] {message}; - Statement t = new ThrowStatement(alloc, 0, 0); - statements.add(0, new IfStatement(isNull, t, 0, 0)); - return; - } - - MessageSend invoke = new MessageSend(); - LombokImmutableList method = behavior.getMethod(); - char[][] utilityTypeName = new char[method.size() - 1][]; - for (int i = 0; i < method.size() - 1; i++) utilityTypeName[i] = method.get(i).toCharArray(); - - invoke.receiver = new QualifiedNameReference(utilityTypeName, new long[method.size() - 1], 0, 0); - invoke.selector = method.get(method.size() - 1).toCharArray(); - invoke.arguments = new Expression[] {new SingleNameReference(data.getPluralName(), 0L), message}; - statements.add(0, invoke); + Statement nullCheck = EclipseHandlerUtil.generateNullCheck(null, data.getPluralName(), typeNode, "%s cannot be null"); + statements.add(0, nullCheck); } protected abstract char[][] getEmptyMakerReceiver(String targetFqn); diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index 578fa2a3..19388eda 100755 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -79,7 +79,6 @@ import org.mangosdk.spi.ProviderFor; import lombok.AccessLevel; import lombok.Builder; import lombok.Builder.ObtainVia; -import lombok.Singular.NullCollectionBehavior; import lombok.ConfigurationKeys; import lombok.Singular; import lombok.ToString; @@ -1036,21 +1035,9 @@ public class HandleBuilder extends EclipseAnnotationHandler { return null; } - NullCollectionBehavior behavior = getNullBehaviorFor(ann, singularInstance, node); - return new SingularData(child, singularName, pluralName, typeArgs == null ? Collections.emptyList() : Arrays.asList(typeArgs), targetFqn, singularizer, source, behavior, setterPrefix.toCharArray()); + return new SingularData(child, singularName, pluralName, typeArgs == null ? Collections.emptyList() : Arrays.asList(typeArgs), targetFqn, singularizer, source, singularInstance.ignoreNullCollections(), setterPrefix.toCharArray()); } return null; } - - static NullCollectionBehavior getNullBehaviorFor(AnnotationValues ann, Singular singularInstance, EclipseNode node) { - NullCollectionBehavior behavior; - if (ann.isExplicit("nullBehavior")) { - behavior = singularInstance.nullBehavior(); - } else { - behavior = node.getAst().readConfiguration(ConfigurationKeys.SINGULAR_NULL_COLLECTIONS); - } - if (behavior == null) return NullCollectionBehavior.NULL_POINTER_EXCEPTION; - return behavior; - } } diff --git a/src/core/lombok/eclipse/handlers/HandleConstructor.java b/src/core/lombok/eclipse/handlers/HandleConstructor.java index a5716d9c..f69a2d1c 100755 --- a/src/core/lombok/eclipse/handlers/HandleConstructor.java +++ b/src/core/lombok/eclipse/handlers/HandleConstructor.java @@ -411,7 +411,7 @@ public class HandleConstructor { Argument parameter = new Argument(fieldName, fieldPos, copyType(field.type, source), Modifier.FINAL); Annotation[] copyableAnnotations = findCopyableAnnotations(fieldNode); if (hasNonNullAnnotations(fieldNode)) { - Statement nullCheck = generateNullCheck(parameter, sourceNode); + Statement nullCheck = generateNullCheck(parameter, sourceNode, null); if (nullCheck != null) nullChecks.add(nullCheck); } parameter.annotations = copyAnnotations(source, copyableAnnotations); diff --git a/src/core/lombok/eclipse/handlers/HandleNonNull.java b/src/core/lombok/eclipse/handlers/HandleNonNull.java index c61ce02d..c4d42c2e 100644 --- a/src/core/lombok/eclipse/handlers/HandleNonNull.java +++ b/src/core/lombok/eclipse/handlers/HandleNonNull.java @@ -145,7 +145,7 @@ public class HandleNonNull extends EclipseAnnotationHandler { // and if they exist, create a new method in the class: 'private static T lombok$nullCheck(T expr, String msg) {if (expr == null) throw NPE; return expr;}' and // wrap all references to it in the super/this to a call to this method. - Statement nullCheck = generateNullCheck(param, annotationNode); + Statement nullCheck = generateNullCheck(param, annotationNode, null); if (nullCheck == null) { // @NonNull applied to a primitive. Kinda pointless. Let's generate a warning. diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java index eda2c271..8ee54d0f 100644 --- a/src/core/lombok/eclipse/handlers/HandleSetter.java +++ b/src/core/lombok/eclipse/handlers/HandleSetter.java @@ -245,7 +245,7 @@ public class HandleSetter extends EclipseAnnotationHandler { if (!hasNonNullAnnotations(fieldNode) && !hasNonNullAnnotations(fieldNode, onParam)) { statements.add(assignment); } else { - Statement nullCheck = generateNullCheck(field.type, paramName, sourceNode); + Statement nullCheck = generateNullCheck(field.type, paramName, sourceNode, null); if (nullCheck != null) statements.add(nullCheck); statements.add(assignment); } diff --git a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java index 4b450a07..0a5acd56 100755 --- a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java @@ -79,7 +79,6 @@ import org.mangosdk.spi.ProviderFor; import lombok.AccessLevel; import lombok.Builder; import lombok.Builder.ObtainVia; -import lombok.Singular.NullCollectionBehavior; import lombok.ConfigurationKeys; import lombok.Singular; import lombok.ToString; @@ -567,7 +566,7 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { } if (hasNonNullAnnotations(fieldNode.originalFieldNode)) { - Statement nullCheck = generateNullCheck((FieldDeclaration) fieldNode.originalFieldNode.get(), sourceNode); + Statement nullCheck = generateNullCheck((FieldDeclaration) fieldNode.originalFieldNode.get(), sourceNode, null); if (nullCheck != null) statements.add(nullCheck); } } @@ -1044,8 +1043,7 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { return null; } - NullCollectionBehavior behavior = HandleBuilder.getNullBehaviorFor(ann, singularInstance, node); - return new SingularData(child, singularName, pluralName, typeArgs == null ? Collections.emptyList() : Arrays.asList(typeArgs), targetFqn, singularizer, source, behavior); + return new SingularData(child, singularName, pluralName, typeArgs == null ? Collections.emptyList() : Arrays.asList(typeArgs), targetFqn, singularizer, source, singularInstance.ignoreNullCollections()); } return null; diff --git a/src/core/lombok/eclipse/handlers/HandleWith.java b/src/core/lombok/eclipse/handlers/HandleWith.java index 4771818d..5cdc02df 100644 --- a/src/core/lombok/eclipse/handlers/HandleWith.java +++ b/src/core/lombok/eclipse/handlers/HandleWith.java @@ -278,7 +278,7 @@ public class HandleWith extends EclipseAnnotationHandler { List statements = new ArrayList(5); if (hasNonNullAnnotations(fieldNode)) { - Statement nullCheck = generateNullCheck(field, sourceNode); + Statement nullCheck = generateNullCheck(field, sourceNode, null); if (nullCheck != null) statements.add(nullCheck); } statements.add(returnStatement); diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index 75f3de2c..5c4b4297 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -61,7 +61,6 @@ import lombok.Builder; import lombok.Builder.ObtainVia; import lombok.ConfigurationKeys; import lombok.Singular; -import lombok.Singular.NullCollectionBehavior; import lombok.ToString; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; @@ -908,22 +907,9 @@ public class HandleBuilder extends JavacAnnotationHandler { return null; } - NullCollectionBehavior behavior = getNullBehaviorFor(ann, singularInstance, node); - - return new SingularData(child, singularName, pluralName, typeArgs, targetFqn, singularizer, behavior, setterPrefix); + return new SingularData(child, singularName, pluralName, typeArgs, targetFqn, singularizer, singularInstance.ignoreNullCollections(), setterPrefix); } return null; } - - static NullCollectionBehavior getNullBehaviorFor(AnnotationValues ann, Singular singularInstance, JavacNode node) { - NullCollectionBehavior behavior; - if (ann.isExplicit("nullBehavior")) { - behavior = singularInstance.nullBehavior(); - } else { - behavior = node.getAst().readConfiguration(ConfigurationKeys.SINGULAR_NULL_COLLECTIONS); - } - if (behavior == null) return NullCollectionBehavior.NULL_POINTER_EXCEPTION; - return behavior; - } } diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index 9c7ce042..32bf574b 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -248,7 +248,7 @@ public class HandleSetter extends JavacAnnotationHandler { if (!hasNonNullAnnotations(field) && !hasNonNullAnnotations(field, onParam)) { statements.append(treeMaker.Exec(assign)); } else { - JCStatement nullCheck = generateNullCheck(treeMaker, fieldDecl.vartype, paramName, source); + JCStatement nullCheck = generateNullCheck(treeMaker, fieldDecl.vartype, paramName, source, null); if (nullCheck != null) statements.append(nullCheck); statements.append(treeMaker.Exec(assign)); } diff --git a/src/core/lombok/javac/handlers/HandleSuperBuilder.java b/src/core/lombok/javac/handlers/HandleSuperBuilder.java index 692ee60b..b4b75d79 100644 --- a/src/core/lombok/javac/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/javac/handlers/HandleSuperBuilder.java @@ -59,7 +59,6 @@ import com.sun.tools.javac.util.Name; import lombok.AccessLevel; import lombok.Builder; import lombok.Builder.ObtainVia; -import lombok.Singular.NullCollectionBehavior; import lombok.ConfigurationKeys; import lombok.Singular; import lombok.ToString; @@ -976,8 +975,7 @@ public class HandleSuperBuilder extends JavacAnnotationHandler { return null; } - NullCollectionBehavior behavior = HandleBuilder.getNullBehaviorFor(ann, singularInstance, node); - return new SingularData(child, singularName, pluralName, typeArgs, targetFqn, singularizer, behavior); + return new SingularData(child, singularName, pluralName, typeArgs, targetFqn, singularizer, singularInstance.ignoreNullCollections()); } return null; diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 0ef8c359..3ec9f159 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -1537,12 +1537,12 @@ public class JavacHandlerUtil { * Generates a new statement that checks if the given local is null, and if so, throws a configured exception with the * local variable name as message. */ - public static JCStatement generateNullCheck(JavacTreeMaker maker, JCExpression typeNode, Name varName, JavacNode source) { + public static JCStatement generateNullCheck(JavacTreeMaker maker, JCExpression typeNode, Name varName, JavacNode source, String customMessage) { NullCheckExceptionType exceptionType = source.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE); if (exceptionType == null) exceptionType = NullCheckExceptionType.NULL_POINTER_EXCEPTION; - if (isPrimitive(typeNode)) return null; - JCLiteral message = maker.Literal(exceptionType.toExceptionMessage(varName.toString())); + if (typeNode != null && isPrimitive(typeNode)) return null; + JCLiteral message = maker.Literal(exceptionType.toExceptionMessage(varName.toString(), customMessage)); LombokImmutableList method = exceptionType.getMethod(); if (method != null) { @@ -1569,7 +1569,7 @@ public class JavacHandlerUtil { * stripped as a result of @Accessors.prefix. */ public static JCStatement generateNullCheck(JavacTreeMaker maker, JCVariableDecl varDecl, JavacNode source) { - return generateNullCheck(maker, varDecl.vartype, varDecl.name, source); + return generateNullCheck(maker, varDecl.vartype, varDecl.name, source, null); } /** diff --git a/src/core/lombok/javac/handlers/JavacSingularsRecipes.java b/src/core/lombok/javac/handlers/JavacSingularsRecipes.java index 9dab3da5..f4128f12 100644 --- a/src/core/lombok/javac/handlers/JavacSingularsRecipes.java +++ b/src/core/lombok/javac/handlers/JavacSingularsRecipes.java @@ -37,7 +37,6 @@ import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCBlock; import com.sun.tools.javac.tree.JCTree.JCExpression; -import com.sun.tools.javac.tree.JCTree.JCLiteral; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; import com.sun.tools.javac.tree.JCTree.JCModifiers; import com.sun.tools.javac.tree.JCTree.JCStatement; @@ -51,7 +50,6 @@ import com.sun.tools.javac.util.Name; import lombok.AccessLevel; import lombok.ConfigurationKeys; -import lombok.Singular.NullCollectionBehavior; import lombok.core.LombokImmutableList; import lombok.core.SpiLoadUtil; import lombok.core.TypeLibrary; @@ -122,13 +120,13 @@ public class JavacSingularsRecipes { private final String targetFqn; private final JavacSingularizer singularizer; private final String setterPrefix; - private final NullCollectionBehavior nullCollectionBehavior; + private final boolean ignoreNullCollections; - public SingularData(JavacNode annotation, Name singularName, Name pluralName, List typeArgs, String targetFqn, JavacSingularizer singularizer, NullCollectionBehavior nullCollectionBehavior) { - this(annotation, singularName, pluralName, typeArgs, targetFqn, singularizer, nullCollectionBehavior, ""); + public SingularData(JavacNode annotation, Name singularName, Name pluralName, List typeArgs, String targetFqn, JavacSingularizer singularizer, boolean ignoreNullCollections) { + this(annotation, singularName, pluralName, typeArgs, targetFqn, singularizer, ignoreNullCollections, ""); } - public SingularData(JavacNode annotation, Name singularName, Name pluralName, List typeArgs, String targetFqn, JavacSingularizer singularizer, NullCollectionBehavior nullCollectionBehavior, String setterPrefix) { + public SingularData(JavacNode annotation, Name singularName, Name pluralName, List typeArgs, String targetFqn, JavacSingularizer singularizer, boolean ignoreNullCollections, String setterPrefix) { this.annotation = annotation; this.singularName = singularName; this.pluralName = pluralName; @@ -136,7 +134,7 @@ public class JavacSingularsRecipes { this.targetFqn = targetFqn; this.singularizer = singularizer; this.setterPrefix = setterPrefix; - this.nullCollectionBehavior = nullCollectionBehavior; + this.ignoreNullCollections = ignoreNullCollections; } public JavacNode getAnnotation() { @@ -167,8 +165,8 @@ public class JavacSingularsRecipes { return singularizer; } - public NullCollectionBehavior getNullCollectionBehavior() { - return nullCollectionBehavior; + public boolean isIgnoreNullCollections() { + return ignoreNullCollections; } public String getTargetSimpleType() { @@ -273,17 +271,19 @@ public class JavacSingularsRecipes { generateClearMethod(cfv, deprecate, maker, returnTypeMaker.make(), returnStatementMaker.make(), data, builderType, source, access); } - private void finishAndInjectMethod(CheckerFrameworkVersion cfv, JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean deprecate, ListBuffer statements, Name methodName, List jcVariableDecls, AccessLevel access, NullCollectionBehavior nullBehavior) { + private void finishAndInjectMethod(CheckerFrameworkVersion cfv, JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean deprecate, ListBuffer statements, Name methodName, List jcVariableDecls, AccessLevel access, Boolean ignoreNullCollections) { if (returnStatement != null) statements.append(returnStatement); JCBlock body = maker.Block(0, statements.toList()); JCModifiers mods = makeMods(maker, cfv, builderType, deprecate, access); List typeParams = List.nil(); List thrown = List.nil(); - if (nullBehavior == NullCollectionBehavior.IGNORE) { - for (JCVariableDecl d : jcVariableDecls) createRelevantNullableAnnotation(builderType, d); - } else if (nullBehavior != null) { - for (JCVariableDecl d : jcVariableDecls) createRelevantNonNullAnnotation(builderType, d); + if (ignoreNullCollections != null) { + if (ignoreNullCollections.booleanValue()) { + for (JCVariableDecl d : jcVariableDecls) createRelevantNullableAnnotation(builderType, d); + } else { + for (JCVariableDecl d : jcVariableDecls) createRelevantNonNullAnnotation(builderType, d); + } } JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, jcVariableDecls, thrown, body, null); @@ -343,33 +343,21 @@ public class JavacSingularsRecipes { JCExpression paramType = getPluralMethodParamType(builderType); paramType = addTypeArgs(getTypeArgumentsCount(), true, builderType, paramType, data.getTypeArgs(), source); long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext()); - NullCollectionBehavior behavior = data.getNullCollectionBehavior(); - if (behavior == null) behavior = NullCollectionBehavior.IGNORE; + boolean ignoreNullCollections = data.isIgnoreNullCollections(); JCModifiers paramMods = maker.Modifiers(paramFlags); JCVariableDecl param = maker.VarDef(paramMods, data.getPluralName(), paramType, null); statements.prepend(createConstructBuilderVarIfNeeded(maker, data, builderType, source)); - if (behavior == NullCollectionBehavior.IGNORE) { + if (ignoreNullCollections) { JCExpression incomingIsNotNull = maker.Binary(CTC_NOT_EQUAL, maker.Ident(data.getPluralName()), maker.Literal(CTC_BOT, null)); JCStatement onNotNull = maker.Block(0, statements.toList()); statements = new ListBuffer(); statements.add(maker.If(incomingIsNotNull, onNotNull, null)); } else { - JCLiteral message = maker.Literal(behavior.toExceptionMessage(data.getPluralName().toString())); - if (behavior.getExceptionType() != null) { - JCExpression incomingIsNull = maker.Binary(CTC_EQUAL, maker.Ident(data.getPluralName()), maker.Literal(CTC_BOT, null)); - JCExpression exType = genTypeRef(builderType, behavior.getExceptionType()); - JCExpression exception = maker.NewClass(null, List.nil(), exType, List.of(message), null); - JCStatement onNull = maker.Throw(exception); - statements.prepend(maker.If(incomingIsNull, onNull, null)); - } else { - LombokImmutableList method = behavior.getMethod(); - JCExpression invoke = maker.Apply(List.nil(), chainDots(builderType, method), List.of(maker.Ident(data.getPluralName()), message)); - statements.prepend(maker.Exec(invoke)); - } + statements.prepend(JavacHandlerUtil.generateNullCheck(maker, null, data.getPluralName(), builderType, "%s cannot be null")); } - finishAndInjectMethod(cfv, maker, returnType, returnStatement, data, builderType, source, deprecate, statements, name, List.of(param), access, behavior); + finishAndInjectMethod(cfv, maker, returnType, returnStatement, data, builderType, source, deprecate, statements, name, List.of(param), access, ignoreNullCollections); } protected ListBuffer generatePluralMethodStatements(JavacTreeMaker maker, SingularData data, JavacNode builderType, JCTree source) { diff --git a/test/transform/resource/after-delombok/BuilderDefaultsWarnings.java b/test/transform/resource/after-delombok/BuilderDefaultsWarnings.java index 04cd3bbb..3c84c71b 100644 --- a/test/transform/resource/after-delombok/BuilderDefaultsWarnings.java +++ b/test/transform/resource/after-delombok/BuilderDefaultsWarnings.java @@ -38,7 +38,9 @@ public class BuilderDefaultsWarnings { } @java.lang.SuppressWarnings("all") public BuilderDefaultsWarnings.BuilderDefaultsWarningsBuilder items(final java.util.Collection items) { - if (items == null) throw new java.lang.NullPointerException("items cannot be null"); + if (items == null) { + throw new java.lang.NullPointerException("items cannot be null"); + } if (this.items == null) this.items = new java.util.ArrayList(); this.items.addAll(items); return this; diff --git a/test/transform/resource/after-delombok/BuilderSingularAnnotatedTypes.java b/test/transform/resource/after-delombok/BuilderSingularAnnotatedTypes.java index 4cc247ef..f6fa22de 100644 --- a/test/transform/resource/after-delombok/BuilderSingularAnnotatedTypes.java +++ b/test/transform/resource/after-delombok/BuilderSingularAnnotatedTypes.java @@ -36,7 +36,9 @@ class BuilderSingularAnnotatedTypes { } @java.lang.SuppressWarnings("all") public BuilderSingularAnnotatedTypes.BuilderSingularAnnotatedTypesBuilder foos(final java.util.Collection foos) { - if (foos == null) throw new java.lang.NullPointerException("foos cannot be null"); + if (foos == null) { + throw new java.lang.NullPointerException("foos cannot be null"); + } if (this.foos == null) this.foos = new java.util.ArrayList<@MyAnnotation @NonNull String>(); this.foos.addAll(foos); return this; @@ -64,7 +66,9 @@ class BuilderSingularAnnotatedTypes { } @java.lang.SuppressWarnings("all") public BuilderSingularAnnotatedTypes.BuilderSingularAnnotatedTypesBuilder bars(final java.util.Map bars) { - if (bars == null) throw new java.lang.NullPointerException("bars cannot be null"); + if (bars == null) { + throw new java.lang.NullPointerException("bars cannot be null"); + } if (this.bars$key == null) { this.bars$key = new java.util.ArrayList<@MyAnnotation @NonNull String>(); this.bars$value = new java.util.ArrayList<@MyAnnotation @NonNull Integer>(); diff --git a/test/transform/resource/after-delombok/BuilderSingularAnnotatedTypesWithSetterPrefix.java b/test/transform/resource/after-delombok/BuilderSingularAnnotatedTypesWithSetterPrefix.java index 90cc5570..6dbbeee4 100644 --- a/test/transform/resource/after-delombok/BuilderSingularAnnotatedTypesWithSetterPrefix.java +++ b/test/transform/resource/after-delombok/BuilderSingularAnnotatedTypesWithSetterPrefix.java @@ -36,7 +36,9 @@ class BuilderSingularAnnotatedTypesWithSetterPrefix { } @java.lang.SuppressWarnings("all") public BuilderSingularAnnotatedTypesWithSetterPrefix.BuilderSingularAnnotatedTypesWithSetterPrefixBuilder withFoos(final java.util.Collection foos) { - if (foos == null) throw new java.lang.NullPointerException("foos cannot be null"); + if (foos == null) { + throw new java.lang.NullPointerException("foos cannot be null"); + } if (this.foos == null) this.foos = new java.util.ArrayList<@MyAnnotation @NonNull String>(); this.foos.addAll(foos); return this; @@ -64,7 +66,9 @@ class BuilderSingularAnnotatedTypesWithSetterPrefix { } @java.lang.SuppressWarnings("all") public BuilderSingularAnnotatedTypesWithSetterPrefix.BuilderSingularAnnotatedTypesWithSetterPrefixBuilder withBars(final java.util.Map bars) { - if (bars == null) throw new java.lang.NullPointerException("bars cannot be null"); + if (bars == null) { + throw new java.lang.NullPointerException("bars cannot be null"); + } if (this.bars$key == null) { this.bars$key = new java.util.ArrayList<@MyAnnotation @NonNull String>(); this.bars$value = new java.util.ArrayList<@MyAnnotation @NonNull Integer>(); diff --git a/test/transform/resource/after-delombok/BuilderSingularGuavaListsSets.java b/test/transform/resource/after-delombok/BuilderSingularGuavaListsSets.java index 650ab2fd..32b7c084 100644 --- a/test/transform/resource/after-delombok/BuilderSingularGuavaListsSets.java +++ b/test/transform/resource/after-delombok/BuilderSingularGuavaListsSets.java @@ -41,7 +41,9 @@ class BuilderSingularGuavaListsSets { } @java.lang.SuppressWarnings("all") public BuilderSingularGuavaListsSets.BuilderSingularGuavaListsSetsBuilder cards(final java.lang.Iterable cards) { - if (cards == null) throw new java.lang.NullPointerException("cards cannot be null"); + if (cards == null) { + throw new java.lang.NullPointerException("cards cannot be null"); + } if (this.cards == null) this.cards = com.google.common.collect.ImmutableList.builder(); this.cards.addAll(cards); return this; @@ -59,7 +61,9 @@ class BuilderSingularGuavaListsSets { } @java.lang.SuppressWarnings("all") public BuilderSingularGuavaListsSets.BuilderSingularGuavaListsSetsBuilder frogs(final java.lang.Iterable frogs) { - if (frogs == null) throw new java.lang.NullPointerException("frogs cannot be null"); + if (frogs == null) { + throw new java.lang.NullPointerException("frogs cannot be null"); + } if (this.frogs == null) this.frogs = com.google.common.collect.ImmutableList.builder(); this.frogs.addAll(frogs); return this; @@ -77,7 +81,9 @@ class BuilderSingularGuavaListsSets { } @java.lang.SuppressWarnings("all") public BuilderSingularGuavaListsSets.BuilderSingularGuavaListsSetsBuilder rawSet(final java.lang.Iterable rawSet) { - if (rawSet == null) throw new java.lang.NullPointerException("rawSet cannot be null"); + if (rawSet == null) { + throw new java.lang.NullPointerException("rawSet cannot be null"); + } if (this.rawSet == null) this.rawSet = com.google.common.collect.ImmutableSet.builder(); this.rawSet.addAll(rawSet); return this; @@ -95,7 +101,9 @@ class BuilderSingularGuavaListsSets { } @java.lang.SuppressWarnings("all") public BuilderSingularGuavaListsSets.BuilderSingularGuavaListsSetsBuilder passes(final java.lang.Iterable passes) { - if (passes == null) throw new java.lang.NullPointerException("passes cannot be null"); + if (passes == null) { + throw new java.lang.NullPointerException("passes cannot be null"); + } if (this.passes == null) this.passes = com.google.common.collect.ImmutableSortedSet.naturalOrder(); this.passes.addAll(passes); return this; @@ -113,7 +121,9 @@ class BuilderSingularGuavaListsSets { } @java.lang.SuppressWarnings("all") public BuilderSingularGuavaListsSets.BuilderSingularGuavaListsSetsBuilder users(final com.google.common.collect.Table users) { - if (users == null) throw new java.lang.NullPointerException("users cannot be null"); + if (users == null) { + throw new java.lang.NullPointerException("users cannot be null"); + } if (this.users == null) this.users = com.google.common.collect.ImmutableTable.builder(); this.users.putAll(users); return this; diff --git a/test/transform/resource/after-delombok/BuilderSingularGuavaMaps.java b/test/transform/resource/after-delombok/BuilderSingularGuavaMaps.java index 6af1bf8d..9e3c6c15 100644 --- a/test/transform/resource/after-delombok/BuilderSingularGuavaMaps.java +++ b/test/transform/resource/after-delombok/BuilderSingularGuavaMaps.java @@ -31,7 +31,9 @@ class BuilderSingularGuavaMaps { } @java.lang.SuppressWarnings("all") public BuilderSingularGuavaMaps.BuilderSingularGuavaMapsBuilder battleaxes(final java.util.Map battleaxes) { - if (battleaxes == null) throw new java.lang.NullPointerException("battleaxes cannot be null"); + if (battleaxes == null) { + throw new java.lang.NullPointerException("battleaxes cannot be null"); + } if (this.battleaxes == null) this.battleaxes = com.google.common.collect.ImmutableMap.builder(); this.battleaxes.putAll(battleaxes); return this; @@ -49,7 +51,9 @@ class BuilderSingularGuavaMaps { } @java.lang.SuppressWarnings("all") public BuilderSingularGuavaMaps.BuilderSingularGuavaMapsBuilder vertices(final java.util.Map vertices) { - if (vertices == null) throw new java.lang.NullPointerException("vertices cannot be null"); + if (vertices == null) { + throw new java.lang.NullPointerException("vertices cannot be null"); + } if (this.vertices == null) this.vertices = com.google.common.collect.ImmutableSortedMap.naturalOrder(); this.vertices.putAll(vertices); return this; @@ -67,7 +71,9 @@ class BuilderSingularGuavaMaps { } @java.lang.SuppressWarnings("all") public BuilderSingularGuavaMaps.BuilderSingularGuavaMapsBuilder rawMap(final java.util.Map rawMap) { - if (rawMap == null) throw new java.lang.NullPointerException("rawMap cannot be null"); + if (rawMap == null) { + throw new java.lang.NullPointerException("rawMap cannot be null"); + } if (this.rawMap == null) this.rawMap = com.google.common.collect.ImmutableBiMap.builder(); this.rawMap.putAll(rawMap); return this; diff --git a/test/transform/resource/after-delombok/BuilderSingularLists.java b/test/transform/resource/after-delombok/BuilderSingularLists.java index 43eb7702..f722dfcd 100644 --- a/test/transform/resource/after-delombok/BuilderSingularLists.java +++ b/test/transform/resource/after-delombok/BuilderSingularLists.java @@ -30,7 +30,9 @@ class BuilderSingularLists { } @java.lang.SuppressWarnings("all") public BuilderSingularLists.BuilderSingularListsBuilder children(final java.util.Collection children) { - if (children == null) throw new java.lang.NullPointerException("children cannot be null"); + if (children == null) { + throw new java.lang.NullPointerException("children cannot be null"); + } if (this.children == null) this.children = new java.util.ArrayList(); this.children.addAll(children); return this; @@ -48,7 +50,9 @@ class BuilderSingularLists { } @java.lang.SuppressWarnings("all") public BuilderSingularLists.BuilderSingularListsBuilder scarves(final java.util.Collection scarves) { - if (scarves == null) throw new java.lang.NullPointerException("scarves cannot be null"); + if (scarves == null) { + throw new java.lang.NullPointerException("scarves cannot be null"); + } if (this.scarves == null) this.scarves = new java.util.ArrayList(); this.scarves.addAll(scarves); return this; @@ -66,7 +70,9 @@ class BuilderSingularLists { } @java.lang.SuppressWarnings("all") public BuilderSingularLists.BuilderSingularListsBuilder rawList(final java.util.Collection rawList) { - if (rawList == null) throw new java.lang.NullPointerException("rawList cannot be null"); + if (rawList == null) { + throw new java.lang.NullPointerException("rawList cannot be null"); + } if (this.rawList == null) this.rawList = new java.util.ArrayList(); this.rawList.addAll(rawList); return this; diff --git a/test/transform/resource/after-delombok/BuilderSingularMaps.java b/test/transform/resource/after-delombok/BuilderSingularMaps.java index d10dd090..f6073141 100644 --- a/test/transform/resource/after-delombok/BuilderSingularMaps.java +++ b/test/transform/resource/after-delombok/BuilderSingularMaps.java @@ -46,7 +46,9 @@ class BuilderSingularMaps { } @SuppressWarnings("all") public BuilderSingularMaps.BuilderSingularMapsBuilder women(java.util.Map women) { - if (women == null) throw new NullPointerException("women cannot be null"); + if (women == null) { + throw new NullPointerException("women cannot be null"); + } if (this.women$key == null) { this.women$key = new java.util.ArrayList(); this.women$value = new java.util.ArrayList(); @@ -77,7 +79,9 @@ class BuilderSingularMaps { } @SuppressWarnings("all") public BuilderSingularMaps.BuilderSingularMapsBuilder men(java.util.Map men) { - if (men == null) throw new NullPointerException("men cannot be null"); + if (men == null) { + throw new NullPointerException("men cannot be null"); + } if (this.men$key == null) { this.men$key = new java.util.ArrayList(); this.men$value = new java.util.ArrayList(); @@ -108,7 +112,9 @@ class BuilderSingularMaps { } @SuppressWarnings("all") public BuilderSingularMaps.BuilderSingularMapsBuilder rawMap(java.util.Map rawMap) { - if (rawMap == null) throw new NullPointerException("rawMap cannot be null"); + if (rawMap == null) { + throw new NullPointerException("rawMap cannot be null"); + } if (this.rawMap$key == null) { this.rawMap$key = new java.util.ArrayList(); this.rawMap$value = new java.util.ArrayList(); @@ -139,7 +145,9 @@ class BuilderSingularMaps { } @SuppressWarnings("all") public BuilderSingularMaps.BuilderSingularMapsBuilder stringMap(java.util.Map stringMap) { - if (stringMap == null) throw new NullPointerException("stringMap cannot be null"); + if (stringMap == null) { + throw new NullPointerException("stringMap cannot be null"); + } if (this.stringMap$key == null) { this.stringMap$key = new java.util.ArrayList(); this.stringMap$value = new java.util.ArrayList(); diff --git a/test/transform/resource/after-delombok/BuilderSingularMapsWithSetterPrefix.java b/test/transform/resource/after-delombok/BuilderSingularMapsWithSetterPrefix.java index baa8f311..b3e92fb0 100644 --- a/test/transform/resource/after-delombok/BuilderSingularMapsWithSetterPrefix.java +++ b/test/transform/resource/after-delombok/BuilderSingularMapsWithSetterPrefix.java @@ -46,15 +46,16 @@ class BuilderSingularMapsWithSetterPrefix { } @SuppressWarnings("all") public BuilderSingularMapsWithSetterPrefix.BuilderSingularMapsWithSetterPrefixBuilder withWomen(java.util.Map women) { - if (women != null) { - if (this.women$key == null) { - this.women$key = new java.util.ArrayList(); - this.women$value = new java.util.ArrayList(); - } - for (java.util.Map.Entry $lombokEntry : women.entrySet()) { - this.women$key.add($lombokEntry.getKey()); - this.women$value.add($lombokEntry.getValue()); - } + if (women == null) { + throw new NullPointerException("women cannot be null"); + } + if (this.women$key == null) { + this.women$key = new java.util.ArrayList(); + this.women$value = new java.util.ArrayList(); + } + for (java.util.Map.Entry $lombokEntry : women.entrySet()) { + this.women$key.add($lombokEntry.getKey()); + this.women$value.add($lombokEntry.getValue()); } return this; } @@ -78,15 +79,16 @@ class BuilderSingularMapsWithSetterPrefix { } @SuppressWarnings("all") public BuilderSingularMapsWithSetterPrefix.BuilderSingularMapsWithSetterPrefixBuilder withMen(java.util.Map men) { - if (men != null) { - if (this.men$key == null) { - this.men$key = new java.util.ArrayList(); - this.men$value = new java.util.ArrayList(); - } - for (java.util.Map.Entry $lombokEntry : men.entrySet()) { - this.men$key.add($lombokEntry.getKey()); - this.men$value.add($lombokEntry.getValue()); - } + if (men == null) { + throw new NullPointerException("men cannot be null"); + } + if (this.men$key == null) { + this.men$key = new java.util.ArrayList(); + this.men$value = new java.util.ArrayList(); + } + for (java.util.Map.Entry $lombokEntry : men.entrySet()) { + this.men$key.add($lombokEntry.getKey()); + this.men$value.add($lombokEntry.getValue()); } return this; } @@ -142,15 +144,16 @@ class BuilderSingularMapsWithSetterPrefix { } @SuppressWarnings("all") public BuilderSingularMapsWithSetterPrefix.BuilderSingularMapsWithSetterPrefixBuilder withStringMap(java.util.Map stringMap) { - if (stringMap != null) { - if (this.stringMap$key == null) { - this.stringMap$key = new java.util.ArrayList(); - this.stringMap$value = new java.util.ArrayList(); - } - for (java.util.Map.Entry $lombokEntry : stringMap.entrySet()) { - this.stringMap$key.add($lombokEntry.getKey()); - this.stringMap$value.add($lombokEntry.getValue()); - } + if (stringMap == null) { + throw new NullPointerException("stringMap cannot be null"); + } + if (this.stringMap$key == null) { + this.stringMap$key = new java.util.ArrayList(); + this.stringMap$value = new java.util.ArrayList(); + } + for (java.util.Map.Entry $lombokEntry : stringMap.entrySet()) { + this.stringMap$key.add($lombokEntry.getKey()); + this.stringMap$value.add($lombokEntry.getValue()); } return this; } diff --git a/test/transform/resource/after-delombok/BuilderSingularNoAuto.java b/test/transform/resource/after-delombok/BuilderSingularNoAuto.java index 6b2fd5b2..ba6ddc52 100644 --- a/test/transform/resource/after-delombok/BuilderSingularNoAuto.java +++ b/test/transform/resource/after-delombok/BuilderSingularNoAuto.java @@ -28,7 +28,9 @@ class BuilderSingularNoAuto { } @java.lang.SuppressWarnings("all") public BuilderSingularNoAuto.BuilderSingularNoAutoBuilder things(final java.util.Collection things) { - if (things == null) throw new java.lang.NullPointerException("things cannot be null"); + if (things == null) { + throw new java.lang.NullPointerException("things cannot be null"); + } if (this.things == null) this.things = new java.util.ArrayList(); this.things.addAll(things); return this; @@ -46,7 +48,9 @@ class BuilderSingularNoAuto { } @java.lang.SuppressWarnings("all") public BuilderSingularNoAuto.BuilderSingularNoAutoBuilder widgets(final java.util.Collection widgets) { - if (widgets == null) throw new java.lang.NullPointerException("widgets cannot be null"); + if (widgets == null) { + throw new java.lang.NullPointerException("widgets cannot be null"); + } if (this.widgets == null) this.widgets = new java.util.ArrayList(); this.widgets.addAll(widgets); return this; @@ -64,7 +68,9 @@ class BuilderSingularNoAuto { } @java.lang.SuppressWarnings("all") public BuilderSingularNoAuto.BuilderSingularNoAutoBuilder items(final java.util.Collection items) { - if (items == null) throw new java.lang.NullPointerException("items cannot be null"); + if (items == null) { + throw new java.lang.NullPointerException("items