Search in sources :

Example 1 with IExpressionFragment

use of org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment in project che 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.internal.corext.dom.fragments.IExpressionFragment) Expression(org.eclipse.jdt.core.dom.Expression)

Example 2 with IExpressionFragment

use of org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment in project che 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) {
            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, null, nameSuggestions.length + 1);
        }
        for (int i = 0; i < nameSuggestions.length; i++) {
            nameGroup.addProposal(nameSuggestions[i], null, 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.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.internal.corext.fix.LinkedProposalPositionGroup) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 3 with IExpressionFragment

use of org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment in project che 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.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 4 with IExpressionFragment

use of org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment in project che 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.internal.corext.dom.fragments.IExpressionFragment) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 5 with IExpressionFragment

use of org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment 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)

Aggregations

IExpressionFragment (org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment)10 Expression (org.eclipse.jdt.core.dom.Expression)6 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 CastExpression (org.eclipse.jdt.core.dom.CastExpression)3 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)3 AST (org.eclipse.jdt.core.dom.AST)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)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 ASTNode (org.eclipse.jdt.core.dom.ASTNode)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 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)1 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)1