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

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

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom VariableDeclarationStatement 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:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(VariableDeclarationStatement node) {

    List<VariableDeclarationFragment> fragments = node.fragments();

    for (VariableDeclarationFragment fragment : fragments) {
        String identifier = fragment.getName().getIdentifier();
        int beginLine = cu.getLineNumber(fragment.getStartPosition());
        int endLine = cu.getLineNumber(fragment.getStartPosition() + fragment.getLength());
        int beginColumn = cu.getColumnNumber(node.getStartPosition());
        int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

        SimpleName name = fragment.getName();

        if (name != null) {
            endLine = cu.getLineNumber(name.getStartPosition() + name.getLength());
            endColumn = cu.getColumnNumber(name.getStartPosition() + name.getLength());
        }//from ww  w.  j  a va  2 s  . c  o  m

        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine,
                beginColumn, endColumn, identifier));

    }

    return true;
}

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

License:Open Source License

/**
 * This method should be used if this variable is initialized in
 * {@link VariableDeclarationFragment} and we want split it on separate
 * {@link VariableDeclarationStatement} and assignment. There is such quick assist in Eclipse.
 *//*from  w  ww  . j a va2 s  . com*/
private void splitVariable() throws Exception {
    AstEditor editor = m_javaInfo.getEditor();
    // prepare current information
    VariableDeclaration oldFragment = m_declaration;
    VariableDeclarationStatement oldStatement = getDeclarationStatement();
    // add new VariableDeclarationStatement before oldStatement
    // we should create new variable because "m_variable" is place of _assignment_
    {
        int position = oldStatement.getStartPosition();
        String source = "";
        // add type
        Type newType;
        {
            Type oldType = oldStatement.getType();
            newType = editor.getParser().parseType(position, oldType);
            source += editor.getSource(oldType) + " ";
        }
        // add variable
        SimpleName newVariable;
        {
            String identifier = AstNodeUtils.getVariableName(m_variable);
            newVariable = editor.getParser().parseVariable(position + source.length(), identifier, null,
                    AstNodeUtils.getTypeBinding(newType), false, Modifier.NONE);
            source += identifier;
        }
        // create fragment
        VariableDeclarationFragment newFragment;
        {
            newFragment = m_variable.getAST().newVariableDeclarationFragment();
            newFragment.setName(newVariable);
            AstNodeUtils.copySourceRange(newFragment, newVariable);
        }
        // create statement
        VariableDeclarationStatement newStatement;
        {
            newStatement = m_variable.getAST().newVariableDeclarationStatement(newFragment);
            newStatement.setType(newType);
            AstNodeUtils.setSourceRange(newStatement, newType, newFragment, 1);
            source += ";";
        }
        // modify source
        {
            String prefix = editor.getWhitespaceToLeft(position, false);
            source += editor.getGeneration().getEndOfLine() + prefix;
            editor.replaceSubstring(position, 0, source);
        }
        // add new statement to AST
        {
            List<Statement> statements = DomGenerics.statements((Block) oldStatement.getParent());
            int index = statements.indexOf(oldStatement);
            statements.add(index, newStatement);
        }
    }
    // convert old declaration to assignment
    replaceDeclarationWithAssignment(editor, oldFragment);
    rememberDeclaration();
}

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

License:Open Source License

/**
 * Replaces given {@link VariableDeclarationStatement} with {@link ExpressionStatement} with
 * {@link Assignment} to the same variable.
 *///w  w  w . j a v a2s.c  om
protected final void replaceDeclarationWithAssignment(AstEditor editor, VariableDeclaration oldFragment)
        throws Exception {
    VariableDeclarationStatement oldStatement = (VariableDeclarationStatement) oldFragment.getParent();
    Assert.isTrue(oldStatement.fragments().size() == 1);
    int oldStart = oldStatement.getStartPosition();
    // prepare ExpressionStatement with assignment of existing expression to existing variable
    ExpressionStatement newStatement;
    {
        AST ast = m_variable.getAST();
        // prepare assignment
        Assignment newAssignment;
        {
            newAssignment = ast.newAssignment();
            // reuse variable
            {
                oldFragment.setName(ast.newSimpleName("__foo"));
                newAssignment.setLeftHandSide(m_variable);
            }
            // reuse initializer
            {
                Expression initializer = oldFragment.getInitializer();
                oldFragment.setInitializer(null);
                newAssignment.setRightHandSide(initializer);
            }
            // source range
            AstNodeUtils.setSourceRange(newAssignment, newAssignment.getLeftHandSide(),
                    newAssignment.getRightHandSide());
        }
        // prepare ExpressionStatement
        newStatement = ast.newExpressionStatement(newAssignment);
        AstNodeUtils.setSourceRange(newStatement, newAssignment, 1);
    }
    // replace old expression in AST
    {
        List<Statement> oldStatements = DomGenerics.statements((Block) oldStatement.getParent());
        int index = oldStatements.indexOf(oldStatement);
        oldStatements.set(index, newStatement);
    }
    // patch source to make it same as changed AST
    editor.replaceSubstring(oldStart, m_variable.getStartPosition() - oldStart, "");
}

From source file:org.evosuite.junit.TestExtractingVisitor.java

License:Open Source License

private int[] getLengths(VariableDeclarationStatement variableDeclStmt,
        List<VariableReference> lengthsVarRefs) {
    int[] lengths = new int[lengthsVarRefs.size()];
    String variable = retrieveVariableName(variableDeclStmt);
    int lineNumber = testReader.getLineNumber(variableDeclStmt.getStartPosition());
    Object value = null;/*from   w ww. j av  a  2s .  c  o  m*/
    if (cursorableTrace != null) {
        value = cursorableTrace.getVariableValueAfter(lineNumber, variable);
    }
    int idx = 0;
    while ((value != null) && value.getClass().isArray()) {
        lengths[idx] = Array.getLength(value);
        if (lengths[idx] > 0) {
            value = Array.get(value, 0);
        } else {
            value = null;
        }
        idx++;
    }
    if (idx == lengths.length) {
        return lengths;
    }

    idx = 0;
    for (VariableReference lengthVarRef : lengthsVarRefs) {
        // TODO This is a hack. We should use the variablereferences
        // instead!
        lengths[idx] = Integer.valueOf(lengthVarRef.toString());
        idx++;
    }
    return lengths;
}

From source file:sharpen.core.CSharpBuilder.java

License:Open Source License

public boolean visit(VariableDeclarationStatement node) {
    for (Object f : node.fragments()) {
        VariableDeclarationFragment variable = (VariableDeclarationFragment) f;
        addStatement(new CSDeclarationStatement(node.getStartPosition(), createVariableDeclaration(variable)));
    }/*  w  w w  .j a va  2 s . c  o m*/
    return false;
}