From 9198551defb7dd71d872c7b86af0a3f0bf0ec545 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 17 Sep 2018 23:44:26 +0200 Subject: Finishing work on making lombok do sensible things with TYPE_USE annotations and for example their use on the typearg in a collection type which is being `@Singular`-ized. --- doc/changelog.markdown | 2 + .../eclipse/handlers/EclipseHandlerUtil.java | 18 +++ .../lombok/eclipse/handlers/HandleNonNull.java | 21 +++- .../eclipse/handlers/HandleSuperBuilder.java | 10 +- .../singulars/EclipseGuavaSingularizer.java | 10 +- .../EclipseJavaUtilListSetSingularizer.java | 11 +- .../singulars/EclipseJavaUtilMapSingularizer.java | 16 ++- .../lombok/javac/handlers/HandleSuperBuilder.java | 8 +- .../lombok/javac/handlers/JavacHandlerUtil.java | 72 ++++++++++- .../handlers/singulars/JavacGuavaSingularizer.java | 6 +- .../JavacJavaUtilListSetSingularizer.java | 6 +- .../singulars/JavacJavaUtilMapSingularizer.java | 11 +- .../BuilderSingularAnnotatedTypes.java | 58 +++++---- .../SuperBuilderSingularAnnotatedTypes.java | 137 +++++++++++++++++++++ .../after-delombok/SuperBuilderWithNonNull.java | 10 +- .../after-ecj/BuilderDefaultsWarnings.java | 4 +- .../after-ecj/BuilderSingularAnnotatedTypes.java | 60 +++++---- .../after-ecj/BuilderSingularGuavaListsSets.java | 20 +-- .../after-ecj/BuilderSingularGuavaMaps.java | 12 +- .../resource/after-ecj/BuilderSingularLists.java | 12 +- .../resource/after-ecj/BuilderSingularMaps.java | 16 +-- .../resource/after-ecj/BuilderSingularNoAuto.java | 12 +- .../after-ecj/BuilderSingularRedirectToGuava.java | 12 +- .../resource/after-ecj/BuilderSingularSets.java | 16 +-- .../BuilderSingularToBuilderWithNull.java | 4 +- .../after-ecj/BuilderSingularWithPrefixes.java | 4 +- .../resource/after-ecj/BuilderWithDeprecated.java | 8 +- .../resource/after-ecj/BuilderWithToBuilder.java | 4 +- .../resource/after-ecj/SuperBuilderBasic.java | 4 +- .../SuperBuilderSingularAnnotatedTypes.java | 131 ++++++++++++++++++++ .../SuperBuilderWithCustomBuilderMethod.java | 4 +- .../after-ecj/SuperBuilderWithGenerics.java | 4 +- .../after-ecj/SuperBuilderWithGenerics2.java | 4 +- .../after-ecj/SuperBuilderWithNonNull.java | 4 +- .../after-ecj/SuperBuilderWithPrefixes.java | 4 +- .../before/BuilderSingularAnnotatedTypes.java | 10 +- .../before/SuperBuilderSingularAnnotatedTypes.java | 14 +++ 37 files changed, 612 insertions(+), 147 deletions(-) create mode 100644 test/transform/resource/after-delombok/SuperBuilderSingularAnnotatedTypes.java create mode 100644 test/transform/resource/after-ecj/SuperBuilderSingularAnnotatedTypes.java create mode 100644 test/transform/resource/before/SuperBuilderSingularAnnotatedTypes.java diff --git a/doc/changelog.markdown b/doc/changelog.markdown index 04c9a052..220e0dc0 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -4,6 +4,8 @@ Lombok Changelog ### v1.18.3 "Edgy Guinea Pig" * PLATFORM: Support for Eclipse Photon. [Issue #1831](https://github.com/rzwitserloot/lombok/issues/1831) * FEATURE: The `@FieldNameConstants` feature has been completely redesigned. [Issue #1774](https://github.com/rzwitserloot/lombok/issues/1774) [FieldNameConstants documentation](https://projectlombok.org/features/experimental/FieldNameConstants) +* FEATURE: Lombok's `@NonNull` annotation can now be used on types (annotation on types has been introduced in JDK 8). `@Builder`'s `@Singular` annotation now properly deals with annotations on the generics type on the collection: `@Singular List<@NonNull String> names;` now does the right thing. +* BREAKING CHANGE: Lombok will now always copy specific annotations around (from field to getter, from field to builder 'setter', etcetera): A specific curated list of known annotations where that is the right thing to do (generally, `@NonNull` style annotations from various libraries), as well as any annotations you explicitly list in the `lombok.copyableAnnotations` config key in your `lombok.config` file. Also, lombok is more consistent about copying these annotations. (Previous behaviour: Lombok used to copy any annotation whose simple name was `NonNull`, `Nullable`, or `CheckForNull`). [Issue #1570](https://github.com/rzwitserloot/lombok/issues/1570) and [Issue #1634](https://github.com/rzwitserloot/lombok/issues/1634) * BUGFIX: When using lombok to compile modularized (`module-info.java`-style) code, if the module name has dots in it, it wouldn't work. [Issue #1808](https://github.com/rzwitserloot/lombok/issues/1808) * BUGFIX: Errors about lombok not reading a module providing `org.mapstruct.ap.spi` when trying to use lombok in jigsaw-mode on JDK 11. [Issue #1806](https://github.com/rzwitserloot/lombok/issues/1806) * BUGFIX: Fix NetBeans compile on save. [Issue #1770](https://github.com/rzwitserloot/lombok/issues/1770) diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index a972c1fe..5d582aad 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -449,6 +449,24 @@ public class EclipseHandlerUtil { return out; } + public static Annotation[] getTypeUseAnnotations(TypeReference from) { + Annotation[][] a; + try { + a = (Annotation[][]) reflect(TYPE_REFERENCE__ANNOTATIONS, from); + } catch (Exception e) { + return null; + } + if (a == null) return null; + Annotation[] b = a[a.length - 1]; + return b.length == 0 ? null : b; + } + + public static void removeTypeUseAnnotations(TypeReference from) { + try { + reflectSet(TYPE_REFERENCE__ANNOTATIONS, from, null); + } catch (Exception ignore) {} + } + public static TypeReference namePlusTypeParamsToTypeReference(char[] typeName, TypeParameter[] params, long p) { if (params != null && params.length > 0) { TypeReference[] refs = new TypeReference[params.length]; diff --git a/src/core/lombok/eclipse/handlers/HandleNonNull.java b/src/core/lombok/eclipse/handlers/HandleNonNull.java index d09993ed..ebc62909 100644 --- a/src/core/lombok/eclipse/handlers/HandleNonNull.java +++ b/src/core/lombok/eclipse/handlers/HandleNonNull.java @@ -58,7 +58,26 @@ import org.mangosdk.spi.ProviderFor; @ProviderFor(EclipseAnnotationHandler.class) @HandlerPriority(value = 512) // 2^9; onParameter=@__(@NonNull) has to run first. public class HandleNonNull extends EclipseAnnotationHandler { + public static final HandleNonNull INSTANCE = new HandleNonNull(); + + public void fix(EclipseNode method) { + for (EclipseNode m : method.down()) { + if (m.getKind() != Kind.ARGUMENT) continue; + for (EclipseNode c : m.down()) { + if (c.getKind() == Kind.ANNOTATION) { + if (annotationTypeMatches(NonNull.class, c)) { + handle0((Annotation) c.get(), c, true); + } + } + } + } + } + @Override public void handle(AnnotationValues annotation, Annotation ast, EclipseNode annotationNode) { + handle0(ast, annotationNode, false); + } + + private void handle0(Annotation ast, EclipseNode annotationNode, boolean force) { handleFlagUsage(annotationNode, ConfigurationKeys.NON_NULL_FLAG_USAGE, "@NonNull"); if (annotationNode.up().getKind() == Kind.FIELD) { @@ -88,7 +107,7 @@ public class HandleNonNull extends EclipseAnnotationHandler { return; } - if (isGenerated(declaration)) return; + if (!force && isGenerated(declaration)) return; if (declaration.isAbstract()) { // This used to be a warning, but as @NonNull also has a documentary purpose, better to not warn about this. Since 1.16.7 diff --git a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java index 559cca20..3070f1dc 100644 --- a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java @@ -102,6 +102,7 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { private static final AbstractMethodDeclaration[] EMPTY_METHODS = {}; private static class BuilderFieldData { + Annotation[] annotations; TypeReference type; char[] rawName; char[] name; @@ -154,9 +155,12 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { EclipseNode isDefault = findAnnotation(Builder.Default.class, fieldNode); boolean isFinal = ((fd.modifiers & ClassFileConstants.AccFinal) != 0) || (valuePresent && !hasAnnotation(NonFinal.class, fieldNode)); + Annotation[] copyableAnnotations = findCopyableAnnotations(fieldNode); + BuilderFieldData bfd = new BuilderFieldData(); bfd.rawName = fieldNode.getName().toCharArray(); bfd.name = removePrefixFromField(fieldNode); + bfd.annotations = copyAnnotations(fd, copyableAnnotations); bfd.type = fd.type; bfd.singularData = getSingularData(fieldNode, ast); bfd.originalFieldNode = fieldNode; @@ -665,13 +669,13 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { }; if (bfd.singularData == null || bfd.singularData.getSingularizer() == null) { - generateSimpleSetterMethodForBuilder(builderType, deprecate, bfd.createdFields.get(0), bfd.nameOfSetFlag, returnTypeMaker.make(), returnStatementMaker.make(), sourceNode); + generateSimpleSetterMethodForBuilder(builderType, deprecate, bfd.createdFields.get(0), bfd.nameOfSetFlag, returnTypeMaker.make(), returnStatementMaker.make(), sourceNode, bfd.annotations); } else { bfd.singularData.getSingularizer().generateMethods(bfd.singularData, deprecate, builderType, true, returnTypeMaker, returnStatementMaker); } } - private void generateSimpleSetterMethodForBuilder(EclipseNode builderType, boolean deprecate, EclipseNode fieldNode, char[] nameOfSetFlag, TypeReference returnType, Statement returnStatement, EclipseNode sourceNode) { + private void generateSimpleSetterMethodForBuilder(EclipseNode builderType, boolean deprecate, EclipseNode fieldNode, char[] nameOfSetFlag, TypeReference returnType, Statement returnStatement, EclipseNode sourceNode, Annotation[] annosOnParam) { TypeDeclaration td = (TypeDeclaration) builderType.get(); AbstractMethodDeclaration[] existing = td.methods; if (existing == null) existing = EMPTY_METHODS; @@ -688,7 +692,7 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { String setterName = fieldNode.getName(); MethodDeclaration setter = HandleSetter.createSetter(td, deprecate, fieldNode, setterName, nameOfSetFlag, returnType, returnStatement, ClassFileConstants.AccPublic, - sourceNode, Collections.emptyList(), Collections.emptyList()); + sourceNode, Collections.emptyList(), annosOnParam != null ? Arrays.asList(copyAnnotations(sourceNode.get(), annosOnParam)) : Collections.emptyList()); injectMethod(builderType, setter); } diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java index f1687c9c..17fc5e09 100644 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java @@ -32,6 +32,7 @@ import lombok.core.GuavaTypeMap; import lombok.core.LombokImmutableList; import lombok.core.handlers.HandlerUtil; import lombok.eclipse.EclipseNode; +import lombok.eclipse.handlers.HandleNonNull; import lombok.eclipse.handlers.EclipseSingularsRecipes.EclipseSingularizer; import lombok.eclipse.handlers.EclipseSingularsRecipes.SingularData; import lombok.eclipse.handlers.EclipseSingularsRecipes.StatementMaker; @@ -150,14 +151,17 @@ abstract class EclipseGuavaSingularizer extends EclipseSingularizer { md.arguments = new Argument[suffixes.size()]; for (int i = 0; i < suffixes.size(); i++) { TypeReference tr = cloneParamType(i, data.getTypeArgs(), builderType); - md.arguments[i] = new Argument(names[i], 0, tr, 0); + Annotation[] typeUseAnns = getTypeUseAnnotations(tr); + removeTypeUseAnnotations(tr); + md.arguments[i] = new Argument(names[i], 0, tr, ClassFileConstants.AccFinal); + md.arguments[i].annotations = typeUseAnns; } md.returnType = returnType; md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName(getAddMethodName(), new String(data.getSingularName())).toCharArray(); md.annotations = deprecate ? new Annotation[] { generateDeprecatedAnnotation(data.getSource()) } : null; data.setGeneratedByRecursive(md); - injectMethod(builderType, md); + HandleNonNull.INSTANCE.fix(injectMethod(builderType, md)); } void generatePluralMethod(boolean deprecate, TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) { @@ -182,7 +186,7 @@ abstract class EclipseGuavaSingularizer extends EclipseSingularizer { TypeReference paramType; paramType = new QualifiedTypeReference(fromQualifiedName(getAddAllTypeName()), NULL_POSS); paramType = addTypeArgs(getTypeArgumentsCount(), true, builderType, paramType, data.getTypeArgs()); - Argument param = new Argument(data.getPluralName(), 0, paramType, 0); + Argument param = new Argument(data.getPluralName(), 0, paramType, ClassFileConstants.AccFinal); md.arguments = new Argument[] {param}; md.returnType = returnType; md.selector = fluent ? data.getPluralName() : HandlerUtil.buildAccessorName(getAddMethodName() + "All", new String(data.getPluralName())).toCharArray(); diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSetSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSetSingularizer.java index 5f86a4dc..c7315790 100644 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSetSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSetSingularizer.java @@ -30,6 +30,7 @@ import java.util.List; import lombok.core.handlers.HandlerUtil; import lombok.eclipse.EclipseNode; +import lombok.eclipse.handlers.HandleNonNull; import lombok.eclipse.handlers.EclipseSingularsRecipes.SingularData; import lombok.eclipse.handlers.EclipseSingularsRecipes.StatementMaker; import lombok.eclipse.handlers.EclipseSingularsRecipes.TypeReferenceMaker; @@ -138,14 +139,18 @@ abstract class EclipseJavaUtilListSetSingularizer extends EclipseJavaUtilSingula md.statements = statements.toArray(new Statement[statements.size()]); TypeReference paramType = cloneParamType(0, data.getTypeArgs(), builderType); - Argument param = new Argument(data.getSingularName(), 0, paramType, 0); + Annotation[] typeUseAnns = getTypeUseAnnotations(paramType); + removeTypeUseAnnotations(paramType); + Argument param = new Argument(data.getSingularName(), 0, paramType, ClassFileConstants.AccFinal); + param.annotations = typeUseAnns; md.arguments = new Argument[] {param}; md.returnType = returnType; md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName("add", new String(data.getSingularName())).toCharArray(); md.annotations = deprecate ? new Annotation[] { generateDeprecatedAnnotation(data.getSource()) } : null; data.setGeneratedByRecursive(md); - injectMethod(builderType, md); + + HandleNonNull.INSTANCE.fix(injectMethod(builderType, md)); } void generatePluralMethod(boolean deprecate, TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) { @@ -169,7 +174,7 @@ abstract class EclipseJavaUtilListSetSingularizer extends EclipseJavaUtilSingula TypeReference paramType = new QualifiedTypeReference(TypeConstants.JAVA_UTIL_COLLECTION, NULL_POSS); paramType = addTypeArgs(1, true, builderType, paramType, data.getTypeArgs()); - Argument param = new Argument(data.getPluralName(), 0, paramType, 0); + Argument param = new Argument(data.getPluralName(), 0, paramType, ClassFileConstants.AccFinal); md.arguments = new Argument[] {param}; md.returnType = returnType; md.selector = fluent ? data.getPluralName() : HandlerUtil.buildAccessorName("addAll", new String(data.getPluralName())).toCharArray(); diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java index 69c2186a..174cd5fc 100644 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java @@ -59,6 +59,7 @@ import lombok.eclipse.handlers.EclipseSingularsRecipes.EclipseSingularizer; import lombok.eclipse.handlers.EclipseSingularsRecipes.SingularData; import lombok.eclipse.handlers.EclipseSingularsRecipes.StatementMaker; import lombok.eclipse.handlers.EclipseSingularsRecipes.TypeReferenceMaker; +import lombok.eclipse.handlers.HandleNonNull; @ProviderFor(EclipseSingularizer.class) public class EclipseJavaUtilMapSingularizer extends EclipseJavaUtilSingularizer { @@ -214,16 +215,23 @@ public class EclipseJavaUtilMapSingularizer extends EclipseJavaUtilSingularizer md.statements = statements.toArray(new Statement[statements.size()]); TypeReference keyParamType = cloneParamType(0, data.getTypeArgs(), builderType); - Argument keyParam = new Argument(keyParamName, 0, keyParamType, 0); TypeReference valueParamType = cloneParamType(1, data.getTypeArgs(), builderType); - Argument valueParam = new Argument(valueParamName, 0, valueParamType, 0); + Annotation[] typeUseAnnsKey = getTypeUseAnnotations(keyParamType); + Annotation[] typeUseAnnsValue = getTypeUseAnnotations(valueParamType); + + removeTypeUseAnnotations(keyParamType); + removeTypeUseAnnotations(valueParamType); + Argument keyParam = new Argument(keyParamName, 0, keyParamType, ClassFileConstants.AccFinal); + Argument valueParam = new Argument(valueParamName, 0, valueParamType, ClassFileConstants.AccFinal); + keyParam.annotations = typeUseAnnsKey; + valueParam.annotations = typeUseAnnsValue; md.arguments = new Argument[] {keyParam, valueParam}; md.returnType = returnType; md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName("put", new String(data.getSingularName())).toCharArray(); md.annotations = deprecate ? new Annotation[] { generateDeprecatedAnnotation(data.getSource()) } : null; data.setGeneratedByRecursive(md); - injectMethod(builderType, md); + HandleNonNull.INSTANCE.fix(injectMethod(builderType, md)); } private void generatePluralMethod(boolean deprecate, TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) { @@ -280,7 +288,7 @@ public class EclipseJavaUtilMapSingularizer extends EclipseJavaUtilSingularizer TypeReference paramType = new QualifiedTypeReference(JAVA_UTIL_MAP, NULL_POSS); paramType = addTypeArgs(2, true, builderType, paramType, data.getTypeArgs()); - Argument param = new Argument(data.getPluralName(), 0, paramType, 0); + Argument param = new Argument(data.getPluralName(), 0, paramType, ClassFileConstants.AccFinal); md.arguments = new Argument[] {param}; md.returnType = returnType; md.selector = fluent ? data.getPluralName() : HandlerUtil.buildAccessorName("putAll", new String(data.getPluralName())).toCharArray(); diff --git a/src/core/lombok/javac/handlers/HandleSuperBuilder.java b/src/core/lombok/javac/handlers/HandleSuperBuilder.java index bcb7ee33..66d6e47e 100644 --- a/src/core/lombok/javac/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/javac/handlers/HandleSuperBuilder.java @@ -80,6 +80,7 @@ public class HandleSuperBuilder extends JavacAnnotationHandler { private static final String SELF_METHOD = "self"; private static class BuilderFieldData { + List annotations; JCExpression type; Name rawName; Name name; @@ -135,6 +136,7 @@ public class HandleSuperBuilder extends JavacAnnotationHandler { BuilderFieldData bfd = new BuilderFieldData(); bfd.rawName = fd.name; bfd.name = removePrefixFromField(fieldNode); + bfd.annotations = findCopyableAnnotations(fieldNode); bfd.type = fd.vartype; bfd.singularData = getSingularData(fieldNode); bfd.originalFieldNode = fieldNode; @@ -616,13 +618,13 @@ public class HandleSuperBuilder extends JavacAnnotationHandler { }}; if (fieldNode.singularData == null || fieldNode.singularData.getSingularizer() == null) { - generateSimpleSetterMethodForBuilder(builderType, deprecate, fieldNode.createdFields.get(0), fieldNode.nameOfSetFlag, source, true, true, returnTypeMaker.make(), returnStatementMaker.make()); + generateSimpleSetterMethodForBuilder(builderType, deprecate, fieldNode.createdFields.get(0), fieldNode.nameOfSetFlag, source, true, true, returnTypeMaker.make(), returnStatementMaker.make(), fieldNode.annotations); } else { fieldNode.singularData.getSingularizer().generateMethods(fieldNode.singularData, deprecate, builderType, source.get(), true, returnTypeMaker, returnStatementMaker); } } - private void generateSimpleSetterMethodForBuilder(JavacNode builderType, boolean deprecate, JavacNode fieldNode, Name nameOfSetFlag, JavacNode source, boolean fluent, boolean chain, JCExpression returnType, JCStatement returnStatement) { + private void generateSimpleSetterMethodForBuilder(JavacNode builderType, boolean deprecate, JavacNode fieldNode, Name nameOfSetFlag, JavacNode source, boolean fluent, boolean chain, JCExpression returnType, JCStatement returnStatement, List annosOnParam) { Name fieldName = ((JCVariableDecl) fieldNode.get()).name; for (JavacNode child : builderType.down()) { @@ -636,7 +638,7 @@ public class HandleSuperBuilder extends JavacAnnotationHandler { JavacTreeMaker maker = fieldNode.getTreeMaker(); - JCMethodDecl newMethod = HandleSetter.createSetter(Flags.PUBLIC, deprecate, fieldNode, maker, setterName, nameOfSetFlag, returnType, returnStatement, source, List.nil(), List.nil()); + JCMethodDecl newMethod = HandleSetter.createSetter(Flags.PUBLIC, deprecate, fieldNode, maker, setterName, nameOfSetFlag, returnType, returnStatement, source, List.nil(), annosOnParam); injectMethod(builderType, newMethod); } diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index b1557533..e4e40095 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -27,6 +27,7 @@ import static lombok.javac.Javac.*; import static lombok.javac.JavacAugments.JCTree_generatedNode; import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -1039,6 +1040,57 @@ public class JavacHandlerUtil { return (field.mods.flags & Flags.ENUM) != 0; } + static class JCAnnotatedTypeReflect { + private static Class TYPE; + private static Constructor CONSTRUCTOR; + private static Field ANNOTATIONS, UNDERLYING_TYPE; + + private static void init(Class in) { + if (TYPE != null) return; + if (!in.getName().equals("com.sun.tools.javac.tree.JCTree$JCAnnotatedType")) return; + try { + CONSTRUCTOR = in.getDeclaredConstructor(List.class, JCExpression.class); + CONSTRUCTOR.setAccessible(true); + ANNOTATIONS = in.getDeclaredField("annotations"); + UNDERLYING_TYPE = in.getDeclaredField("underlyingType"); + TYPE = in; + } catch (Exception ignore) {} + } + + static boolean is(JCTree obj) { + if (obj == null) return false; + init(obj.getClass()); + return obj.getClass() == TYPE; + } + + @SuppressWarnings("unchecked") + static List getAnnotations(JCTree obj) { + init(obj.getClass()); + try { + return (List) ANNOTATIONS.get(obj); + } catch (Exception e) { + return List.nil(); + } + } + + static JCExpression getUnderlyingType(JCTree obj) { + init(obj.getClass()); + try { + return (JCExpression) UNDERLYING_TYPE.get(obj); + } catch (Exception e) { + return null; + } + } + + static JCExpression create(List annotations, JCExpression underlyingType) { + try { + return (JCExpression) CONSTRUCTOR.newInstance(annotations, underlyingType); + } catch (Exception e) { + return null; + } + } + } + // jdk9 support, types have changed, names stay the same static class ClassSymbolMembersField { private static final Field membersField; @@ -1570,6 +1622,16 @@ public class JavacHandlerUtil { return out.toList(); } + public static List getTypeUseAnnotations(JCExpression from) { + if (!JCAnnotatedTypeReflect.is(from)) return List.nil(); + return JCAnnotatedTypeReflect.getAnnotations(from); + } + + public static JCExpression removeTypeUseAnnotations(JCExpression from) { + if (!JCAnnotatedTypeReflect.is(from)) return from; + return JCAnnotatedTypeReflect.getUnderlyingType(from); + } + public static JCExpression namePlusTypeParamsToTypeReference(JavacTreeMaker maker, Name typeName, List params) { if (params.isEmpty()) { return maker.Ident(typeName); @@ -1650,7 +1712,7 @@ public class JavacHandlerUtil { } /** - * Creates a full clone of a given javac AST type node. Every part is cloned (every identifier, every select, every wildcard, every type apply). + * Creates a full clone of a given javac AST type node. Every part is cloned (every identifier, every select, every wildcard, every type apply, every type_use annotation). * * If there's any node in the tree that we don't know how to clone, that part isn't cloned. However, we wouldn't know what could possibly show up that we * can't currently clone; that's just a safeguard. @@ -1712,6 +1774,12 @@ public class JavacHandlerUtil { return maker.Wildcard(newKind, newInner); } + if (JCAnnotatedTypeReflect.is(in)) { + JCExpression underlyingType = cloneType0(maker, JCAnnotatedTypeReflect.getUnderlyingType(in)); + List anns = copyAnnotations(JCAnnotatedTypeReflect.getAnnotations(in)); + return JCAnnotatedTypeReflect.create(anns, underlyingType); + } + // This is somewhat unsafe, but it's better than outright throwing an exception here. Returning null will just cause an exception down the pipeline. return (JCExpression) in; } @@ -1887,7 +1955,7 @@ public class JavacHandlerUtil { public static boolean isDirectDescendantOfObject(JavacNode typeNode) { if (!(typeNode.get() instanceof JCClassDecl)) throw new IllegalArgumentException("not a type node"); - JCTree extending = Javac.getExtendsClause((JCClassDecl)typeNode.get()); + JCTree extending = Javac.getExtendsClause((JCClassDecl) typeNode.get()); if (extending == null) return true; String p = extending.toString(); return p.equals("Object") || p.equals("java.lang.Object"); diff --git a/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java index ffaf6674..74010d52 100644 --- a/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java +++ b/src/core/lombok/javac/handlers/singulars/JavacGuavaSingularizer.java @@ -39,6 +39,7 @@ import lombok.javac.handlers.JavacSingularsRecipes.StatementMaker; import com.sun.tools.javac.code.Flags; 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.JCMethodDecl; @@ -125,7 +126,10 @@ abstract class JavacGuavaSingularizer extends JavacSingularizer { ListBuffer params = new ListBuffer(); for (int i = 0; i < suffixes.size(); i++) { JCExpression pt = cloneParamType(i, maker, data.getTypeArgs(), builderType, source); - JCVariableDecl p = maker.VarDef(maker.Modifiers(paramFlags), names[i], pt, null); + List typeUseAnns = getTypeUseAnnotations(pt); + pt = removeTypeUseAnnotations(pt); + JCModifiers paramMods = typeUseAnns.isEmpty() ? maker.Modifiers(paramFlags) : maker.Modifiers(paramFlags, typeUseAnns); + JCVariableDecl p = maker.VarDef(paramMods, names[i], pt, null); params.append(p); } diff --git a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSetSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSetSingularizer.java index 39e53ebb..26ff8ba6 100644 --- a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSetSingularizer.java +++ b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilListSetSingularizer.java @@ -36,6 +36,7 @@ import lombok.javac.handlers.JavacSingularsRecipes.StatementMaker; import com.sun.tools.javac.code.Flags; 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.JCMethodDecl; @@ -124,7 +125,10 @@ abstract class JavacJavaUtilListSetSingularizer extends JavacJavaUtilSingularize long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext()); if (!fluent) name = builderType.toName(HandlerUtil.buildAccessorName("add", name.toString())); JCExpression paramType = cloneParamType(0, maker, data.getTypeArgs(), builderType, source); - JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getSingularName(), paramType, null); + List typeUseAnns = getTypeUseAnnotations(paramType); + paramType = removeTypeUseAnnotations(paramType); + JCModifiers paramMods = typeUseAnns.isEmpty() ? maker.Modifiers(paramFlags) : maker.Modifiers(paramFlags, typeUseAnns); + JCVariableDecl param = maker.VarDef(paramMods, data.getSingularName(), paramType, null); JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), thrown, body, null); injectMethod(builderType, method); } diff --git a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilMapSingularizer.java b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilMapSingularizer.java index 34350f40..a009b88c 100644 --- a/src/core/lombok/javac/handlers/singulars/JavacJavaUtilMapSingularizer.java +++ b/src/core/lombok/javac/handlers/singulars/JavacJavaUtilMapSingularizer.java @@ -40,6 +40,7 @@ import org.mangosdk.spi.ProviderFor; import com.sun.tools.javac.code.Flags; 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.JCMethodDecl; @@ -165,8 +166,14 @@ public class JavacJavaUtilMapSingularizer extends JavacJavaUtilSingularizer { if (!fluent) name = builderType.toName(HandlerUtil.buildAccessorName("put", name.toString())); JCExpression paramTypeKey = cloneParamType(0, maker, data.getTypeArgs(), builderType, source); JCExpression paramTypeValue = cloneParamType(1, maker, data.getTypeArgs(), builderType, source); - JCVariableDecl paramKey = maker.VarDef(maker.Modifiers(paramFlags), keyName, paramTypeKey, null); - JCVariableDecl paramValue = maker.VarDef(maker.Modifiers(paramFlags), valueName, paramTypeValue, null); + List typeUseAnnsKey = getTypeUseAnnotations(paramTypeKey); + List typeUseAnnsValue = getTypeUseAnnotations(paramTypeValue); + paramTypeKey = removeTypeUseAnnotations(paramTypeKey); + paramTypeValue = removeTypeUseAnnotations(paramTypeValue); + JCModifiers paramModsKey = typeUseAnnsKey.isEmpty() ? maker.Modifiers(paramFlags) : maker.Modifiers(paramFlags, typeUseAnnsKey); + JCModifiers paramModsValue = typeUseAnnsValue.isEmpty() ? maker.Modifiers(paramFlags) : maker.Modifiers(paramFlags, typeUseAnnsValue); + JCVariableDecl paramKey = maker.VarDef(paramModsKey, keyName, paramTypeKey, null); + JCVariableDecl paramValue = maker.VarDef(paramModsValue, valueName, paramTypeValue, null); JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(paramKey, paramValue), thrown, body, null); injectMethod(builderType, method); } diff --git a/test/transform/resource/after-delombok/BuilderSingularAnnotatedTypes.java b/test/transform/resource/after-delombok/BuilderSingularAnnotatedTypes.java index d621d376..93825659 100644 --- a/test/transform/resource/after-delombok/BuilderSingularAnnotatedTypes.java +++ b/test/transform/resource/after-delombok/BuilderSingularAnnotatedTypes.java @@ -1,34 +1,42 @@ +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; import java.util.Set; import java.util.Map; import lombok.NonNull; +@Target(ElementType.TYPE_USE) +@interface MyAnnotation { +} class BuilderSingularAnnotatedTypes { - private Set<@NonNull String> foos; - private Map<@NonNull String, @NonNull Integer> bars; + private Set<@MyAnnotation @NonNull String> foos; + private Map<@MyAnnotation @NonNull String, @MyAnnotation @NonNull Integer> bars; @java.lang.SuppressWarnings("all") - BuilderSingularAnnotatedTypes(final Set<@NonNull String> foos, final Map<@NonNull String, @NonNull Integer> bars) { + BuilderSingularAnnotatedTypes(final Set<@MyAnnotation @NonNull String> foos, final Map<@MyAnnotation @NonNull String, @MyAnnotation @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; + private java.util.ArrayList<@MyAnnotation @NonNull String> foos; @java.lang.SuppressWarnings("all") - private java.util.ArrayList<@NonNull String> bars$key; + private java.util.ArrayList<@MyAnnotation @NonNull String> bars$key; @java.lang.SuppressWarnings("all") - private java.util.ArrayList<@NonNull Integer> bars$value; + private java.util.ArrayList<@MyAnnotation @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>(); + public BuilderSingularAnnotatedTypesBuilder foo(@MyAnnotation @NonNull final String foo) { + if (foo == null) { + throw new java.lang.NullPointerException("foo is marked @NonNull but is null"); + } + if (this.foos == null) this.foos = new java.util.ArrayList<@MyAnnotation @NonNull String>(); this.foos.add(foo); return this; } @java.lang.SuppressWarnings("all") - public BuilderSingularAnnotatedTypesBuilder foos(final java.util.Collection foos) { - if (this.foos == null) this.foos = new java.util.ArrayList<@NonNull String>(); + public BuilderSingularAnnotatedTypesBuilder foos(final java.util.Collection foos) { + if (this.foos == null) this.foos = new java.util.ArrayList<@MyAnnotation @NonNull String>(); this.foos.addAll(foos); return this; } @@ -38,22 +46,28 @@ class BuilderSingularAnnotatedTypes { return this; } @java.lang.SuppressWarnings("all") - public BuilderSingularAnnotatedTypesBuilder bar(final @NonNull String barKey, final @NonNull Integer barValue) { + public BuilderSingularAnnotatedTypesBuilder bar(@MyAnnotation @NonNull final String barKey, @MyAnnotation @NonNull final Integer barValue) { + if (barKey == null) { + throw new java.lang.NullPointerException("barKey is marked @NonNull but is null"); + } + if (barValue == null) { + throw new java.lang.NullPointerException("barValue is marked @NonNull but is null"); + } 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 = new java.util.ArrayList<@MyAnnotation @NonNull String>(); + this.bars$value = new java.util.ArrayList<@MyAnnotation @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 bars) { + public BuilderSingularAnnotatedTypesBuilder bars(final java.util.Map bars) { 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 = new java.util.ArrayList<@MyAnnotation @NonNull String>(); + this.bars$value = new java.util.ArrayList<@MyAnnotation @NonNull Integer>(); } - for (final java.util.Map.Entry $lombokEntry : bars.entrySet()) { + for (final java.util.Map.Entry $lombokEntry : bars.entrySet()) { this.bars$key.add($lombokEntry.getKey()); this.bars$value.add($lombokEntry.getValue()); } @@ -69,7 +83,7 @@ class BuilderSingularAnnotatedTypes { } @java.lang.SuppressWarnings("all") public BuilderSingularAnnotatedTypes build() { - java.util.Set<@NonNull String> foos; + java.util.Set<@MyAnnotation @NonNull String> foos; switch (this.foos == null ? 0 : this.foos.size()) { case 0: foos = java.util.Collections.emptySet(); @@ -78,11 +92,11 @@ class BuilderSingularAnnotatedTypes { 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 = new java.util.LinkedHashSet<@MyAnnotation @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; + java.util.Map<@MyAnnotation @NonNull String, @MyAnnotation @NonNull Integer> bars; switch (this.bars$key == null ? 0 : this.bars$key.size()) { case 0: bars = java.util.Collections.emptyMap(); @@ -91,8 +105,8 @@ class BuilderSingularAnnotatedTypes { 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 = new java.util.LinkedHashMap<@MyAnnotation @NonNull String, @MyAnnotation @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), (@MyAnnotation @NonNull Integer) this.bars$value.get($i)); bars = java.util.Collections.unmodifiableMap(bars); } return new BuilderSingularAnnotatedTypes(foos, bars); diff --git a/test/transform/resource/after-delombok/SuperBuilderSingularAnnotatedTypes.java b/test/transform/resource/after-delombok/SuperBuilderSingularAnnotatedTypes.java new file mode 100644 index 00000000..1baf81aa --- /dev/null +++ b/test/transform/resource/after-delombok/SuperBuilderSingularAnnotatedTypes.java @@ -0,0 +1,137 @@ +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; +import java.util.Set; +import java.util.Map; +import lombok.NonNull; +@Target(ElementType.TYPE_USE) +@interface MyAnnotation { +} +class SuperBuilderSingularAnnotatedTypes { + private Set<@MyAnnotation @NonNull String> foos; + private Map<@MyAnnotation @NonNull String, @MyAnnotation @NonNull Integer> bars; + @java.lang.SuppressWarnings("all") + public static abstract class SuperBuilderSingularAnnotatedTypesBuilder> { + @java.lang.SuppressWarnings("all") + private java.util.ArrayList<@MyAnnotation @NonNull String> foos; + @java.lang.SuppressWarnings("all") + private java.util.ArrayList<@MyAnnotation @NonNull String> bars$key; + @java.lang.SuppressWarnings("all") + private java.util.ArrayList<@MyAnnotation @NonNull Integer> bars$value; + @java.lang.SuppressWarnings("all") + protected abstract B self(); + @java.lang.SuppressWarnings("all") + public abstract C build(); + @java.lang.SuppressWarnings("all") + public B foo(@MyAnnotation @NonNull final String foo) { + if (foo == null) { + throw new java.lang.NullPointerException("foo is marked @NonNull but is null"); + } + if (this.foos == null) this.foos = new java.util.ArrayList<@MyAnnotation @NonNull String>(); + this.foos.add(foo); + return self(); + } + @java.lang.SuppressWarnings("all") + public B foos(final java.util.Collection foos) { + if (this.foos == null) this.foos = new java.util.ArrayList<@MyAnnotation @NonNull String>(); + this.foos.addAll(foos); + return self(); + } + @java.lang.SuppressWarnings("all") + public B clearFoos() { + if (this.foos != null) this.foos.clear(); + return self(); + } + @java.lang.SuppressWarnings("all") + public B bar(@MyAnnotation @NonNull final String barKey, @MyAnnotation @NonNull final Integer barValue) { + if (barKey == null) { + throw new java.lang.NullPointerException("barKey is marked @NonNull but is null"); + } + if (barValue == null) { + throw new java.lang.NullPointerException("barValue is marked @NonNull but is 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>(); + } + this.bars$key.add(barKey); + this.bars$value.add(barValue); + return self(); + } + @java.lang.SuppressWarnings("all") + public B bars(final java.util.Map bars) { + 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>(); + } + for (final java.util.Map.Entry $lombokEntry : bars.entrySet()) { + this.bars$key.add($lombokEntry.getKey()); + this.bars$value.add($lombokEntry.getValue()); + } + return self(); + } + @java.lang.SuppressWarnings("all") + public B clearBars() { + if (this.bars$key != null) { + this.bars$key.clear(); + this.bars$value.clear(); + } + return self(); + } + @java.lang.Override + @java.lang.SuppressWarnings("all") + public java.lang.String toString() { + return "SuperBuilderSingularAnnotatedTypes.SuperBuilderSingularAnnotatedTypesBuilder(foos=" + this.foos + ", bars$key=" + this.bars$key + ", bars$value=" + this.bars$value + ")"; + } + } + @java.lang.SuppressWarnings("all") + private static final class SuperBuilderSingularAnnotatedTypesBuilderImpl extends SuperBuilderSingularAnnotatedTypesBuilder { + @java.lang.SuppressWarnings("all") + private SuperBuilderSingularAnnotatedTypesBuilderImpl() { + } + @java.lang.Override + @java.lang.SuppressWarnings("all") + protected SuperBuilderSingularAnnotatedTypesBuilderImpl self() { + return this; + } + @java.lang.Override + @java.lang.SuppressWarnings("all") + public SuperBuilderSingularAnnotatedTypes build() { + return new SuperBuilderSingularAnnotatedTypes(this); + } + } + @java.lang.SuppressWarnings("all") + protected SuperBuilderSingularAnnotatedTypes(final SuperBuilderSingularAnnotatedTypesBuilder b) { + java.util.Set<@MyAnnotation @NonNull String> foos; + switch (b.foos == null ? 0 : b.foos.size()) { + case 0: + foos = java.util.Collections.emptySet(); + break; + case 1: + foos = java.util.Collections.singleton(b.foos.get(0)); + break; + default: + foos = new java.util.LinkedHashSet<@MyAnnotation @NonNull String>(b.foos.size() < 1073741824 ? 1 + b.foos.size() + (b.foos.size() - 3) / 3 : java.lang.Integer.MAX_VALUE); + foos.addAll(b.foos); + foos = java.util.Collections.unmodifiableSet(foos); + } + this.foos = foos; + java.util.Map<@MyAnnotation @NonNull String, @MyAnnotation @NonNull Integer> bars; + switch (b.bars$key == null ? 0 : b.bars$key.size()) { + case 0: + bars = java.util.Collections.emptyMap(); + break; + case 1: + bars = java.util.Collections.singletonMap(b.bars$key.get(0), b.bars$value.get(0)); + break; + default: + bars = new java.util.LinkedHashMap<@MyAnnotation @NonNull String, @MyAnnotation @NonNull Integer>(b.bars$key.size() < 1073741824 ? 1 + b.bars$key.size() + (b.bars$key.size() - 3) / 3 : java.lang.Integer.MAX_VALUE); + for (int $i = 0; $i < b.bars$key.size(); $i++) bars.put(b.bars$key.get($i), (@MyAnnotation @NonNull Integer) b.bars$value.get($i)); + bars = java.util.Collections.unmodifiableMap(bars); + } + this.bars = bars; + } + @java.lang.SuppressWarnings("all") + public static SuperBuilderSingularAnnotatedTypesBuilder builder() { + return new SuperBuilderSingularAnnotatedTypesBuilderImpl(); + } +} \ No newline at end of file diff --git a/test/transform/resource/after-delombok/SuperBuilderWithNonNull.java b/test/transform/resource/after-delombok/SuperBuilderWithNonNull.java index 5eba938f..cac5482b 100644 --- a/test/transform/resource/after-delombok/SuperBuilderWithNonNull.java +++ b/test/transform/resource/after-delombok/SuperBuilderWithNonNull.java @@ -18,7 +18,10 @@ public class SuperBuilderWithNonNull { @java.lang.SuppressWarnings("all") public abstract C build(); @java.lang.SuppressWarnings("all") - public B nonNullParentField(final String nonNullParentField) { + public B nonNullParentField(@lombok.NonNull final String nonNullParentField) { + if (nonNullParentField == null) { + throw new java.lang.NullPointerException("nonNullParentField is marked @NonNull but is null"); + } this.nonNullParentField = nonNullParentField; nonNullParentField$set = true; return self(); @@ -72,7 +75,10 @@ public class SuperBuilderWithNonNull { @java.lang.SuppressWarnings("all") public abstract C build(); @java.lang.SuppressWarnings("all") - public B nonNullChildField(final String nonNullChildField) { + public B nonNullChildField(@lombok.NonNull final String nonNullChildField) { + if (nonNullChildField == null) { + throw new java.lang.NullPointerException("nonNullChildField is marked @NonNull but is null"); + } this.nonNullChildField = nonNullChildField; return self(); } diff --git a/test/transform/resource/after-ecj/BuilderDefaultsWarnings.java b/test/transform/resource/after-ecj/BuilderDefaultsWarnings.java index 236632d0..1078f452 100644 --- a/test/transform/resource/after-ecj/BuilderDefaultsWarnings.java +++ b/test/transform/resource/after-ecj/BuilderDefaultsWarnings.java @@ -16,13 +16,13 @@ public @Builder class BuilderDefaultsWarnings { this.z = z; return this; } - public @java.lang.SuppressWarnings("all") BuilderDefaultsWarningsBuilder item(String item) { + public @java.lang.SuppressWarnings("all") BuilderDefaultsWarningsBuilder item(final String item) { if ((this.items == null)) this.items = new java.util.ArrayList(); this.items.add(item); return this; } - public @java.lang.SuppressWarnings("all") BuilderDefaultsWarningsBuilder items(java.util.Collection items) { + public @java.lang.SuppressWarnings("all") BuilderDefaultsWarningsBuilder items(final java.util.Collection items) { if ((this.items == null)) this.items = new java.util.ArrayList(); this.items.addAll(items); diff --git a/test/transform/resource/after-ecj/BuilderSingularAnnotatedTypes.java b/test/transform/resource/after-ecj/BuilderSingularAnnotatedTypes.java index 0168a13c..26023e1a 100644 --- a/test/transform/resource/after-ecj/BuilderSingularAnnotatedTypes.java +++ b/test/transform/resource/after-ecj/BuilderSingularAnnotatedTypes.java @@ -1,24 +1,32 @@ +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; import java.util.Set; import java.util.Map; import lombok.NonNull; import lombok.Singular; +@Target(ElementType.TYPE_USE) @interface MyAnnotation { +} @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; + private @java.lang.SuppressWarnings("all") java.util.ArrayList<@MyAnnotation @NonNull String> foos; + private @java.lang.SuppressWarnings("all") java.util.ArrayList<@MyAnnotation @NonNull String> bars$key; + private @java.lang.SuppressWarnings("all") java.util.ArrayList<@MyAnnotation @NonNull Integer> bars$value; @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder() { super(); } - public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder foo(@NonNull String foo) { + public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder foo(final @MyAnnotation @NonNull String foo) { + if ((foo == null)) + { + throw new java.lang.NullPointerException("foo is marked @NonNull but is null"); + } if ((this.foos == null)) - this.foos = new java.util.ArrayList<@NonNull String>(); + this.foos = new java.util.ArrayList<@MyAnnotation @NonNull String>(); this.foos.add(foo); return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder foos(java.util.Collection foos) { + public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder foos(final java.util.Collection foos) { if ((this.foos == null)) - this.foos = new java.util.ArrayList<@NonNull String>(); + this.foos = new java.util.ArrayList<@MyAnnotation @NonNull String>(); this.foos.addAll(foos); return this; } @@ -27,23 +35,31 @@ import lombok.Singular; this.foos.clear(); return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder bar(@NonNull String barKey, @NonNull Integer barValue) { + public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder bar(final @MyAnnotation @NonNull String barKey, final @MyAnnotation @NonNull Integer barValue) { + if ((barKey == null)) + { + throw new java.lang.NullPointerException("barKey is marked @NonNull but is null"); + } + if ((barValue == null)) + { + throw new java.lang.NullPointerException("barValue is marked @NonNull but is null"); + } 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 = new java.util.ArrayList<@MyAnnotation @NonNull String>(); + this.bars$value = new java.util.ArrayList<@MyAnnotation @NonNull Integer>(); } this.bars$key.add(barKey); this.bars$value.add(barValue); return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder bars(java.util.Map bars) { + public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder bars(final java.util.Map bars) { 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 = new java.util.ArrayList<@MyAnnotation @NonNull String>(); + this.bars$value = new java.util.ArrayList<@MyAnnotation @NonNull Integer>(); } - for (java.util.Map.Entry $lombokEntry : bars.entrySet()) + for (java.util.Map.Entry $lombokEntry : bars.entrySet()) { this.bars$key.add($lombokEntry.getKey()); this.bars$value.add($lombokEntry.getValue()); @@ -59,7 +75,7 @@ import lombok.Singular; return this; } public @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypes build() { - java.util.Set<@NonNull String> foos; + java.util.Set<@MyAnnotation @NonNull String> foos; switch (((this.foos == null) ? 0 : this.foos.size())) { case 0 : foos = java.util.Collections.emptySet(); @@ -68,11 +84,11 @@ import lombok.Singular; 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 = new java.util.LinkedHashSet<@MyAnnotation @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; + java.util.Map<@MyAnnotation @NonNull String, @MyAnnotation @NonNull Integer> bars; switch (((this.bars$key == null) ? 0 : this.bars$key.size())) { case 0 : bars = java.util.Collections.emptyMap(); @@ -81,7 +97,7 @@ import lombok.Singular; 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)); + bars = new java.util.LinkedHashMap<@MyAnnotation @NonNull String, @MyAnnotation @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); @@ -92,9 +108,9 @@ import lombok.Singular; 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) { + private @Singular Set<@MyAnnotation @NonNull String> foos; + private @Singular Map<@MyAnnotation @NonNull String, @MyAnnotation @NonNull Integer> bars; + @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypes(final Set<@MyAnnotation @NonNull String> foos, final Map<@MyAnnotation @NonNull String, @MyAnnotation @NonNull Integer> bars) { super(); this.foos = foos; this.bars = bars; @@ -102,4 +118,4 @@ import lombok.Singular; public static @java.lang.SuppressWarnings("all") BuilderSingularAnnotatedTypesBuilder builder() { return new BuilderSingularAnnotatedTypesBuilder(); } -} +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/BuilderSingularGuavaListsSets.java b/test/transform/resource/after-ecj/BuilderSingularGuavaListsSets.java index 12c2b293..9b50d33b 100644 --- a/test/transform/resource/after-ecj/BuilderSingularGuavaListsSets.java +++ b/test/transform/resource/after-ecj/BuilderSingularGuavaListsSets.java @@ -14,13 +14,13 @@ import lombok.Singular; @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder() { super(); } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder card(T card) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder card(final T card) { if ((this.cards == null)) this.cards = com.google.common.collect.ImmutableList.builder(); this.cards.add(card); return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder cards(java.lang.Iterable cards) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder cards(final java.lang.Iterable cards) { if ((this.cards == null)) this.cards = com.google.common.collect.ImmutableList.builder(); this.cards.addAll(cards); @@ -30,13 +30,13 @@ import lombok.Singular; this.cards = null; return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder frog(Number frog) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder frog(final Number frog) { if ((this.frogs == null)) this.frogs = com.google.common.collect.ImmutableList.builder(); this.frogs.add(frog); return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder frogs(java.lang.Iterable frogs) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder frogs(final java.lang.Iterable frogs) { if ((this.frogs == null)) this.frogs = com.google.common.collect.ImmutableList.builder(); this.frogs.addAll(frogs); @@ -46,13 +46,13 @@ import lombok.Singular; this.frogs = null; return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder rawSet(java.lang.Object rawSet) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder rawSet(final java.lang.Object rawSet) { if ((this.rawSet == null)) this.rawSet = com.google.common.collect.ImmutableSet.builder(); this.rawSet.add(rawSet); return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder rawSet(java.lang.Iterable rawSet) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder rawSet(final java.lang.Iterable rawSet) { if ((this.rawSet == null)) this.rawSet = com.google.common.collect.ImmutableSet.builder(); this.rawSet.addAll(rawSet); @@ -62,13 +62,13 @@ import lombok.Singular; this.rawSet = null; return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder pass(String pass) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder pass(final String pass) { if ((this.passes == null)) this.passes = com.google.common.collect.ImmutableSortedSet.naturalOrder(); this.passes.add(pass); return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder passes(java.lang.Iterable passes) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder passes(final java.lang.Iterable passes) { if ((this.passes == null)) this.passes = com.google.common.collect.ImmutableSortedSet.naturalOrder(); this.passes.addAll(passes); @@ -78,13 +78,13 @@ import lombok.Singular; this.passes = null; return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder user(Number rowKey, Number columnKey, String value) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder user(final Number rowKey, final Number columnKey, final String value) { if ((this.users == null)) this.users = com.google.common.collect.ImmutableTable.builder(); this.users.put(rowKey, columnKey, value); return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder users(com.google.common.collect.Table users) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaListsSetsBuilder users(final com.google.common.collect.Table users) { if ((this.users == null)) this.users = com.google.common.collect.ImmutableTable.builder(); this.users.putAll(users); diff --git a/test/transform/resource/after-ecj/BuilderSingularGuavaMaps.java b/test/transform/resource/after-ecj/BuilderSingularGuavaMaps.java index 44533ac1..1dc04a07 100644 --- a/test/transform/resource/after-ecj/BuilderSingularGuavaMaps.java +++ b/test/transform/resource/after-ecj/BuilderSingularGuavaMaps.java @@ -10,13 +10,13 @@ import lombok.Singular; @java.lang.SuppressWarnings("all") BuilderSingularGuavaMapsBuilder() { super(); } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaMapsBuilder battleaxe(K key, V value) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaMapsBuilder battleaxe(final K key, final V value) { if ((this.battleaxes == null)) this.battleaxes = com.google.common.collect.ImmutableMap.builder(); this.battleaxes.put(key, value); return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaMapsBuilder battleaxes(java.util.Map battleaxes) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaMapsBuilder battleaxes(final java.util.Map battleaxes) { if ((this.battleaxes == null)) this.battleaxes = com.google.common.collect.ImmutableMap.builder(); this.battleaxes.putAll(battleaxes); @@ -26,13 +26,13 @@ import lombok.Singular; this.battleaxes = null; return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaMapsBuilder vertex(Integer key, V value) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaMapsBuilder vertex(final Integer key, final V value) { if ((this.vertices == null)) this.vertices = com.google.common.collect.ImmutableSortedMap.naturalOrder(); this.vertices.put(key, value); return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaMapsBuilder vertices(java.util.Map vertices) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaMapsBuilder vertices(final java.util.Map vertices) { if ((this.vertices == null)) this.vertices = com.google.common.collect.ImmutableSortedMap.naturalOrder(); this.vertices.putAll(vertices); @@ -42,13 +42,13 @@ import lombok.Singular; this.vertices = null; return this; } - public @java.lang.SuppressWarnings("all") BuilderSingularGuavaMapsBuilder rawMap(java.lang.Object key, java.lang.Object value) { + public @java.lang.SuppressWarnings("all") BuilderSingularGuavaMapsBuilder r