Search in sources :

Example 11 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.

the class PromoteTempToFieldRefactoring method addLocalDeclarationSplit.

private void addLocalDeclarationSplit(ASTRewrite rewrite) {
    VariableDeclarationStatement tempDeclarationStatement = getTempDeclarationStatement();
    ASTNode parentStatement = tempDeclarationStatement.getParent();
    ListRewrite listRewrite;
    if (parentStatement instanceof SwitchStatement) {
        listRewrite = rewrite.getListRewrite(parentStatement, SwitchStatement.STATEMENTS_PROPERTY);
    } else if (parentStatement instanceof Block) {
        listRewrite = rewrite.getListRewrite(parentStatement, Block.STATEMENTS_PROPERTY);
    } else {
        // should not happen. VariableDeclaration's can not be in a control statement body
        listRewrite = null;
        Assert.isTrue(false);
    }
    int statementIndex = listRewrite.getOriginalList().indexOf(tempDeclarationStatement);
    Assert.isTrue(statementIndex != -1);
    Statement newStatement = createNewAssignmentStatement(rewrite);
    List<VariableDeclarationFragment> fragments = tempDeclarationStatement.fragments();
    int fragmentIndex = fragments.indexOf(fTempDeclarationNode);
    Assert.isTrue(fragmentIndex != -1);
    if (fragments.size() == 1) {
        rewrite.replace(tempDeclarationStatement, newStatement, null);
        return;
    }
    for (int i1 = fragmentIndex, n = fragments.size(); i1 < n; i1++) {
        VariableDeclarationFragment fragment = fragments.get(i1);
        rewrite.remove(fragment, null);
    }
    if (fragmentIndex == 0)
        rewrite.remove(tempDeclarationStatement, null);
    Assert.isTrue(tempHasInitializer());
    listRewrite.insertAt(newStatement, statementIndex + 1, null);
    if (fragmentIndex + 1 < fragments.size()) {
        VariableDeclarationFragment firstFragmentAfter = fragments.get(fragmentIndex + 1);
        VariableDeclarationFragment copyfirstFragmentAfter = (VariableDeclarationFragment) rewrite.createCopyTarget(firstFragmentAfter);
        VariableDeclarationStatement statement = getAST().newVariableDeclarationStatement(copyfirstFragmentAfter);
        Type type = (Type) rewrite.createCopyTarget(tempDeclarationStatement.getType());
        statement.setType(type);
        List<IExtendedModifier> modifiers = tempDeclarationStatement.modifiers();
        if (modifiers.size() > 0) {
            ListRewrite modifiersRewrite = rewrite.getListRewrite(tempDeclarationStatement, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
            ASTNode firstModifier = (ASTNode) modifiers.get(0);
            ASTNode lastModifier = (ASTNode) modifiers.get(modifiers.size() - 1);
            ASTNode modifiersCopy = modifiersRewrite.createCopyTarget(firstModifier, lastModifier);
            statement.modifiers().add(modifiersCopy);
        }
        for (int i = fragmentIndex + 2; i < fragments.size(); i++) {
            VariableDeclarationFragment fragment = fragments.get(i);
            VariableDeclarationFragment fragmentCopy = (VariableDeclarationFragment) rewrite.createCopyTarget(fragment);
            statement.fragments().add(fragmentCopy);
        }
        listRewrite.insertAt(statement, statementIndex + 2, null);
    }
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) Statement(org.eclipse.jdt.core.dom.Statement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 12 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.

the class ExtractTempRefactoring method getForInitializedVariables.

// return List<IVariableBinding>
private static List<IVariableBinding> getForInitializedVariables(VariableDeclarationExpression variableDeclarations) {
    List<IVariableBinding> forInitializerVariables = new ArrayList<IVariableBinding>(1);
    for (Iterator<VariableDeclarationFragment> iter = variableDeclarations.fragments().iterator(); iter.hasNext(); ) {
        VariableDeclarationFragment fragment = iter.next();
        IVariableBinding binding = fragment.resolveBinding();
        if (binding != null)
            forInitializerVariables.add(binding);
    }
    return forInitializerVariables;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ArrayList(java.util.ArrayList) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 13 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.

the class InlineConstantRefactoring method findDeclaration.

private RefactoringStatus findDeclaration() throws JavaModelException {
    fDeclarationSelectedChecked = true;
    fDeclarationSelected = false;
    ASTNode parent = fSelectedConstantName.getParent();
    if (parent instanceof VariableDeclarationFragment) {
        VariableDeclarationFragment parentDeclaration = (VariableDeclarationFragment) parent;
        if (parentDeclaration.getName() == fSelectedConstantName) {
            fDeclarationSelected = true;
            fDeclarationCuRewrite = fSelectionCuRewrite;
            fDeclaration = (VariableDeclarationFragment) fSelectedConstantName.getParent();
            return null;
        }
    }
    VariableDeclarationFragment declaration = (VariableDeclarationFragment) fSelectionCuRewrite.getRoot().findDeclaringNode(fSelectedConstantName.resolveBinding());
    if (declaration != null) {
        fDeclarationCuRewrite = fSelectionCuRewrite;
        fDeclaration = declaration;
        return null;
    }
    if (fField.getCompilationUnit() == null)
        return RefactoringStatus.createStatus(RefactoringStatus.FATAL, RefactoringCoreMessages.InlineConstantRefactoring_binary_file, null, Corext.getPluginId(), RefactoringStatusCodes.DECLARED_IN_CLASSFILE, null);
    fDeclarationCuRewrite = new CompilationUnitRewrite(fField.getCompilationUnit());
    fDeclaration = ASTNodeSearchUtil.getFieldDeclarationFragmentNode(fField, fDeclarationCuRewrite.getRoot());
    return null;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 14 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.

the class JavaTextSelection method resolveInVariableInitializer.

public boolean resolveInVariableInitializer() {
    if (fInVariableInitializerRequested)
        return fInVariableInitializer;
    fInVariableInitializerRequested = true;
    resolveSelectedNodes();
    ASTNode node = getStartNode();
    ASTNode last = null;
    while (node != null) {
        int nodeType = node.getNodeType();
        if (node instanceof AbstractTypeDeclaration) {
            fInVariableInitializer = false;
            break;
        } else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
            fInVariableInitializer = false;
            break;
        } else if (nodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT && ((VariableDeclarationFragment) node).getInitializer() == last) {
            fInVariableInitializer = true;
            break;
        } else if (nodeType == ASTNode.SINGLE_VARIABLE_DECLARATION && ((SingleVariableDeclaration) node).getInitializer() == last) {
            fInVariableInitializer = true;
            break;
        } else if (nodeType == ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION && ((AnnotationTypeMemberDeclaration) node).getDefault() == last) {
            fInVariableInitializer = true;
            break;
        }
        last = node;
        node = node.getParent();
    }
    return fInVariableInitializer;
}
Also used : AnnotationTypeMemberDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 15 with VariableDeclarationFragment

use of org.eclipse.jdt.core.dom.VariableDeclarationFragment in project che by eclipse.

the class InferTypeArgumentsConstraintCreator method endVisitFieldVariableDeclaration.

private void endVisitFieldVariableDeclaration(Type type, List<VariableDeclarationFragment> list) {
    ConstraintVariable2 typeCv = getConstraintVariable(type);
    if (typeCv == null)
        return;
    for (Iterator<VariableDeclarationFragment> iter = list.iterator(); iter.hasNext(); ) {
        VariableDeclarationFragment fragment = iter.next();
        ConstraintVariable2 fragmentCv = getConstraintVariable(fragment);
        fTCModel.createElementEqualsConstraints(typeCv, fragmentCv);
    }
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)

Aggregations

VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)82 ASTNode (org.eclipse.jdt.core.dom.ASTNode)39 AST (org.eclipse.jdt.core.dom.AST)35 Expression (org.eclipse.jdt.core.dom.Expression)30 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)26 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)24 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)24 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)22 Type (org.eclipse.jdt.core.dom.Type)22 SimpleName (org.eclipse.jdt.core.dom.SimpleName)21 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)21 Assignment (org.eclipse.jdt.core.dom.Assignment)19 ArrayList (java.util.ArrayList)15 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)14 Block (org.eclipse.jdt.core.dom.Block)13 CastExpression (org.eclipse.jdt.core.dom.CastExpression)13 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)13 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)13 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)12 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)12