Search in sources :

Example 6 with NullLiteral

use of org.eclipse.jdt.internal.compiler.ast.NullLiteral 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 7 with NullLiteral

use of org.eclipse.jdt.internal.compiler.ast.NullLiteral in project lombok by rzwitserloot.

the class HandleCleanup method handle.

public void handle(AnnotationValues<Cleanup> annotation, Annotation ast, EclipseNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.CLEANUP_FLAG_USAGE, "@Cleanup");
    String cleanupName = annotation.getInstance().value();
    if (cleanupName.length() == 0) {
        annotationNode.addError("cleanupName cannot be the empty string.");
        return;
    }
    if (annotationNode.up().getKind() != Kind.LOCAL) {
        annotationNode.addError("@Cleanup is legal only on local variable declarations.");
        return;
    }
    LocalDeclaration decl = (LocalDeclaration) annotationNode.up().get();
    if (decl.initialization == null) {
        annotationNode.addError("@Cleanup variable declarations need to be initialized.");
        return;
    }
    EclipseNode ancestor = annotationNode.up().directUp();
    ASTNode blockNode = ancestor.get();
    final boolean isSwitch;
    final Statement[] statements;
    if (blockNode instanceof AbstractMethodDeclaration) {
        isSwitch = false;
        statements = ((AbstractMethodDeclaration) blockNode).statements;
    } else if (blockNode instanceof Block) {
        isSwitch = false;
        statements = ((Block) blockNode).statements;
    } else if (blockNode instanceof SwitchStatement) {
        isSwitch = true;
        statements = ((SwitchStatement) blockNode).statements;
    } else {
        annotationNode.addError("@Cleanup is legal only on a local variable declaration inside a block.");
        return;
    }
    if (statements == null) {
        annotationNode.addError("LOMBOK BUG: Parent block does not contain any statements.");
        return;
    }
    int start = 0;
    for (; start < statements.length; start++) {
        if (statements[start] == decl)
            break;
    }
    if (start == statements.length) {
        annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent.");
        return;
    }
    // We start with try{} *AFTER* the var declaration.
    start++;
    int end;
    if (isSwitch) {
        end = start + 1;
        for (; end < statements.length; end++) {
            if (statements[end] instanceof CaseStatement) {
                break;
            }
        }
    } else
        end = statements.length;
    // At this point:
    // start-1 = Local Declaration marked with @Cleanup
    // start = first instruction that needs to be wrapped into a try block
    // end = last instruction of the scope -OR- last instruction before the next case label in switch statements.
    // hence:
    // [start, end) = statements for the try block.
    Statement[] tryBlock = new Statement[end - start];
    System.arraycopy(statements, start, tryBlock, 0, end - start);
    // Remove the stuff we just dumped into the tryBlock, and then leave room for the try node.
    // Remove room for every statement moved into try block...
    int newStatementsLength = statements.length - (end - start);
    // But add room for the TryStatement node itself.
    newStatementsLength += 1;
    Statement[] newStatements = new Statement[newStatementsLength];
    // copy all statements before the try block verbatim.
    System.arraycopy(statements, 0, newStatements, 0, start);
    // For switch statements.
    System.arraycopy(statements, end, newStatements, start + 1, statements.length - end);
    doAssignmentCheck(annotationNode, tryBlock, decl.name);
    TryStatement tryStatement = new TryStatement();
    setGeneratedBy(tryStatement, ast);
    tryStatement.tryBlock = new Block(0);
    tryStatement.tryBlock.statements = tryBlock;
    setGeneratedBy(tryStatement.tryBlock, ast);
    // Positions for in-method generated nodes are special
    int ss = decl.declarationSourceEnd + 1;
    int se = ss;
    if (tryBlock.length > 0) {
        // +1 for the closing semicolon. Yes, there could be spaces. Bummer.
        se = tryBlock[tryBlock.length - 1].sourceEnd + 1;
        tryStatement.sourceStart = ss;
        tryStatement.sourceEnd = se;
        tryStatement.tryBlock.sourceStart = ss;
        tryStatement.tryBlock.sourceEnd = se;
    }
    newStatements[start] = tryStatement;
    Statement[] finallyBlock = new Statement[1];
    MessageSend unsafeClose = new MessageSend();
    setGeneratedBy(unsafeClose, ast);
    unsafeClose.sourceStart = ast.sourceStart;
    unsafeClose.sourceEnd = ast.sourceEnd;
    SingleNameReference receiver = new SingleNameReference(decl.name, 0);
    setGeneratedBy(receiver, ast);
    unsafeClose.receiver = receiver;
    long nameSourcePosition = (long) ast.sourceStart << 32 | ast.sourceEnd;
    if (ast.memberValuePairs() != null)
        for (MemberValuePair pair : ast.memberValuePairs()) {
            if (pair.name != null && new String(pair.name).equals("value")) {
                nameSourcePosition = (long) pair.value.sourceStart << 32 | pair.value.sourceEnd;
                break;
            }
        }
    unsafeClose.nameSourcePosition = nameSourcePosition;
    unsafeClose.selector = cleanupName.toCharArray();
    int pS = ast.sourceStart, pE = ast.sourceEnd;
    long p = (long) pS << 32 | pE;
    SingleNameReference varName = new SingleNameReference(decl.name, p);
    setGeneratedBy(varName, ast);
    NullLiteral nullLiteral = new NullLiteral(pS, pE);
    setGeneratedBy(nullLiteral, ast);
    MessageSend preventNullAnalysis = preventNullAnalysis(ast, varName);
    EqualExpression equalExpression = new EqualExpression(preventNullAnalysis, nullLiteral, OperatorIds.NOT_EQUAL);
    equalExpression.sourceStart = pS;
    equalExpression.sourceEnd = pE;
    setGeneratedBy(equalExpression, ast);
    Block closeBlock = new Block(0);
    closeBlock.statements = new Statement[1];
    closeBlock.statements[0] = unsafeClose;
    setGeneratedBy(closeBlock, ast);
    IfStatement ifStatement = new IfStatement(equalExpression, closeBlock, 0, 0);
    setGeneratedBy(ifStatement, ast);
    finallyBlock[0] = ifStatement;
    tryStatement.finallyBlock = new Block(0);
    // Positions for in-method generated nodes are special
    if (!isSwitch) {
        tryStatement.finallyBlock.sourceStart = blockNode.sourceEnd;
        tryStatement.finallyBlock.sourceEnd = blockNode.sourceEnd;
    }
    setGeneratedBy(tryStatement.finallyBlock, ast);
    tryStatement.finallyBlock.statements = finallyBlock;
    tryStatement.catchArguments = null;
    tryStatement.catchBlocks = null;
    if (blockNode instanceof AbstractMethodDeclaration) {
        ((AbstractMethodDeclaration) blockNode).statements = newStatements;
    } else if (blockNode instanceof Block) {
        ((Block) blockNode).statements = newStatements;
    } else if (blockNode instanceof SwitchStatement) {
        ((SwitchStatement) blockNode).statements = newStatements;
    }
    ancestor.rebuild();
}
Also used : LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) TryStatement(org.eclipse.jdt.internal.compiler.ast.TryStatement) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) MemberValuePair(org.eclipse.jdt.internal.compiler.ast.MemberValuePair) TryStatement(org.eclipse.jdt.internal.compiler.ast.TryStatement) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) EclipseNode(lombok.eclipse.EclipseNode) Block(org.eclipse.jdt.internal.compiler.ast.Block) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral)

Example 8 with NullLiteral

use of org.eclipse.jdt.internal.compiler.ast.NullLiteral in project lombok by rzwitserloot.

the class EclipseJavaUtilListSetSingularizer method generateClearMethod.

private void generateClearMethod(boolean deprecate, TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType) {
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = ClassFileConstants.AccPublic;
    FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    FieldReference thisDotField2 = new FieldReference(data.getPluralName(), 0L);
    thisDotField2.receiver = new ThisReference(0, 0);
    md.selector = HandlerUtil.buildAccessorName("clear", new String(data.getPluralName())).toCharArray();
    MessageSend clearMsg = new MessageSend();
    clearMsg.receiver = thisDotField2;
    clearMsg.selector = "clear".toCharArray();
    Statement clearStatement = new IfStatement(new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.NOT_EQUAL), clearMsg, 0, 0);
    md.statements = returnStatement != null ? new Statement[] { clearStatement, returnStatement } : new Statement[] { clearStatement };
    md.returnType = returnType;
    md.annotations = deprecate ? new Annotation[] { generateDeprecatedAnnotation(data.getSource()) } : null;
    injectMethod(builderType, md);
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation)

Example 9 with NullLiteral

use of org.eclipse.jdt.internal.compiler.ast.NullLiteral in project lombok by rzwitserloot.

the class EclipseGuavaSingularizer method generateClearMethod.

void generateClearMethod(boolean deprecate, TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType) {
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = ClassFileConstants.AccPublic;
    FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    Assignment a = new Assignment(thisDotField, new NullLiteral(0, 0), 0);
    md.selector = HandlerUtil.buildAccessorName("clear", new String(data.getPluralName())).toCharArray();
    md.statements = returnStatement != null ? new Statement[] { a, returnStatement } : new Statement[] { a };
    md.returnType = returnType;
    md.annotations = deprecate ? new Annotation[] { generateDeprecatedAnnotation(data.getSource()) } : null;
    injectMethod(builderType, md);
}
Also used : Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation)

Example 10 with NullLiteral

use of org.eclipse.jdt.internal.compiler.ast.NullLiteral in project lombok by rzwitserloot.

the class HandleConstructor method getDefaultExpr.

private static Expression getDefaultExpr(TypeReference type, int s, int e) {
    boolean array = type instanceof ArrayTypeReference;
    if (array)
        return new NullLiteral(s, e);
    char[] lastToken = type.getLastToken();
    if (Arrays.equals(TypeConstants.BOOLEAN, lastToken))
        return new FalseLiteral(s, e);
    if (Arrays.equals(TypeConstants.CHAR, lastToken))
        return new CharLiteral(new char[] { '\'', '\\', '0', '\'' }, s, e);
    if (Arrays.equals(TypeConstants.BYTE, lastToken) || Arrays.equals(TypeConstants.SHORT, lastToken) || Arrays.equals(TypeConstants.INT, lastToken))
        return IntLiteral.buildIntLiteral(new char[] { '0' }, s, e);
    if (Arrays.equals(TypeConstants.LONG, lastToken))
        return LongLiteral.buildLongLiteral(new char[] { '0', 'L' }, s, e);
    if (Arrays.equals(TypeConstants.FLOAT, lastToken))
        return new FloatLiteral(new char[] { '0', 'F' }, s, e);
    if (Arrays.equals(TypeConstants.DOUBLE, lastToken))
        return new DoubleLiteral(new char[] { '0', 'D' }, s, e);
    return new NullLiteral(s, e);
}
Also used : CharLiteral(org.eclipse.jdt.internal.compiler.ast.CharLiteral) FloatLiteral(org.eclipse.jdt.internal.compiler.ast.FloatLiteral) DoubleLiteral(org.eclipse.jdt.internal.compiler.ast.DoubleLiteral) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) FalseLiteral(org.eclipse.jdt.internal.compiler.ast.FalseLiteral)

Aggregations

NullLiteral (org.eclipse.jdt.internal.compiler.ast.NullLiteral)17 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)14 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)13 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)12 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)11 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)11 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)10 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)9 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)8 ConditionalExpression (org.eclipse.jdt.internal.compiler.ast.ConditionalExpression)7 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)7 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)7 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)7 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)6 Block (org.eclipse.jdt.internal.compiler.ast.Block)6 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)6 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)6 BinaryExpression (org.eclipse.jdt.internal.compiler.ast.BinaryExpression)5 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)4 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)4