use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class LocalCorrectionsSubProcessor method createMissingDefaultProposal.
private static void createMissingDefaultProposal(IInvocationContext context, SwitchStatement switchStatement, Image image, Collection<ICommandAccess> proposals) {
AST ast = switchStatement.getAST();
ASTRewrite astRewrite = ASTRewrite.create(ast);
ListRewrite listRewrite = astRewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
SwitchCase newSwitchCase = ast.newSwitchCase();
newSwitchCase.setExpression(null);
listRewrite.insertLast(newSwitchCase, null);
listRewrite.insertLast(ast.newBreakStatement(), null);
String label = CorrectionMessages.LocalCorrectionsSubProcessor_add_default_case_description;
proposals.add(new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_MISSING_DEFAULT_CASE, image));
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class LocalCorrectionsSubProcessor method addRedundantSuperInterfaceProposal.
public static void addRedundantSuperInterfaceProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (!(selectedNode instanceof Name)) {
return;
}
ASTNode node = ASTNodes.getNormalizedNode(selectedNode);
ASTRewrite rewrite = ASTRewrite.create(node.getAST());
rewrite.remove(node, null);
String label = CorrectionMessages.LocalCorrectionsSubProcessor_remove_redundant_superinterface;
// JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_TOOL_DELETE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_REDUNDANT_SUPER_INTERFACE, image);
proposals.add(proposal);
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class LocalCorrectionsSubProcessor method createMissingCaseProposals.
public static void createMissingCaseProposals(IInvocationContext context, SwitchStatement switchStatement, ArrayList<String> enumConstNames, Collection<ICommandAccess> proposals) {
List<Statement> statements = switchStatement.statements();
int defaultIndex = statements.size();
for (int i = 0; i < statements.size(); i++) {
Statement curr = statements.get(i);
if (curr instanceof SwitchCase && ((SwitchCase) curr).getExpression() == null) {
defaultIndex = i;
break;
}
}
boolean hasDefault = defaultIndex < statements.size();
AST ast = switchStatement.getAST();
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
if (enumConstNames.size() > 0) {
ASTRewrite astRewrite = ASTRewrite.create(ast);
ListRewrite listRewrite = astRewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
for (int i = 0; i < enumConstNames.size(); i++) {
SwitchCase newSwitchCase = ast.newSwitchCase();
newSwitchCase.setExpression(ast.newName(enumConstNames.get(i)));
listRewrite.insertAt(newSwitchCase, defaultIndex, null);
defaultIndex++;
if (!hasDefault) {
listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
defaultIndex++;
}
}
if (!hasDefault) {
SwitchCase newSwitchCase = ast.newSwitchCase();
newSwitchCase.setExpression(null);
listRewrite.insertAt(newSwitchCase, defaultIndex, null);
defaultIndex++;
listRewrite.insertAt(ast.newBreakStatement(), defaultIndex, null);
}
String label = CorrectionMessages.LocalCorrectionsSubProcessor_add_missing_cases_description;
proposals.add(new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_MISSING_CASE_STATEMENTS, image));
}
if (!hasDefault) {
createMissingDefaultProposal(context, switchStatement, image, proposals);
}
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class LocalCorrectionsSubProcessor method addFallThroughProposals.
public static void addFallThroughProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode instanceof SwitchCase && selectedNode.getLocationInParent() == SwitchStatement.STATEMENTS_PROPERTY) {
AST ast = selectedNode.getAST();
ASTNode parent = selectedNode.getParent();
// insert break:
ASTRewrite rewrite = ASTRewrite.create(ast);
ListRewrite listRewrite = rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
listRewrite.insertBefore(ast.newBreakStatement(), selectedNode, null);
String label = CorrectionMessages.LocalCorrectionsSubProcessor_insert_break_statement;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_BREAK_STATEMENT, image);
proposals.add(proposal);
// insert //$FALL-THROUGH$:
rewrite = ASTRewrite.create(ast);
rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
listRewrite = rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
//$NON-NLS-1$
ASTNode fallThroughComment = rewrite.createStringPlaceholder("//$FALL-THROUGH$", ASTNode.EMPTY_STATEMENT);
listRewrite.insertBefore(fallThroughComment, selectedNode, null);
label = CorrectionMessages.LocalCorrectionsSubProcessor_insert_fall_through;
image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_FALL_THROUGH, image);
proposals.add(proposal);
}
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class LocalCorrectionsSubProcessor method addDeprecatedFieldsToMethodsProposals.
public static void addDeprecatedFieldsToMethodsProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode instanceof Name) {
IBinding binding = ((Name) selectedNode).resolveBinding();
if (binding instanceof IVariableBinding) {
IVariableBinding variableBinding = (IVariableBinding) binding;
if (variableBinding.isField()) {
String qualifiedName = variableBinding.getDeclaringClass().getTypeDeclaration().getQualifiedName();
String fieldName = variableBinding.getName();
String[] methodName = getMethod(JavaModelUtil.concatenateName(qualifiedName, fieldName));
if (methodName != null) {
AST ast = selectedNode.getAST();
ASTRewrite astRewrite = ASTRewrite.create(ast);
ImportRewrite importRewrite = StubUtility.createImportRewrite(context.getASTRoot(), true);
MethodInvocation method = ast.newMethodInvocation();
String qfn = importRewrite.addImport(methodName[0]);
method.setExpression(ast.newName(qfn));
method.setName(ast.newSimpleName(methodName[1]));
ASTNode parent = selectedNode.getParent();
ICompilationUnit cu = context.getCompilationUnit();
// add explicit type arguments if necessary (for 1.8 and later, we're optimistic that inference just works):
if (Invocations.isInvocationWithArguments(parent) && !JavaModelUtil.is18OrHigher(cu.getJavaProject())) {
IMethodBinding methodBinding = Invocations.resolveBinding(parent);
if (methodBinding != null) {
ITypeBinding[] parameterTypes = methodBinding.getParameterTypes();
int i = Invocations.getArguments(parent).indexOf(selectedNode);
if (parameterTypes.length >= i && parameterTypes[i].isParameterizedType()) {
ITypeBinding[] typeArguments = parameterTypes[i].getTypeArguments();
for (int j = 0; j < typeArguments.length; j++) {
ITypeBinding typeArgument = typeArguments[j];
typeArgument = Bindings.normalizeForDeclarationUse(typeArgument, ast);
if (!TypeRules.isJavaLangObject(typeArgument)) {
// add all type arguments if at least one is found to be necessary:
List<Type> typeArgumentsList = method.typeArguments();
for (int k = 0; k < typeArguments.length; k++) {
typeArgument = typeArguments[k];
typeArgument = Bindings.normalizeForDeclarationUse(typeArgument, ast);
typeArgumentsList.add(importRewrite.addImport(typeArgument, ast));
}
break;
}
}
}
}
}
astRewrite.replace(selectedNode, method, null);
String label = Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_replacefieldaccesswithmethod_description, BasicElementLabels.getJavaElementName(ASTNodes.asString(method)));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, astRewrite, IProposalRelevance.REPLACE_FIELD_ACCESS_WITH_METHOD, image);
proposal.setImportRewrite(importRewrite);
proposals.add(proposal);
}
}
}
}
}
Aggregations