Example usage for org.eclipse.jdt.internal.compiler.ast ThrowStatement ThrowStatement

List of usage examples for org.eclipse.jdt.internal.compiler.ast ThrowStatement ThrowStatement

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast ThrowStatement ThrowStatement.

Prototype

public ThrowStatement(Expression exception, int sourceStart, int sourceEnd) 

Source Link

Usage

From source file:lombok.eclipse.handlers.ast.EclipseASTMaker.java

License:Open Source License

@Override
public ASTNode visitThrow(final lombok.ast.Throw node, final Void p) {
    final ThrowStatement throwStatement = new ThrowStatement(build(node.getExpression(), Expression.class), 0,
            0);/*from   w  w w. j a  v a 2s  .co m*/
    setGeneratedByAndCopyPos(throwStatement, source, posHintOf(node));
    return throwStatement;
}

From source file:lombok.eclipse.handlers.EclipseHandlerUtil.java

License:Open Source License

/**
 * 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./*from w ww .  jav a  2 s  . co  m*/
 * 
 * @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;
}

From source file:lombok.eclipse.handlers.HandleSneakyThrows.java

License:Open Source License

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;// ww w  .  j  a  va  2 s  .co  m
    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;
}

From source file:lombok.eclipse.handlers.HandleUtilityClass.java

License:Open Source License

private void createPrivateDefaultConstructor(EclipseNode typeNode, EclipseNode sourceNode) {
    ASTNode source = sourceNode.get();/*from  w ww .ja v  a 2  s .  co m*/

    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);
}

From source file:lombok.eclipse.handlers.PKG.java

License:Open Source License

static Statement generateNullCheck(AbstractVariableDeclaration variable, ASTNode source) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;

    if (isPrimitive(variable.type))
        return null;
    AllocationExpression exception = new AllocationExpression();
    Eclipse.setGeneratedBy(exception, source);
    exception.type = new QualifiedTypeReference(fromQualifiedName("java.lang.NullPointerException"),
            new long[] { p, p, p });
    Eclipse.setGeneratedBy(exception.type, source);
    exception.arguments = new Expression[] { new StringLiteral(variable.name, pS, pE, 0) };
    Eclipse.setGeneratedBy(exception.arguments[0], source);
    ThrowStatement throwStatement = new ThrowStatement(exception, pS, pE);
    Eclipse.setGeneratedBy(throwStatement, source);

    SingleNameReference varName = new SingleNameReference(variable.name, p);
    Eclipse.setGeneratedBy(varName, source);
    NullLiteral nullLiteral = new NullLiteral(pS, pE);
    Eclipse.setGeneratedBy(nullLiteral, source);
    EqualExpression equalExpression = new EqualExpression(varName, nullLiteral, OperatorIds.EQUAL_EQUAL);
    equalExpression.sourceStart = pS;//from w  w w.ja  v  a 2 s  . c o  m
    equalExpression.sourceEnd = pE;
    Eclipse.setGeneratedBy(equalExpression, source);
    IfStatement ifStatement = new IfStatement(equalExpression, throwStatement, 0, 0);
    Eclipse.setGeneratedBy(ifStatement, source);
    return ifStatement;
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void consumeStatementThrow() {
    // ThrowStatement ::= 'throw' Expression ';'
    this.expressionLengthPtr--;
    pushOnAstStack(new ThrowStatement(this.expressionStack[this.expressionPtr--], this.intStack[this.intPtr--],
            this.endStatementPosition));
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.AstGenerator.java

License:Open Source License

public ThrowStatement throwStatement(Expression exception) {
    return new ThrowStatement(exception, this.sourceStart, this.sourceEnd);
}

From source file:org.nabucco.framework.mda.template.java.extract.statement.JavaAstStatementExtractorVisitor.java

License:Open Source License

@Override
public boolean visit(ThrowStatement throwStatement, BlockScope scope) {

    Expression exception = copy(throwStatement.exception, scope);

    ThrowStatement throwCopy = new ThrowStatement(exception, throwStatement.sourceStart,
            throwStatement.sourceEnd);//from w w w  .  j a v  a 2 s .c om

    this.statement = throwCopy;

    return false;
}