Search in sources :

Example 31 with VariableDeclarationStatement

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

the class InlineTempRefactoring method removeTemp.

private void removeTemp(CompilationUnitRewrite cuRewrite) {
    VariableDeclaration variableDeclaration = getVariableDeclaration();
    TextEditGroup groupDesc = cuRewrite.createGroupDescription(RefactoringCoreMessages.InlineTempRefactoring_remove_edit_name);
    ASTNode parent = variableDeclaration.getParent();
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
    rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
    if (parent instanceof VariableDeclarationStatement && ((VariableDeclarationStatement) parent).fragments().size() == 1) {
        sourceRangeComputer.addTightSourceNode(parent);
        rewrite.remove(parent, groupDesc);
    } else {
        sourceRangeComputer.addTightSourceNode(variableDeclaration);
        rewrite.remove(variableDeclaration, groupDesc);
    }
}
Also used : TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 32 with VariableDeclarationStatement

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

the class ExtractTempRefactoring method createAndInsertTempDeclaration.

private void createAndInsertTempDeclaration() throws CoreException {
    Expression initializer = getSelectedExpression().createCopyTarget(fCURewrite.getASTRewrite(), true);
    VariableDeclarationStatement vds = createTempDeclaration(initializer);
    if ((!fReplaceAllOccurrences) || (retainOnlyReplacableMatches(getMatchingFragments()).length <= 1)) {
        insertAt(getSelectedExpression().getAssociatedNode(), vds);
        return;
    }
    ASTNode[] firstReplaceNodeParents = getParents(getFirstReplacedExpression().getAssociatedNode());
    ASTNode[] commonPath = findDeepestCommonSuperNodePathForReplacedNodes();
    Assert.isTrue(commonPath.length <= firstReplaceNodeParents.length);
    ASTNode deepestCommonParent = firstReplaceNodeParents[commonPath.length - 1];
    if (deepestCommonParent instanceof Block)
        insertAt(firstReplaceNodeParents[commonPath.length], vds);
    else
        insertAt(deepestCommonParent, vds);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 33 with VariableDeclarationStatement

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

the class ExtractTempRefactoring method createTempDeclaration.

private VariableDeclarationStatement createTempDeclaration(Expression initializer) throws CoreException {
    AST ast = fCURewrite.getAST();
    VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
    vdf.setName(ast.newSimpleName(fTempName));
    vdf.setInitializer(initializer);
    VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
    if (fDeclareFinal) {
        vds.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
    }
    vds.setType(createTempType());
    if (fLinkedProposalModel != null) {
        ASTRewrite rewrite = fCURewrite.getASTRewrite();
        LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
        nameGroup.addPosition(rewrite.track(vdf.getName()), true);
        String[] nameSuggestions = guessTempNames();
        if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fTempName)) {
            nameGroup.addProposal(fTempName, null, nameSuggestions.length + 1);
        }
        for (int i = 0; i < nameSuggestions.length; i++) {
            nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i);
        }
    }
    return vds;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 34 with VariableDeclarationStatement

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

the class CallInliner method addNewLocals.

private void addNewLocals(TextEditGroup textEditGroup) {
    if (fLocals.isEmpty())
        return;
    for (Iterator<VariableDeclarationStatement> iter = fLocals.iterator(); iter.hasNext(); ) {
        ASTNode element = iter.next();
        fListRewrite.insertAt(element, fInsertionIndex++, textEditGroup);
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Example 35 with VariableDeclarationStatement

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

the class TempDeclarationFinder method findTempDeclaration.

/**
	 * @return <code>null</code> if the selection is invalid or does not cover a temp
	 * declaration or reference.
	 */
public static VariableDeclaration findTempDeclaration(CompilationUnit cu, int selectionOffset, int selectionLength) {
    TempSelectionAnalyzer analyzer = new TempSelectionAnalyzer(selectionOffset, selectionLength);
    cu.accept(analyzer);
    ASTNode[] selected = analyzer.getSelectedNodes();
    if (selected == null || selected.length != 1)
        return null;
    ASTNode selectedNode = selected[0];
    if (selectedNode instanceof VariableDeclaration)
        return (VariableDeclaration) selectedNode;
    if (selectedNode instanceof Name) {
        Name reference = (Name) selectedNode;
        IBinding binding = reference.resolveBinding();
        if (binding == null)
            return null;
        ASTNode declaringNode = cu.findDeclaringNode(binding);
        if (declaringNode instanceof VariableDeclaration)
            return (VariableDeclaration) declaringNode;
        else
            return null;
    } else if (selectedNode instanceof VariableDeclarationStatement) {
        VariableDeclarationStatement vds = (VariableDeclarationStatement) selectedNode;
        if (vds.fragments().size() != 1)
            return null;
        return (VariableDeclaration) vds.fragments().get(0);
    }
    return null;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Aggregations

VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)48 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)33 ASTNode (org.eclipse.jdt.core.dom.ASTNode)29 AST (org.eclipse.jdt.core.dom.AST)20 Expression (org.eclipse.jdt.core.dom.Expression)20 Type (org.eclipse.jdt.core.dom.Type)20 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)18 Block (org.eclipse.jdt.core.dom.Block)15 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)13 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)12 SimpleName (org.eclipse.jdt.core.dom.SimpleName)12 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)12 Statement (org.eclipse.jdt.core.dom.Statement)12 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)11 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)10 Assignment (org.eclipse.jdt.core.dom.Assignment)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)9 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)9 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)9 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)9