diff options
author | Roel Spilker <r.spilker@gmail.com> | 2009-08-01 02:10:29 +0200 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2009-08-01 02:10:29 +0200 |
commit | c4d97b008afc85e495784929c312c6828aefde1d (patch) | |
tree | 920c80009a00c0a27e03c8f2aeaae459c28d7ad4 | |
parent | f2c837bb47771a7eba5ad5a885af162d8d133559 (diff) | |
download | lombok-c4d97b008afc85e495784929c312c6828aefde1d.tar.gz lombok-c4d97b008afc85e495784929c312c6828aefde1d.tar.bz2 lombok-c4d97b008afc85e495784929c312c6828aefde1d.zip |
Moved the check to see if a variable is null to the PKG utility classes
-rw-r--r-- | src/lombok/eclipse/handlers/HandleSetter.java | 32 | ||||
-rw-r--r-- | src/lombok/eclipse/handlers/PKG.java | 25 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleSetter.java | 15 | ||||
-rw-r--r-- | src/lombok/javac/handlers/PKG.java | 11 |
4 files changed, 53 insertions, 30 deletions
diff --git a/src/lombok/eclipse/handlers/HandleSetter.java b/src/lombok/eclipse/handlers/HandleSetter.java index f3e61e3e..d850e549 100644 --- a/src/lombok/eclipse/handlers/HandleSetter.java +++ b/src/lombok/eclipse/handlers/HandleSetter.java @@ -21,36 +21,33 @@ */ package lombok.eclipse.handlers; -import static lombok.eclipse.handlers.PKG.*; - +import static lombok.eclipse.Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; +import static lombok.eclipse.Eclipse.annotationTypeMatches; +import static lombok.eclipse.Eclipse.copyAnnotations; +import static lombok.eclipse.Eclipse.copyType; +import static lombok.eclipse.handlers.PKG.findNonNullAnnotations; +import static lombok.eclipse.handlers.PKG.generateNullCheck; +import static lombok.eclipse.handlers.PKG.injectMethod; +import static lombok.eclipse.handlers.PKG.methodExists; +import static lombok.eclipse.handlers.PKG.toModifier; import lombok.AccessLevel; import lombok.Setter; 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; 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; @@ -155,16 +152,7 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> { } else { param.annotations = copyAnnotations(nonNulls); - - AllocationExpression exception = new AllocationExpression(); - exception.type = new QualifiedTypeReference(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 }; + method.statements = new Statement[] { generateNullCheck(field), assignment }; } return method; } diff --git a/src/lombok/eclipse/handlers/PKG.java b/src/lombok/eclipse/handlers/PKG.java index 6d524e8c..2eae1edf 100644 --- a/src/lombok/eclipse/handlers/PKG.java +++ b/src/lombok/eclipse/handlers/PKG.java @@ -21,6 +21,8 @@ */ package lombok.eclipse.handlers; +import static lombok.eclipse.Eclipse.fromQualifiedName; + import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; @@ -31,9 +33,21 @@ 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.AbstractVariableDeclaration; +import org.eclipse.jdt.internal.compiler.ast.AllocationExpression; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration; +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.IfStatement; +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.ThrowStatement; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; import org.eclipse.jdt.internal.compiler.ast.TypeReference; @@ -231,4 +245,15 @@ class PKG { } return result.toArray(new Annotation[0]); } + + + static Statement generateNullCheck(AbstractVariableDeclaration variable) { + 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)}; + ThrowStatement throwStatement = new ThrowStatement(exception, 0, 0); + + return new IfStatement(new EqualExpression(new SingleNameReference(variable.name, 0), + new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL), throwStatement, 0, 0); + } } diff --git a/src/lombok/javac/handlers/HandleSetter.java b/src/lombok/javac/handlers/HandleSetter.java index 2f41e716..9fb995e4 100644 --- a/src/lombok/javac/handlers/HandleSetter.java +++ b/src/lombok/javac/handlers/HandleSetter.java @@ -21,7 +21,12 @@ */ package lombok.javac.handlers; -import static lombok.javac.handlers.PKG.*; +import static lombok.javac.handlers.PKG.findNonNullAnnotations; +import static lombok.javac.handlers.PKG.generateNullCheck; +import static lombok.javac.handlers.PKG.injectMethod; +import static lombok.javac.handlers.PKG.methodExists; +import static lombok.javac.handlers.PKG.toJavacModifier; +import static lombok.javac.handlers.PKG.toSetterName; import lombok.AccessLevel; import lombok.Setter; import lombok.core.AnnotationValues; @@ -34,8 +39,6 @@ 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; @@ -127,11 +130,7 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { 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)); + statements = List.<JCStatement>of(generateNullCheck(treeMaker, field), 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 ea794296..006e92a3 100644 --- a/src/lombok/javac/handlers/PKG.java +++ b/src/lombok/javac/handlers/PKG.java @@ -30,14 +30,17 @@ 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 com.sun.tools.javac.util.Name; /** * Container for static utility methods relevant to this package. @@ -269,4 +272,12 @@ class PKG { } 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); + } } |