From 4b878f9ba996f852ce555c3024512ae34e34774e Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 23 May 2014 04:07:39 +0200 Subject: Added confkey to make @NonNull generate a different exception because of the IllegalArgumentException vs. NullPointerException that we really don’t want to get into. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eclipse/handlers/EclipseHandlerUtil.java | 24 +++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java') diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index a93f321b..022cad91 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -1396,17 +1396,35 @@ public class EclipseHandlerUtil { } /** - * Generates a new statement that checks if the given variable is null, and if so, throws a {@code NullPointerException} with the + * Generates a new statement that checks if the given 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(AbstractVariableDeclaration variable, ASTNode source) { + public static Statement generateNullCheck(AbstractVariableDeclaration variable, EclipseNode sourceNode) { + String exceptionType = sourceNode.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE); + if (exceptionType == null) { + exceptionType = HandlerUtil.DEFAULT_EXCEPTION_FOR_NON_NULL; + } else { + if (!HandlerUtil.isLegalBasicClassReference(exceptionType)) { + sourceNode.addWarning("Configuration key contains invalid java type reference '" + exceptionType + "'; use something like 'java.lang.NullPointerException' as value for this key."); + exceptionType = HandlerUtil.DEFAULT_EXCEPTION_FOR_NON_NULL; + } + } + + ASTNode source = sourceNode.get(); + int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; if (isPrimitive(variable.type)) return null; AllocationExpression exception = new AllocationExpression(); setGeneratedBy(exception, source); - exception.type = new QualifiedTypeReference(fromQualifiedName("java.lang.NullPointerException"), new long[]{p, p, p}); + int partCount = 0; + for (int i = 0; i < exceptionType.length(); i++) if (exceptionType.charAt(i) == '.') partCount++; + long[] ps = new long[partCount]; + Arrays.fill(ps, 0L); + exception.type = new QualifiedTypeReference(fromQualifiedName(exceptionType), ps); setGeneratedBy(exception.type, source); exception.arguments = new Expression[] { new StringLiteral(variable.name, pS, pE, 0)}; setGeneratedBy(exception.arguments[0], source); -- cgit