aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/javac
diff options
context:
space:
mode:
Diffstat (limited to 'src/lombok/javac')
-rw-r--r--src/lombok/javac/handlers/HandleSetter.java18
-rw-r--r--src/lombok/javac/handlers/PKG.java25
2 files changed, 37 insertions, 6 deletions
diff --git a/src/lombok/javac/handlers/HandleSetter.java b/src/lombok/javac/handlers/HandleSetter.java
index d11b4250..b4720387 100644
--- a/src/lombok/javac/handlers/HandleSetter.java
+++ b/src/lombok/javac/handlers/HandleSetter.java
@@ -34,6 +34,8 @@ import lombok.javac.JavacAST.Node;
import org.mangosdk.spi.ProviderFor;
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.JCAssign;
@@ -119,7 +121,21 @@ 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;
+ JCAnnotation nonNull = findNonNullAnnotation(field);
+ if (nonNull == null) {
+ statements = List.<JCStatement>of(treeMaker.Exec(assign));
+ }
+ else {
+ JCExpression npe = chainDots(treeMaker, field, "java", "lang", "NullPointerException");
+ JCTree exception = treeMaker.NewClass(null, List.<JCExpression>nil(), npe, List.<JCExpression>of(treeMaker.Literal(fieldDecl.name.toString())), null);
+ JCStatement throwStatement = treeMaker.Throw(exception);
+ JCStatement nullCheck = treeMaker.If(treeMaker.Binary(JCTree.EQ, treeMaker.Ident(fieldDecl.name), treeMaker.Literal(TypeTags.BOT, null)), throwStatement, null);
+ statements = List.<JCStatement>of(nullCheck, treeMaker.Exec(assign));
+ }
+
+ JCBlock methodBody = treeMaker.Block(0, statements);
Name methodName = field.toName(toSetterName(fieldDecl));
JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(0), fieldDecl.name, fieldDecl.vartype, null);
JCExpression methodType = treeMaker.Type(field.getSymbolTable().voidType);
diff --git a/src/lombok/javac/handlers/PKG.java b/src/lombok/javac/handlers/PKG.java
index 205a2b6e..bc5b691c 100644
--- a/src/lombok/javac/handlers/PKG.java
+++ b/src/lombok/javac/handlers/PKG.java
@@ -23,20 +23,22 @@ package lombok.javac.handlers;
import java.lang.reflect.Modifier;
+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.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.JCVariableDecl;
import com.sun.tools.javac.util.List;
-import lombok.AccessLevel;
-import lombok.core.TransformationsUtil;
-import lombok.core.AST.Kind;
-import lombok.javac.JavacAST;
-
/**
* Container for static utility methods relevant to this package.
*/
@@ -251,4 +253,17 @@ class PKG {
return e;
}
+
+ static JCAnnotation findNonNullAnnotation(Node fieldNode) {
+ for ( Node child : fieldNode.down() ) {
+ if ( child.getKind() == Kind.ANNOTATION ) {
+ JCAnnotation annotation = (JCAnnotation) child.get();
+ String name = annotation.annotationType.toString();
+ if (name.equals("NonNull") || name.endsWith(".NonNull")) {
+ return annotation;
+ }
+ }
+ }
+ return null;
+ }
}