Search in sources :

Example 1 with NullCheckExceptionType

use of lombok.core.configuration.NullCheckExceptionType in project lombok by rzwitserloot.

the class EclipseHandlerUtil method generateNullCheck.

/**
 * 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, EclipseNode sourceNode) {
    NullCheckExceptionType exceptionType = sourceNode.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE);
    if (exceptionType == null)
        exceptionType = NullCheckExceptionType.NULL_POINTER_EXCEPTION;
    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);
    int partCount = 1;
    String exceptionTypeStr = exceptionType.getExceptionType();
    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);
    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;
    setGeneratedBy(throwBlock, source);
    IfStatement ifStatement = new IfStatement(equalExpression, throwBlock, 0, 0);
    setGeneratedBy(ifStatement, source);
    return ifStatement;
}
Also used : EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) NullCheckExceptionType(lombok.core.configuration.NullCheckExceptionType) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) StringLiteral(org.eclipse.jdt.internal.compiler.ast.StringLiteral) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) Block(org.eclipse.jdt.internal.compiler.ast.Block) ThrowStatement(org.eclipse.jdt.internal.compiler.ast.ThrowStatement) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral)

Example 2 with NullCheckExceptionType

use of lombok.core.configuration.NullCheckExceptionType in project lombok by rzwitserloot.

the class JavacHandlerUtil method generateNullCheck.

/**
	 * 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 JCStatement generateNullCheck(JavacTreeMaker maker, JavacNode variable, JavacNode source) {
    NullCheckExceptionType exceptionType = source.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE);
    if (exceptionType == null)
        exceptionType = NullCheckExceptionType.NULL_POINTER_EXCEPTION;
    JCVariableDecl varDecl = (JCVariableDecl) variable.get();
    if (isPrimitive(varDecl.vartype))
        return null;
    Name fieldName = varDecl.name;
    JCExpression exType = genTypeRef(variable, exceptionType.getExceptionType());
    JCExpression exception = maker.NewClass(null, List.<JCExpression>nil(), exType, List.<JCExpression>of(maker.Literal(exceptionType.toExceptionMessage(fieldName.toString()))), 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);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) NullCheckExceptionType(lombok.core.configuration.NullCheckExceptionType) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name)

Example 3 with NullCheckExceptionType

use of lombok.core.configuration.NullCheckExceptionType in project lombok by rzwitserloot.

the class JavacHandlerUtil method generateNullCheck.

/**
 * 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, JavacNode variable, JCVariableDecl varDecl, 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;
    JCExpression exType = genTypeRef(variable, exceptionType.getExceptionType());
    JCExpression exception = maker.NewClass(null, List.<JCExpression>nil(), exType, List.<JCExpression>of(maker.Literal(exceptionType.toExceptionMessage(fieldName.toString()))), 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);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) NullCheckExceptionType(lombok.core.configuration.NullCheckExceptionType) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) Name(com.sun.tools.javac.util.Name)

Aggregations

NullCheckExceptionType (lombok.core.configuration.NullCheckExceptionType)3 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)2 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)2 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)2 Name (com.sun.tools.javac.util.Name)2 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)1 ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)1 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)1 ArrayQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference)1 Block (org.eclipse.jdt.internal.compiler.ast.Block)1 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)1 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)1 NullLiteral (org.eclipse.jdt.internal.compiler.ast.NullLiteral)1 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)1 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)1 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)1 StringLiteral (org.eclipse.jdt.internal.compiler.ast.StringLiteral)1 ThrowStatement (org.eclipse.jdt.internal.compiler.ast.ThrowStatement)1