aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok/javac/handlers
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/javac/handlers
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/javac/handlers')
-rw-r--r--src/core/lombok/javac/handlers/HandleNonNull.java32
-rw-r--r--src/core/lombok/javac/handlers/JavacHandlerUtil.java6
2 files changed, 36 insertions, 2 deletions
diff --git a/src/core/lombok/javac/handlers/HandleNonNull.java b/src/core/lombok/javac/handlers/HandleNonNull.java
index 49b987ce..079d5b04 100644
--- a/src/core/lombok/javac/handlers/HandleNonNull.java
+++ b/src/core/lombok/javac/handlers/HandleNonNull.java
@@ -31,13 +31,17 @@ import org.mangosdk.spi.ProviderFor;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCAssert;
+import com.sun.tools.javac.tree.JCTree.JCAssign;
import com.sun.tools.javac.tree.JCTree.JCBinary;
import com.sun.tools.javac.tree.JCTree.JCBlock;
import com.sun.tools.javac.tree.JCTree.JCExpression;
+import com.sun.tools.javac.tree.JCTree.JCExpressionStatement;
+import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.JCTree.JCIf;
import com.sun.tools.javac.tree.JCTree.JCLiteral;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
+import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
import com.sun.tools.javac.tree.JCTree.JCParens;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCSynchronized;
@@ -45,6 +49,7 @@ import com.sun.tools.javac.tree.JCTree.JCThrow;
import com.sun.tools.javac.tree.JCTree.JCTry;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;
+import com.sun.tools.javac.util.Name;
import lombok.ConfigurationKeys;
import lombok.NonNull;
@@ -167,8 +172,31 @@ public class HandleNonNull extends JavacAnnotationHandler<NonNull> {
* If it is not of this form, returns null.
*/
public String returnVarNameIfNullCheck(JCStatement stat) {
- boolean isIf = stat instanceof JCIf;
- if (!isIf && !(stat instanceof JCAssert)) return null;
+ boolean isIf = stat instanceof JCIf;
+ boolean isExpression = stat instanceof JCExpressionStatement;
+ if (!isIf && !(stat instanceof JCAssert) && !isExpression) return null;
+
+ if (isExpression) {
+ /* Check if the statements contains a call to checkNotNull or requireNonNull */
+ JCExpression expression = ((JCExpressionStatement) stat).expr;
+ if (expression instanceof JCAssign) expression = ((JCAssign) expression).rhs;
+ if (!(expression instanceof JCMethodInvocation)) return null;
+
+ JCMethodInvocation invocation = (JCMethodInvocation) expression;
+ JCExpression method = invocation.meth;
+ Name name = null;
+ if (method instanceof JCFieldAccess) {
+ name = ((JCFieldAccess) method).name;
+ } else if (method instanceof JCIdent) {
+ name = ((JCIdent) method).name;
+ }
+ if (name == null || (!name.contentEquals("checkNotNull") && !name.contentEquals("requireNonNull"))) return null;
+
+ if (invocation.args.isEmpty()) return null;
+ JCExpression firstArgument = invocation.args.head;
+ if (!(firstArgument instanceof JCIdent)) return null;
+ return ((JCIdent) firstArgument).toString();
+ }
if (isIf) {
/* Check that the if's statement is a throw statement, possibly in a block. */
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
index 76f3c1ad..5f0f39b0 100644
--- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java
+++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java
@@ -1529,6 +1529,12 @@ public class JavacHandlerUtil {
if (isPrimitive(typeNode)) return null;
JCLiteral message = maker.Literal(exceptionType.toExceptionMessage(varName.toString()));
+
+ LombokImmutableList<String> method = exceptionType.getMethod();
+ if (method != null) {
+ return maker.Exec(maker.Apply(List.<JCExpression>nil(), chainDots(source, method), List.of(maker.Ident(varName), message)));
+ }
+
if (exceptionType == NullCheckExceptionType.ASSERTION) {
return maker.Assert(maker.Binary(CTC_NOT_EQUAL, maker.Ident(varName), maker.Literal(CTC_BOT, null)), message);
}