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 | |
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')
-rw-r--r-- | src/lombok/core/TransformationsUtil.java | 7 | ||||
-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 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleData.java | 14 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleGetter.java | 5 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleSetter.java | 13 | ||||
-rw-r--r-- | src/lombok/javac/handlers/PKG.java | 12 |
9 files changed, 58 insertions, 30 deletions
diff --git a/src/lombok/core/TransformationsUtil.java b/src/lombok/core/TransformationsUtil.java index 12b90d0e..5b7400a3 100644 --- a/src/lombok/core/TransformationsUtil.java +++ b/src/lombok/core/TransformationsUtil.java @@ -27,6 +27,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.regex.Pattern; /** * Container for static utility methods useful for some of the standard lombok transformations, regardless of @@ -79,6 +80,12 @@ public class TransformationsUtil { return buildName(prefix, fieldName.toString()); } + public static final Pattern PRIMITIVE_TYPE_NAME_PATTERN = Pattern.compile( + "^(boolean|byte|short|int|long|float|double|char)$"); + + public static final Pattern NON_NULL_PATTERN = Pattern.compile("^no[tn]null$", Pattern.CASE_INSENSITIVE); + public static final Pattern NULLABLE_PATTERN = Pattern.compile("^nullable$", Pattern.CASE_INSENSITIVE); + /** * Generates a getter name from a given field name. * 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)}; diff --git a/src/lombok/javac/handlers/HandleData.java b/src/lombok/javac/handlers/HandleData.java index 3b4f8951..2e1d01b1 100644 --- a/src/lombok/javac/handlers/HandleData.java +++ b/src/lombok/javac/handlers/HandleData.java @@ -27,6 +27,7 @@ import java.lang.reflect.Modifier; import lombok.Data; import lombok.core.AnnotationValues; +import lombok.core.TransformationsUtil; import lombok.core.AST.Kind; import lombok.javac.JavacAnnotationHandler; import lombok.javac.JavacAST.Node; @@ -79,7 +80,7 @@ public class HandleData implements JavacAnnotationHandler<Data> { if ( (fieldFlags & Flags.STATIC) != 0 ) continue; if ( (fieldFlags & Flags.TRANSIENT) == 0 ) nodesForEquality = nodesForEquality.append(child); boolean isFinal = (fieldFlags & Flags.FINAL) != 0; - boolean isNonNull = !findAnnotations(child, NON_NULL_PATTERN).isEmpty(); + boolean isNonNull = !findAnnotations(child, TransformationsUtil.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()); @@ -113,8 +114,8 @@ public class HandleData implements JavacAnnotationHandler<Data> { for ( Node fieldNode : fields ) { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); - List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); + List<JCAnnotation> nonNulls = findAnnotations(fieldNode, TransformationsUtil.NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(fieldNode, TransformationsUtil.NULLABLE_PATTERN); JCVariableDecl param = maker.VarDef(maker.Modifiers(Flags.FINAL, nonNulls.appendList(nullables)), field.name, field.vartype, null); params = params.append(param); JCFieldAccess thisX = maker.Select(maker.Ident(fieldNode.toName("this")), field.name); @@ -122,7 +123,8 @@ public class HandleData implements JavacAnnotationHandler<Data> { assigns = assigns.append(maker.Exec(assign)); if (!nonNulls.isEmpty()) { - nullChecks = nullChecks.append(generateNullCheck(maker, fieldNode)); + JCStatement nullCheck = generateNullCheck(maker, fieldNode); + if (nullCheck != null) nullChecks = nullChecks.append(nullCheck); } } @@ -168,8 +170,8 @@ public class HandleData implements JavacAnnotationHandler<Data> { for ( JCExpression arg : typeApply.arguments ) tArgs = tArgs.append(arg); pType = maker.TypeApply(typeApply.clazz, tArgs); } else pType = field.vartype; - List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN); + List<JCAnnotation> nonNulls = findAnnotations(fieldNode, TransformationsUtil.NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(fieldNode, TransformationsUtil.NULLABLE_PATTERN); JCVariableDecl param = maker.VarDef(maker.Modifiers(Flags.FINAL, 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/HandleGetter.java b/src/lombok/javac/handlers/HandleGetter.java index 774a9955..7fddc0e9 100644 --- a/src/lombok/javac/handlers/HandleGetter.java +++ b/src/lombok/javac/handlers/HandleGetter.java @@ -25,6 +25,7 @@ import static lombok.javac.handlers.PKG.*; import lombok.AccessLevel; import lombok.Getter; import lombok.core.AnnotationValues; +import lombok.core.TransformationsUtil; import lombok.core.AST.Kind; import lombok.javac.Javac; import lombok.javac.JavacAnnotationHandler; @@ -128,8 +129,8 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> { List<JCExpression> throwsClauses = List.nil(); JCExpression annotationMethodDefaultValue = null; - List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); + List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(field, TransformationsUtil.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 412b1c43..253b0325 100644 --- a/src/lombok/javac/handlers/HandleSetter.java +++ b/src/lombok/javac/handlers/HandleSetter.java @@ -25,6 +25,7 @@ import static lombok.javac.handlers.PKG.*; import lombok.AccessLevel; import lombok.Setter; import lombok.core.AnnotationValues; +import lombok.core.TransformationsUtil; import lombok.core.AST.Kind; import lombok.javac.Javac; import lombok.javac.JavacAST; @@ -120,13 +121,15 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { JCAssign assign = treeMaker.Assign(thisX, treeMaker.Ident(fieldDecl.name)); List<JCStatement> statements; - List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); - List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); + List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); + if (nonNulls.isEmpty()) { statements = List.<JCStatement>of(treeMaker.Exec(assign)); - } - else { - statements = List.<JCStatement>of(generateNullCheck(treeMaker, field), treeMaker.Exec(assign)); + } else { + JCStatement nullCheck = generateNullCheck(treeMaker, field); + if (nullCheck != null) statements = List.<JCStatement>of(nullCheck, treeMaker.Exec(assign)); + else statements = List.<JCStatement>of(treeMaker.Exec(assign)); } JCBlock methodBody = treeMaker.Block(0, statements); diff --git a/src/lombok/javac/handlers/PKG.java b/src/lombok/javac/handlers/PKG.java index 42cfed13..4b05b9ae 100644 --- a/src/lombok/javac/handlers/PKG.java +++ b/src/lombok/javac/handlers/PKG.java @@ -51,6 +51,11 @@ class PKG { //Prevent instantiation } + static boolean isPrimitive(JCExpression ref) { + String typeName = ref.toString(); + return TransformationsUtil.PRIMITIVE_TYPE_NAME_PATTERN.matcher(typeName).matches(); + } + static java.util.List<String> toAllGetterNames(JCVariableDecl field) { CharSequence fieldName = field.name; @@ -258,9 +263,6 @@ class PKG { return e; } - 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<JCAnnotation> findAnnotations(Node fieldNode, Pattern namePattern) { List<JCAnnotation> result = List.nil(); for ( Node child : fieldNode.down() ) { @@ -278,7 +280,9 @@ class PKG { } static JCStatement generateNullCheck(TreeMaker treeMaker, JavacAST.Node variable) { - Name fieldName = ((JCVariableDecl) variable.get()).name; + JCVariableDecl varDecl = (JCVariableDecl) variable.get(); + if (isPrimitive(varDecl.vartype)) return null; + Name fieldName = varDecl.name; JCExpression npe = chainDots(treeMaker, variable, "java", "lang", "NullPointerException"); JCTree exception = treeMaker.NewClass(null, List.<JCExpression>nil(), npe, List.<JCExpression>of(treeMaker.Literal(fieldName.toString())), null); JCStatement throwStatement = treeMaker.Throw(exception); |