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/javac | |
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/javac')
-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 |
4 files changed, 27 insertions, 17 deletions
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); |