Search in sources :

Example 1 with CorrectPackageDeclarationProposal

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

the class ReorgQuickFixTest method testWrongPackageStatementInEnum.

@Test
public void testWrongPackageStatementInEnum() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test2;\n");
    buf.append("\n");
    buf.append("public enum 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 enum 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 enum 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 2 with CorrectPackageDeclarationProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal 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 3 with CorrectPackageDeclarationProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal 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 4 with CorrectPackageDeclarationProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal 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 5 with CorrectPackageDeclarationProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal 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

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)5 CorrectPackageDeclarationProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal)5 ChangeCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal)5 ArrayList (java.util.ArrayList)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)4 Test (org.junit.Test)4 IPackageDeclaration (org.eclipse.jdt.core.IPackageDeclaration)1 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)1 CreatePackageChange (org.eclipse.jdt.internal.corext.refactoring.changes.CreatePackageChange)1 MoveCompilationUnitChange (org.eclipse.jdt.internal.corext.refactoring.changes.MoveCompilationUnitChange)1 CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)1