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/Eclipse.java | 15 ++++++++++++--- src/lombok/eclipse/handlers/HandleData.java | 19 +++++++++---------- .../eclipse/handlers/HandleEqualsAndHashCode.java | 8 ++++---- src/lombok/eclipse/handlers/HandleGetter.java | 7 ++++--- src/lombok/eclipse/handlers/HandleSetter.java | 20 +++++++------------- src/lombok/eclipse/handlers/PKG.java | 9 ++++++--- src/lombok/javac/handlers/HandleData.java | 12 ++++++++---- .../javac/handlers/HandleEqualsAndHashCode.java | 8 ++++---- src/lombok/javac/handlers/HandleGetter.java | 5 +++-- src/lombok/javac/handlers/HandleSetter.java | 12 ++++-------- src/lombok/javac/handlers/PKG.java | 8 ++++++-- 11 files changed, 67 insertions(+), 56 deletions(-) (limited to 'src/lombok/eclipse/handlers/HandleData.java') diff --git a/src/lombok/eclipse/Eclipse.java b/src/lombok/eclipse/Eclipse.java index 33164ab5..017affa1 100644 --- a/src/lombok/eclipse/Eclipse.java +++ b/src/lombok/eclipse/Eclipse.java @@ -255,10 +255,19 @@ public class Eclipse { } public static Annotation[] copyAnnotations(Annotation[] annotations) { - if (annotations == null) return null; - Annotation[] outs = new Annotation[annotations.length]; + return copyAnnotations(annotations, null); + } + + public static Annotation[] copyAnnotations(Annotation[] annotations1, Annotation[] annotations2) { + if (annotations1 == null && annotations2 == null) return null; + if (annotations1 == null) annotations1 = new Annotation[0]; + if (annotations2 == null) annotations2 = new Annotation[0]; + Annotation[] outs = new Annotation[annotations1.length + annotations2.length]; int idx = 0; - for ( Annotation annotation : annotations ) { + for ( Annotation annotation : annotations1 ) { + outs[idx++] = copyAnnotation(annotation); + } + for ( Annotation annotation : annotations2 ) { outs[idx++] = copyAnnotation(annotation); } return outs; 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)); } diff --git a/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index d8e1562e..f837d4a6 100644 --- a/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -153,19 +153,19 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler { } MethodDeclaration method = generateGetter((TypeDeclaration) fieldNode.up().get(), field, getterName, modifier, pos); - Annotation[] nonNulls = findNonNullAnnotations(field); - if (nonNulls.length != 0) { - method.annotations = copyAnnotations(nonNulls); + Annotation[] copiedAnnotations = copyAnnotations( + findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN)); + if (copiedAnnotations.length != 0) { + method.annotations = copiedAnnotations; } injectMethod(fieldNode.up(), method); diff --git a/src/lombok/eclipse/handlers/HandleSetter.java b/src/lombok/eclipse/handlers/HandleSetter.java index d850e549..6214c86d 100644 --- a/src/lombok/eclipse/handlers/HandleSetter.java +++ b/src/lombok/eclipse/handlers/HandleSetter.java @@ -21,15 +21,8 @@ */ package lombok.eclipse.handlers; -import static lombok.eclipse.Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; -import static lombok.eclipse.Eclipse.annotationTypeMatches; -import static lombok.eclipse.Eclipse.copyAnnotations; -import static lombok.eclipse.Eclipse.copyType; -import static lombok.eclipse.handlers.PKG.findNonNullAnnotations; -import static lombok.eclipse.handlers.PKG.generateNullCheck; -import static lombok.eclipse.handlers.PKG.injectMethod; -import static lombok.eclipse.handlers.PKG.methodExists; -import static lombok.eclipse.handlers.PKG.toModifier; +import static lombok.eclipse.Eclipse.*; +import static lombok.eclipse.handlers.PKG.*; import lombok.AccessLevel; import lombok.Setter; import lombok.core.AnnotationValues; @@ -146,14 +139,15 @@ public class HandleSetter implements EclipseAnnotationHandler { method.bodyStart = method.declarationSourceStart = method.sourceStart = ast.sourceStart; method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = ast.sourceEnd; - Annotation[] nonNulls = findNonNullAnnotations(field); + Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); + Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); if (nonNulls.length == 0) { method.statements = new Statement[] { assignment }; - } - else { - param.annotations = copyAnnotations(nonNulls); + } else { method.statements = new Statement[] { generateNullCheck(field), assignment }; } + Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables); + if (copiedAnnotations.length != 0) param.annotations = copiedAnnotations; return method; } } diff --git a/src/lombok/eclipse/handlers/PKG.java b/src/lombok/eclipse/handlers/PKG.java index 2eae1edf..17096b70 100644 --- a/src/lombok/eclipse/handlers/PKG.java +++ b/src/lombok/eclipse/handlers/PKG.java @@ -26,6 +26,7 @@ import static lombok.eclipse.Eclipse.fromQualifiedName; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; +import java.util.regex.Pattern; import lombok.AccessLevel; import lombok.core.AST.Kind; @@ -231,14 +232,17 @@ class PKG { type.add(method, Kind.METHOD).recursiveSetHandled(); } - static Annotation[] findNonNullAnnotations(FieldDeclaration field) { + static final Pattern NON_NULL_PATTERN = Pattern.compile("^no[tn]null$", Pattern.CASE_INSENSITIVE); + static final Pattern NULLABLE_PATTERN = Pattern.compile("^nullable$", Pattern.CASE_INSENSITIVE); + + static Annotation[] findAnnotations(FieldDeclaration field, Pattern namePattern) { List result = new ArrayList(); for (Annotation annotation : field.annotations) { TypeReference typeRef = annotation.type; if ( typeRef != null && typeRef.getTypeName()!= null ) { char[][] typeName = typeRef.getTypeName(); String suspect = new String(typeName[typeName.length - 1]); - if (suspect.equalsIgnoreCase("NonNull") || suspect.equalsIgnoreCase("NotNull")) { + if ( namePattern.matcher(suspect).matches() ) { result.add(annotation); } } @@ -246,7 +250,6 @@ class PKG { return result.toArray(new Annotation[0]); } - static Statement generateNullCheck(AbstractVariableDeclaration variable) { AllocationExpression exception = new AllocationExpression(); exception.type = new QualifiedTypeReference(fromQualifiedName("java.lang.NullPointerException"), new long[]{0, 0, 0}); diff --git a/src/lombok/javac/handlers/HandleData.java b/src/lombok/javac/handlers/HandleData.java index e65462d8..648ff91e 100644 --- a/src/lombok/javac/handlers/HandleData.java +++ b/src/lombok/javac/handlers/HandleData.java @@ -79,7 +79,7 @@ public class HandleData implements JavacAnnotationHandler { if ( (fieldFlags & Flags.STATIC) != 0 ) continue; if ( (fieldFlags & Flags.TRANSIENT) == 0 ) nodesForEquality = nodesForEquality.append(child); boolean isFinal = (fieldFlags & Flags.FINAL) != 0; - boolean isNonNull = !findNonNullAnnotations(child).isEmpty(); + boolean isNonNull = !findAnnotations(child, NON_NULL_PATTERN).isEmpty(); if ( (isFinal || isNonNull) && fieldDecl.init == null ) nodesForConstructor = nodesForConstructor.append(child); new HandleGetter().generateGetterForField(child, annotationNode.get()); if ( !isFinal ) new HandleSetter().generateSetterForField(child, annotationNode.get()); @@ -114,8 +114,9 @@ public class HandleData implements JavacAnnotationHandler { for ( Node fieldNode : fields ) { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); - List nonNulls = findNonNullAnnotations(fieldNode); - JCVariableDecl param = maker.VarDef(maker.Modifiers(0, nonNulls), field.name, field.vartype, null); + List nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); + List nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); + JCVariableDecl param = maker.VarDef(maker.Modifiers(0, nonNulls.appendList(nullables)), field.name, field.vartype, null); params = params.append(param); JCFieldAccess thisX = maker.Select(maker.Ident(fieldNode.toName("this")), field.name); @@ -169,7 +170,10 @@ public class HandleData implements JavacAnnotationHandler { for ( JCExpression arg : typeApply.arguments ) tArgs = tArgs.append(arg); pType = maker.TypeApply(typeApply.clazz, tArgs); } else pType = field.vartype; - JCVariableDecl param = maker.VarDef(maker.Modifiers(0, findNonNullAnnotations(fieldNode)), field.name, pType, null); + + List nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); + List nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); + JCVariableDecl param = maker.VarDef(maker.Modifiers(0, nonNulls.appendList(nullables)), field.name, pType, null); params = params.append(param); args = args.append(maker.Ident(field.name)); } diff --git a/src/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java index 1a1158eb..e3e7c1ac 100644 --- a/src/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -119,20 +119,20 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler { List throwsClauses = List.nil(); JCExpression annotationMethodDefaultValue = null; - List annotations = findNonNullAnnotations(field); - return treeMaker.MethodDef(treeMaker.Modifiers(access, annotations), methodName, methodType, + List nonNulls = findAnnotations(field, NON_NULL_PATTERN); + List nullables = findAnnotations(field, NULLABLE_PATTERN); + return treeMaker.MethodDef(treeMaker.Modifiers(access, nonNulls.appendList(nullables)), methodName, methodType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue); } } diff --git a/src/lombok/javac/handlers/HandleSetter.java b/src/lombok/javac/handlers/HandleSetter.java index 9fb995e4..c10e731c 100644 --- a/src/lombok/javac/handlers/HandleSetter.java +++ b/src/lombok/javac/handlers/HandleSetter.java @@ -21,12 +21,7 @@ */ package lombok.javac.handlers; -import static lombok.javac.handlers.PKG.findNonNullAnnotations; -import static lombok.javac.handlers.PKG.generateNullCheck; -import static lombok.javac.handlers.PKG.injectMethod; -import static lombok.javac.handlers.PKG.methodExists; -import static lombok.javac.handlers.PKG.toJavacModifier; -import static lombok.javac.handlers.PKG.toSetterName; +import static lombok.javac.handlers.PKG.*; import lombok.AccessLevel; import lombok.Setter; import lombok.core.AnnotationValues; @@ -125,7 +120,8 @@ public class HandleSetter implements JavacAnnotationHandler { JCAssign assign = treeMaker.Assign(thisX, treeMaker.Ident(fieldDecl.name)); List statements; - List nonNulls = findNonNullAnnotations(field); + List nonNulls = findAnnotations(field, NON_NULL_PATTERN); + List nullables = findAnnotations(field, NULLABLE_PATTERN); if (nonNulls.isEmpty()) { statements = List.of(treeMaker.Exec(assign)); } @@ -136,7 +132,7 @@ public class HandleSetter implements JavacAnnotationHandler { JCBlock methodBody = treeMaker.Block(0, statements); Name methodName = field.toName(toSetterName(fieldDecl)); - JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(0, nonNulls), fieldDecl.name, fieldDecl.vartype, null); + JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(0, nonNulls.appendList(nullables)), 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 006e92a3..42cfed13 100644 --- a/src/lombok/javac/handlers/PKG.java +++ b/src/lombok/javac/handlers/PKG.java @@ -22,6 +22,7 @@ package lombok.javac.handlers; import java.lang.reflect.Modifier; +import java.util.regex.Pattern; import lombok.AccessLevel; import lombok.core.TransformationsUtil; @@ -257,7 +258,10 @@ class PKG { return e; } - static List findNonNullAnnotations(Node fieldNode) { + static final Pattern NON_NULL_PATTERN = Pattern.compile("^no[tn]null$", Pattern.CASE_INSENSITIVE); + static final Pattern NULLABLE_PATTERN = Pattern.compile("^nullable$", Pattern.CASE_INSENSITIVE); + + static List findAnnotations(Node fieldNode, Pattern namePattern) { List result = List.nil(); for ( Node child : fieldNode.down() ) { if ( child.getKind() == Kind.ANNOTATION ) { @@ -265,7 +269,7 @@ class PKG { String name = annotation.annotationType.toString(); int idx = name.lastIndexOf("."); String suspect = idx == -1 ? name : name.substring(idx + 1); - if (suspect.equalsIgnoreCase("NonNull") || suspect.equalsIgnoreCase("NotNull")) { + if (namePattern.matcher(suspect).matches()) { result = result.append(annotation); } } -- cgit