From f2c837bb47771a7eba5ad5a885af162d8d133559 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Sat, 1 Aug 2009 01:42:52 +0200 Subject: @Setter will copy all NotNull and NonNull (case-insensitive) annotations to the parameter @Getter will copy them to the getter method Added @NonNull to lombok to support null-checks in the setter --- src/lombok/javac/handlers/HandleGetter.java | 3 ++- src/lombok/javac/handlers/HandleSetter.java | 8 ++++---- src/lombok/javac/handlers/PKG.java | 11 +++++++---- 3 files changed, 13 insertions(+), 9 deletions(-) (limited to 'src/lombok/javac') diff --git a/src/lombok/javac/handlers/HandleGetter.java b/src/lombok/javac/handlers/HandleGetter.java index 3d3227e4..370a405b 100644 --- a/src/lombok/javac/handlers/HandleGetter.java +++ b/src/lombok/javac/handlers/HandleGetter.java @@ -128,7 +128,8 @@ public class HandleGetter implements JavacAnnotationHandler { List throwsClauses = List.nil(); JCExpression annotationMethodDefaultValue = null; - return treeMaker.MethodDef(treeMaker.Modifiers(access, List.nil()), methodName, methodType, + List annotations = findNonNullAnnotations(field); + return treeMaker.MethodDef(treeMaker.Modifiers(access, annotations), methodName, methodType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue); } } diff --git a/src/lombok/javac/handlers/HandleSetter.java b/src/lombok/javac/handlers/HandleSetter.java index b4720387..2f41e716 100644 --- a/src/lombok/javac/handlers/HandleSetter.java +++ b/src/lombok/javac/handlers/HandleSetter.java @@ -121,10 +121,9 @@ public class HandleSetter implements JavacAnnotationHandler { JCFieldAccess thisX = treeMaker.Select(treeMaker.Ident(field.toName("this")), fieldDecl.name); JCAssign assign = treeMaker.Assign(thisX, treeMaker.Ident(fieldDecl.name)); - List statements; - JCAnnotation nonNull = findNonNullAnnotation(field); - if (nonNull == null) { + List nonNulls = findNonNullAnnotations(field); + if (nonNulls.isEmpty()) { statements = List.of(treeMaker.Exec(assign)); } else { @@ -137,7 +136,8 @@ public class HandleSetter implements JavacAnnotationHandler { JCBlock methodBody = treeMaker.Block(0, statements); Name methodName = field.toName(toSetterName(fieldDecl)); - JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(0), fieldDecl.name, fieldDecl.vartype, null); + + JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(0, nonNulls), fieldDecl.name, fieldDecl.vartype, null); JCExpression methodType = treeMaker.Type(field.getSymbolTable().voidType); List methodGenericParams = List.nil(); diff --git a/src/lombok/javac/handlers/PKG.java b/src/lombok/javac/handlers/PKG.java index bc5b691c..ea794296 100644 --- a/src/lombok/javac/handlers/PKG.java +++ b/src/lombok/javac/handlers/PKG.java @@ -254,16 +254,19 @@ class PKG { return e; } - static JCAnnotation findNonNullAnnotation(Node fieldNode) { + static List findNonNullAnnotations(Node fieldNode) { + List result = List.nil(); for ( Node child : fieldNode.down() ) { if ( child.getKind() == Kind.ANNOTATION ) { JCAnnotation annotation = (JCAnnotation) child.get(); String name = annotation.annotationType.toString(); - if (name.equals("NonNull") || name.endsWith(".NonNull")) { - return annotation; + int idx = name.lastIndexOf("."); + String suspect = idx == -1 ? name : name.substring(idx + 1); + if (suspect.equalsIgnoreCase("NonNull") || suspect.equalsIgnoreCase("NotNull")) { + result = result.append(annotation); } } } - return null; + return result; } } -- cgit