Search in sources :

Example 1 with CorrectMainTypeNameProposal

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

the class ReorgCorrectionsSubProcessor method getWrongTypeNameProposals.

public static void getWrongTypeNameProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    boolean isLinked = cu.getResource().isLinked();
    IJavaProject javaProject = cu.getJavaProject();
    String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
    String compliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
    CompilationUnit root = context.getASTRoot();
    ASTNode coveredNode = problem.getCoveredNode(root);
    if (!(coveredNode instanceof SimpleName))
        return;
    ASTNode parentType = coveredNode.getParent();
    if (!(parentType instanceof AbstractTypeDeclaration))
        return;
    String currTypeName = ((SimpleName) coveredNode).getIdentifier();
    String newTypeName = JavaCore.removeJavaLikeExtension(cu.getElementName());
    boolean hasOtherPublicTypeBefore = false;
    boolean found = false;
    List<AbstractTypeDeclaration> types = root.types();
    for (int i = 0; i < types.size(); i++) {
        AbstractTypeDeclaration curr = types.get(i);
        if (parentType != curr) {
            if (newTypeName.equals(curr.getName().getIdentifier())) {
                return;
            }
            if (!found && Modifier.isPublic(curr.getModifiers())) {
                hasOtherPublicTypeBefore = true;
            }
        } else {
            found = true;
        }
    }
    if (!JavaConventions.validateJavaTypeName(newTypeName, sourceLevel, compliance).matches(IStatus.ERROR)) {
        proposals.add(new CorrectMainTypeNameProposal(cu, context, currTypeName, newTypeName, IProposalRelevance.RENAME_TYPE));
    }
    if (!hasOtherPublicTypeBefore) {
        String newCUName = JavaModelUtil.getRenamedCUName(cu, currTypeName);
        ICompilationUnit newCU = ((IPackageFragment) (cu.getParent())).getCompilationUnit(newCUName);
        if (!newCU.exists() && !isLinked && !JavaConventions.validateCompilationUnitName(newCUName, sourceLevel, compliance).matches(IStatus.ERROR)) {
            RenameCompilationUnitChange change = new RenameCompilationUnitChange(cu, newCUName);
            // rename CU
            String label = Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_renamecu_description, BasicElementLabels.getResourceName(newCUName));
            proposals.add(new ChangeCorrectionProposal(label, change, IProposalRelevance.RENAME_CU, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME)));
        }
    }
}
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) SimpleName(org.eclipse.jdt.core.dom.SimpleName) CorrectMainTypeNameProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectMainTypeNameProposal) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) RenameCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange) IJavaProject(org.eclipse.jdt.core.IJavaProject) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 2 with CorrectMainTypeNameProposal

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

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

the class ReorgQuickFixTest method testWrongTypeName.

@Test
public void testWrongTypeName() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("X.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 CorrectMainTypeNameProposal) {
            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 X {\n");
            buf.append("}\n");
            assertEqualString(preview, buf.toString());
        } else {
            assertTrue("Duplicated proposal", hasMove);
            hasMove = false;
            curr.apply(null);
            ICompilationUnit cu2 = pack1.getCompilationUnit("E.java");
            assertTrue("CU does not exist", cu2.exists());
            buf = new StringBuffer();
            buf.append("package test1;\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) 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)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 CorrectMainTypeNameProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectMainTypeNameProposal)3 ChangeCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal)3 ArrayList (java.util.ArrayList)2 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)2 Test (org.junit.Test)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 RenameCompilationUnitChange (org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange)1