Search in sources :

Example 1 with NewVariableCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal in project che by eclipse.

the class UnresolvedElementsSubProcessor method addEnhancedForWithoutTypeProposals.

private static void addEnhancedForWithoutTypeProposals(ICompilationUnit cu, ASTNode selectedNode, Collection<ICommandAccess> proposals) {
    if (selectedNode instanceof SimpleName && (selectedNode.getLocationInParent() == SimpleType.NAME_PROPERTY || selectedNode.getLocationInParent() == NameQualifiedType.NAME_PROPERTY)) {
        ASTNode type = selectedNode.getParent();
        if (type.getLocationInParent() == SingleVariableDeclaration.TYPE_PROPERTY) {
            SingleVariableDeclaration svd = (SingleVariableDeclaration) type.getParent();
            if (svd.getLocationInParent() == EnhancedForStatement.PARAMETER_PROPERTY) {
                if (svd.getName().getLength() == 0) {
                    SimpleName simpleName = (SimpleName) selectedNode;
                    String name = simpleName.getIdentifier();
                    int relevance = StubUtility.hasLocalVariableName(cu.getJavaProject(), name) ? 10 : 7;
                    String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_create_loop_variable_description, BasicElementLabels.getJavaElementName(name));
                    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
                    proposals.add(new NewVariableCorrectionProposal(label, cu, NewVariableCorrectionProposal.LOCAL, simpleName, null, relevance, image));
                }
            }
        }
    }
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NewVariableCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal) Image(org.eclipse.swt.graphics.Image)

Example 2 with NewVariableCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal in project che by eclipse.

the class UnresolvedElementsSubProcessor method addNewVariableProposals.

private static void addNewVariableProposals(ICompilationUnit cu, Name node, SimpleName simpleName, Collection<ICommandAccess> proposals) {
    String name = simpleName.getIdentifier();
    BodyDeclaration bodyDeclaration = ASTResolving.findParentBodyDeclaration(node, true);
    int type = bodyDeclaration.getNodeType();
    if (type == ASTNode.METHOD_DECLARATION) {
        int relevance = StubUtility.hasParameterName(cu.getJavaProject(), name) ? IProposalRelevance.CREATE_PARAMETER_PREFIX_OR_SUFFIX_MATCH : IProposalRelevance.CREATE_PARAMETER;
        String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createparameter_description, BasicElementLabels.getJavaElementName(name));
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
        proposals.add(new NewVariableCorrectionProposal(label, cu, NewVariableCorrectionProposal.PARAM, simpleName, null, relevance, image));
    }
    if (type == ASTNode.INITIALIZER || type == ASTNode.METHOD_DECLARATION && !ASTResolving.isInsideConstructorInvocation((MethodDeclaration) bodyDeclaration, node)) {
        int relevance = StubUtility.hasLocalVariableName(cu.getJavaProject(), name) ? IProposalRelevance.CREATE_LOCAL_PREFIX_OR_SUFFIX_MATCH : IProposalRelevance.CREATE_LOCAL;
        String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createlocal_description, BasicElementLabels.getJavaElementName(name));
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
        proposals.add(new NewVariableCorrectionProposal(label, cu, NewVariableCorrectionProposal.LOCAL, simpleName, null, relevance, image));
    }
    if (node.getParent().getNodeType() == ASTNode.ASSIGNMENT) {
        Assignment assignment = (Assignment) node.getParent();
        if (assignment.getLeftHandSide() == node && assignment.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
            ASTNode statement = assignment.getParent();
            ASTRewrite rewrite = ASTRewrite.create(statement.getAST());
            if (ASTNodes.isControlStatementBody(assignment.getParent().getLocationInParent())) {
                rewrite.replace(statement, rewrite.getAST().newBlock(), null);
            } else {
                rewrite.remove(statement, null);
            }
            String label = CorrectionMessages.UnresolvedElementsSubProcessor_removestatement_description;
            //JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_TOOL_DELETE);
            ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_ASSIGNMENT, image);
            proposals.add(proposal);
        }
    }
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) NewVariableCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) Image(org.eclipse.swt.graphics.Image)

Example 3 with NewVariableCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal in project che by eclipse.

the class LocalCorrectionsSubProcessor method getAssignmentHasNoEffectProposals.

public static void getAssignmentHasNoEffectProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    CompilationUnit root = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(root);
    if (!(selectedNode instanceof Assignment)) {
        return;
    }
    ASTNode assignedNode = ((Assignment) selectedNode).getLeftHandSide();
    ASTNode assignExpression = ((Assignment) selectedNode).getRightHandSide();
    if (!(assignedNode instanceof SimpleName) && !(assignExpression instanceof SimpleName)) {
        return;
    }
    IBinding binding = (assignedNode instanceof SimpleName) ? ((SimpleName) assignedNode).resolveBinding() : ((SimpleName) assignExpression).resolveBinding();
    if (!(binding instanceof IVariableBinding)) {
        return;
    }
    ITypeBinding typeBinding = Bindings.getBindingOfParentType(selectedNode);
    if (typeBinding == null) {
        return;
    }
    IVariableBinding fieldBinding = Bindings.findFieldInHierarchy(typeBinding, binding.getName());
    if (fieldBinding == null || fieldBinding.getDeclaringClass() != typeBinding && Modifier.isPrivate(fieldBinding.getModifiers())) {
        return;
    }
    if (binding != fieldBinding) {
        if (assignedNode instanceof SimpleName) {
            String label = CorrectionMessages.LocalCorrectionsSubProcessor_qualify_left_hand_side_description;
            proposals.add(createNoSideEffectProposal(context, (SimpleName) assignedNode, fieldBinding, label, IProposalRelevance.QUALIFY_LHS));
        }
        if (assignExpression instanceof SimpleName) {
            String label = CorrectionMessages.LocalCorrectionsSubProcessor_qualify_right_hand_side_description;
            proposals.add(createNoSideEffectProposal(context, (SimpleName) assignExpression, fieldBinding, label, IProposalRelevance.QUALIFY_RHS));
        }
    }
    if (binding == fieldBinding && ASTResolving.findParentBodyDeclaration(selectedNode) instanceof MethodDeclaration) {
        SimpleName simpleName = (SimpleName) ((assignedNode instanceof SimpleName) ? assignedNode : assignExpression);
        String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createparameter_description, BasicElementLabels.getJavaElementName(simpleName.getIdentifier()));
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
        proposals.add(new NewVariableCorrectionProposal(label, context.getCompilationUnit(), NewVariableCorrectionProposal.PARAM, simpleName, null, IProposalRelevance.CREATE_PARAMETER, image));
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NewVariableCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal) Image(org.eclipse.swt.graphics.Image)

Example 4 with NewVariableCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal in project che by eclipse.

the class TypeMismatchSubProcessor method addTypeMismatchInForEachProposals.

public static void addTypeMismatchInForEachProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null || selectedNode.getLocationInParent() != EnhancedForStatement.EXPRESSION_PROPERTY) {
        return;
    }
    EnhancedForStatement forStatement = (EnhancedForStatement) selectedNode.getParent();
    ITypeBinding expressionBinding = forStatement.getExpression().resolveTypeBinding();
    if (expressionBinding == null) {
        return;
    }
    ITypeBinding expectedBinding;
    if (expressionBinding.isArray()) {
        expectedBinding = expressionBinding.getComponentType();
    } else {
        //$NON-NLS-1$
        IMethodBinding iteratorMethod = Bindings.findMethodInHierarchy(expressionBinding, "iterator", new String[0]);
        if (iteratorMethod == null) {
            return;
        }
        ITypeBinding[] typeArguments = iteratorMethod.getReturnType().getTypeArguments();
        if (typeArguments.length != 1) {
            return;
        }
        expectedBinding = typeArguments[0];
    }
    AST ast = astRoot.getAST();
    expectedBinding = Bindings.normalizeForDeclarationUse(expectedBinding, ast);
    SingleVariableDeclaration parameter = forStatement.getParameter();
    ICompilationUnit cu = context.getCompilationUnit();
    if (parameter.getName().getLength() == 0) {
        SimpleName simpleName = null;
        if (parameter.getType() instanceof SimpleType) {
            SimpleType type = (SimpleType) parameter.getType();
            if (type.getName() instanceof SimpleName) {
                simpleName = (SimpleName) type.getName();
            }
        } else if (parameter.getType() instanceof NameQualifiedType) {
            simpleName = ((NameQualifiedType) parameter.getType()).getName();
        }
        if (simpleName != null) {
            String name = simpleName.getIdentifier();
            int relevance = StubUtility.hasLocalVariableName(cu.getJavaProject(), name) ? 10 : 7;
            String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_create_loop_variable_description, BasicElementLabels.getJavaElementName(name));
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
            proposals.add(new NewVariableCorrectionProposal(label, cu, NewVariableCorrectionProposal.LOCAL, simpleName, null, relevance, image));
            return;
        }
    }
    String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_incompatible_for_each_type_description, new String[] { BasicElementLabels.getJavaElementName(parameter.getName().getIdentifier()), BindingLabelProvider.getBindingLabel(expectedBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS) });
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewrite rewrite = ASTRewrite.create(ast);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.INCOMPATIBLE_FOREACH_TYPE, image);
    ImportRewrite importRewrite = proposal.createImportRewrite(astRoot);
    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(ASTResolving.findParentBodyDeclaration(selectedNode), importRewrite);
    Type newType = importRewrite.addImport(expectedBinding, ast, importRewriteContext);
    rewrite.replace(parameter.getType(), newType, null);
    proposals.add(proposal);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NewVariableCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType)

Example 5 with NewVariableCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal in project che by eclipse.

the class UnresolvedElementsSubProcessor method addNewFieldForType.

private static void addNewFieldForType(ICompilationUnit targetCU, ITypeBinding binding, ITypeBinding senderDeclBinding, SimpleName simpleName, boolean isWriteAccess, boolean mustBeConst, Collection<ICommandAccess> proposals) {
    String name = simpleName.getIdentifier();
    String nameLabel = BasicElementLabels.getJavaElementName(name);
    String label;
    Image image;
    if (senderDeclBinding.isEnum() && !isWriteAccess) {
        label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createenum_description, new Object[] { nameLabel, ASTResolving.getTypeSignature(senderDeclBinding) });
        image = JavaPluginImages.get(JavaPluginImages.IMG_FIELD_PUBLIC);
        proposals.add(new NewVariableCorrectionProposal(label, targetCU, NewVariableCorrectionProposal.ENUM_CONST, simpleName, senderDeclBinding, 10, image));
    } else {
        if (!mustBeConst) {
            if (binding == null) {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createfield_description, nameLabel);
                image = JavaPluginImages.get(JavaPluginImages.IMG_FIELD_PRIVATE);
            } else {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createfield_other_description, new Object[] { nameLabel, ASTResolving.getTypeSignature(senderDeclBinding) });
                image = JavaPluginImages.get(JavaPluginImages.IMG_FIELD_PUBLIC);
            }
            int fieldRelevance = StubUtility.hasFieldName(targetCU.getJavaProject(), name) ? IProposalRelevance.CREATE_FIELD_PREFIX_OR_SUFFIX_MATCH : IProposalRelevance.CREATE_FIELD;
            proposals.add(new NewVariableCorrectionProposal(label, targetCU, NewVariableCorrectionProposal.FIELD, simpleName, senderDeclBinding, fieldRelevance, image));
        }
        if (!isWriteAccess && !senderDeclBinding.isAnonymous()) {
            if (binding == null) {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createconst_description, nameLabel);
                image = JavaPluginImages.get(JavaPluginImages.IMG_FIELD_PRIVATE);
            } else {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createconst_other_description, new Object[] { nameLabel, ASTResolving.getTypeSignature(senderDeclBinding) });
                image = JavaPluginImages.get(JavaPluginImages.IMG_FIELD_PUBLIC);
            }
            int constRelevance = StubUtility.hasConstantName(targetCU.getJavaProject(), name) ? IProposalRelevance.CREATE_CONSTANT_PREFIX_OR_SUFFIX_MATCH : IProposalRelevance.CREATE_CONSTANT;
            proposals.add(new NewVariableCorrectionProposal(label, targetCU, NewVariableCorrectionProposal.CONST_FIELD, simpleName, senderDeclBinding, constRelevance, image));
        }
    }
}
Also used : NewVariableCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal) Image(org.eclipse.swt.graphics.Image)

Aggregations

NewVariableCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal)5 Image (org.eclipse.swt.graphics.Image)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)2 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)2 AST (org.eclipse.jdt.core.dom.AST)1 Assignment (org.eclipse.jdt.core.dom.Assignment)1 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)1 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)1 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)1 SimpleType (org.eclipse.jdt.core.dom.SimpleType)1 Type (org.eclipse.jdt.core.dom.Type)1 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)1