From dd593a3af08d9fd300ecd3a11a3551507aa35b6f Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Sat, 1 Aug 2009 02:38:56 +0200 Subject: The constructors will now also add non-final fields if they have a NonNull annotation The constructor will test for null-values The constructor and static constructor will copy the NonNull annotations from the fields --- src/lombok/eclipse/handlers/HandleData.java | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/lombok/eclipse/handlers/HandleData.java') diff --git a/src/lombok/eclipse/handlers/HandleData.java b/src/lombok/eclipse/handlers/HandleData.java index f1623e39..0296d5e4 100644 --- a/src/lombok/eclipse/handlers/HandleData.java +++ b/src/lombok/eclipse/handlers/HandleData.java @@ -89,7 +89,8 @@ public class HandleData implements EclipseAnnotationHandler { //Skip static fields. if ( (fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0 ) continue; boolean isFinal = (fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0; - if ( isFinal && fieldDecl.initialization == null ) nodesForConstructor.add(child); + boolean isNonNull = findNonNullAnnotations(fieldDecl).length != 0; + if ( (isFinal || isNonNull) && fieldDecl.initialization == null ) nodesForConstructor.add(child); new HandleGetter().generateGetterForField(child, annotationNode.get()); if ( !isFinal ) new HandleSetter().generateSetterForField(child, annotationNode.get()); } @@ -139,18 +140,27 @@ public class HandleData implements EclipseAnnotationHandler { List args = new ArrayList(); List assigns = new ArrayList(); + List nullChecks = new ArrayList(); for ( Node fieldNode : fields ) { FieldDeclaration field = (FieldDeclaration) fieldNode.get(); FieldReference thisX = new FieldReference(("this." + new String(field.name)).toCharArray(), p); thisX.receiver = new ThisReference((int)(p >> 32), (int)p); thisX.token = field.name; + assigns.add(new Assignment(thisX, new SingleNameReference(field.name, p), (int)p)); long fieldPos = (((long)field.sourceStart) << 32) | field.sourceEnd; - args.add(new Argument(field.name, fieldPos, copyType(field.type), 0)); + Argument argument = new Argument(field.name, fieldPos, copyType(field.type), 0); + Annotation[] nonNulls = findNonNullAnnotations(field); + if (nonNulls.length != 0) { + nullChecks.add(generateNullCheck(field)); + argument.annotations = copyAnnotations(nonNulls); + } + args.add(argument); } - constructor.statements = assigns.isEmpty() ? null : assigns.toArray(new Statement[assigns.size()]); + nullChecks.addAll(assigns); + constructor.statements = nullChecks.isEmpty() ? null : nullChecks.toArray(new Statement[nullChecks.size()]); constructor.arguments = args.isEmpty() ? null : args.toArray(new Argument[args.size()]); return constructor; } @@ -188,6 +198,12 @@ public class HandleData implements EclipseAnnotationHandler { FieldDeclaration field = (FieldDeclaration) fieldNode.get(); long fieldPos = (((long)field.sourceStart) << 32) | field.sourceEnd; assigns.add(new SingleNameReference(field.name, fieldPos)); + + Argument argument = new Argument(field.name, fieldPos, copyType(field.type), 0); + Annotation[] nonNulls = findNonNullAnnotations(field); + if (nonNulls.length != 0) { + argument.annotations = copyAnnotations(nonNulls); + } args.add(new Argument(field.name, fieldPos, copyType(field.type), 0)); } -- cgit From fe0da3f53f1e88b704e21463cc5fea3d998e394a Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Thu, 27 Aug 2009 23:05:14 +0200 Subject: Now @Nullable is also copied over. --- src/lombok/eclipse/handlers/HandleData.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'src/lombok/eclipse/handlers/HandleData.java') diff --git a/src/lombok/eclipse/handlers/HandleData.java b/src/lombok/eclipse/handlers/HandleData.java index 0296d5e4..38e3135d 100644 --- a/src/lombok/eclipse/handlers/HandleData.java +++ b/src/lombok/eclipse/handlers/HandleData.java @@ -89,7 +89,7 @@ public class HandleData implements EclipseAnnotationHandler { //Skip static fields. if ( (fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0 ) continue; boolean isFinal = (fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0; - boolean isNonNull = findNonNullAnnotations(fieldDecl).length != 0; + boolean isNonNull = findAnnotations(fieldDecl, NON_NULL_PATTERN).length != 0; if ( (isFinal || isNonNull) && fieldDecl.initialization == null ) nodesForConstructor.add(child); new HandleGetter().generateGetterForField(child, annotationNode.get()); if ( !isFinal ) new HandleSetter().generateSetterForField(child, annotationNode.get()); @@ -151,11 +151,11 @@ public class HandleData implements EclipseAnnotationHandler { assigns.add(new Assignment(thisX, new SingleNameReference(field.name, p), (int)p)); long fieldPos = (((long)field.sourceStart) << 32) | field.sourceEnd; Argument argument = new Argument(field.name, fieldPos, copyType(field.type), 0); - Annotation[] nonNulls = findNonNullAnnotations(field); - if (nonNulls.length != 0) { - nullChecks.add(generateNullCheck(field)); - argument.annotations = copyAnnotations(nonNulls); - } + Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); + Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); + if (nonNulls.length != 0) nullChecks.add(generateNullCheck(field)); + Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables); + if (copiedAnnotations.length != 0) argument.annotations = copiedAnnotations; args.add(argument); } @@ -200,10 +200,9 @@ public class HandleData implements EclipseAnnotationHandler { assigns.add(new SingleNameReference(field.name, fieldPos)); Argument argument = new Argument(field.name, fieldPos, copyType(field.type), 0); - Annotation[] nonNulls = findNonNullAnnotations(field); - if (nonNulls.length != 0) { - argument.annotations = copyAnnotations(nonNulls); - } + Annotation[] copiedAnnotations = copyAnnotations( + findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN)); + if (copiedAnnotations.length != 0) argument.annotations = copiedAnnotations; args.add(new Argument(field.name, fieldPos, copyType(field.type), 0)); } -- cgit