Search in sources :

Example 6 with ChangeCorrectionProposal

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

the class ReorgQuickFixTest method testWrongPackageStatement.

@Test
public void testWrongPackageStatement() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test2;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertNumberOfProposals(proposals, 2);
    assertCorrectLabels(proposals);
    boolean hasRename = true, hasMove = true;
    for (int i = 0; i < proposals.size(); i++) {
        ChangeCorrectionProposal curr = (ChangeCorrectionProposal) proposals.get(i);
        if (curr instanceof CorrectPackageDeclarationProposal) {
            assertTrue("Duplicated proposal", hasRename);
            hasRename = false;
            CUCorrectionProposal proposal = (CUCorrectionProposal) curr;
            String preview = getPreviewContent(proposal);
            buf = new StringBuffer();
            buf.append("package test1;\n");
            buf.append("\n");
            buf.append("public class E {\n");
            buf.append("}\n");
            assertEqualString(preview, buf.toString());
        } else {
            assertTrue("Duplicated proposal", hasMove);
            hasMove = false;
            curr.apply(null);
            IPackageFragment pack2 = fSourceFolder.getPackageFragment("test2");
            ICompilationUnit cu2 = pack2.getCompilationUnit("E.java");
            assertTrue("CU does not exist", cu2.exists());
            buf = new StringBuffer();
            buf.append("package test2;\n");
            buf.append("\n");
            buf.append("public class E {\n");
            buf.append("}\n");
            assertEqualStringIgnoreDelim(cu2.getSource(), buf.toString());
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CorrectPackageDeclarationProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ArrayList(java.util.ArrayList) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) Test(org.junit.Test)

Example 7 with ChangeCorrectionProposal

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

the class ReorgQuickFixTest method testWrongPackageStatementFromDefault.

@Test
public void testWrongPackageStatementFromDefault() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test2;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertNumberOfProposals(proposals, 2);
    assertCorrectLabels(proposals);
    boolean hasRename = true, hasMove = true;
    for (int i = 0; i < proposals.size(); i++) {
        ChangeCorrectionProposal curr = (ChangeCorrectionProposal) proposals.get(i);
        if (curr instanceof CorrectPackageDeclarationProposal) {
            assertTrue("Duplicated proposal", hasRename);
            hasRename = false;
            CUCorrectionProposal proposal = (CUCorrectionProposal) curr;
            String preview = getPreviewContent(proposal);
            buf = new StringBuffer();
            buf.append("\n");
            buf.append("\n");
            buf.append("public class E {\n");
            buf.append("}\n");
            assertEqualString(preview, buf.toString());
        } else {
            assertTrue("Duplicated proposal", hasMove);
            hasMove = false;
            curr.apply(null);
            IPackageFragment pack2 = fSourceFolder.getPackageFragment("test2");
            ICompilationUnit cu2 = pack2.getCompilationUnit("E.java");
            assertTrue("CU does not exist", cu2.exists());
            buf = new StringBuffer();
            buf.append("package test2;\n");
            buf.append("\n");
            buf.append("public class E {\n");
            buf.append("}\n");
            assertEqualStringIgnoreDelim(cu2.getSource(), buf.toString());
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CorrectPackageDeclarationProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ArrayList(java.util.ArrayList) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) Test(org.junit.Test)

Example 8 with ChangeCorrectionProposal

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

the class ReorgQuickFixTest method testWrongTypeName_bug180330.

@Test
public void testWrongTypeName_bug180330() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("p", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package p;\n");
    buf.append("public class \\u0042 {\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertCorrectLabels(proposals);
    assertNumberOfProposals(proposals, 2);
    boolean hasRename = true, hasMove = true;
    for (int i = 0; i < proposals.size(); i++) {
        ChangeCorrectionProposal curr = (ChangeCorrectionProposal) proposals.get(i);
        if (curr instanceof CorrectMainTypeNameProposal) {
            assertTrue("Duplicated proposal", hasRename);
            hasRename = false;
            CUCorrectionProposal proposal = (CUCorrectionProposal) curr;
            String preview = getPreviewContent(proposal);
            buf = new StringBuffer();
            buf.append("package p;\n");
            buf.append("public class C {\n");
            buf.append("}\n");
            assertEqualString(preview, buf.toString());
        } else {
            assertTrue("Duplicated proposal", hasMove);
            hasMove = false;
            curr.apply(null);
            ICompilationUnit cu2 = pack1.getCompilationUnit("B.java");
            assertTrue("CU does not exist", cu2.exists());
            buf = new StringBuffer();
            buf.append("package p;\n");
            buf.append("public class \\u0042 {\n");
            buf.append("}\n");
            assertEqualStringIgnoreDelim(cu2.getSource(), buf.toString());
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ArrayList(java.util.ArrayList) CorrectMainTypeNameProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectMainTypeNameProposal) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) Test(org.junit.Test)

Example 9 with ChangeCorrectionProposal

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

the class ReorgQuickFixTest method testWrongDefaultPackageStatement.

@Test
public void testWrongDefaultPackageStatement() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test2", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("public class E {\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertNumberOfProposals(proposals, 2);
    assertCorrectLabels(proposals);
    boolean hasRename = true, hasMove = true;
    for (int i = 0; i < proposals.size(); i++) {
        ChangeCorrectionProposal curr = (ChangeCorrectionProposal) proposals.get(i);
        if (curr instanceof CorrectPackageDeclarationProposal) {
            assertTrue("Duplicated proposal", hasRename);
            hasRename = false;
            CUCorrectionProposal proposal = (CUCorrectionProposal) curr;
            String preview = getPreviewContent(proposal);
            buf = new StringBuffer();
            buf.append("package test2;\n");
            buf.append("\n");
            buf.append("public class E {\n");
            buf.append("}\n");
            assertEqualString(preview, buf.toString());
        } else {
            assertTrue("Duplicated proposal", hasMove);
            hasMove = false;
            curr.apply(null);
            IPackageFragment pack2 = fSourceFolder.getPackageFragment("");
            ICompilationUnit cu2 = pack2.getCompilationUnit("E.java");
            assertTrue("CU does not exist", cu2.exists());
            buf = new StringBuffer();
            buf.append("public class E {\n");
            buf.append("}\n");
            assertEqualStringIgnoreDelim(cu2.getSource(), buf.toString());
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CorrectPackageDeclarationProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ArrayList(java.util.ArrayList) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) Test(org.junit.Test)

Example 10 with ChangeCorrectionProposal

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

the class ReorgCorrectionsSubProcessor method getWrongPackageDeclNameProposals.

public static void getWrongPackageDeclNameProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    boolean isLinked = cu.getResource().isLinked();
    // correct package declaration
    // bug 38357
    int relevance = cu.getPackageDeclarations().length == 0 ? IProposalRelevance.MISSING_PACKAGE_DECLARATION : IProposalRelevance.CORRECT_PACKAGE_DECLARATION;
    proposals.add(new CorrectPackageDeclarationProposal(cu, problem, relevance));
    // move to package
    IPackageDeclaration[] packDecls = cu.getPackageDeclarations();
    //$NON-NLS-1$
    String newPackName = packDecls.length > 0 ? packDecls[0].getElementName() : "";
    IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(cu);
    IPackageFragment newPack = root.getPackageFragment(newPackName);
    ICompilationUnit newCU = newPack.getCompilationUnit(cu.getElementName());
    if (!newCU.exists() && !isLinked) {
        String label;
        if (newPack.isDefaultPackage()) {
            label = Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_default_description, BasicElementLabels.getFileName(cu));
        } else {
            String packageLabel = JavaElementLabels.getElementLabel(newPack, JavaElementLabels.ALL_DEFAULT);
            label = Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_description, new Object[] { BasicElementLabels.getFileName(cu), packageLabel });
        }
        CompositeChange composite = new CompositeChange(label);
        composite.add(new CreatePackageChange(newPack));
        composite.add(new MoveCompilationUnitChange(cu, newPack));
        proposals.add(new ChangeCorrectionProposal(label, composite, IProposalRelevance.MOVE_CU_TO_PACKAGE, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_MOVE)));
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CorrectPackageDeclarationProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) CreatePackageChange(org.eclipse.jdt.internal.corext.refactoring.changes.CreatePackageChange) MoveCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.MoveCompilationUnitChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration)

Aggregations

ChangeCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal)16 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)12 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)8 ArrayList (java.util.ArrayList)7 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)7 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)5 Test (org.junit.Test)5 CorrectPackageDeclarationProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal)4 ICommandAccess (org.eclipse.jdt.ui.text.java.correction.ICommandAccess)4 Image (org.eclipse.swt.graphics.Image)4 IType (org.eclipse.jdt.core.IType)3 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)3 IDocument (org.eclipse.jface.text.IDocument)3 Hashtable (java.util.Hashtable)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 CorrectMainTypeNameProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectMainTypeNameProposal)2 IMarker (org.eclipse.core.resources.IMarker)1 IResource (org.eclipse.core.resources.IResource)1 CoreException (org.eclipse.core.runtime.CoreException)1