Example usage for org.eclipse.jdt.core.dom VariableDeclaration getParent

List of usage examples for org.eclipse.jdt.core.dom VariableDeclaration getParent

Introduction

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

Prototype

public final ASTNode getParent() 

Source Link

Document

Returns this node's parent node, or null if this is the root node.

Usage

From source file:de.ovgu.cide.export.physical.internal.LocalVariableHelper.java

License:Open Source License

private static Formal createFormal(VariableDeclaration decl) {
    if (decl instanceof SingleVariableDeclaration)
        return new Formal(((SingleVariableDeclaration) decl).getType(), decl.getName().getIdentifier(),
                formalNr++);//from  ww  w .  ja  v  a 2  s.  c om
    else {
        assert decl instanceof VariableDeclarationFragment;

        Type type = null;
        if (decl.getParent() instanceof VariableDeclarationStatement) {
            type = ((VariableDeclarationStatement) decl.getParent()).getType();
        } else if (decl.getParent() instanceof VariableDeclarationExpression) {
            type = ((VariableDeclarationExpression) decl.getParent()).getType();
        } else if (decl.getParent() instanceof FieldDeclaration)
            ;// not a local variable
        else
            assert false;

        if (type != null)
            return new Formal(type, decl.getName().getIdentifier(), formalNr++);
    }
    return null;
}

From source file:edu.cmu.cs.crystal.tac.eclipse.EclipseFieldDeclaration.java

License:Open Source License

/**
 * @param node/*from  ww w  . j  a v a 2  s .  c  o  m*/
 * @param query
 */
public EclipseFieldDeclaration(VariableDeclaration node, IEclipseVariableQuery query) {
    super(node, query);
    if (!(node.getParent() instanceof FieldDeclaration))
        throw new IllegalArgumentException("Not a field declaration: " + node);
}

From source file:edu.cmu.cs.crystal.tac.eclipse.EclipseTACInstructionFactory.java

License:Open Source License

public TACInstruction create(VariableDeclaration node, IEclipseVariableQuery eclipseVariableQuery) {
    if (node.getParent() instanceof FieldDeclaration) {
        // this can happen in the constructor, where the CFG inlines field initializers
        // need to create a store iff this declaration has an initializer
        if (node.getInitializer() == null)
            return null;
        return new StoreFieldInstructionImpl(node, eclipseVariableQuery.variable(node.getInitializer()),
                new EclipseFieldDeclaration(node, eclipseVariableQuery), eclipseVariableQuery);

    } else {// ww w . ja  v a2 s .  c om
        // local variable
        SourceVariableDeclarationImpl decl = new SourceVariableDeclarationImpl(node, eclipseVariableQuery);
        if (node.getInitializer() == null)
            return decl;
        // copy result of previous initializer into declared variable
        CopyInstructionImpl init = new CopyInstructionImpl(node,
                eclipseVariableQuery.variable(node.getInitializer()), true,
                eclipseVariableQuery.sourceVariable(decl.resolveBinding()), eclipseVariableQuery);
        return new EclipseInstructionSequence(node, new TACInstruction[] { decl, init }, eclipseVariableQuery);
    }
}

From source file:org.autorefactor.refactoring.rules.AbstractClassSubstituteRefactoring.java

License:Open Source License

private void replaceClass(final ClassInstanceCreation originalInstanceCreation,
        final List<VariableDeclaration> variableDecls, final List<MethodInvocation> methodCallsToRefactor) {
    final ASTBuilder b = ctx.getASTBuilder();

    final Type substituteType = substituteType(b, originalInstanceCreation.getType(), originalInstanceCreation);
    if (substituteType != null) {
        ctx.getRefactorings().replace(originalInstanceCreation.getType(), substituteType);
        originalInstanceCreation.setType(substituteType);
    }//from  w ww .j av  a  2 s  .  co m

    for (final MethodInvocation methodCall : methodCallsToRefactor) {
        final MethodInvocation copyOfMethodCall = b.copySubtree(methodCall);
        refactorMethod(b, methodCall, copyOfMethodCall);
        ctx.getRefactorings().replace(methodCall, copyOfMethodCall);
    }

    for (final VariableDeclaration variableDecl : variableDecls) {
        final VariableDeclarationStatement oldDeclareStmt = (VariableDeclarationStatement) variableDecl
                .getParent();
        final Type substituteVarType = substituteType(b, oldDeclareStmt.getType(),
                (ASTNode) oldDeclareStmt.fragments().get(0));
        if (substituteVarType != null) {
            ctx.getRefactorings().replace(oldDeclareStmt.getType(), substituteVarType);
        }
    }
}

From source file:org.autorefactor.refactoring.rules.AbstractClassSubstituteRefactoring.java

License:Open Source License

private boolean canInstantiationBeRefactored(final ASTNode node, final ITypeBinding nodeTypeBinding,
        final List<VariableDeclaration> variablesToRefactor,
        final List<MethodInvocation> methodCallsToRefactor) {
    ASTNode parentNode = node.getParent();

    switch (parentNode.getNodeType()) {
    case ASSIGNMENT:
    case RETURN_STATEMENT:
    case CAST_EXPRESSION:
    case INSTANCEOF_EXPRESSION:
    case CLASS_INSTANCE_CREATION:
    case CONSTRUCTOR_INVOCATION:
    case CONDITIONAL_EXPRESSION:
        return false;

    case PARENTHESIZED_EXPRESSION:
        return canInstantiationBeRefactored(parentNode, nodeTypeBinding, variablesToRefactor,
                methodCallsToRefactor);// w w  w . j  a v  a2s.  c  o m

    case ENHANCED_FOR_STATEMENT:
        return canInvokeIterator();

    case SINGLE_VARIABLE_DECLARATION:
    case VARIABLE_DECLARATION_EXPRESSION:
    case VARIABLE_DECLARATION_FRAGMENT:
    case VARIABLE_DECLARATION_STATEMENT:
        final VariableDeclaration varDecl = (VariableDeclaration) parentNode;
        if (varDecl.getParent() instanceof VariableDeclarationStatement) {
            final VariableDeclarationStatement variableDeclaration = (VariableDeclarationStatement) varDecl
                    .getParent();
            if (isTypeCompatible(variableDeclaration.getType().resolveBinding(), nodeTypeBinding)) {
                variablesToRefactor.add(varDecl);
                return true;
            }
        }
        return false;

    case METHOD_INVOCATION:
        final MethodInvocation mi = (MethodInvocation) parentNode;
        if (isObjectPassedInParameter(node, mi) || !canMethodBeRefactored(mi, methodCallsToRefactor)) {
            return false;
        } else if (!isMethodReturningExistingClass(mi)) {
            return true;
        }
        return canInstantiationBeRefactored(parentNode, nodeTypeBinding, variablesToRefactor,
                methodCallsToRefactor);

    default:
        return true;
    }
}

From source file:org.autorefactor.refactoring.rules.VariableDefinitionsUsesVisitor.java

License:Open Source License

private static ASTNode getDeclaringScope(VariableDeclaration variableDeclaration) {
    ASTNode node = variableDeclaration.getParent();
    while (isVariableDeclaration(node)) {
        node = node.getParent();/*from ww w .j av  a  2 s . c  om*/
    }
    return node;
}

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

License:Open Source License

/**
 * Creates unique field for this component.
 *///from  ww w  . j  ava 2  s.  c om
private void splitUniqueField() throws Exception {
    // add new field and use its name
    {
        AstEditor editor = m_javaInfo.getEditor();
        VariableDeclaration oldDeclaration = m_declaration;
        FieldDeclaration oldField = (FieldDeclaration) oldDeclaration.getParent();
        String oldFieldName = getName();
        String newFieldName = editor.getUniqueVariableName(-1, oldFieldName, null);
        // prepare modifiers
        String modifiers;
        {
            modifiers = "private ";
            if (AstNodeUtils.isStatic(oldField)) {
                modifiers += "static ";
            }
        }
        // check when field has assignment of our component
        if (oldDeclaration.getName() == m_variable) {
            // use temporary name for all references
            modifyName("__tmpField");
            // add new field with oldFieldName
            {
                String fieldSource = modifiers + editor.getSource(oldField.getType()) + " " + oldFieldName
                        + ";";
                editor.addFieldDeclaration(fieldSource, new BodyDeclarationTarget(oldField, true));
            }
            // change references: this component -> newFieldName, other -> oldFieldName
            {
                // prepare list of references for this JavaInfo (not just on same variable)
                // NB: we need this list because during replacing references it will become temporary invalid
                List<Expression> componentReferences = getComponentReferences();
                // replace references
                for (Expression reference : getReferences()) {
                    if (componentReferences.contains(reference)) {
                        modifyVariableName(reference, newFieldName);
                    } else {
                        modifyVariableName(reference, oldFieldName);
                    }
                }
            }
        } else {
            String fieldSource = modifiers + editor.getSource(oldField.getType()) + " " + newFieldName + ";";
            editor.addFieldDeclaration(fieldSource, new BodyDeclarationTarget(oldField, false));
            replaceComponentReferences(newFieldName);
        }
    }
    // use field variable support
    m_javaInfo.setVariableSupport(new FieldUniqueVariableSupport(m_javaInfo, m_variable));
}

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

License:Open Source License

@Override
public void convertFieldToLocal() throws Exception {
    AstEditor editor = m_javaInfo.getEditor();
    // prepare current information
    VariableDeclaration oldFragment = m_declaration;
    FieldDeclaration oldField = (FieldDeclaration) oldFragment.getParent();
    String typeString = editor.getSource(oldField.getType());
    // variable is first place where JavaInfo is assigned
    Assignment assignment = (Assignment) m_variable.getParent();
    Assert.isTrue(assignment.getLeftHandSide() == m_variable);
    Expression oldInitializer = assignment.getRightHandSide();
    Statement oldStatement = (Statement) assignment.getParent();
    ITypeBinding typeBinding = AstNodeUtils.getTypeBinding(oldField.getType());
    // rename variable to make it local-like
    String localName = m_utils.convertName(assignment.getStartPosition(), getName(),
            JavaCore.CODEASSIST_FIELD_PREFIXES, JavaCore.CODEASSIST_FIELD_SUFFIXES,
            JavaCore.CODEASSIST_LOCAL_PREFIXES, JavaCore.CODEASSIST_LOCAL_SUFFIXES, m_declaration);
    setName(localName);/*from ww w.j a va2 s  . c o m*/
    // replace "this.fieldName" with "localName"
    {
        for (Expression reference : getReferences()) {
            if (reference instanceof FieldAccess) {
                SimpleName simpleReference = parseVariableSimpleName(reference.getStartPosition(), localName,
                        typeBinding);
                editor.replaceSubstring(reference, localName);
                AstEditor.replaceNode(reference, simpleReference);
                if (reference == m_variable) {
                    m_variable = simpleReference;
                }
            }
        }
    }
    // add type source (before changes in AST because we insert new nodes)
    Type newType;
    {
        int oldStart = m_variable.getStartPosition();
        editor.replaceSubstring(oldStart, 0, typeString + " ");
        newType = editor.getParser().parseType(oldStart, oldField.getType());
    }
    // replace assignment with variable declaration
    SimpleName localVariable;
    {
        AST ast = m_variable.getAST();
        // prepare new fragment, reuse variable and initializer
        VariableDeclarationFragment newFragment = ast.newVariableDeclarationFragment();
        {
            assignment.setLeftHandSide(ast.newSimpleName("__foo"));
            editor.replaceSubstring(m_variable, localName);
            localVariable = parseVariableSimpleName(m_variable.getStartPosition(), localName, typeBinding);
            m_variable = localVariable;
            newFragment.setName(localVariable);
        }
        {
            assignment.setRightHandSide(ast.newSimpleName("__bar"));
            newFragment.setInitializer(oldInitializer);
        }
        AstNodeUtils.setSourceRange(newFragment, m_variable, oldInitializer);
        // prepare new statement
        VariableDeclarationStatement newStatement = ast.newVariableDeclarationStatement(newFragment);
        newStatement.setType(newType);
        AstNodeUtils.setSourceRange(newStatement, newType, oldStatement);
        // replace old statement in AST
        {
            List<Statement> statements = DomGenerics.statements((Block) oldStatement.getParent());
            int index = statements.indexOf(oldStatement);
            statements.set(index, newStatement);
        }
    }
    // remove old field
    editor.removeVariableDeclaration(oldFragment);
    // use local variable support
    m_javaInfo.setVariableSupport(new LocalUniqueVariableSupport(m_javaInfo, localVariable));
}

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

License:Open Source License

@Override
public void convertLocalToField() throws Exception {
    AstEditor editor = m_javaInfo.getEditor();
    // add new field and use its name
    {/*from   ww w . jav a2  s  .  c o m*/
        String fieldName = addUniqueField(isStaticContext(), m_declaration);
        setName(fieldName);
    }
    // replace local variable declaration with assignment to field
    {
        VariableDeclaration declaration = m_declaration;
        if (declaration.getInitializer() != null) {
            replaceDeclarationWithAssignment(editor, declaration);
        } else {
            editor.removeStatement((Statement) declaration.getParent());
        }
    }
    // use field variable support
    m_javaInfo.setVariableSupport(new FieldUniqueVariableSupport(m_javaInfo, m_variable));
}

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 va2 s. c o m*/
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, "");
}