Search in sources :

Example 1 with ICommandAccess

use of org.eclipse.jdt.ui.text.java.correction.ICommandAccess in project che by eclipse.

the class SuppressWarningsSubProcessor method addSuppressWarningsProposals.

public static void addSuppressWarningsProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    if (problem.isError() && !JavaCore.ENABLED.equals(context.getCompilationUnit().getJavaProject().getOption(JavaCore.COMPILER_PB_SUPPRESS_OPTIONAL_ERRORS, true))) {
        return;
    }
    if (JavaCore.DISABLED.equals(context.getCompilationUnit().getJavaProject().getOption(JavaCore.COMPILER_PB_SUPPRESS_WARNINGS, true))) {
        return;
    }
    String warningToken = CorrectionEngine.getWarningToken(problem.getProblemId());
    if (warningToken == null) {
        return;
    }
    for (Iterator<ICommandAccess> iter = proposals.iterator(); iter.hasNext(); ) {
        Object element = iter.next();
        if (element instanceof SuppressWarningsProposal && warningToken.equals(((SuppressWarningsProposal) element).getWarningToken())) {
            // only one at a time
            return;
        }
    }
    ASTNode node = problem.getCoveringNode(context.getASTRoot());
    if (node == null) {
        return;
    }
    ASTNode target = node;
    int relevance = IProposalRelevance.ADD_SUPPRESSWARNINGS;
    do {
        relevance = addSuppressWarningsProposalIfPossible(context.getCompilationUnit(), target, warningToken, relevance, proposals);
        if (relevance == 0)
            return;
        target = target.getParent();
    } while (target != null);
    ASTNode importStatement = ASTNodes.getParent(node, ImportDeclaration.class);
    if (importStatement != null && !context.getASTRoot().types().isEmpty()) {
        target = (ASTNode) context.getASTRoot().types().get(0);
        if (target != null) {
            addSuppressWarningsProposalIfPossible(context.getCompilationUnit(), target, warningToken, IProposalRelevance.ADD_SUPPRESSWARNINGS, proposals);
        }
    }
}
Also used : ICommandAccess(org.eclipse.jdt.ui.text.java.correction.ICommandAccess) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 2 with ICommandAccess

use of org.eclipse.jdt.ui.text.java.correction.ICommandAccess in project che by eclipse.

the class QuickAssistProcessor method getSplitVariableProposals.

private static boolean getSplitVariableProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    VariableDeclarationFragment fragment;
    if (node instanceof VariableDeclarationFragment) {
        fragment = (VariableDeclarationFragment) node;
    } else if (node.getLocationInParent() == VariableDeclarationFragment.NAME_PROPERTY) {
        fragment = (VariableDeclarationFragment) node.getParent();
    } else {
        return false;
    }
    if (fragment.getInitializer() == null) {
        return false;
    }
    Statement statement;
    ASTNode fragParent = fragment.getParent();
    if (fragParent instanceof VariableDeclarationStatement) {
        statement = (VariableDeclarationStatement) fragParent;
    } else if (fragParent instanceof VariableDeclarationExpression) {
        if (fragParent.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
            return false;
        }
        statement = (Statement) fragParent.getParent();
    } else {
        return false;
    }
    // statement is ForStatement or VariableDeclarationStatement
    ASTNode statementParent = statement.getParent();
    StructuralPropertyDescriptor property = statement.getLocationInParent();
    if (!property.isChildListProperty()) {
        return false;
    }
    List<? extends ASTNode> list = ASTNodes.getChildListProperty(statementParent, (ChildListPropertyDescriptor) property);
    if (resultingCollections == null) {
        return true;
    }
    AST ast = statement.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    String label = CorrectionMessages.QuickAssistProcessor_splitdeclaration_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_VARIABLE_DECLARATION, image);
    boolean commandConflict = false;
    for (Iterator<ICommandAccess> iterator = resultingCollections.iterator(); iterator.hasNext(); ) {
        Object completionProposal = iterator.next();
        if (completionProposal instanceof ChangeCorrectionProposal) {
            if (SPLIT_JOIN_VARIABLE_DECLARATION_ID.equals(((ChangeCorrectionProposal) completionProposal).getCommandId())) {
                commandConflict = true;
            }
        }
    }
    if (!commandConflict) {
        proposal.setCommandId(SPLIT_JOIN_VARIABLE_DECLARATION_ID);
    }
    Statement newStatement;
    int insertIndex = list.indexOf(statement);
    Expression placeholder = (Expression) rewrite.createMoveTarget(fragment.getInitializer());
    ITypeBinding binding = fragment.getInitializer().resolveTypeBinding();
    if (placeholder instanceof ArrayInitializer && binding != null && binding.isArray()) {
        ArrayCreation creation = ast.newArrayCreation();
        creation.setInitializer((ArrayInitializer) placeholder);
        final ITypeBinding componentType = binding.getElementType();
        Type type = null;
        if (componentType.isPrimitive())
            type = ast.newPrimitiveType(PrimitiveType.toCode(componentType.getName()));
        else
            type = ast.newSimpleType(ast.newSimpleName(componentType.getName()));
        creation.setType(ast.newArrayType(type, binding.getDimensions()));
        placeholder = creation;
    }
    Assignment assignment = ast.newAssignment();
    assignment.setRightHandSide(placeholder);
    assignment.setLeftHandSide(ast.newSimpleName(fragment.getName().getIdentifier()));
    if (statement instanceof VariableDeclarationStatement) {
        newStatement = ast.newExpressionStatement(assignment);
        // add after declaration
        insertIndex += 1;
    } else {
        rewrite.replace(fragment.getParent(), assignment, null);
        VariableDeclarationFragment newFrag = ast.newVariableDeclarationFragment();
        newFrag.setName(ast.newSimpleName(fragment.getName().getIdentifier()));
        newFrag.extraDimensions().addAll(DimensionRewrite.copyDimensions(fragment.extraDimensions(), rewrite));
        VariableDeclarationExpression oldVarDecl = (VariableDeclarationExpression) fragParent;
        VariableDeclarationStatement newVarDec = ast.newVariableDeclarationStatement(newFrag);
        newVarDec.setType((Type) rewrite.createCopyTarget(oldVarDecl.getType()));
        newVarDec.modifiers().addAll(ASTNodeFactory.newModifiers(ast, oldVarDecl.getModifiers()));
        newStatement = newVarDec;
    }
    ListRewrite listRewriter = rewrite.getListRewrite(statementParent, (ChildListPropertyDescriptor) property);
    listRewriter.insertAt(newStatement, insertIndex, null);
    resultingCollections.add(proposal);
    return true;
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Image(org.eclipse.swt.graphics.Image) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ICommandAccess(org.eclipse.jdt.ui.text.java.correction.ICommandAccess) IType(org.eclipse.jdt.core.IType)

Example 3 with ICommandAccess

use of org.eclipse.jdt.ui.text.java.correction.ICommandAccess in project che by eclipse.

the class LocalCorrectionsSubProcessor method getMissingEnumConstantCaseProposals.

public static void getMissingEnumConstantCaseProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    for (Iterator<ICommandAccess> iterator = proposals.iterator(); iterator.hasNext(); ) {
        ICommandAccess proposal = iterator.next();
        if (proposal instanceof ChangeCorrectionProposal) {
            if (CorrectionMessages.LocalCorrectionsSubProcessor_add_missing_cases_description.equals(((ChangeCorrectionProposal) proposal).getName())) {
                return;
            }
        }
    }
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) {
        SwitchStatement statement = (SwitchStatement) selectedNode.getParent();
        ITypeBinding binding = statement.getExpression().resolveTypeBinding();
        if (binding == null || !binding.isEnum()) {
            return;
        }
        ArrayList<String> missingEnumCases = new ArrayList<String>();
        boolean hasDefault = evaluateMissingSwitchCases(binding, statement.statements(), missingEnumCases);
        if (missingEnumCases.size() == 0 && hasDefault)
            return;
        createMissingCaseProposals(context, statement, missingEnumCases, proposals);
    }
}
Also used : ICommandAccess(org.eclipse.jdt.ui.text.java.correction.ICommandAccess) ArrayList(java.util.ArrayList) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal)

Example 4 with ICommandAccess

use of org.eclipse.jdt.ui.text.java.correction.ICommandAccess in project flux by eclipse.

the class QuickAssistProcessor method getAssists.

public IJavaCompletionProposal[] getAssists(IInvocationContext context, IProblemLocation[] locations) throws CoreException {
    ASTNode coveringNode = context.getCoveringNode();
    if (coveringNode != null) {
        ArrayList<ASTNode> coveredNodes = AdvancedQuickAssistProcessor.getFullyCoveredNodes(context, coveringNode);
        ArrayList<ICommandAccess> resultingCollections = new ArrayList<ICommandAccess>();
        boolean noErrorsAtLocation = noErrorsAtLocation(locations);
        // quick assists that show up also if there is an error/warning
        getRenameLocalProposals(context, coveringNode, locations, resultingCollections);
        getRenameRefactoringProposal(context, coveringNode, locations, resultingCollections);
        getAssignToVariableProposals(context, coveringNode, locations, resultingCollections);
        getAssignParamToFieldProposals(context, coveringNode, resultingCollections);
        getInferDiamondArgumentsProposal(context, coveringNode, locations, resultingCollections);
        getGenerateForLoopProposals(context, coveringNode, locations, resultingCollections);
        if (noErrorsAtLocation) {
            boolean problemsAtLocation = locations.length != 0;
            getCatchClauseToThrowsProposals(context, coveringNode, resultingCollections);
            getPickoutTypeFromMulticatchProposals(context, coveringNode, coveredNodes, resultingCollections);
            getConvertToMultiCatchProposals(context, coveringNode, resultingCollections);
            getUnrollMultiCatchProposals(context, coveringNode, resultingCollections);
            getUnWrapProposals(context, coveringNode, resultingCollections);
            getJoinVariableProposals(context, coveringNode, resultingCollections);
            getSplitVariableProposals(context, coveringNode, resultingCollections);
            getAddFinallyProposals(context, coveringNode, resultingCollections);
            getAddElseProposals(context, coveringNode, resultingCollections);
            getAddBlockProposals(context, coveringNode, resultingCollections);
            getInvertEqualsProposal(context, coveringNode, resultingCollections);
            getArrayInitializerToArrayCreation(context, coveringNode, resultingCollections);
            getCreateInSuperClassProposals(context, coveringNode, resultingCollections);
            getExtractVariableProposal(context, problemsAtLocation, resultingCollections);
            getExtractMethodProposal(context, coveringNode, problemsAtLocation, resultingCollections);
            getInlineLocalProposal(context, coveringNode, resultingCollections);
            getConvertLocalToFieldProposal(context, coveringNode, resultingCollections);
            getConvertAnonymousToNestedProposal(context, coveringNode, resultingCollections);
            getConvertAnonymousClassCreationsToLambdaProposals(context, coveringNode, resultingCollections);
            getConvertLambdaToAnonymousClassCreationsProposals(context, coveringNode, resultingCollections);
            getChangeLambdaBodyToBlockProposal(context, coveringNode, resultingCollections);
            getChangeLambdaBodyToExpressionProposal(context, coveringNode, resultingCollections);
            if (!getConvertForLoopProposal(context, coveringNode, resultingCollections))
                getConvertIterableLoopProposal(context, coveringNode, resultingCollections);
            getConvertEnhancedForLoopProposal(context, coveringNode, resultingCollections);
            getRemoveBlockProposals(context, coveringNode, resultingCollections);
            getMakeVariableDeclarationFinalProposals(context, resultingCollections);
            getConvertStringConcatenationProposals(context, resultingCollections);
            getMissingCaseStatementProposals(context, coveringNode, resultingCollections);
        }
        return resultingCollections.toArray(new IJavaCompletionProposal[resultingCollections.size()]);
    }
    return null;
}
Also used : ICommandAccess(org.eclipse.jdt.ui.text.java.correction.ICommandAccess) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList)

Example 5 with ICommandAccess

use of org.eclipse.jdt.ui.text.java.correction.ICommandAccess in project che by eclipse.

the class LocalCorrectionsSubProcessor method addTypePrametersToRawTypeReference.

public static void addTypePrametersToRawTypeReference(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    IProposableFix fix = Java50Fix.createRawTypeReferenceFix(context.getASTRoot(), problem);
    if (fix != null) {
        for (Iterator<ICommandAccess> iter = proposals.iterator(); iter.hasNext(); ) {
            Object element = iter.next();
            if (element instanceof FixCorrectionProposal) {
                FixCorrectionProposal fixProp = (FixCorrectionProposal) element;
                if (RAW_TYPE_REFERENCE_ID.equals(fixProp.getCommandId())) {
                    return;
                }
            }
        }
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        Map<String, String> options = new Hashtable<String, String>();
        options.put(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES, CleanUpOptions.TRUE);
        FixCorrectionProposal proposal = new FixCorrectionProposal(fix, new Java50CleanUp(options), IProposalRelevance.RAW_TYPE_REFERENCE, image, context);
        proposal.setCommandId(RAW_TYPE_REFERENCE_ID);
        proposals.add(proposal);
    }
    //Infer Generic Type Arguments... proposal
    boolean hasInferTypeArgumentsProposal = false;
    for (Iterator<ICommandAccess> iterator = proposals.iterator(); iterator.hasNext(); ) {
        Object completionProposal = iterator.next();
        if (completionProposal instanceof ChangeCorrectionProposal) {
            if (IJavaEditorActionDefinitionIds.INFER_TYPE_ARGUMENTS_ACTION.equals(((ChangeCorrectionProposal) completionProposal).getCommandId())) {
                hasInferTypeArgumentsProposal = true;
                break;
            }
        }
    }
    if (!hasInferTypeArgumentsProposal) {
        final ICompilationUnit cu = context.getCompilationUnit();
        ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(CorrectionMessages.LocalCorrectionsSubProcessor_InferGenericTypeArguments, null, IProposalRelevance.INFER_GENERIC_TYPE_ARGUMENTS, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE)) {

            @Override
            public void apply(IDocument document) {
                //							action.run(new StructuredSelection(cu));
                throw new UnsupportedOperationException();
            }

            @Override
            public String getActionId() {
                return "javaInferTypeArguments";
            }

            /**
						 * {@inheritDoc}
						 */
            @Override
            public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
                return CorrectionMessages.LocalCorrectionsSubProcessor_InferGenericTypeArguments_description;
            }
        };
        proposal.setCommandId(IJavaEditorActionDefinitionIds.INFER_TYPE_ARGUMENTS_ACTION);
        proposals.add(proposal);
    }
    addTypeArgumentsFromContext(context, problem, proposals);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ICommandAccess(org.eclipse.jdt.ui.text.java.correction.ICommandAccess) Hashtable(java.util.Hashtable) IProposableFix(org.eclipse.jdt.internal.corext.fix.IProposableFix) Image(org.eclipse.swt.graphics.Image) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) FixCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.FixCorrectionProposal) Java50CleanUp(org.eclipse.jdt.internal.ui.fix.Java50CleanUp) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

ICommandAccess (org.eclipse.jdt.ui.text.java.correction.ICommandAccess)9 ArrayList (java.util.ArrayList)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 ChangeCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal)4 HashSet (java.util.HashSet)2 IType (org.eclipse.jdt.core.IType)2 IProblemLocation (org.eclipse.jdt.ui.text.java.IProblemLocation)2 Hashtable (java.util.Hashtable)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 AST (org.eclipse.jdt.core.dom.AST)1 ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)1 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)1 ArrayType (org.eclipse.jdt.core.dom.ArrayType)1 Assignment (org.eclipse.jdt.core.dom.Assignment)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)1 DoStatement (org.eclipse.jdt.core.dom.DoStatement)1 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)1 Expression (org.eclipse.jdt.core.dom.Expression)1