diff options
Diffstat (limited to 'src/core/lombok/eclipse')
-rw-r--r-- | src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java | 37 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleNonNull.java | 37 |
2 files changed, 47 insertions, 27 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 78b11873..010dc9d8 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -66,6 +66,7 @@ import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer; import org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference; import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference; +import org.eclipse.jdt.internal.compiler.ast.AssertStatement; import org.eclipse.jdt.internal.compiler.ast.Block; import org.eclipse.jdt.internal.compiler.ast.CastExpression; import org.eclipse.jdt.internal.compiler.ast.CharLiteral; @@ -1786,27 +1787,39 @@ public class EclipseHandlerUtil { if (isPrimitive(variable.type)) return null; AllocationExpression exception = new AllocationExpression(); setGeneratedBy(exception, source); - int partCount = 1; + + SingleNameReference varName = new SingleNameReference(variable.name, p); + setGeneratedBy(varName, source); + NullLiteral nullLiteral = new NullLiteral(pS, pE); + setGeneratedBy(nullLiteral, source); + + int equalOperator = exceptionType == NullCheckExceptionType.ASSERTION ? OperatorIds.NOT_EQUAL : OperatorIds.EQUAL_EQUAL; + EqualExpression equalExpression = new EqualExpression(varName, nullLiteral, equalOperator); + equalExpression.sourceStart = pS; equalExpression.statementEnd = equalExpression.sourceEnd = pE; + setGeneratedBy(equalExpression, source); + + StringLiteral message = new StringLiteral(exceptionType.toExceptionMessage(new String(variable.name)).toCharArray(), pS, pE, 0); + setGeneratedBy(message, source); + + if (exceptionType == NullCheckExceptionType.ASSERTION) { + Statement assertStatement = new AssertStatement(message, equalExpression, pS); + setGeneratedBy(assertStatement, source); + return assertStatement; + } + String exceptionTypeStr = exceptionType.getExceptionType(); + int partCount = 1; for (int i = 0; i < exceptionTypeStr.length(); i++) if (exceptionTypeStr.charAt(i) == '.') partCount++; long[] ps = new long[partCount]; Arrays.fill(ps, 0L); exception.type = new QualifiedTypeReference(fromQualifiedName(exceptionTypeStr), ps); setGeneratedBy(exception.type, source); - exception.arguments = new Expression[] { - new StringLiteral(exceptionType.toExceptionMessage(new String(variable.name)).toCharArray(), pS, pE, 0) - }; - setGeneratedBy(exception.arguments[0], source); + exception.arguments = new Expression[] {message}; + ThrowStatement throwStatement = new ThrowStatement(exception, pS, pE); setGeneratedBy(throwStatement, source); - SingleNameReference varName = new SingleNameReference(variable.name, p); - setGeneratedBy(varName, source); - NullLiteral nullLiteral = new NullLiteral(pS, pE); - setGeneratedBy(nullLiteral, source); - EqualExpression equalExpression = new EqualExpression(varName, nullLiteral, OperatorIds.EQUAL_EQUAL); - equalExpression.sourceStart = pS; equalExpression.statementEnd = equalExpression.sourceEnd = pE; - setGeneratedBy(equalExpression, source); + Block throwBlock = new Block(0); throwBlock.statements = new Statement[] {throwStatement}; throwBlock.sourceStart = pS; throwBlock.sourceEnd = pE; diff --git a/src/core/lombok/eclipse/handlers/HandleNonNull.java b/src/core/lombok/eclipse/handlers/HandleNonNull.java index 1672618d..77c77e1e 100644 --- a/src/core/lombok/eclipse/handlers/HandleNonNull.java +++ b/src/core/lombok/eclipse/handlers/HandleNonNull.java @@ -21,27 +21,18 @@ */ package lombok.eclipse.handlers; -import static lombok.core.handlers.HandlerUtil.*; +import static lombok.core.handlers.HandlerUtil.handleFlagUsage; import static lombok.eclipse.Eclipse.isPrimitive; import static lombok.eclipse.handlers.EclipseHandlerUtil.*; import java.util.Arrays; -import lombok.ConfigurationKeys; -import lombok.NonNull; -import lombok.core.AST.Kind; -import lombok.core.AnnotationValues; -import lombok.core.HandlerPriority; -import lombok.eclipse.DeferUntilPostDiet; -import lombok.eclipse.EclipseAST; -import lombok.eclipse.EclipseAnnotationHandler; -import lombok.eclipse.EclipseNode; - 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.Annotation; import org.eclipse.jdt.internal.compiler.ast.Argument; +import org.eclipse.jdt.internal.compiler.ast.AssertStatement; import org.eclipse.jdt.internal.compiler.ast.Block; import org.eclipse.jdt.internal.compiler.ast.EqualExpression; import org.eclipse.jdt.internal.compiler.ast.Expression; @@ -56,6 +47,16 @@ import org.eclipse.jdt.internal.compiler.ast.TryStatement; import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.mangosdk.spi.ProviderFor; +import lombok.ConfigurationKeys; +import lombok.NonNull; +import lombok.core.AST.Kind; +import lombok.core.AnnotationValues; +import lombok.core.HandlerPriority; +import lombok.eclipse.DeferUntilPostDiet; +import lombok.eclipse.EclipseAST; +import lombok.eclipse.EclipseAnnotationHandler; +import lombok.eclipse.EclipseNode; + @DeferUntilPostDiet @ProviderFor(EclipseAnnotationHandler.class) @HandlerPriority(value = 512) // 2^9; onParameter=@__(@NonNull) has to run first. @@ -191,9 +192,11 @@ public class HandleNonNull extends EclipseAnnotationHandler<NonNull> { } public char[] returnVarNameIfNullCheck(Statement stat) { - if (!(stat instanceof IfStatement)) return null; + boolean isIf = stat instanceof IfStatement; + if (!isIf && !(stat instanceof AssertStatement)) return null; - /* Check that the if's statement is a throw statement, possibly in a block. */ { + if (isIf) { + /* Check that the if's statement is a throw statement, possibly in a block. */ Statement then = ((IfStatement) stat).thenStatement; if (then instanceof Block) { Statement[] blockStatements = ((Block) then).statements; @@ -206,11 +209,15 @@ public class HandleNonNull extends EclipseAnnotationHandler<NonNull> { /* Check that the if's conditional is like 'x == null'. Return from this method (don't generate a nullcheck) if 'x' is equal to our own variable's name: There's already a nullcheck here. */ { - Expression cond = ((IfStatement) stat).condition; + Expression cond = isIf ? ((IfStatement) stat).condition : ((AssertStatement) stat).assertExpression; if (!(cond instanceof EqualExpression)) return null; EqualExpression bin = (EqualExpression) cond; int operatorId = ((bin.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT); - if (operatorId != OperatorIds.EQUAL_EQUAL) return null; + if (isIf) { + if (operatorId != OperatorIds.EQUAL_EQUAL) return null; + } else { + if (operatorId != OperatorIds.NOT_EQUAL) return null; + } if (!(bin.left instanceof SingleNameReference)) return null; if (!(bin.right instanceof NullLiteral)) return null; return ((SingleNameReference) bin.left).token; |