diff options
author | Rawi01 <Rawi01@users.noreply.github.com> | 2023-03-13 19:55:52 +0100 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2023-03-21 17:13:54 +0100 |
commit | 6f204d98c450092f4a6252677fc44657704b9897 (patch) | |
tree | c315d0a55aa93f0fb399c776574a937acebce8ba /src/core/lombok/javac/handlers/HandleNonNull.java | |
parent | 13642eb896ae6dbbaebab6df3c3758296102ead8 (diff) | |
download | lombok-6f204d98c450092f4a6252677fc44657704b9897.tar.gz lombok-6f204d98c450092f4a6252677fc44657704b9897.tar.bz2 lombok-6f204d98c450092f4a6252677fc44657704b9897.zip |
[fixes #3366] Handle @NonNull type annotations on record components
Diffstat (limited to 'src/core/lombok/javac/handlers/HandleNonNull.java')
-rw-r--r-- | src/core/lombok/javac/handlers/HandleNonNull.java | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/src/core/lombok/javac/handlers/HandleNonNull.java b/src/core/lombok/javac/handlers/HandleNonNull.java index 271bedbb..e817916d 100644 --- a/src/core/lombok/javac/handlers/HandleNonNull.java +++ b/src/core/lombok/javac/handlers/HandleNonNull.java @@ -218,48 +218,43 @@ public class HandleNonNull extends JavacAnnotationHandler<NonNull> { @Override public void handle(AnnotationValues<NonNull> annotation, JCAnnotation ast, JavacNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.NON_NULL_FLAG_USAGE, "@NonNull"); - if (annotationNode.up().getKind() == Kind.FIELD) { + + final JavacNode node; + if (annotationNode.up().getKind() == Kind.TYPE_USE) { + node = annotationNode.directUp().directUp(); + } else { + node = annotationNode.up(); + } + + if (node.getKind() == Kind.FIELD) { // This is meaningless unless the field is used to generate a method (@Setter, @RequiredArgsConstructor, etc), // but in that case those handlers will take care of it. However, we DO check if the annotation is applied to // a primitive, because those handlers trigger on any annotation named @NonNull and we only want the warning // behaviour on _OUR_ 'lombok.NonNull'. try { - if (isPrimitive(((JCVariableDecl) annotationNode.up().get()).vartype)) { + if (isPrimitive(((JCVariableDecl) node.get()).vartype)) { annotationNode.addWarning("@NonNull is meaningless on a primitive."); } } catch (Exception ignore) {} - JCVariableDecl fDecl = (JCVariableDecl) annotationNode.up().get(); + JCVariableDecl fDecl = (JCVariableDecl) node.get(); if ((fDecl.mods.flags & RECORD) != 0) { // well, these kinda double as parameters (of the compact constructor), so we do some work here. - List<JCMethodDecl> compactConstructors = addCompactConstructorIfNeeded(annotationNode.up().up(), annotationNode); + List<JCMethodDecl> compactConstructors = addCompactConstructorIfNeeded(node.up(), annotationNode); for (JCMethodDecl ctr : compactConstructors) { - addNullCheckIfNeeded(ctr, annotationNode.up(), annotationNode); + addNullCheckIfNeeded(ctr, node, annotationNode); } } return; } - JCMethodDecl declaration; - JavacNode paramNode; + if (node.getKind() != Kind.ARGUMENT) return; - switch (annotationNode.up().getKind()) { - case ARGUMENT: - paramNode = annotationNode.up(); - break; - case TYPE_USE: - JavacNode typeNode = annotationNode.directUp(); - paramNode = typeNode.directUp(); - break; - default: - return; - } - - if (paramNode.getKind() != Kind.ARGUMENT) return; + JCMethodDecl declaration; try { - declaration = (JCMethodDecl) paramNode.up().get(); + declaration = (JCMethodDecl) node.up().get(); } catch (Exception e) { return; } @@ -276,7 +271,7 @@ public class HandleNonNull extends JavacAnnotationHandler<NonNull> { return; } - addNullCheckIfNeeded(declaration, paramNode, annotationNode); + addNullCheckIfNeeded(declaration, node, annotationNode); } public boolean isNullCheck(JCStatement stat) { |