Search in sources :

Example 36 with VariableDeclarationFragment

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

the class ExtractMethodRefactoring method createDeclaration.

private VariableDeclarationStatement createDeclaration(IVariableBinding binding, Expression intilizer) {
    VariableDeclaration original = ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration());
    VariableDeclarationFragment fragment = fAST.newVariableDeclarationFragment();
    fragment.setName((SimpleName) ASTNode.copySubtree(fAST, original.getName()));
    fragment.setInitializer(intilizer);
    VariableDeclarationStatement result = fAST.newVariableDeclarationStatement(fragment);
    result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original)));
    result.setType(ASTNodeFactory.newType(fAST, original, fImportRewriter, new ContextSensitiveImportRewriteContext(original, fImportRewriter)));
    return result;
}
Also used : ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 37 with VariableDeclarationFragment

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

the class PromoteTempToFieldRefactoring method getNamesOfFieldsInDeclaringType.

private String[] getNamesOfFieldsInDeclaringType() {
    final AbstractTypeDeclaration type = getEnclosingType();
    if (type instanceof TypeDeclaration) {
        FieldDeclaration[] fields = ((TypeDeclaration) type).getFields();
        List<String> result = new ArrayList<String>(fields.length);
        for (int i = 0; i < fields.length; i++) {
            for (Iterator<VariableDeclarationFragment> iter = fields[i].fragments().iterator(); iter.hasNext(); ) {
                VariableDeclarationFragment field = iter.next();
                result.add(field.getName().getIdentifier());
            }
        }
        return result.toArray(new String[result.size()]);
    }
    return new String[] {};
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ArrayList(java.util.ArrayList) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 38 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 39 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 40 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)

Aggregations

VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)101 ASTNode (org.eclipse.jdt.core.dom.ASTNode)44 AST (org.eclipse.jdt.core.dom.AST)38 Expression (org.eclipse.jdt.core.dom.Expression)33 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)33 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)30 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)28 Assignment (org.eclipse.jdt.core.dom.Assignment)25 SimpleName (org.eclipse.jdt.core.dom.SimpleName)25 Type (org.eclipse.jdt.core.dom.Type)25 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)22 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)21 Block (org.eclipse.jdt.core.dom.Block)17 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)16 ArrayList (java.util.ArrayList)15 CastExpression (org.eclipse.jdt.core.dom.CastExpression)15 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)15 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)15 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)15 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)14