aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/eclipse/handlers
diff options
context:
space:
mode:
authorRoel Spilker <r.spilker@gmail.com>2009-07-31 18:44:13 +0200
committerRoel Spilker <r.spilker@gmail.com>2009-07-31 18:44:13 +0200
commit0cfa9e99d99fc353d0c486e96cc53f5214ab031c (patch)
tree5ea939bd0c1eec4a554786d95e32b3af23e1238c /src/lombok/eclipse/handlers
parent2bdc1210d7a26df8b69563f0de22524398ba9bfd (diff)
downloadlombok-0cfa9e99d99fc353d0c486e96cc53f5214ab031c.tar.gz
lombok-0cfa9e99d99fc353d0c486e96cc53f5214ab031c.tar.bz2
lombok-0cfa9e99d99fc353d0c486e96cc53f5214ab031c.zip
Added support for @NonNull in the @Setter annotation
Diffstat (limited to 'src/lombok/eclipse/handlers')
-rw-r--r--src/lombok/eclipse/handlers/HandleSetter.java27
-rw-r--r--src/lombok/eclipse/handlers/PKG.java15
2 files changed, 41 insertions, 1 deletions
diff --git a/src/lombok/eclipse/handlers/HandleSetter.java b/src/lombok/eclipse/handlers/HandleSetter.java
index adac30d7..567a7fbe 100644
--- a/src/lombok/eclipse/handlers/HandleSetter.java
+++ b/src/lombok/eclipse/handlers/HandleSetter.java
@@ -32,15 +32,24 @@ import lombok.eclipse.EclipseAnnotationHandler;
import lombok.eclipse.EclipseAST.Node;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
+import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.Argument;
import org.eclipse.jdt.internal.compiler.ast.Assignment;
+import org.eclipse.jdt.internal.compiler.ast.EqualExpression;
+import org.eclipse.jdt.internal.compiler.ast.Expression;
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
import org.eclipse.jdt.internal.compiler.ast.FieldReference;
+import org.eclipse.jdt.internal.compiler.ast.IfStatement;
import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
+import org.eclipse.jdt.internal.compiler.ast.OperatorIds;
+import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
import org.eclipse.jdt.internal.compiler.ast.Statement;
+import org.eclipse.jdt.internal.compiler.ast.StringLiteral;
import org.eclipse.jdt.internal.compiler.ast.ThisReference;
+import org.eclipse.jdt.internal.compiler.ast.ThrowStatement;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
@@ -119,6 +128,7 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> {
private MethodDeclaration generateSetter(TypeDeclaration parent, FieldDeclaration field, String name,
int modifier, ASTNode ast) {
+
long pos = (((long)ast.sourceStart) << 32) | ast.sourceEnd;
MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
method.modifiers = modifier;
@@ -137,7 +147,22 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> {
Assignment assignment = new Assignment(thisX, new SingleNameReference(field.name, pos), (int)pos);
method.bodyStart = method.declarationSourceStart = method.sourceStart = ast.sourceStart;
method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = ast.sourceEnd;
- method.statements = new Statement[] { assignment };
+
+ Annotation nonNull = findNonNullannotation(field);
+ if (nonNull == null) {
+ method.statements = new Statement[] { assignment };
+ }
+ else {
+ AllocationExpression exception = new AllocationExpression();
+ exception.type = new QualifiedTypeReference(Eclipse.fromQualifiedName("java.lang.NullPointerException"), new long[]{0, 0, 0});
+ exception.arguments = new Expression[] { new StringLiteral(field.name, 0, field.name.length - 1, 0)};
+ ThrowStatement throwStatement = new ThrowStatement(exception, 0, 0);
+
+ IfStatement nullCheck = new IfStatement(new EqualExpression(new SingleNameReference(field.name, 0),
+ new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL), throwStatement, 0, 0);
+
+ method.statements = new Statement[] { nullCheck, assignment };
+ }
return method;
}
}
diff --git a/src/lombok/eclipse/handlers/PKG.java b/src/lombok/eclipse/handlers/PKG.java
index 7fdf7afd..db38e3df 100644
--- a/src/lombok/eclipse/handlers/PKG.java
+++ b/src/lombok/eclipse/handlers/PKG.java
@@ -29,9 +29,11 @@ import lombok.eclipse.EclipseAST;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.TypeReference;
class PKG {
private PKG() {}
@@ -212,4 +214,17 @@ class PKG {
type.add(method, Kind.METHOD).recursiveSetHandled();
}
+
+ static Annotation findNonNullannotation(FieldDeclaration field) {
+ for (Annotation annotation : field.annotations) {
+ TypeReference typeRef = annotation.type;
+ if ( typeRef != null && typeRef.getTypeName()!= null ) {
+ char[][] typeName = typeRef.getTypeName();
+ if (new String(typeName[typeName.length - 1]).equals("NonNull")) {
+ return annotation;
+ }
+ }
+ }
+ return null;
+ }
}