Search in sources :

Example 1 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 isLiteralNodeSelected.

// !!! -- same as in ExtractTempRefactoring
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)

Example 2 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 checkExpression.

private RefactoringStatus checkExpression() throws JavaModelException {
    RefactoringStatus result = new RefactoringStatus();
    result.merge(checkExpressionBinding());
    if (result.hasFatalError()) {
        return result;
    }
    checkAllStaticFinal();
    IExpressionFragment selectedExpression = getSelectedExpression();
    Expression associatedExpression = selectedExpression.getAssociatedExpression();
    if (associatedExpression instanceof NullLiteral) {
        result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_null_literals));
    } else if (!ConstantChecks.isLoadTimeConstant(selectedExpression)) {
        result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_not_load_time_constant));
    } else if (associatedExpression instanceof SimpleName) {
        if (associatedExpression.getParent() instanceof QualifiedName && associatedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || associatedExpression.getParent() instanceof FieldAccess && associatedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) {
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_select_expression);
        }
    }
    return result;
}
Also used : IExpressionFragment(org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment) Expression(org.eclipse.jdt.core.dom.Expression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral)

Example 3 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 createConstantDeclaration.

private void createConstantDeclaration() throws CoreException {
    Type type = getConstantType();
    IExpressionFragment fragment = getSelectedExpression();
    Expression initializer = getSelectedExpression().createCopyTarget(fCuRewrite.getASTRewrite(), true);
    AST ast = fCuRewrite.getAST();
    VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
    variableDeclarationFragment.setName(ast.newSimpleName(fConstantName));
    variableDeclarationFragment.setInitializer(initializer);
    FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment);
    fieldDeclaration.setType(type);
    Modifier.ModifierKeyword accessModifier = Modifier.ModifierKeyword.toKeyword(fVisibility);
    if (accessModifier != null) {
        fieldDeclaration.modifiers().add(ast.newModifier(accessModifier));
    }
    fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
    fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
    // boolean createComments = JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaProject()).createComments;
    // if (createComments) {
    String comment = CodeGeneration.getFieldComment(fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu));
    if (comment != null && comment.length() > 0 && !"/**\n *\n */\n".equals(comment)) {
        Javadoc doc = (Javadoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
        fieldDeclaration.setJavadoc(doc);
    }
    // }
    AbstractTypeDeclaration parent = getContainingTypeDeclarationNode();
    ListRewrite listRewrite = fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty());
    TextEditGroup msg = fCuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant);
    if (insertFirst()) {
        listRewrite.insertFirst(fieldDeclaration, msg);
    } else {
        listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg);
    }
    if (fLinkedProposalModel != null) {
        ASTRewrite rewrite = fCuRewrite.getASTRewrite();
        LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
        nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true);
        String[] nameSuggestions = guessConstantNames();
        if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) {
            nameGroup.addProposal(fConstantName, nameSuggestions.length + 1);
        }
        for (int i = 0; i < nameSuggestions.length; i++) {
            nameGroup.addProposal(nameSuggestions[i], nameSuggestions.length - i);
        }
        LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
        typeGroup.addPosition(rewrite.track(type), true);
        ITypeBinding typeBinding = guessBindingForReference(fragment.getAssociatedExpression());
        if (typeBinding != null) {
            ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
            for (int i = 0; i < relaxingTypes.length; i++) {
                typeGroup.addProposal(relaxingTypes[i], fCuRewrite.getCu(), relaxingTypes.length - i);
            }
        }
        boolean isInterface = parent.resolveBinding() != null && parent.resolveBinding().isInterface();
        ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), isInterface);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) Type(org.eclipse.jdt.core.dom.Type) IExpressionFragment(org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) TextEditGroup(org.eclipse.text.edits.TextEditGroup) LinkedProposalPositionGroup(org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalPositionGroup) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 4 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 checkSelection.

private RefactoringStatus checkSelection(IProgressMonitor pm) throws JavaModelException {
    try {
        // $NON-NLS-1$
        pm.beginTask("", 2);
        IExpressionFragment selectedExpression = getSelectedExpression();
        if (selectedExpression == null) {
            String message = RefactoringCoreMessages.ExtractConstantRefactoring_select_expression;
            return CodeRefactoringUtil.checkMethodSyntaxErrors(fSelectionStart, fSelectionLength, fCuRewrite.getRoot(), message);
        }
        pm.worked(1);
        RefactoringStatus result = new RefactoringStatus();
        result.merge(checkExpression());
        if (result.hasFatalError()) {
            return result;
        }
        pm.worked(1);
        return result;
    } finally {
        pm.done();
    }
}
Also used : IExpressionFragment(org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 5 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 getExcludedVariableNames.

private String[] getExcludedVariableNames() {
    if (fExcludedVariableNames == null) {
        try {
            IExpressionFragment expr = getSelectedExpression();
            Collection<String> takenNames = new ScopeAnalyzer(fCuRewrite.getRoot()).getUsedVariableNames(expr.getStartPosition(), expr.getLength());
            fExcludedVariableNames = takenNames.toArray(new String[takenNames.size()]);
        } catch (JavaModelException e) {
            fExcludedVariableNames = new String[0];
        }
    }
    return fExcludedVariableNames;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IExpressionFragment(org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer)

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