diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-08-28 00:02:15 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-08-28 00:02:15 +0200 |
commit | e1ee1b7d2db1ea998aa4d6aa3f6b4141315a9496 (patch) | |
tree | 5f014017561f6a9cff56e8ad35f2c6e3cf7b882e /src/lombok/eclipse/handlers | |
parent | d16775032b638789f998f3f362b7ed7d4f5098ae (diff) | |
download | lombok-e1ee1b7d2db1ea998aa4d6aa3f6b4141315a9496.tar.gz lombok-e1ee1b7d2db1ea998aa4d6aa3f6b4141315a9496.tar.bz2 lombok-e1ee1b7d2db1ea998aa4d6aa3f6b4141315a9496.zip |
null checks are no longer generated if you put @NonNull on primitives.
Diffstat (limited to 'src/lombok/eclipse/handlers')
-rw-r--r-- | src/lombok/eclipse/handlers/HandleData.java | 14 | ||||
-rw-r--r-- | src/lombok/eclipse/handlers/HandleGetter.java | 4 | ||||
-rw-r--r-- | src/lombok/eclipse/handlers/HandleSetter.java | 8 | ||||
-rw-r--r-- | src/lombok/eclipse/handlers/PKG.java | 11 |
4 files changed, 24 insertions, 13 deletions
diff --git a/src/lombok/eclipse/handlers/HandleData.java b/src/lombok/eclipse/handlers/HandleData.java index f072ea64..a6af683a 100644 --- a/src/lombok/eclipse/handlers/HandleData.java +++ b/src/lombok/eclipse/handlers/HandleData.java @@ -32,6 +32,7 @@ import java.util.List; import lombok.AccessLevel; import lombok.Data; import lombok.core.AnnotationValues; +import lombok.core.TransformationsUtil; import lombok.core.AST.Kind; import lombok.eclipse.Eclipse; import lombok.eclipse.EclipseAnnotationHandler; @@ -89,7 +90,7 @@ public class HandleData implements EclipseAnnotationHandler<Data> { //Skip static fields. if ( (fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0 ) continue; boolean isFinal = (fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0; - boolean isNonNull = findAnnotations(fieldDecl, NON_NULL_PATTERN).length != 0; + boolean isNonNull = findAnnotations(fieldDecl, TransformationsUtil.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,9 +152,12 @@ public class HandleData implements EclipseAnnotationHandler<Data> { 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), Modifier.FINAL); - Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); - Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); - if (nonNulls.length != 0) nullChecks.add(generateNullCheck(field)); + Annotation[] nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); + Annotation[] nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); + if (nonNulls.length != 0) { + Statement nullCheck = generateNullCheck(field); + if (nullCheck != null) nullChecks.add(nullCheck); + } Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables); if (copiedAnnotations.length != 0) argument.annotations = copiedAnnotations; args.add(argument); @@ -201,7 +205,7 @@ public class HandleData implements EclipseAnnotationHandler<Data> { Argument argument = new Argument(field.name, fieldPos, copyType(field.type), 0); Annotation[] copiedAnnotations = copyAnnotations( - findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN)); + findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN)); if (copiedAnnotations.length != 0) argument.annotations = copiedAnnotations; args.add(new Argument(field.name, fieldPos, copyType(field.type), Modifier.FINAL)); } diff --git a/src/lombok/eclipse/handlers/HandleGetter.java b/src/lombok/eclipse/handlers/HandleGetter.java index 5fb7876a..c3abd9f3 100644 --- a/src/lombok/eclipse/handlers/HandleGetter.java +++ b/src/lombok/eclipse/handlers/HandleGetter.java @@ -21,13 +21,13 @@ */ package lombok.eclipse.handlers; +import static lombok.eclipse.Eclipse.*; import static lombok.eclipse.handlers.PKG.*; import lombok.AccessLevel; import lombok.Getter; import lombok.core.AnnotationValues; import lombok.core.TransformationsUtil; import lombok.core.AST.Kind; -import static lombok.eclipse.Eclipse.*; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseAST.Node; @@ -114,7 +114,7 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> { MethodDeclaration method = generateGetter((TypeDeclaration) fieldNode.up().get(), field, getterName, modifier, pos); Annotation[] copiedAnnotations = copyAnnotations( - findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN)); + findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN), findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN)); if (copiedAnnotations.length != 0) { method.annotations = copiedAnnotations; } diff --git a/src/lombok/eclipse/handlers/HandleSetter.java b/src/lombok/eclipse/handlers/HandleSetter.java index 5ad9b193..17747d8c 100644 --- a/src/lombok/eclipse/handlers/HandleSetter.java +++ b/src/lombok/eclipse/handlers/HandleSetter.java @@ -142,12 +142,14 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> { method.bodyStart = method.declarationSourceStart = method.sourceStart = ast.sourceStart; method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = ast.sourceEnd; - Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); - Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); + Annotation[] nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); + Annotation[] nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); if (nonNulls.length == 0) { method.statements = new Statement[] { assignment }; } else { - method.statements = new Statement[] { generateNullCheck(field), assignment }; + Statement nullCheck = generateNullCheck(field); + if (nullCheck != null) method.statements = new Statement[] { nullCheck, assignment }; + else method.statements = new Statement[] { assignment }; } Annotation[] copiedAnnotations = copyAnnotations(nonNulls, nullables); if (copiedAnnotations.length != 0) param.annotations = copiedAnnotations; diff --git a/src/lombok/eclipse/handlers/PKG.java b/src/lombok/eclipse/handlers/PKG.java index 17096b70..87ccd736 100644 --- a/src/lombok/eclipse/handlers/PKG.java +++ b/src/lombok/eclipse/handlers/PKG.java @@ -29,7 +29,9 @@ import java.util.List; import java.util.regex.Pattern; import lombok.AccessLevel; +import lombok.core.TransformationsUtil; import lombok.core.AST.Kind; +import lombok.eclipse.Eclipse; import lombok.eclipse.EclipseAST; import org.eclipse.jdt.internal.compiler.ast.ASTNode; @@ -55,6 +57,11 @@ import org.eclipse.jdt.internal.compiler.ast.TypeReference; class PKG { private PKG() {} + static boolean isPrimitive(TypeReference ref) { + if (ref.dimensions() > 0) return false; + return TransformationsUtil.PRIMITIVE_TYPE_NAME_PATTERN.matcher(Eclipse.toQualifiedName(ref.getTypeName())).matches(); + } + static int toModifier(AccessLevel value) { switch ( value ) { case MODULE: @@ -232,9 +239,6 @@ class PKG { type.add(method, Kind.METHOD).recursiveSetHandled(); } - 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<Annotation> result = new ArrayList<Annotation>(); for (Annotation annotation : field.annotations) { @@ -251,6 +255,7 @@ class PKG { } static Statement generateNullCheck(AbstractVariableDeclaration variable) { + if (isPrimitive(variable.type)) return null; AllocationExpression exception = new AllocationExpression(); exception.type = new QualifiedTypeReference(fromQualifiedName("java.lang.NullPointerException"), new long[]{0, 0, 0}); exception.arguments = new Expression[] { new StringLiteral(variable.name, 0, variable.name.length - 1, 0)}; |