Search in sources :

Example 6 with IExpressionFragment

use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls by eclipse.

the class ExtractTempRefactoring method shouldReplaceSelectedExpressionWithTempDeclaration.

private boolean shouldReplaceSelectedExpressionWithTempDeclaration() throws JavaModelException {
    IExpressionFragment selectedFragment = getSelectedExpression();
    IExpressionFragment firstExpression = getFirstReplacedExpression();
    if (firstExpression.getStartPosition() < selectedFragment.getStartPosition()) {
        return false;
    }
    ASTNode associatedNode = selectedFragment.getAssociatedNode();
    return (associatedNode.getParent() instanceof ExpressionStatement || associatedNode.getParent() instanceof LambdaExpression) && selectedFragment.matches(ASTFragmentFactory.createFragmentForFullSubtree(associatedNode));
}
Also used : IExpressionFragment(org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 7 with IExpressionFragment

use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls by eclipse.

the class ExtractConstantRefactoring method getConstantType.

private Type getConstantType() throws JavaModelException {
    if (fConstantTypeCache == null) {
        IExpressionFragment fragment = getSelectedExpression();
        ITypeBinding typeBinding = guessBindingForReference(fragment.getAssociatedExpression());
        AST ast = fCuRewrite.getAST();
        typeBinding = Bindings.normalizeForDeclarationUse(typeBinding, ast);
        ImportRewrite importRewrite = fCuRewrite.getImportRewrite();
        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fCuRewrite.getRoot(), fSelectionStart, importRewrite);
        fConstantTypeCache = importRewrite.addImport(typeBinding, ast, context, TypeLocation.FIELD);
    }
    return fConstantTypeCache;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) IExpressionFragment(org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 8 with IExpressionFragment

use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls by eclipse.

the class ExtractTempRefactoring method checkSelection.

private RefactoringStatus checkSelection(IProgressMonitor pm) throws JavaModelException {
    try {
        // $NON-NLS-1$
        pm.beginTask("", 8);
        IExpressionFragment selectedExpression = getSelectedExpression();
        if (selectedExpression == null) {
            String message = RefactoringCoreMessages.ExtractTempRefactoring_select_expression;
            return CodeRefactoringUtil.checkMethodSyntaxErrors(fSelectionStart, fSelectionLength, fCompilationUnitNode, message);
        }
        pm.worked(1);
        if (isUsedInExplicitConstructorCall()) {
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_explicit_constructor);
        }
        pm.worked(1);
        ASTNode associatedNode = selectedExpression.getAssociatedNode();
        if (getEnclosingBodyNode() == null || ASTNodes.getParent(associatedNode, Annotation.class) != null) {
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_expr_in_method_or_initializer);
        }
        pm.worked(1);
        if (associatedNode instanceof Name && associatedNode.getParent() instanceof ClassInstanceCreation && associatedNode.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_name_in_new);
        }
        pm.worked(1);
        RefactoringStatus result = new RefactoringStatus();
        result.merge(checkExpression());
        if (result.hasFatalError()) {
            return result;
        }
        pm.worked(1);
        result.merge(checkExpressionFragmentIsRValue());
        if (result.hasFatalError()) {
            return result;
        }
        pm.worked(1);
        if (isUsedInForInitializerOrUpdater(getSelectedExpression().getAssociatedExpression())) {
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_for_initializer_updater);
        }
        pm.worked(1);
        if (isReferringToLocalVariableFromFor(getSelectedExpression().getAssociatedExpression())) {
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_refers_to_for_variable);
        }
        pm.worked(1);
        return result;
    } finally {
        pm.done();
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IExpressionFragment(org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 9 with IExpressionFragment

use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls 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.ls.core.internal.corext.dom.fragments.IASTFragment) IExpressionFragment(org.eclipse.jdt.ls.core.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) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) 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 10 with IExpressionFragment

use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls by eclipse.

the class ExtractTempRefactoring method isLiteralNodeSelected.

private boolean isLiteralNodeSelected() throws JavaModelException {
    IExpressionFragment fragment = getSelectedExpression();
    if (fragment == null) {
        return false;
    }
    Expression expression = fragment.getAssociatedExpression();
    if (expression == null) {
        return false;
    }
    switch(expression.getNodeType()) {
        case ASTNode.BOOLEAN_LITERAL:
        case ASTNode.CHARACTER_LITERAL:
        case ASTNode.NULL_LITERAL:
        case ASTNode.NUMBER_LITERAL:
            return true;
        default:
            return false;
    }
}
Also used : IExpressionFragment(org.eclipse.jdt.ls.core.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) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression)

Aggregations

IExpressionFragment (org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment)10 Expression (org.eclipse.jdt.core.dom.Expression)5 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)3 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)3 AST (org.eclipse.jdt.core.dom.AST)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 CastExpression (org.eclipse.jdt.core.dom.CastExpression)2 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)2 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)2 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 SourceRange (org.eclipse.jdt.core.SourceRange)1 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)1 Assignment (org.eclipse.jdt.core.dom.Assignment)1 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)1