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)));
}
}
}
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());
}
}
}
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());
}
}
}
Aggregations