Search in sources :

Example 21 with CUCorrectionProposal

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

the class AssistQuickFixTest method testAssignParamToField5.

@Test
public void testAssignParamToField5() throws Exception {
    IPreferenceStore store = JavaPlugin.getDefault().getPreferenceStore();
    store.setValue(PreferenceConstants.CODEGEN_KEYWORD_THIS, true);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    private int p1;\n");
    buf.append("\n");
    buf.append("    public void foo(int p1, int p2) {\n");
    buf.append("        this.p1 = p1;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("int p2");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 2);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview1 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    private int p1;\n");
    buf.append("    private int p2;\n");
    buf.append("\n");
    buf.append("    public void foo(int p1, int p2) {\n");
    buf.append("        this.p1 = p1;\n");
    buf.append("        this.p2 = p2;\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(1);
    String preview2 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    private int p1;\n");
    buf.append("\n");
    buf.append("    public void foo(int p1, int p2) {\n");
    buf.append("        this.p1 = p1;\n");
    buf.append("        this.p1 = p2;\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) Test(org.junit.Test)

Example 22 with CUCorrectionProposal

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

the class AssistQuickFixTest method testAssignParamToField6.

@Test
public void testAssignParamToField6() throws Exception {
    IPreferenceStore store = JavaPlugin.getDefault().getPreferenceStore();
    store.setValue(PreferenceConstants.CODEGEN_KEYWORD_THIS, true);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    private Float p1;\n");
    buf.append("    private Number p2;\n");
    buf.append("\n");
    buf.append("    public void foo(Float p1, Integer p2) {\n");
    buf.append("        this.p1 = p1;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("Integer p2");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 2);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview1 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    private Float p1;\n");
    buf.append("    private Number p2;\n");
    buf.append("\n");
    buf.append("    public void foo(Float p1, Integer p2) {\n");
    buf.append("        this.p1 = p1;\n");
    buf.append("        this.p2 = p2;\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(1);
    String preview2 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    private Float p1;\n");
    buf.append("    private Number p2;\n");
    buf.append("    private Integer p22;\n");
    buf.append("\n");
    buf.append("    public void foo(Float p1, Integer p2) {\n");
    buf.append("        this.p1 = p1;\n");
    buf.append("        this.p22 = p2;\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) Test(org.junit.Test)

Example 23 with CUCorrectionProposal

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

the class AssistQuickFixTest method testSplitDeclaration1.

@Test
public void testSplitDeclaration1() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        int i = 9;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    String str = "=";
    AssistContext context = getCorrectionContext(cu, buf.toString().indexOf(str), 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 1);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        int i;\n");
    buf.append("        i = 9;\n");
    buf.append("    }\n");
    buf.append("}\n");
    assertEqualString(preview, buf.toString());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 24 with CUCorrectionProposal

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

the class AssistQuickFixTest method testAssignToLocal2.

@Test
public void testAssignToLocal2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("public class E {\n");
    buf.append("    public Vector goo() {\n");
    buf.append("        return null;\n");
    buf.append("    }\n");
    buf.append("    public void foo() {\n");
    buf.append("        goo().iterator();\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("goo().iterator()");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 2);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview1 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.Iterator;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("public class E {\n");
    buf.append("    private Iterator iterator;\n");
    buf.append("    public Vector goo() {\n");
    buf.append("        return null;\n");
    buf.append("    }\n");
    buf.append("    public void foo() {\n");
    buf.append("        iterator = goo().iterator();\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(1);
    String preview2 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.Iterator;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("public class E {\n");
    buf.append("    public Vector goo() {\n");
    buf.append("        return null;\n");
    buf.append("    }\n");
    buf.append("    public void foo() {\n");
    buf.append("        Iterator iterator = goo().iterator();\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 25 with CUCorrectionProposal

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

the class LocalCorrectionsSubProcessor method getInvalidOperatorProposals.

public static void getInvalidOperatorProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    CompilationUnit root = context.getASTRoot();
    AST ast = root.getAST();
    ASTNode selectedNode = problem.getCoveringNode(root);
    while (selectedNode instanceof ParenthesizedExpression) {
        selectedNode = ((ParenthesizedExpression) selectedNode).getExpression();
    }
    if (selectedNode instanceof PrefixExpression) {
        // !x instanceof X -> !(x instanceof X)
        PrefixExpression expression = (PrefixExpression) selectedNode;
        if (expression.getOperator() == PrefixExpression.Operator.NOT) {
            ASTNode parent = expression.getParent();
            String label = null;
            switch(parent.getNodeType()) {
                case ASTNode.INSTANCEOF_EXPRESSION:
                    label = CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_instanceof_description;
                    break;
                case ASTNode.INFIX_EXPRESSION:
                    InfixExpression infixExpression = (InfixExpression) parent;
                    label = Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_description, infixExpression.getOperator().toString());
                    break;
            }
            if (label != null) {
                ASTRewrite rewrite = ASTRewrite.create(ast);
                rewrite.replace(selectedNode, rewrite.createMoveTarget(expression.getOperand()), null);
                ParenthesizedExpression newParentExpr = ast.newParenthesizedExpression();
                newParentExpr.setExpression((Expression) rewrite.createMoveTarget(parent));
                PrefixExpression newPrefixExpr = ast.newPrefixExpression();
                newPrefixExpr.setOperand(newParentExpr);
                newPrefixExpr.setOperator(PrefixExpression.Operator.NOT);
                rewrite.replace(parent, newPrefixExpr, null);
                Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
                ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVALID_OPERATOR, image);
                proposals.add(proposal);
            }
        }
    } else if (selectedNode instanceof InfixExpression && isBitOperation((((InfixExpression) selectedNode).getOperator()))) {
        // a & b == c -> (a & b) == c
        final CompareInBitWiseOpFinder opFinder = new CompareInBitWiseOpFinder(selectedNode);
        if (opFinder.getCompareExpression() != null) {
            // compare operation inside bit operations: set parents
            String label = CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_bitop_description;
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
            CUCorrectionProposal proposal = new CUCorrectionProposal(label, context.getCompilationUnit(), IProposalRelevance.INVALID_OPERATOR, image) {

                @Override
                protected void addEdits(IDocument document, TextEdit edit) throws CoreException {
                    InfixExpression compareExpression = opFinder.getCompareExpression();
                    InfixExpression expression = opFinder.getParentInfixExpression();
                    ASTNode left = compareExpression.getLeftOperand();
                    if (expression.getStartPosition() < left.getStartPosition()) {
                        edit.addChild(new InsertEdit(expression.getStartPosition(), String.valueOf('(')));
                        edit.addChild(new InsertEdit(ASTNodes.getExclusiveEnd(left), String.valueOf(')')));
                    }
                    ASTNode rigth = compareExpression.getRightOperand();
                    int selEnd = ASTNodes.getExclusiveEnd(expression);
                    if (selEnd > ASTNodes.getExclusiveEnd(rigth)) {
                        edit.addChild(new InsertEdit(rigth.getStartPosition(), String.valueOf('(')));
                        edit.addChild(new InsertEdit(selEnd, String.valueOf(')')));
                    }
                }
            };
            proposals.add(proposal);
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) InsertEdit(org.eclipse.text.edits.InsertEdit) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)290 ArrayList (java.util.ArrayList)285 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)285 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)284 Test (org.junit.Test)284 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)228 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)57 List (java.util.List)48 Hashtable (java.util.Hashtable)47 ProblemLocation (org.eclipse.jdt.internal.ui.text.correction.ProblemLocation)7 Ignore (org.junit.Ignore)7 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)6 ChangeCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal)6 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)5 CorrectPackageDeclarationProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 Iterator (java.util.Iterator)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)2 IDocument (org.eclipse.jface.text.IDocument)2