diff options
Diffstat (limited to 'src/core/lombok/javac/handlers/JavacHandlerUtil.java')
-rw-r--r-- | src/core/lombok/javac/handlers/JavacHandlerUtil.java | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 6fa70ff3..5f0f39b0 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -1516,34 +1516,46 @@ public class JavacHandlerUtil { * variable name as message. */ public static JCStatement generateNullCheck(JavacTreeMaker maker, JavacNode variable, JavacNode source) { - return generateNullCheck(maker, variable, (JCVariableDecl) variable.get(), source); + return generateNullCheck(maker, (JCVariableDecl) variable.get(), source); } - + /** - * Generates a new statement that checks if the given variable is null, and if so, throws a configured exception with the - * variable name as message. - * - * This is a special case method reserved for use when the provided declaration differs from the - * variable's declaration, i.e. in a constructor or setter where the local parameter is named the same but with the prefix - * stripped as a result of @Accessors.prefix. + * Generates a new statement that checks if the given local is null, and if so, throws a configured exception with the + * local variable name as message. */ - public static JCStatement generateNullCheck(JavacTreeMaker maker, JavacNode variable, JCVariableDecl varDecl, JavacNode source) { + public static JCStatement generateNullCheck(JavacTreeMaker maker, JCExpression typeNode, Name varName, JavacNode source) { NullCheckExceptionType exceptionType = source.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE); if (exceptionType == null) exceptionType = NullCheckExceptionType.NULL_POINTER_EXCEPTION; - if (isPrimitive(varDecl.vartype)) return null; - Name fieldName = varDecl.name; + 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))); + } - JCLiteral message = maker.Literal(exceptionType.toExceptionMessage(fieldName.toString())); if (exceptionType == NullCheckExceptionType.ASSERTION) { - return maker.Assert(maker.Binary(CTC_NOT_EQUAL, maker.Ident(fieldName), maker.Literal(CTC_BOT, null)), message); + return maker.Assert(maker.Binary(CTC_NOT_EQUAL, maker.Ident(varName), maker.Literal(CTC_BOT, null)), message); } - JCExpression exType = genTypeRef(variable, exceptionType.getExceptionType()); + JCExpression exType = genTypeRef(source, exceptionType.getExceptionType()); JCExpression exception = maker.NewClass(null, List.<JCExpression>nil(), exType, List.<JCExpression>of(message), null); JCStatement throwStatement = maker.Throw(exception); JCBlock throwBlock = maker.Block(0, List.of(throwStatement)); - return maker.If(maker.Binary(CTC_EQUAL, maker.Ident(fieldName), maker.Literal(CTC_BOT, null)), throwBlock, null); + return maker.If(maker.Binary(CTC_EQUAL, maker.Ident(varName), maker.Literal(CTC_BOT, null)), throwBlock, null); + } + + /** + * Generates a new statement that checks if the given variable is null, and if so, throws a configured exception with the + * variable name as message. + * + * This is a special case method reserved for use when the provided declaration differs from the + * variable's declaration, i.e. in a constructor or setter where the local parameter is named the same but with the prefix + * stripped as a result of @Accessors.prefix. + */ + public static JCStatement generateNullCheck(JavacTreeMaker maker, JCVariableDecl varDecl, JavacNode source) { + return generateNullCheck(maker, varDecl.vartype, varDecl.name, source); } /** |