Search in sources :

Example 1 with AssignToVariableAssistProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.AssignToVariableAssistProposal in project eclipse.jdt.ls by eclipse.

the class RefactorProposalUtility method getAssignFieldProposal.

public static CUCorrectionProposal getAssignFieldProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, boolean returnAsCommand, IProblemLocationCore[] locations) throws CoreException {
    ASTNode node = context.getCoveringNode();
    Statement statement = ASTResolving.findParentStatement(node);
    if (!(statement instanceof ExpressionStatement)) {
        return null;
    }
    ExpressionStatement expressionStatement = (ExpressionStatement) statement;
    Expression expression = expressionStatement.getExpression();
    if (expression.getNodeType() == ASTNode.ASSIGNMENT) {
        return null;
    }
    ITypeBinding typeBinding = expression.resolveTypeBinding();
    typeBinding = Bindings.normalizeTypeBinding(typeBinding);
    if (typeBinding == null) {
        return null;
    }
    if (containsMatchingProblem(locations, IProblem.UnusedObjectAllocation)) {
        return null;
    }
    final ICompilationUnit cu = context.getCompilationUnit();
    ASTNode type = ASTResolving.findParentType(expression);
    if (type != null) {
        int relevance;
        if (context.getSelectionLength() == 0) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ZERO_SELECTION;
        } else if (problemsAtLocation) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ERROR;
        } else {
            relevance = IProposalRelevance.EXTRACT_LOCAL;
        }
        if (returnAsCommand) {
            return new AssignToVariableAssistCommandProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_FIELD, AssignToVariableAssistProposal.FIELD, expressionStatement, typeBinding, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(ASSIGN_FIELD_COMMAND, params));
        } else {
            return new AssignToVariableAssistProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_FIELD, AssignToVariableAssistProposal.FIELD, expressionStatement, typeBinding, relevance);
        }
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) Statement(org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AssignToVariableAssistProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.AssignToVariableAssistProposal)

Example 2 with AssignToVariableAssistProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.AssignToVariableAssistProposal in project eclipse.jdt.ls by eclipse.

the class QuickAssistProcessor method getAssignAllParamsToFieldsProposals.

private static boolean getAssignAllParamsToFieldsProposals(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) {
    node = ASTNodes.getNormalizedNode(node);
    ASTNode parent = node.getParent();
    if (!(parent instanceof SingleVariableDeclaration) || !(parent.getParent() instanceof MethodDeclaration)) {
        return false;
    }
    MethodDeclaration methodDecl = (MethodDeclaration) parent.getParent();
    if (methodDecl.getBody() == null) {
        return false;
    }
    List<SingleVariableDeclaration> parameters = methodDecl.parameters();
    if (parameters.size() <= 1) {
        return false;
    }
    ITypeBinding parentType = Bindings.getBindingOfParentType(node);
    if (parentType == null || parentType.isInterface()) {
        return false;
    }
    for (SingleVariableDeclaration param : parameters) {
        IVariableBinding binding = param.resolveBinding();
        if (binding == null || binding.getType() == null) {
            return false;
        }
    }
    if (resultingCollections == null) {
        return true;
    }
    AssignToVariableAssistProposal fieldProposal = new AssignToVariableAssistProposal(context.getCompilationUnit(), parameters, IProposalRelevance.ASSIGN_ALL_PARAMS_TO_NEW_FIELDS);
    resultingCollections.add(fieldProposal);
    return true;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AssignToVariableAssistProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.AssignToVariableAssistProposal) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 3 with AssignToVariableAssistProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.AssignToVariableAssistProposal in project eclipse.jdt.ls by eclipse.

the class QuickAssistProcessor method getAssignParamToFieldProposals.

private static boolean getAssignParamToFieldProposals(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) {
    node = ASTNodes.getNormalizedNode(node);
    ASTNode parent = node.getParent();
    if (!(parent instanceof SingleVariableDeclaration) || !(parent.getParent() instanceof MethodDeclaration)) {
        return false;
    }
    SingleVariableDeclaration paramDecl = (SingleVariableDeclaration) parent;
    IVariableBinding binding = paramDecl.resolveBinding();
    MethodDeclaration methodDecl = (MethodDeclaration) parent.getParent();
    if (binding == null || methodDecl.getBody() == null) {
        return false;
    }
    ITypeBinding typeBinding = binding.getType();
    if (typeBinding == null) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    ITypeBinding parentType = Bindings.getBindingOfParentType(node);
    if (parentType != null) {
        if (parentType.isInterface()) {
            return false;
        }
        // assign to existing fields
        CompilationUnit root = context.getASTRoot();
        IVariableBinding[] declaredFields = parentType.getDeclaredFields();
        boolean isStaticContext = ASTResolving.isInStaticContext(node);
        for (int i = 0; i < declaredFields.length; i++) {
            IVariableBinding curr = declaredFields[i];
            if (isStaticContext == Modifier.isStatic(curr.getModifiers()) && typeBinding.isAssignmentCompatible(curr.getType())) {
                ASTNode fieldDeclFrag = root.findDeclaringNode(curr);
                if (fieldDeclFrag instanceof VariableDeclarationFragment) {
                    VariableDeclarationFragment fragment = (VariableDeclarationFragment) fieldDeclFrag;
                    if (fragment.getInitializer() == null) {
                        resultingCollections.add(new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, fragment, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_EXISTING_FIELD));
                    }
                }
            }
        }
    }
    AssignToVariableAssistProposal fieldProposal = new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, null, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_NEW_FIELD);
    resultingCollections.add(fieldProposal);
    return true;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AssignToVariableAssistProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.AssignToVariableAssistProposal) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 4 with AssignToVariableAssistProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.AssignToVariableAssistProposal in project eclipse.jdt.ls by eclipse.

the class RefactorProposalUtility method getAssignVariableProposal.

public static CUCorrectionProposal getAssignVariableProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, boolean returnAsCommand, IProblemLocationCore[] locations) throws CoreException {
    ASTNode node = context.getCoveringNode();
    Statement statement = ASTResolving.findParentStatement(node);
    if (!(statement instanceof ExpressionStatement)) {
        return null;
    }
    ExpressionStatement expressionStatement = (ExpressionStatement) statement;
    Expression expression = expressionStatement.getExpression();
    if (expression.getNodeType() == ASTNode.ASSIGNMENT) {
        return null;
    }
    ITypeBinding typeBinding = expression.resolveTypeBinding();
    typeBinding = Bindings.normalizeTypeBinding(typeBinding);
    if (typeBinding == null) {
        return null;
    }
    if (containsMatchingProblem(locations, IProblem.UnusedObjectAllocation)) {
        return null;
    }
    final ICompilationUnit cu = context.getCompilationUnit();
    int relevance;
    if (context.getSelectionLength() == 0) {
        relevance = IProposalRelevance.EXTRACT_LOCAL_ZERO_SELECTION;
    } else if (problemsAtLocation) {
        relevance = IProposalRelevance.EXTRACT_LOCAL_ERROR;
    } else {
        relevance = IProposalRelevance.EXTRACT_LOCAL;
    }
    if (returnAsCommand) {
        return new AssignToVariableAssistCommandProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_VARIABLE, AssignToVariableAssistProposal.LOCAL, expressionStatement, typeBinding, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(ASSIGN_VARIABLE_COMMAND, params));
    } else {
        return new AssignToVariableAssistProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_VARIABLE, AssignToVariableAssistProposal.LOCAL, expressionStatement, typeBinding, relevance);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) Statement(org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AssignToVariableAssistProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.AssignToVariableAssistProposal)

Aggregations

ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 AssignToVariableAssistProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.AssignToVariableAssistProposal)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 Expression (org.eclipse.jdt.core.dom.Expression)2 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)2 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)2 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)2 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2 Statement (org.eclipse.jdt.core.dom.Statement)2 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)1