diff options
Diffstat (limited to 'src/lombok/javac/handlers')
-rw-r--r-- | src/lombok/javac/handlers/HandleData.java | 18 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleEqualsAndHashCode.java | 8 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleGetter.java | 4 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleSetter.java | 14 | ||||
-rw-r--r-- | src/lombok/javac/handlers/PKG.java | 43 |
5 files changed, 71 insertions, 16 deletions
diff --git a/src/lombok/javac/handlers/HandleData.java b/src/lombok/javac/handlers/HandleData.java index 7b094106..3b4f8951 100644 --- a/src/lombok/javac/handlers/HandleData.java +++ b/src/lombok/javac/handlers/HandleData.java @@ -79,7 +79,8 @@ 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; - if ( isFinal && fieldDecl.init == null ) nodesForConstructor = nodesForConstructor.append(child); + 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()); } @@ -106,21 +107,28 @@ public class HandleData implements JavacAnnotationHandler<Data> { TreeMaker maker = typeNode.getTreeMaker(); JCClassDecl type = (JCClassDecl) typeNode.get(); + List<JCStatement> nullChecks = List.nil(); List<JCStatement> assigns = List.nil(); List<JCVariableDecl> params = List.nil(); for ( Node fieldNode : fields ) { JCVariableDecl field = (JCVariableDecl) fieldNode.get(); - JCVariableDecl param = maker.VarDef(maker.Modifiers(Flags.FINAL), field.name, field.vartype, null); + List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(fieldNode, 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); JCAssign assign = maker.Assign(thisX, maker.Ident(field.name)); assigns = assigns.append(maker.Exec(assign)); + + if (!nonNulls.isEmpty()) { + nullChecks = nullChecks.append(generateNullCheck(maker, fieldNode)); + } } JCModifiers mods = maker.Modifiers(isPublic ? Modifier.PUBLIC : Modifier.PRIVATE); return maker.MethodDef(mods, typeNode.toName("<init>"), - null, type.typarams, params, List.<JCExpression>nil(), maker.Block(0L, assigns), null); + null, type.typarams, params, List.<JCExpression>nil(), maker.Block(0L, nullChecks.appendList(assigns)), null); } private JCMethodDecl createStaticConstructor(String name, Node typeNode, List<Node> fields) { @@ -160,7 +168,9 @@ 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; - JCVariableDecl param = maker.VarDef(maker.Modifiers(Flags.FINAL), field.name, pType, null); + List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(fieldNode, 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/HandleEqualsAndHashCode.java b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java index efc154f6..8c3124c9 100644 --- a/src/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -120,20 +120,20 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd return false; } - boolean isDirectDescendentOfObject = true; + boolean isDirectDescendantOfObject = true; JCTree extending = ((JCClassDecl)typeNode.get()).extending; if ( extending != null ) { String p = extending.toString(); - isDirectDescendentOfObject = p.equals("Object") || p.equals("java.lang.Object"); + isDirectDescendantOfObject = p.equals("Object") || p.equals("java.lang.Object"); } - if ( isDirectDescendentOfObject && callSuper ) { + if ( isDirectDescendantOfObject && callSuper ) { errorNode.addError("Generating equals/hashCode with a supercall to java.lang.Object is pointless."); return true; } - if ( !isDirectDescendentOfObject && !callSuper && implicit ) { + if ( !isDirectDescendantOfObject && !callSuper && implicit ) { errorNode.addWarning("Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add '@EqualsAndHashCode(callSuper=false)' to your type."); } diff --git a/src/lombok/javac/handlers/HandleGetter.java b/src/lombok/javac/handlers/HandleGetter.java index 3d3227e4..774a9955 100644 --- a/src/lombok/javac/handlers/HandleGetter.java +++ b/src/lombok/javac/handlers/HandleGetter.java @@ -128,7 +128,9 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> { List<JCExpression> throwsClauses = List.nil(); JCExpression annotationMethodDefaultValue = null; - return treeMaker.MethodDef(treeMaker.Modifiers(access, List.<JCAnnotation>nil()), methodName, methodType, + List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); + List<JCAnnotation> 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 2e25ee57..412b1c43 100644 --- a/src/lombok/javac/handlers/HandleSetter.java +++ b/src/lombok/javac/handlers/HandleSetter.java @@ -119,9 +119,19 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { JCFieldAccess thisX = treeMaker.Select(treeMaker.Ident(field.toName("this")), fieldDecl.name); JCAssign assign = treeMaker.Assign(thisX, treeMaker.Ident(fieldDecl.name)); - JCBlock methodBody = treeMaker.Block(0, List.<JCStatement>of(treeMaker.Exec(assign))); + List<JCStatement> statements; + List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN); + List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN); + if (nonNulls.isEmpty()) { + statements = List.<JCStatement>of(treeMaker.Exec(assign)); + } + else { + statements = List.<JCStatement>of(generateNullCheck(treeMaker, field), treeMaker.Exec(assign)); + } + + JCBlock methodBody = treeMaker.Block(0, statements); Name methodName = field.toName(toSetterName(fieldDecl)); - JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(Flags.FINAL), fieldDecl.name, fieldDecl.vartype, null); + JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(Flags.FINAL, nonNulls.appendList(nullables)), fieldDecl.name, fieldDecl.vartype, null); JCExpression methodType = treeMaker.Type(field.getSymbolTable().voidType); List<JCTypeParameter> methodGenericParams = List.nil(); diff --git a/src/lombok/javac/handlers/PKG.java b/src/lombok/javac/handlers/PKG.java index 205a2b6e..42cfed13 100644 --- a/src/lombok/javac/handlers/PKG.java +++ b/src/lombok/javac/handlers/PKG.java @@ -22,20 +22,26 @@ package lombok.javac.handlers; import java.lang.reflect.Modifier; +import java.util.regex.Pattern; + +import lombok.AccessLevel; +import lombok.core.TransformationsUtil; +import lombok.core.AST.Kind; +import lombok.javac.JavacAST; +import lombok.javac.JavacAST.Node; import com.sun.tools.javac.code.Flags; +import com.sun.tools.javac.code.TypeTags; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.TreeMaker; +import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCClassDecl; import com.sun.tools.javac.tree.JCTree.JCExpression; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; +import com.sun.tools.javac.tree.JCTree.JCStatement; import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.util.List; - -import lombok.AccessLevel; -import lombok.core.TransformationsUtil; -import lombok.core.AST.Kind; -import lombok.javac.JavacAST; +import com.sun.tools.javac.util.Name; /** * Container for static utility methods relevant to this package. @@ -251,4 +257,31 @@ 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() ) { + if ( child.getKind() == Kind.ANNOTATION ) { + JCAnnotation annotation = (JCAnnotation) child.get(); + String name = annotation.annotationType.toString(); + int idx = name.lastIndexOf("."); + String suspect = idx == -1 ? name : name.substring(idx + 1); + if (namePattern.matcher(suspect).matches()) { + result = result.append(annotation); + } + } + } + return result; + } + + static JCStatement generateNullCheck(TreeMaker treeMaker, JavacAST.Node variable) { + Name fieldName = ((JCVariableDecl) variable.get()).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); + return treeMaker.If(treeMaker.Binary(JCTree.EQ, treeMaker.Ident(fieldName), treeMaker.Literal(TypeTags.BOT, null)), throwStatement, null); + } } |