Search in sources :

Example 1 with IASTFragment

use of org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment in project che by eclipse.

the class ExtractTempRefactoring method getSelectedExpression.

private IExpressionFragment getSelectedExpression() throws JavaModelException {
    if (fSelectedExpression != null)
        return fSelectedExpression;
    IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);
    if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
        fSelectedExpression = (IExpressionFragment) selectedFragment;
    } else if (selectedFragment != null) {
        if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
            ExpressionStatement exprStatement = (ExpressionStatement) selectedFragment.getAssociatedNode();
            Expression expression = exprStatement.getExpression();
            fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
        } else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
            Assignment assignment = (Assignment) selectedFragment.getAssociatedNode();
            fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
        }
    }
    if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
        fSelectedExpression = null;
    }
    return fSelectedExpression;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IASTFragment(org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment) IExpressionFragment(org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment) 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) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) SourceRange(org.eclipse.jdt.core.SourceRange)

Example 2 with IASTFragment

use of org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment in project che by eclipse.

the class ExtractTempRefactoring method checkMatchingFragments.

private RefactoringStatus checkMatchingFragments() throws JavaModelException {
    RefactoringStatus result = new RefactoringStatus();
    IASTFragment[] matchingFragments = getMatchingFragments();
    for (int i = 0; i < matchingFragments.length; i++) {
        ASTNode node = matchingFragments[i].getAssociatedNode();
        if (isLeftValue(node) && !isReferringToLocalVariableFromFor((Expression) node)) {
            String msg = RefactoringCoreMessages.ExtractTempRefactoring_assigned_to;
            result.addWarning(msg, JavaStatusContext.create(fCu, node));
        }
    }
    return result;
}
Also used : IASTFragment(org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 3 with IASTFragment

use of org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment in project che by eclipse.

the class ExtractConstantRefactoring method getFragmentsToReplace.

private IASTFragment[] getFragmentsToReplace() throws JavaModelException {
    List<IASTFragment> toReplace = new ArrayList<IASTFragment>();
    if (fReplaceAllOccurrences) {
        Iterator<ASTNode> replacementScope = getReplacementScope();
        while (replacementScope.hasNext()) {
            ASTNode scope = replacementScope.next();
            IASTFragment[] allMatches = ASTFragmentFactory.createFragmentForFullSubtree(scope).getSubFragmentsMatching(getSelectedExpression());
            IASTFragment[] replaceableMatches = retainOnlyReplacableMatches(allMatches);
            for (int i = 0; i < replaceableMatches.length; i++) toReplace.add(replaceableMatches[i]);
        }
    } else if (canReplace(getSelectedExpression()))
        toReplace.add(getSelectedExpression());
    return toReplace.toArray(new IASTFragment[toReplace.size()]);
}
Also used : IASTFragment(org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 4 with IASTFragment

use of org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment in project che by eclipse.

the class ExtractTempRefactoring method addReplaceExpressionWithTemp.

private void addReplaceExpressionWithTemp() throws JavaModelException {
    IASTFragment[] fragmentsToReplace = retainOnlyReplacableMatches(getMatchingFragments());
    //TODO: should not have to prune duplicates here...
    ASTRewrite rewrite = fCURewrite.getASTRewrite();
    HashSet<IASTFragment> seen = new HashSet<IASTFragment>();
    for (int i = 0; i < fragmentsToReplace.length; i++) {
        IASTFragment fragment = fragmentsToReplace[i];
        if (!seen.add(fragment))
            continue;
        SimpleName tempName = fCURewrite.getAST().newSimpleName(fTempName);
        TextEditGroup description = fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_replace);
        fragment.replace(rewrite, tempName, description);
        if (fLinkedProposalModel != null)
            fLinkedProposalModel.getPositionGroup(KEY_NAME, true).addPosition(rewrite.track(tempName), false);
    }
}
Also used : IASTFragment(org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) HashSet(java.util.HashSet)

Example 5 with IASTFragment

use of org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment in project che by eclipse.

the class ExtractConstantRefactoring method replaceExpressionsWithConstant.

private void replaceExpressionsWithConstant() throws JavaModelException {
    ASTRewrite astRewrite = fCuRewrite.getASTRewrite();
    AST ast = astRewrite.getAST();
    IASTFragment[] fragmentsToReplace = getFragmentsToReplace();
    for (int i = 0; i < fragmentsToReplace.length; i++) {
        IASTFragment fragment = fragmentsToReplace[i];
        ASTNode node = fragment.getAssociatedNode();
        boolean inTypeDeclarationAnnotation = isInTypeDeclarationAnnotation(node);
        if (inTypeDeclarationAnnotation && JdtFlags.VISIBILITY_STRING_PRIVATE == getVisibility())
            continue;
        SimpleName ref = ast.newSimpleName(fConstantName);
        Name replacement = ref;
        boolean qualifyReference = qualifyReferencesWithDeclaringClassName();
        if (!qualifyReference) {
            qualifyReference = inTypeDeclarationAnnotation;
        }
        if (qualifyReference) {
            replacement = ast.newQualifiedName(ast.newSimpleName(getContainingTypeBinding().getName()), ref);
        }
        TextEditGroup description = fCuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractConstantRefactoring_replace);
        fragment.replace(astRewrite, replacement, description);
        if (fLinkedProposalModel != null)
            fLinkedProposalModel.getPositionGroup(KEY_NAME, true).addPosition(astRewrite.track(ref), false);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) IASTFragment(org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Aggregations

IASTFragment (org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)2 TextEditGroup (org.eclipse.text.edits.TextEditGroup)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 SourceRange (org.eclipse.jdt.core.SourceRange)1 AST (org.eclipse.jdt.core.dom.AST)1 Assignment (org.eclipse.jdt.core.dom.Assignment)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 Expression (org.eclipse.jdt.core.dom.Expression)1 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)1 Name (org.eclipse.jdt.core.dom.Name)1 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)1 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)1 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)1 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)1 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)1 IExpressionFragment (org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment)1