use of org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal 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.ui.text.java.correction.ChangeCorrectionProposal in project che by eclipse.
the class ReorgCorrectionsSubProcessor method removeImportStatementProposals.
public static void removeImportStatementProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
IProposableFix fix = UnusedCodeFix.createRemoveUnusedImportFix(context.getASTRoot(), problem);
if (fix != null) {
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_DELETE_IMPORT);
Map<String, String> options = new Hashtable<String, String>();
options.put(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS, CleanUpOptions.TRUE);
FixCorrectionProposal proposal = new FixCorrectionProposal(fix, new UnusedCodeCleanUp(options), IProposalRelevance.REMOVE_UNUSED_IMPORT, image, context);
proposals.add(proposal);
}
final ICompilationUnit cu = context.getCompilationUnit();
String name = CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description;
ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(name, null, IProposalRelevance.ORGANIZE_IMPORTS, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE)) {
@Override
public void apply(IDocument document) {
// }
throw new UnsupportedOperationException("apply doesn't supported for action proposal");
}
@Override
public String getActionId() {
return "javaOrganizeImports";
}
};
proposals.add(proposal);
}
use of org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal in project che by eclipse.
the class QuickAssistProcessor method getSplitVariableProposals.
private static boolean getSplitVariableProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
VariableDeclarationFragment fragment;
if (node instanceof VariableDeclarationFragment) {
fragment = (VariableDeclarationFragment) node;
} else if (node.getLocationInParent() == VariableDeclarationFragment.NAME_PROPERTY) {
fragment = (VariableDeclarationFragment) node.getParent();
} else {
return false;
}
if (fragment.getInitializer() == null) {
return false;
}
Statement statement;
ASTNode fragParent = fragment.getParent();
if (fragParent instanceof VariableDeclarationStatement) {
statement = (VariableDeclarationStatement) fragParent;
} else if (fragParent instanceof VariableDeclarationExpression) {
if (fragParent.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
return false;
}
statement = (Statement) fragParent.getParent();
} else {
return false;
}
// statement is ForStatement or VariableDeclarationStatement
ASTNode statementParent = statement.getParent();
StructuralPropertyDescriptor property = statement.getLocationInParent();
if (!property.isChildListProperty()) {
return false;
}
List<? extends ASTNode> list = ASTNodes.getChildListProperty(statementParent, (ChildListPropertyDescriptor) property);
if (resultingCollections == null) {
return true;
}
AST ast = statement.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = CorrectionMessages.QuickAssistProcessor_splitdeclaration_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_VARIABLE_DECLARATION, image);
boolean commandConflict = false;
for (Iterator<ICommandAccess> iterator = resultingCollections.iterator(); iterator.hasNext(); ) {
Object completionProposal = iterator.next();
if (completionProposal instanceof ChangeCorrectionProposal) {
if (SPLIT_JOIN_VARIABLE_DECLARATION_ID.equals(((ChangeCorrectionProposal) completionProposal).getCommandId())) {
commandConflict = true;
}
}
}
if (!commandConflict) {
proposal.setCommandId(SPLIT_JOIN_VARIABLE_DECLARATION_ID);
}
Statement newStatement;
int insertIndex = list.indexOf(statement);
Expression placeholder = (Expression) rewrite.createMoveTarget(fragment.getInitializer());
ITypeBinding binding = fragment.getInitializer().resolveTypeBinding();
if (placeholder instanceof ArrayInitializer && binding != null && binding.isArray()) {
ArrayCreation creation = ast.newArrayCreation();
creation.setInitializer((ArrayInitializer) placeholder);
final ITypeBinding componentType = binding.getElementType();
Type type = null;
if (componentType.isPrimitive())
type = ast.newPrimitiveType(PrimitiveType.toCode(componentType.getName()));
else
type = ast.newSimpleType(ast.newSimpleName(componentType.getName()));
creation.setType(ast.newArrayType(type, binding.getDimensions()));
placeholder = creation;
}
Assignment assignment = ast.newAssignment();
assignment.setRightHandSide(placeholder);
assignment.setLeftHandSide(ast.newSimpleName(fragment.getName().getIdentifier()));
if (statement instanceof VariableDeclarationStatement) {
newStatement = ast.newExpressionStatement(assignment);
// add after declaration
insertIndex += 1;
} else {
rewrite.replace(fragment.getParent(), assignment, null);
VariableDeclarationFragment newFrag = ast.newVariableDeclarationFragment();
newFrag.setName(ast.newSimpleName(fragment.getName().getIdentifier()));
newFrag.extraDimensions().addAll(DimensionRewrite.copyDimensions(fragment.extraDimensions(), rewrite));
VariableDeclarationExpression oldVarDecl = (VariableDeclarationExpression) fragParent;
VariableDeclarationStatement newVarDec = ast.newVariableDeclarationStatement(newFrag);
newVarDec.setType((Type) rewrite.createCopyTarget(oldVarDecl.getType()));
newVarDec.modifiers().addAll(ASTNodeFactory.newModifiers(ast, oldVarDecl.getModifiers()));
newStatement = newVarDec;
}
ListRewrite listRewriter = rewrite.getListRewrite(statementParent, (ChildListPropertyDescriptor) property);
listRewriter.insertAt(newStatement, insertIndex, null);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal in project che by eclipse.
the class LocalCorrectionsSubProcessor method getMissingEnumConstantCaseProposals.
public static void getMissingEnumConstantCaseProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
for (Iterator<ICommandAccess> iterator = proposals.iterator(); iterator.hasNext(); ) {
ICommandAccess proposal = iterator.next();
if (proposal instanceof ChangeCorrectionProposal) {
if (CorrectionMessages.LocalCorrectionsSubProcessor_add_missing_cases_description.equals(((ChangeCorrectionProposal) proposal).getName())) {
return;
}
}
}
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) {
SwitchStatement statement = (SwitchStatement) selectedNode.getParent();
ITypeBinding binding = statement.getExpression().resolveTypeBinding();
if (binding == null || !binding.isEnum()) {
return;
}
ArrayList<String> missingEnumCases = new ArrayList<String>();
boolean hasDefault = evaluateMissingSwitchCases(binding, statement.statements(), missingEnumCases);
if (missingEnumCases.size() == 0 && hasDefault)
return;
createMissingCaseProposals(context, statement, missingEnumCases, proposals);
}
}
use of org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal 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());
}
}
}
Aggregations