Search in sources :

Example 6 with IExpressionFragment

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

Example 7 with IExpressionFragment

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

Example 8 with IExpressionFragment

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

the class InlineConstantRefactoring method checkInitializer.

private RefactoringStatus checkInitializer() {
    Expression initializer = getInitializer();
    if (initializer == null)
        return RefactoringStatus.createStatus(RefactoringStatus.FATAL, RefactoringCoreMessages.InlineConstantRefactoring_blank_finals, null, Corext.getPluginId(), RefactoringStatusCodes.CANNOT_INLINE_BLANK_FINAL, null);
    fInitializerAllStaticFinal = ConstantChecks.isStaticFinalConstant((IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(initializer));
    fInitializerChecked = true;
    return new RefactoringStatus();
}
Also used : IExpressionFragment(org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 9 with IExpressionFragment

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

use of org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment in project che 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);
    }
    return fConstantTypeCache;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) IExpressionFragment(org.eclipse.jdt.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.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

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