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

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

Introduction

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

Prototype

public final int getStartPosition() 

Source Link

Document

Returns the character index into the original source file indicating where the source fragment corresponding to this node begins.

Usage

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;
        }//  ww w .  j  a  v a 2s .c  o m
    }
    // 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());
}

From source file:org.eclipse.wb.internal.core.utils.ast.AstEditor.java

License:Open Source License

/**
 * Inlines all {@link ParenthesizedExpression} that enclose given {@link Expression}.
 */// w  w w  .  ja va2  s  .  co  m
public void inlineParenthesizedExpression(Expression expression) throws Exception {
    while (expression.getParent() instanceof ParenthesizedExpression) {
        ParenthesizedExpression parent = (ParenthesizedExpression) expression.getParent();
        parent.setExpression(expression.getAST().newSimpleName("__wbp_tmp"));
        replaceNode(parent, expression);
        replaceSubstring(parent.getStartPosition(), parent.getLength(), getSource(expression));
        AstNodeUtils.setSourceBegin(expression, parent.getStartPosition());
    }
}