Search in sources :

Example 91 with ASTRewriteCorrectionProposal

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

the class LocalCorrectionsSubProcessor method createMissingDefaultProposal.

private static void createMissingDefaultProposal(IInvocationContext context, SwitchStatement switchStatement, Image image, Collection<ICommandAccess> proposals) {
    AST ast = switchStatement.getAST();
    ASTRewrite astRewrite = ASTRewrite.create(ast);
    ListRewrite listRewrite = astRewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
    SwitchCase newSwitchCase = ast.newSwitchCase();
    newSwitchCase.setExpression(null);
    listRewrite.insertLast(newSwitchCase, null);
    listRewrite.insertLast(ast.newBreakStatement(), null);
    String label = CorrectionMessages.LocalCorrectionsSubProcessor_add_default_case_description;
    proposals.add(new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_MISSING_DEFAULT_CASE, image));
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite)

Example 92 with ASTRewriteCorrectionProposal

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

the class LocalCorrectionsSubProcessor method addRedundantSuperInterfaceProposal.

public static void addRedundantSuperInterfaceProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof Name)) {
        return;
    }
    ASTNode node = ASTNodes.getNormalizedNode(selectedNode);
    ASTRewrite rewrite = ASTRewrite.create(node.getAST());
    rewrite.remove(node, null);
    String label = CorrectionMessages.LocalCorrectionsSubProcessor_remove_redundant_superinterface;
    // JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_TOOL_DELETE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_REDUNDANT_SUPER_INTERFACE, image);
    proposals.add(proposal);
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Image(org.eclipse.swt.graphics.Image)

Example 93 with ASTRewriteCorrectionProposal

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

the class LocalCorrectionsSubProcessor method createMissingCaseProposals.

public static void createMissingCaseProposals(IInvocationContext context, SwitchStatement switchStatement, ArrayList<String> enumConstNames, Collection<ICommandAccess> proposals) {
    List<Statement> statements = switchStatement.statements();
    int defaultIndex = statements.size();
    for (int i = 0; i < statements.size(); i++) {
        Statement curr = statements.get(i);
        if (curr instanceof SwitchCase && ((SwitchCase) curr).getExpression() == null) {
            defaultIndex = i;
            break;
        }
    }
    boolean hasDefault = defaultIndex < statements.size();
    AST ast = switchStatement.getAST();
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    if (enumConstNames.size() > 0) {
        ASTRewrite astRewrite = ASTRewrite.create(ast);
        ListRewrite listRewrite = astRewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
        for (int i = 0; i < enumConstNames.size(); i++) {
            SwitchCase newSwitchCase = ast.newSwitchCase();
            newSwitchCase.setExpression(ast.newName(enumConstNames.get(i)));
            listRewrite.insertAt(newSwitchCase, defaultIndex, null);
            defaultIndex++;
            if (!hasDefault) {
                listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
                defaultIndex++;
            }
        }
        if (!hasDefault) {
            SwitchCase newSwitchCase = ast.newSwitchCase();
            newSwitchCase.setExpression(null);
            listRewrite.insertAt(newSwitchCase, defaultIndex, null);
            defaultIndex++;
            listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
        }
        String label = CorrectionMessages.LocalCorrectionsSubProcessor_add_missing_cases_description;
        proposals.add(new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_MISSING_CASE_STATEMENTS, image));
    }
    if (!hasDefault) {
        createMissingDefaultProposal(context, switchStatement, image, proposals);
    }
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Image(org.eclipse.swt.graphics.Image)

Example 94 with ASTRewriteCorrectionProposal

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

the class LocalCorrectionsSubProcessor method addFallThroughProposals.

public static void addFallThroughProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode instanceof SwitchCase && selectedNode.getLocationInParent() == SwitchStatement.STATEMENTS_PROPERTY) {
        AST ast = selectedNode.getAST();
        ASTNode parent = selectedNode.getParent();
        // insert break:
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ListRewrite listRewrite = rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
        listRewrite.insertBefore(ast.newBreakStatement(), selectedNode, null);
        String label = CorrectionMessages.LocalCorrectionsSubProcessor_insert_break_statement;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_BREAK_STATEMENT, image);
        proposals.add(proposal);
        // insert //$FALL-THROUGH$:
        rewrite = ASTRewrite.create(ast);
        rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
        listRewrite = rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
        //$NON-NLS-1$
        ASTNode fallThroughComment = rewrite.createStringPlaceholder("//$FALL-THROUGH$", ASTNode.EMPTY_STATEMENT);
        listRewrite.insertBefore(fallThroughComment, selectedNode, null);
        label = CorrectionMessages.LocalCorrectionsSubProcessor_insert_fall_through;
        image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_FALL_THROUGH, image);
        proposals.add(proposal);
    }
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) NoCommentSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.NoCommentSourceRangeComputer) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Image(org.eclipse.swt.graphics.Image)

Example 95 with ASTRewriteCorrectionProposal

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

the class LocalCorrectionsSubProcessor method addDeprecatedFieldsToMethodsProposals.

public static void addDeprecatedFieldsToMethodsProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode instanceof Name) {
        IBinding binding = ((Name) selectedNode).resolveBinding();
        if (binding instanceof IVariableBinding) {
            IVariableBinding variableBinding = (IVariableBinding) binding;
            if (variableBinding.isField()) {
                String qualifiedName = variableBinding.getDeclaringClass().getTypeDeclaration().getQualifiedName();
                String fieldName = variableBinding.getName();
                String[] methodName = getMethod(JavaModelUtil.concatenateName(qualifiedName, fieldName));
                if (methodName != null) {
                    AST ast = selectedNode.getAST();
                    ASTRewrite astRewrite = ASTRewrite.create(ast);
                    ImportRewrite importRewrite = StubUtility.createImportRewrite(context.getASTRoot(), true);
                    MethodInvocation method = ast.newMethodInvocation();
                    String qfn = importRewrite.addImport(methodName[0]);
                    method.setExpression(ast.newName(qfn));
                    method.setName(ast.newSimpleName(methodName[1]));
                    ASTNode parent = selectedNode.getParent();
                    ICompilationUnit cu = context.getCompilationUnit();
                    // add explicit type arguments if necessary (for 1.8 and later, we're optimistic that inference just works):
                    if (Invocations.isInvocationWithArguments(parent) && !JavaModelUtil.is18OrHigher(cu.getJavaProject())) {
                        IMethodBinding methodBinding = Invocations.resolveBinding(parent);
                        if (methodBinding != null) {
                            ITypeBinding[] parameterTypes = methodBinding.getParameterTypes();
                            int i = Invocations.getArguments(parent).indexOf(selectedNode);
                            if (parameterTypes.length >= i && parameterTypes[i].isParameterizedType()) {
                                ITypeBinding[] typeArguments = parameterTypes[i].getTypeArguments();
                                for (int j = 0; j < typeArguments.length; j++) {
                                    ITypeBinding typeArgument = typeArguments[j];
                                    typeArgument = Bindings.normalizeForDeclarationUse(typeArgument, ast);
                                    if (!TypeRules.isJavaLangObject(typeArgument)) {
                                        // add all type arguments if at least one is found to be necessary:
                                        List<Type> typeArgumentsList = method.typeArguments();
                                        for (int k = 0; k < typeArguments.length; k++) {
                                            typeArgument = typeArguments[k];
                                            typeArgument = Bindings.normalizeForDeclarationUse(typeArgument, ast);
                                            typeArgumentsList.add(importRewrite.addImport(typeArgument, ast));
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    astRewrite.replace(selectedNode, method, null);
                    String label = Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_replacefieldaccesswithmethod_description, BasicElementLabels.getJavaElementName(ASTNodes.asString(method)));
                    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
                    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, astRewrite, IProposalRelevance.REPLACE_FIELD_ACCESS_WITH_METHOD, image);
                    proposal.setImportRewrite(importRewrite);
                    proposals.add(proposal);
                }
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IType(org.eclipse.jdt.core.IType) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Aggregations

ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)125 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)93 Image (org.eclipse.swt.graphics.Image)70 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 AST (org.eclipse.jdt.core.dom.AST)38 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)35 ASTNode (org.eclipse.jdt.core.dom.ASTNode)33 Expression (org.eclipse.jdt.core.dom.Expression)32 ArrayList (java.util.ArrayList)29 CastExpression (org.eclipse.jdt.core.dom.CastExpression)24 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)23 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)23 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)23 Test (org.junit.Test)23 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)21 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)21 Block (org.eclipse.jdt.core.dom.Block)19 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)19 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)19 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)19