Search in sources :

Example 46 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project flux by eclipse.

the class ModifierCorrectionSubProcessor method addNonAccessibleReferenceProposal.

public static void addNonAccessibleReferenceProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, int kind, int relevance) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode == null) {
        return;
    }
    IBinding binding = null;
    switch(selectedNode.getNodeType()) {
        case ASTNode.SIMPLE_NAME:
            binding = ((SimpleName) selectedNode).resolveBinding();
            break;
        case ASTNode.QUALIFIED_NAME:
            binding = ((QualifiedName) selectedNode).resolveBinding();
            break;
        case ASTNode.SIMPLE_TYPE:
            binding = ((SimpleType) selectedNode).resolveBinding();
            break;
        case ASTNode.NAME_QUALIFIED_TYPE:
            binding = ((NameQualifiedType) selectedNode).resolveBinding();
            break;
        case ASTNode.METHOD_INVOCATION:
            binding = ((MethodInvocation) selectedNode).getName().resolveBinding();
            break;
        case ASTNode.SUPER_METHOD_INVOCATION:
            binding = ((SuperMethodInvocation) selectedNode).getName().resolveBinding();
            break;
        case ASTNode.FIELD_ACCESS:
            binding = ((FieldAccess) selectedNode).getName().resolveBinding();
            break;
        case ASTNode.SUPER_FIELD_ACCESS:
            binding = ((SuperFieldAccess) selectedNode).getName().resolveBinding();
            break;
        case ASTNode.CLASS_INSTANCE_CREATION:
            binding = ((ClassInstanceCreation) selectedNode).resolveConstructorBinding();
            break;
        case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
            binding = ((SuperConstructorInvocation) selectedNode).resolveConstructorBinding();
            break;
        default:
            return;
    }
    ITypeBinding typeBinding = null;
    String name;
    IBinding bindingDecl;
    boolean isLocalVar = false;
    if (binding instanceof IVariableBinding && problem.getProblemId() == IProblem.NotVisibleType) {
        binding = ((IVariableBinding) binding).getType();
    }
    if (binding instanceof IMethodBinding && problem.getProblemId() == IProblem.NotVisibleType) {
        binding = ((IMethodBinding) binding).getReturnType();
    }
    if (binding instanceof IMethodBinding) {
        IMethodBinding methodDecl = (IMethodBinding) binding;
        if (methodDecl.isDefaultConstructor()) {
            //UnresolvedElementsSubProcessor.getConstructorProposals(context, problem, proposals);
            return;
        }
        bindingDecl = methodDecl.getMethodDeclaration();
        typeBinding = methodDecl.getDeclaringClass();
        //$NON-NLS-1$
        name = BasicElementLabels.getJavaElementName(methodDecl.getName() + "()");
    } else if (binding instanceof IVariableBinding) {
        IVariableBinding varDecl = (IVariableBinding) binding;
        typeBinding = varDecl.getDeclaringClass();
        name = BasicElementLabels.getJavaElementName(binding.getName());
        isLocalVar = !varDecl.isField();
        bindingDecl = varDecl.getVariableDeclaration();
    } else if (binding instanceof ITypeBinding) {
        typeBinding = (ITypeBinding) binding;
        bindingDecl = typeBinding.getTypeDeclaration();
        name = BasicElementLabels.getJavaElementName(binding.getName());
    } else {
        return;
    }
    if (typeBinding != null && typeBinding.isFromSource() || isLocalVar) {
        int includedModifiers = 0;
        int excludedModifiers = 0;
        String label;
        switch(kind) {
            case TO_VISIBLE:
                excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
                includedModifiers = getNeededVisibility(selectedNode, typeBinding, binding);
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(includedModifiers) });
                break;
            case TO_STATIC:
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostatic_description, name);
                includedModifiers = Modifier.STATIC;
                if (bindingDecl.getKind() == IBinding.METHOD) {
                    excludedModifiers = Modifier.DEFAULT | Modifier.ABSTRACT;
                }
                break;
            case TO_NON_STATIC:
                if (typeBinding != null && typeBinding.isInterface())
                    return;
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononstatic_description, name);
                excludedModifiers = Modifier.STATIC;
                break;
            case TO_NON_PRIVATE:
                int visibility;
                if (cu.getParent().getElementName().equals(typeBinding.getPackage().getName())) {
                    visibility = Modifier.NONE;
                    excludedModifiers = Modifier.PRIVATE;
                } else {
                    visibility = Modifier.PUBLIC;
                    includedModifiers = Modifier.PUBLIC;
                    excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
                }
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(visibility) });
                break;
            case TO_NON_FINAL:
                if (typeBinding != null && typeBinding.isInterface())
                    return;
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononfinal_description, name);
                excludedModifiers = Modifier.FINAL;
                break;
            default:
                //$NON-NLS-1$
                throw new IllegalArgumentException("not supported");
        }
        ICompilationUnit targetCU = isLocalVar ? cu : ASTResolving.findCompilationUnitForBinding(cu, context.getASTRoot(), typeBinding.getTypeDeclaration());
        if (targetCU != null) {
            //Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            proposals.add(new ModifierChangeCorrectionProposal(label, targetCU, bindingDecl, selectedNode, includedModifiers, excludedModifiers, relevance));
        }
    }
    if (kind == TO_VISIBLE && bindingDecl.getKind() == IBinding.VARIABLE) {
    //			UnresolvedElementsSubProcessor.getVariableProposals(context, problem, (IVariableBinding) bindingDecl, proposals);
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IBinding(org.eclipse.jdt.core.dom.IBinding) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess)

Example 47 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project flux by eclipse.

the class ModifierCorrectionSubProcessor method addRemoveInvalidModifiersProposal.

public static void addRemoveInvalidModifiersProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, int relevance) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode instanceof MethodDeclaration) {
        selectedNode = ((MethodDeclaration) selectedNode).getName();
    }
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    IBinding binding = ((SimpleName) selectedNode).resolveBinding();
    if (binding != null) {
        String methodName = BasicElementLabels.getJavaElementName(binding.getName());
        String label = null;
        int problemId = problem.getProblemId();
        int excludedModifiers = 0;
        int includedModifiers = 0;
        switch(problemId) {
            case IProblem.CannotHideAnInstanceMethodWithAStaticMethod:
            case IProblem.UnexpectedStaticModifierForMethod:
                excludedModifiers = Modifier.STATIC;
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononstatic_description, methodName);
                break;
            case IProblem.UnexpectedStaticModifierForField:
                excludedModifiers = Modifier.STATIC;
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changefieldmodifiertononstatic_description, methodName);
                break;
            case IProblem.IllegalModifierCombinationFinalVolatileForField:
                excludedModifiers = Modifier.VOLATILE;
                label = CorrectionMessages.ModifierCorrectionSubProcessor_removevolatile_description;
                break;
            case IProblem.IllegalModifierForInterfaceMethod:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT);
                break;
            case IProblem.IllegalModifierForInterfaceMethod18:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STRICTFP | Modifier.DEFAULT | Modifier.STATIC);
                if (Modifier.isAbstract(binding.getModifiers())) {
                    excludedModifiers = excludedModifiers | Modifier.STRICTFP;
                }
                break;
            case IProblem.IllegalModifierForInterface:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForClass:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForInterfaceField:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
                break;
            case IProblem.IllegalModifierForMemberInterface:
            case IProblem.IllegalVisibilityModifierForInterfaceMemberType:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.STATIC | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForMemberClass:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForLocalClass:
                excludedModifiers = ~(Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForArgument:
                excludedModifiers = ~Modifier.FINAL;
                break;
            case IProblem.IllegalModifierForField:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL | Modifier.VOLATILE | Modifier.TRANSIENT);
                break;
            case IProblem.IllegalModifierForMethod:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.NATIVE | Modifier.STRICTFP | Modifier.SYNCHRONIZED);
                break;
            case IProblem.IllegalModifierForConstructor:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE);
                break;
            case IProblem.IllegalModifierForVariable:
                excludedModifiers = ~Modifier.FINAL;
                break;
            case IProblem.IllegalModifierForEnum:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForEnumConstant:
                excludedModifiers = ~Modifier.NONE;
                break;
            case IProblem.IllegalModifierForEnumConstructor:
                excludedModifiers = ~Modifier.PRIVATE;
                break;
            case IProblem.IllegalModifierForMemberEnum:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED | Modifier.STATIC | Modifier.STRICTFP);
                break;
            default:
                //$NON-NLS-1$
                Assert.isTrue(false, "not supported");
                return;
        }
        if (label == null)
            label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_removeinvalidmodifiers_description, methodName);
        //			Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, includedModifiers, excludedModifiers, relevance));
        if (problemId == IProblem.IllegalModifierCombinationFinalVolatileForField) {
            proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_removefinal_description, cu, binding, selectedNode, 0, Modifier.FINAL, relevance + 1));
        }
        if (problemId == IProblem.UnexpectedStaticModifierForField && binding instanceof IVariableBinding) {
            ITypeBinding declClass = ((IVariableBinding) binding).getDeclaringClass();
            if (declClass.isMember()) {
                proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostaticfinal_description, cu, binding, selectedNode, Modifier.FINAL, Modifier.VOLATILE, relevance + 1));
                ASTNode parentType = context.getASTRoot().findDeclaringNode(declClass);
                if (parentType != null) {
                    proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_addstatictoparenttype_description, cu, declClass, parentType, Modifier.STATIC, 0, relevance - 1));
                }
            }
        }
        if (problemId == IProblem.UnexpectedStaticModifierForMethod && binding instanceof IMethodBinding) {
            ITypeBinding declClass = ((IMethodBinding) binding).getDeclaringClass();
            if (declClass.isMember()) {
                ASTNode parentType = context.getASTRoot().findDeclaringNode(declClass);
                if (parentType != null) {
                    proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_addstatictoparenttype_description, cu, declClass, parentType, Modifier.STATIC, 0, relevance - 1));
                }
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 48 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project flux by eclipse.

the class ModifierCorrectionSubProcessor method addNeedToEmulateProposal.

public static void addNeedToEmulateProposal(IInvocationContext context, IProblemLocation problem, Collection<ModifierChangeCorrectionProposal> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    IBinding binding = ((SimpleName) selectedNode).resolveBinding();
    if (binding instanceof IVariableBinding) {
        binding = ((IVariableBinding) binding).getVariableDeclaration();
        //			Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertofinal_description, BasicElementLabels.getJavaElementName(binding.getName()));
        proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, Modifier.FINAL, 0, IProposalRelevance.CHANGE_MODIFIER_OF_VARIABLE_TO_FINAL));
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 49 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project flux by eclipse.

the class AdvancedQuickAssistProcessor method getReplaceIfElseWithConditionalProposals.

private static boolean getReplaceIfElseWithConditionalProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    if (!(node instanceof IfStatement)) {
        return false;
    }
    IfStatement ifStatement = (IfStatement) node;
    Statement thenStatement = getSingleStatement(ifStatement.getThenStatement());
    Statement elseStatement = getSingleStatement(ifStatement.getElseStatement());
    if (thenStatement == null || elseStatement == null) {
        return false;
    }
    Expression assigned = null;
    Expression thenExpression = null;
    Expression elseExpression = null;
    ITypeBinding exprBinding = null;
    if (thenStatement instanceof ReturnStatement && elseStatement instanceof ReturnStatement) {
        thenExpression = ((ReturnStatement) thenStatement).getExpression();
        elseExpression = ((ReturnStatement) elseStatement).getExpression();
        MethodDeclaration declaration = ASTResolving.findParentMethodDeclaration(node);
        if (declaration == null || declaration.isConstructor()) {
            return false;
        }
        exprBinding = declaration.getReturnType2().resolveBinding();
    } else if (thenStatement instanceof ExpressionStatement && elseStatement instanceof ExpressionStatement) {
        Expression inner1 = ((ExpressionStatement) thenStatement).getExpression();
        Expression inner2 = ((ExpressionStatement) elseStatement).getExpression();
        if (inner1 instanceof Assignment && inner2 instanceof Assignment) {
            Assignment assign1 = (Assignment) inner1;
            Assignment assign2 = (Assignment) inner2;
            Expression left1 = assign1.getLeftHandSide();
            Expression left2 = assign2.getLeftHandSide();
            if (left1 instanceof Name && left2 instanceof Name && assign1.getOperator() == assign2.getOperator()) {
                IBinding bind1 = ((Name) left1).resolveBinding();
                IBinding bind2 = ((Name) left2).resolveBinding();
                if (bind1 == bind2 && bind1 instanceof IVariableBinding) {
                    assigned = left1;
                    exprBinding = ((IVariableBinding) bind1).getType();
                    thenExpression = assign1.getRightHandSide();
                    elseExpression = assign2.getRightHandSide();
                }
            }
        }
    }
    if (thenExpression == null || elseExpression == null) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    //
    AST ast = node.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    //		TightSourceRangeComputer sourceRangeComputer= new TightSourceRangeComputer();
    //		sourceRangeComputer.addTightSourceNode(ifStatement);
    //		rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_replaceIfWithConditional;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_IF_ELSE_WITH_CONDITIONAL);
    // prepare conditional expression
    ConditionalExpression conditionalExpression = ast.newConditionalExpression();
    Expression conditionCopy = (Expression) rewrite.createCopyTarget(ifStatement.getExpression());
    conditionalExpression.setExpression(conditionCopy);
    Expression thenCopy = (Expression) rewrite.createCopyTarget(thenExpression);
    Expression elseCopy = (Expression) rewrite.createCopyTarget(elseExpression);
    IJavaProject project = context.getCompilationUnit().getJavaProject();
    if (!JavaModelUtil.is50OrHigher(project)) {
        ITypeBinding thenBinding = thenExpression.resolveTypeBinding();
        ITypeBinding elseBinding = elseExpression.resolveTypeBinding();
        if (thenBinding != null && elseBinding != null && exprBinding != null && !elseBinding.isAssignmentCompatible(thenBinding)) {
            CastExpression castException = ast.newCastExpression();
            ImportRewrite importRewrite = proposal.createImportRewrite(context.getASTRoot());
            ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, importRewrite);
            castException.setType(importRewrite.addImport(exprBinding, ast, importRewriteContext));
            castException.setExpression(elseCopy);
            elseCopy = castException;
        }
    } else if (JavaModelUtil.is17OrHigher(project)) {
        addExplicitTypeArgumentsIfNecessary(rewrite, proposal, thenExpression);
        addExplicitTypeArgumentsIfNecessary(rewrite, proposal, elseExpression);
    }
    conditionalExpression.setThenExpression(thenCopy);
    conditionalExpression.setElseExpression(elseCopy);
    // replace 'if' statement with conditional expression
    if (assigned == null) {
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(conditionalExpression);
        rewrite.replace(ifStatement, returnStatement, null);
    } else {
        Assignment assignment = ast.newAssignment();
        assignment.setLeftHandSide((Expression) rewrite.createCopyTarget(assigned));
        assignment.setRightHandSide(conditionalExpression);
        assignment.setOperator(((Assignment) assigned.getParent()).getOperator());
        ExpressionStatement expressionStatement = ast.newExpressionStatement(assignment);
        rewrite.replace(ifStatement, expressionStatement, null);
    }
    // add correction proposal
    resultingCollections.add(proposal);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) IBinding(org.eclipse.jdt.core.dom.IBinding) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) Assignment(org.eclipse.jdt.core.dom.Assignment) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) IJavaProject(org.eclipse.jdt.core.IJavaProject) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 50 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project flux by eclipse.

the class QuickAssistProcessor method getJoinVariableProposals.

private static boolean getJoinVariableProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    ASTNode parent = node.getParent();
    VariableDeclarationFragment fragment = null;
    boolean onFirstAccess = false;
    if (node instanceof SimpleName && node.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY) {
        onFirstAccess = true;
        SimpleName name = (SimpleName) node;
        IBinding binding = name.resolveBinding();
        if (!(binding instanceof IVariableBinding)) {
            return false;
        }
        ASTNode declaring = context.getASTRoot().findDeclaringNode(binding);
        if (declaring instanceof VariableDeclarationFragment) {
            fragment = (VariableDeclarationFragment) declaring;
        } else {
            return false;
        }
    } else if (parent instanceof VariableDeclarationFragment) {
        fragment = (VariableDeclarationFragment) parent;
    } else {
        return false;
    }
    IVariableBinding binding = fragment.resolveBinding();
    Expression initializer = fragment.getInitializer();
    if ((initializer != null && initializer.getNodeType() != ASTNode.NULL_LITERAL) || binding == null || binding.isField()) {
        return false;
    }
    if (!(fragment.getParent() instanceof VariableDeclarationStatement)) {
        return false;
    }
    VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
    SimpleName[] names = LinkedNodeFinder.findByBinding(statement.getParent(), binding);
    if (names.length <= 1 || names[0] != fragment.getName()) {
        return false;
    }
    SimpleName firstAccess = names[1];
    if (onFirstAccess) {
        if (firstAccess != node) {
            return false;
        }
    } else {
        if (firstAccess.getLocationInParent() != Assignment.LEFT_HAND_SIDE_PROPERTY) {
            return false;
        }
    }
    Assignment assignment = (Assignment) firstAccess.getParent();
    if (assignment.getLocationInParent() != ExpressionStatement.EXPRESSION_PROPERTY) {
        return false;
    }
    ExpressionStatement assignParent = (ExpressionStatement) assignment.getParent();
    if (resultingCollections == null) {
        return true;
    }
    AST ast = statement.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    //		TightSourceRangeComputer sourceRangeComputer= new TightSourceRangeComputer();
    //		sourceRangeComputer.addTightSourceNode(assignParent);
    //		rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
    String label = CorrectionMessages.QuickAssistProcessor_joindeclaration_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_VARIABLE_DECLARATION);
    proposal.setCommandId(SPLIT_JOIN_VARIABLE_DECLARATION_ID);
    Expression placeholder = (Expression) rewrite.createMoveTarget(assignment.getRightHandSide());
    rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, placeholder, null);
    if (onFirstAccess) {
        // replace assignment with variable declaration
        rewrite.replace(assignParent, rewrite.createMoveTarget(statement), null);
    } else {
        // different scopes -> remove assignments, set variable initializer
        if (ASTNodes.isControlStatementBody(assignParent.getLocationInParent())) {
            Block block = ast.newBlock();
            rewrite.replace(assignParent, block, null);
        } else {
            rewrite.remove(assignParent, null);
        }
    }
    proposal.setEndPosition(rewrite.track(fragment.getName()));
    resultingCollections.add(proposal);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Assignment(org.eclipse.jdt.core.dom.Assignment) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block)

Aggregations

IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)106 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)49 IBinding (org.eclipse.jdt.core.dom.IBinding)48 SimpleName (org.eclipse.jdt.core.dom.SimpleName)43 ASTNode (org.eclipse.jdt.core.dom.ASTNode)30 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)29 Expression (org.eclipse.jdt.core.dom.Expression)24 ArrayList (java.util.ArrayList)22 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 Name (org.eclipse.jdt.core.dom.Name)13 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)13 Assignment (org.eclipse.jdt.core.dom.Assignment)12 CastExpression (org.eclipse.jdt.core.dom.CastExpression)12 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)12 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)12 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)12 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)11 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)11 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)11