Example usage for org.eclipse.jdt.core.dom ParenthesizedExpression setSourceRange

List of usage examples for org.eclipse.jdt.core.dom ParenthesizedExpression setSourceRange

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom ParenthesizedExpression setSourceRange.

Prototype

public final void setSourceRange(int startPosition, int length) 

Source Link

Document

Sets the source range of the original source file where the source fragment corresponding to this node was found.

Usage

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

public ParenthesizedExpression convertToParenthesizedExpression(
        org.eclipse.jdt.internal.compiler.ast.Expression expression) {
    final ParenthesizedExpression parenthesizedExpression = new ParenthesizedExpression(this.ast);
    if (this.resolveBindings) {
        recordNodes(parenthesizedExpression, expression);
    }//  ww  w .  j ava2  s .  c o m
    parenthesizedExpression.setSourceRange(expression.sourceStart,
            expression.sourceEnd - expression.sourceStart + 1);
    adjustSourcePositionsForParent(expression);
    trimWhiteSpacesAndComments(expression);
    // decrement the number of parenthesis
    int numberOfParenthesis = (expression.bits
            & org.eclipse.jdt.internal.compiler.ast.ASTNode.ParenthesizedMASK) >> org.eclipse.jdt.internal.compiler.ast.ASTNode.ParenthesizedSHIFT;
    expression.bits &= ~org.eclipse.jdt.internal.compiler.ast.ASTNode.ParenthesizedMASK;
    expression.bits |= (numberOfParenthesis
            - 1) << org.eclipse.jdt.internal.compiler.ast.ASTNode.ParenthesizedSHIFT;
    parenthesizedExpression.setExpression(convert(expression));
    return parenthesizedExpression;
}

From source file:org.eclipse.wb.internal.core.model.variable.LocalUniqueVariableSupport.java

License:Open Source License

private void replaceReferenceWithInitializer(Expression reference, Expression initializer) throws Exception {
    AstEditor editor = m_javaInfo.getEditor();
    // check if initializer should be wrapped with ParenthesizedExpression 
    if (reference.getParent() instanceof Expression) {
        Expression parent = (Expression) reference.getParent();
        int parentPrecedence = OperatorPrecedence.getExpressionPrecedence(parent);
        int initializerPrecedence = OperatorPrecedence.getExpressionPrecedence(initializer);
        if (parentPrecedence > initializerPrecedence) {
            String parenthesizedSource = "(" + editor.getSource(initializer) + ")";
            editor.replaceSubstring(reference.getStartPosition(), reference.getLength(), parenthesizedSource);
            // prepare ParenthesizedExpression
            ParenthesizedExpression parenthesized = reference.getAST().newParenthesizedExpression();
            parenthesized.setExpression(initializer);
            parenthesized.setSourceRange(reference.getStartPosition(), 1 + initializer.getLength() + 1);
            // replace "reference"
            AstEditor.replaceNode(reference, parenthesized);
            AstNodeUtils.moveNode(initializer, 1 + parenthesized.getStartPosition());
            m_javaInfo.addRelatedNode(parenthesized);
            return;
        }/*w w w . j a v a2s  . c om*/
    }
    // simple case - just replace "reference" with "initializer"
    editor.replaceSubstring(reference.getStartPosition(), reference.getLength(), editor.getSource(initializer));
    AstEditor.replaceNode(reference, initializer);
    AstNodeUtils.moveNode(initializer, reference.getStartPosition());
}