Search in sources :

Example 1 with ThrowStatement

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

the class HandleSneakyThrows method buildTryCatchBlock.

public Statement buildTryCatchBlock(Statement[] contents, DeclaredException exception, ASTNode source, AbstractMethodDeclaration method) {
    int methodStart = method.bodyStart;
    int methodEnd = method.bodyEnd;
    long methodPosEnd = ((long) methodEnd) << 32 | (methodEnd & 0xFFFFFFFFL);
    TryStatement tryStatement = new TryStatement();
    setGeneratedBy(tryStatement, source);
    tryStatement.tryBlock = new Block(0);
    // Positions for in-method generated nodes are special
    tryStatement.tryBlock.sourceStart = methodStart;
    tryStatement.tryBlock.sourceEnd = methodEnd;
    setGeneratedBy(tryStatement.tryBlock, source);
    tryStatement.tryBlock.statements = contents;
    TypeReference typeReference;
    if (exception.exceptionName.indexOf('.') == -1) {
        typeReference = new SingleTypeReference(exception.exceptionName.toCharArray(), methodPosEnd);
        typeReference.statementEnd = methodEnd;
    } else {
        String[] x = exception.exceptionName.split("\\.");
        char[][] elems = new char[x.length][];
        long[] poss = new long[x.length];
        Arrays.fill(poss, methodPosEnd);
        for (int i = 0; i < x.length; i++) {
            elems[i] = x[i].trim().toCharArray();
        }
        typeReference = new QualifiedTypeReference(elems, poss);
    }
    setGeneratedBy(typeReference, source);
    Argument catchArg = new Argument("$ex".toCharArray(), methodPosEnd, typeReference, Modifier.FINAL);
    setGeneratedBy(catchArg, source);
    catchArg.declarationSourceEnd = catchArg.declarationEnd = catchArg.sourceEnd = methodEnd;
    catchArg.declarationSourceStart = catchArg.modifiersSourceStart = catchArg.sourceStart = methodEnd;
    tryStatement.catchArguments = new Argument[] { catchArg };
    MessageSend sneakyThrowStatement = new MessageSend();
    setGeneratedBy(sneakyThrowStatement, source);
    sneakyThrowStatement.receiver = new QualifiedNameReference(new char[][] { "lombok".toCharArray(), "Lombok".toCharArray() }, new long[2], methodEnd, methodEnd);
    setGeneratedBy(sneakyThrowStatement.receiver, source);
    sneakyThrowStatement.receiver.statementEnd = methodEnd;
    sneakyThrowStatement.selector = "sneakyThrow".toCharArray();
    SingleNameReference exRef = new SingleNameReference("$ex".toCharArray(), methodPosEnd);
    setGeneratedBy(exRef, source);
    exRef.statementEnd = methodEnd;
    sneakyThrowStatement.arguments = new Expression[] { exRef };
    // This is the magic fix for rendering issues
    // In org.eclipse.jdt.core.dom.ASTConverter#convert(org.eclipse.jdt.internal.compiler.ast.MessageSend)
    // a new SimpleName is created and the setSourceRange should receive -1, 0. That's why we provide -2L :-)
    sneakyThrowStatement.nameSourcePosition = -2L;
    sneakyThrowStatement.sourceStart = methodEnd;
    sneakyThrowStatement.sourceEnd = sneakyThrowStatement.statementEnd = methodEnd;
    Statement rethrowStatement = new ThrowStatement(sneakyThrowStatement, methodEnd, methodEnd);
    setGeneratedBy(rethrowStatement, source);
    Block block = new Block(0);
    block.sourceStart = methodEnd;
    block.sourceEnd = methodEnd;
    setGeneratedBy(block, source);
    block.statements = new Statement[] { rethrowStatement };
    tryStatement.catchBlocks = new Block[] { block };
    // Positions for in-method generated nodes are special
    tryStatement.sourceStart = method.bodyStart;
    tryStatement.sourceEnd = method.bodyEnd;
    return tryStatement;
}
Also used : Argument(org.eclipse.jdt.internal.compiler.ast.Argument) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) ThrowStatement(org.eclipse.jdt.internal.compiler.ast.ThrowStatement) TryStatement(org.eclipse.jdt.internal.compiler.ast.TryStatement) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) TryStatement(org.eclipse.jdt.internal.compiler.ast.TryStatement) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) Block(org.eclipse.jdt.internal.compiler.ast.Block) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ThrowStatement(org.eclipse.jdt.internal.compiler.ast.ThrowStatement) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)

Example 2 with ThrowStatement

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

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

the class HandleUtilityClass method createPrivateDefaultConstructor.

private void createPrivateDefaultConstructor(EclipseNode typeNode, EclipseNode sourceNode) {
    ASTNode source = sourceNode.get();
    TypeDeclaration typeDeclaration = ((TypeDeclaration) typeNode.get());
    long p = (long) source.sourceStart << 32 | source.sourceEnd;
    ConstructorDeclaration constructor = new ConstructorDeclaration(((CompilationUnitDeclaration) typeNode.top().get()).compilationResult);
    constructor.modifiers = ClassFileConstants.AccPrivate;
    constructor.selector = typeDeclaration.name;
    constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper);
    constructor.constructorCall.sourceStart = source.sourceStart;
    constructor.constructorCall.sourceEnd = source.sourceEnd;
    constructor.thrownExceptions = null;
    constructor.typeParameters = null;
    constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart;
    constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd;
    constructor.arguments = null;
    AllocationExpression exception = new AllocationExpression();
    setGeneratedBy(exception, source);
    long[] ps = new long[JAVA_LANG_UNSUPPORTED_OPERATION_EXCEPTION.length];
    Arrays.fill(ps, p);
    exception.type = new QualifiedTypeReference(JAVA_LANG_UNSUPPORTED_OPERATION_EXCEPTION, ps);
    setGeneratedBy(exception.type, source);
    exception.arguments = new Expression[] { new StringLiteral(UNSUPPORTED_MESSAGE, source.sourceStart, source.sourceEnd, 0) };
    setGeneratedBy(exception.arguments[0], source);
    ThrowStatement throwStatement = new ThrowStatement(exception, source.sourceStart, source.sourceEnd);
    setGeneratedBy(throwStatement, source);
    constructor.statements = new Statement[] { throwStatement };
    injectMethod(typeNode, constructor);
}
Also used : ExplicitConstructorCall(org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall) StringLiteral(org.eclipse.jdt.internal.compiler.ast.StringLiteral) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) ConstructorDeclaration(org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) ThrowStatement(org.eclipse.jdt.internal.compiler.ast.ThrowStatement) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Aggregations

QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)3 ThrowStatement (org.eclipse.jdt.internal.compiler.ast.ThrowStatement)3 ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)2 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)2 Block (org.eclipse.jdt.internal.compiler.ast.Block)2 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)2 StringLiteral (org.eclipse.jdt.internal.compiler.ast.StringLiteral)2 NullCheckExceptionType (lombok.core.configuration.NullCheckExceptionType)1 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)1 ArrayQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference)1 ConstructorDeclaration (org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration)1 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)1 ExplicitConstructorCall (org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall)1 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)1 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)1 NullLiteral (org.eclipse.jdt.internal.compiler.ast.NullLiteral)1 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)1 QualifiedNameReference (org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)1 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)1 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)1