aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok/eclipse
diff options
context:
space:
mode:
authorRoel Spilker <r.spilker@gmail.com>2019-07-16 02:04:11 +0200
committerRoel Spilker <r.spilker@gmail.com>2019-07-16 02:04:11 +0200
commitaa80e1baf92f3327383b36466a771e92d8a91b05 (patch)
tree312ec2e35698608fbfe877e208ca4c8428b1593c /src/core/lombok/eclipse
parente1d1415d6e147edacaa50d8954737a168fbdafb5 (diff)
downloadlombok-aa80e1baf92f3327383b36466a771e92d8a91b05.tar.gz
lombok-aa80e1baf92f3327383b36466a771e92d8a91b05.tar.bz2
lombok-aa80e1baf92f3327383b36466a771e92d8a91b05.zip
Fixes #1197, add Objects.requireNonNull and Preconditions.checkkNotNull to supported null-check styles
Diffstat (limited to 'src/core/lombok/eclipse')
-rw-r--r--src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java79
-rw-r--r--src/core/lombok/eclipse/handlers/HandleNonNull.java22
2 files changed, 70 insertions, 31 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
index 37976ae3..11a2b9bd 100644
--- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
+++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
@@ -38,26 +38,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import lombok.AccessLevel;
-import lombok.ConfigurationKeys;
-import lombok.Data;
-import lombok.Getter;
-import lombok.Lombok;
-import lombok.core.AST.Kind;
-import lombok.core.AnnotationValues;
-import lombok.core.AnnotationValues.AnnotationValue;
-import lombok.core.TypeResolver;
-import lombok.core.configuration.NullCheckExceptionType;
-import lombok.core.configuration.TypeName;
-import lombok.core.debug.ProblemReporter;
-import lombok.core.handlers.HandlerUtil;
-import lombok.eclipse.Eclipse;
-import lombok.eclipse.EclipseAST;
-import lombok.eclipse.EclipseNode;
-import lombok.experimental.Accessors;
-import lombok.experimental.Tolerate;
-import lombok.permit.Permit;
-
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration;
@@ -121,6 +101,28 @@ import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
import org.eclipse.jdt.internal.compiler.lookup.WildcardBinding;
+import lombok.AccessLevel;
+import lombok.ConfigurationKeys;
+import lombok.Data;
+import lombok.Getter;
+import lombok.Lombok;
+import lombok.core.AST.Kind;
+import lombok.core.AnnotationValues;
+import lombok.core.AnnotationValues.AnnotationValue;
+import lombok.core.LombokImmutableList;
+import lombok.core.TypeResolver;
+import lombok.core.configuration.NullCheckExceptionType;
+import lombok.core.configuration.TypeName;
+import lombok.core.debug.ProblemReporter;
+import lombok.core.handlers.HandlerUtil;
+import lombok.core.handlers.HandlerUtil.FieldAccess;
+import lombok.eclipse.Eclipse;
+import lombok.eclipse.EclipseAST;
+import lombok.eclipse.EclipseNode;
+import lombok.experimental.Accessors;
+import lombok.experimental.Tolerate;
+import lombok.permit.Permit;
+
/**
* Container for static utility methods useful to handlers written for eclipse.
*/
@@ -1818,8 +1820,6 @@ public class EclipseHandlerUtil {
/**
* Generates a new statement that checks if the given local variable is null, and if so, throws a specified exception with the
* variable name as message.
- *
- * @param exName The name of the exception to throw; normally {@code java.lang.NullPointerException}.
*/
public static Statement generateNullCheck(TypeReference type, char[] variable, EclipseNode sourceNode) {
NullCheckExceptionType exceptionType = sourceNode.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE);
@@ -1828,24 +1828,44 @@ public class EclipseHandlerUtil {
ASTNode source = sourceNode.get();
int pS = source.sourceStart, pE = source.sourceEnd;
- long p = (long)pS << 32 | pE;
+ long p = (long) pS << 32 | pE;
if (isPrimitive(type)) return null;
+ SingleNameReference varName = new SingleNameReference(variable, p);
+ setGeneratedBy(varName, source);
+
+ StringLiteral message = new StringLiteral(exceptionType.toExceptionMessage(new String(variable)).toCharArray(), pS, pE, 0);
+ setGeneratedBy(message, source);
+
+ LombokImmutableList<String> method = exceptionType.getMethod();
+ if (method != null) {
+
+ MessageSend invocation = new MessageSend();
+ invocation.sourceStart = pS; invocation.sourceEnd = pE;
+ setGeneratedBy(invocation, source);
+
+ char[][] utilityTypeName = new char[method.size() - 1][];
+ for (int i = 0; i < method.size() - 1; i++) {
+ utilityTypeName[i] = method.get(i).toCharArray();
+ }
+
+ invocation.receiver = new QualifiedNameReference(utilityTypeName, new long[method.size()], pS, pE);
+ setGeneratedBy(invocation.receiver, source);
+ invocation.selector = method.get(method.size() - 1).toCharArray();
+ invocation.arguments = new Expression[] {varName, message};
+ return invocation;
+ }
+
AllocationExpression exception = new AllocationExpression();
setGeneratedBy(exception, source);
- SingleNameReference varName = new SingleNameReference(variable, 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)).toCharArray(), pS, pE, 0);
- setGeneratedBy(message, source);
if (exceptionType == NullCheckExceptionType.ASSERTION) {
Statement assertStatement = new AssertStatement(message, equalExpression, pS);
@@ -1865,7 +1885,6 @@ public class EclipseHandlerUtil {
ThrowStatement throwStatement = new ThrowStatement(exception, pS, pE);
setGeneratedBy(throwStatement, 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 77c77e1e..c61ce02d 100644
--- a/src/core/lombok/eclipse/handlers/HandleNonNull.java
+++ b/src/core/lombok/eclipse/handlers/HandleNonNull.java
@@ -33,10 +33,12 @@ 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.Assignment;
import org.eclipse.jdt.internal.compiler.ast.Block;
import org.eclipse.jdt.internal.compiler.ast.EqualExpression;
import org.eclipse.jdt.internal.compiler.ast.Expression;
import org.eclipse.jdt.internal.compiler.ast.IfStatement;
+import org.eclipse.jdt.internal.compiler.ast.MessageSend;
import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
import org.eclipse.jdt.internal.compiler.ast.OperatorIds;
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
@@ -61,6 +63,9 @@ import lombok.eclipse.EclipseNode;
@ProviderFor(EclipseAnnotationHandler.class)
@HandlerPriority(value = 512) // 2^9; onParameter=@__(@NonNull) has to run first.
public class HandleNonNull extends EclipseAnnotationHandler<NonNull> {
+ private static final char[] REQUIRE_NON_NULL = "requireNonNull".toCharArray();
+ private static final char[] CHECK_NOT_NULL = "checkNotNull".toCharArray();
+
public static final HandleNonNull INSTANCE = new HandleNonNull();
public void fix(EclipseNode method) {
@@ -193,7 +198,22 @@ public class HandleNonNull extends EclipseAnnotationHandler<NonNull> {
public char[] returnVarNameIfNullCheck(Statement stat) {
boolean isIf = stat instanceof IfStatement;
- if (!isIf && !(stat instanceof AssertStatement)) return null;
+ boolean isExpression = stat instanceof Expression;
+ if (!isIf && !(stat instanceof AssertStatement) && !isExpression) return null;
+
+ if (isExpression) {
+ /* Check if the statements contains a call to checkNotNull or requireNonNull */
+ Expression expression = (Expression) stat;
+ if (expression instanceof Assignment) expression = ((Assignment) expression).expression;
+ if (!(expression instanceof MessageSend)) return null;
+
+ MessageSend invocation = (MessageSend) expression;
+ if (!Arrays.equals(invocation.selector, CHECK_NOT_NULL) && !Arrays.equals(invocation.selector, REQUIRE_NON_NULL)) return null;
+ if (invocation.arguments == null || invocation.arguments.length == 0) return null;
+ Expression firstArgument = invocation.arguments[0];
+ if (!(firstArgument instanceof SingleNameReference)) return null;
+ return ((SingleNameReference) firstArgument).token;
+ }
if (isIf) {
/* Check that the if's statement is a throw statement, possibly in a block. */