Example usage for org.eclipse.jdt.core.dom VariableDeclarationExpression getLocationInParent

List of usage examples for org.eclipse.jdt.core.dom VariableDeclarationExpression getLocationInParent

Introduction

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

Prototype

public final StructuralPropertyDescriptor getLocationInParent() 

Source Link

Document

Returns the location of this node within its parent, or null if this is a root node.

Usage

From source file:de.ovgu.cide.export.physical.ahead.MethodObjectHelper.java

License:Open Source License

private static void moveLocalVariableDeclarationsToFields(final MethodDeclaration executeMethod,
        final TypeDeclaration methodObjectClass, final RefactoringColorManager colorManager) {

    final AST ast = executeMethod.getAST();
    final Set<String> localVariableNames = new HashSet<String>();
    executeMethod.accept(new ASTVisitor() {
        @Override//  w  w w  . j  a  v a  2 s. c  om
        public boolean visit(VariableDeclarationExpression node) {
            assert colorManager.getColors(node).equals(colorManager
                    .getColors(executeMethod)) : "Colored VariableDeclarationExpression not handled yet";
            return super.visit(node);
        }

        @Override
        public void endVisit(VariableDeclarationStatement node) {
            assert node.getLocationInParent().isChildListProperty();
            Set<IFeature> colors = colorManager.getColors(node);
            Block replacementBlock = ast.newBlock();
            Iterator<VariableDeclarationFragment> fragmentIterator = node.fragments().iterator();
            while (fragmentIterator.hasNext()) {
                VariableDeclarationFragment fragment = fragmentIterator.next();
                checkDuplicate(fragment);
                addField(methodObjectClass, node.getType(), fragment.getName().getIdentifier(), colors);
                Expression init = fragment.getInitializer();
                if (init == null)
                    fragmentIterator.remove();
                else {
                    fragment.setInitializer(null);
                    Assignment assignment = ast.newAssignment();
                    assignment.setLeftHandSide(ast.newSimpleName(fragment.getName().getIdentifier()));
                    assignment.setRightHandSide(init);
                    ExpressionStatement assignExpr = ast.newExpressionStatement(assignment);
                    replacementBlock.statements().add(assignExpr);
                    colorManager.addColors(assignExpr, colorManager.getOwnColors(fragment));
                    colorManager.addColors(assignExpr, colorManager.getOwnColors(node));
                }
            }
            Statement replacement = replacementBlock;
            if (replacementBlock.statements().size() == 1) {
                replacement = (Statement) replacementBlock.statements().get(0);
                replacementBlock.statements().clear();
            }
            RefactoringUtils.replaceASTNode(node, replacement);

            /*
             * remove reference from LocalVariableHelper, because this now
             * no longer references a local variable, but a field.
             */
        }

        private void checkDuplicate(VariableDeclarationFragment fragment) {
            String variableName = fragment.getName().getIdentifier();
            assert !localVariableNames.contains(variableName) : "Duplicate local variable name not supported";
            localVariableNames.add(variableName);
        }

        @Override
        public boolean visit(SimpleName node) {
            return visitName(node);
        }

        @Override
        public boolean visit(QualifiedName node) {
            return visitName(node);
        }

        private boolean visitName(Name name) {
            LocalVariableHelper.removeName(name);
            return true;
        }
    });

}

From source file:edu.illinois.jflow.core.transformations.code.ExtractClosureAnalyzer.java

License:Open Source License

@Override
public void endVisit(VariableDeclarationExpression node) {
    if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED && getFirstSelectedNode() == node) {
        if (node.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
            invalidSelection(JFlowRefactoringCoreMessages.ExtractClosureAnalyzer_resource_in_try_with_resources,
                    JavaStatusContext.create(fCUnit, getSelection()));
        }/*www  .  ja  va  2s .  co m*/
    }
    checkTypeInDeclaration(node.getType());
    super.endVisit(node);
}