use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class LocalCorrectionsSubProcessor method addCasesOmittedProposals.
public static void addCasesOmittedProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) {
AST ast = selectedNode.getAST();
SwitchStatement parent = (SwitchStatement) selectedNode.getParent();
for (Statement statement : (List<Statement>) parent.statements()) {
if (statement instanceof SwitchCase && ((SwitchCase) statement).isDefault()) {
// insert //$CASES-OMITTED$:
ASTRewrite rewrite = ASTRewrite.create(ast);
rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
ListRewrite listRewrite = rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
ASTNode casesOmittedComment = //$NON-NLS-1$
rewrite.createStringPlaceholder("//$CASES-OMITTED$", ASTNode.EMPTY_STATEMENT);
listRewrite.insertBefore(casesOmittedComment, statement, null);
String label = CorrectionMessages.LocalCorrectionsSubProcessor_insert_cases_omitted;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_CASES_OMITTED, image);
proposals.add(proposal);
break;
}
}
}
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class LocalCorrectionsSubProcessor method getInvalidOperatorProposals.
public static void getInvalidOperatorProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
CompilationUnit root = context.getASTRoot();
AST ast = root.getAST();
ASTNode selectedNode = problem.getCoveringNode(root);
while (selectedNode instanceof ParenthesizedExpression) {
selectedNode = ((ParenthesizedExpression) selectedNode).getExpression();
}
if (selectedNode instanceof PrefixExpression) {
// !x instanceof X -> !(x instanceof X)
PrefixExpression expression = (PrefixExpression) selectedNode;
if (expression.getOperator() == PrefixExpression.Operator.NOT) {
ASTNode parent = expression.getParent();
String label = null;
switch(parent.getNodeType()) {
case ASTNode.INSTANCEOF_EXPRESSION:
label = CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_instanceof_description;
break;
case ASTNode.INFIX_EXPRESSION:
InfixExpression infixExpression = (InfixExpression) parent;
label = Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_description, infixExpression.getOperator().toString());
break;
}
if (label != null) {
ASTRewrite rewrite = ASTRewrite.create(ast);
rewrite.replace(selectedNode, rewrite.createMoveTarget(expression.getOperand()), null);
ParenthesizedExpression newParentExpr = ast.newParenthesizedExpression();
newParentExpr.setExpression((Expression) rewrite.createMoveTarget(parent));
PrefixExpression newPrefixExpr = ast.newPrefixExpression();
newPrefixExpr.setOperand(newParentExpr);
newPrefixExpr.setOperator(PrefixExpression.Operator.NOT);
rewrite.replace(parent, newPrefixExpr, null);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVALID_OPERATOR, image);
proposals.add(proposal);
}
}
} else if (selectedNode instanceof InfixExpression && isBitOperation((((InfixExpression) selectedNode).getOperator()))) {
// a & b == c -> (a & b) == c
final CompareInBitWiseOpFinder opFinder = new CompareInBitWiseOpFinder(selectedNode);
if (opFinder.getCompareExpression() != null) {
// compare operation inside bit operations: set parents
String label = CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_bitop_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
CUCorrectionProposal proposal = new CUCorrectionProposal(label, context.getCompilationUnit(), IProposalRelevance.INVALID_OPERATOR, image) {
@Override
protected void addEdits(IDocument document, TextEdit edit) throws CoreException {
InfixExpression compareExpression = opFinder.getCompareExpression();
InfixExpression expression = opFinder.getParentInfixExpression();
ASTNode left = compareExpression.getLeftOperand();
if (expression.getStartPosition() < left.getStartPosition()) {
edit.addChild(new InsertEdit(expression.getStartPosition(), String.valueOf('(')));
edit.addChild(new InsertEdit(ASTNodes.getExclusiveEnd(left), String.valueOf(')')));
}
ASTNode rigth = compareExpression.getRightOperand();
int selEnd = ASTNodes.getExclusiveEnd(expression);
if (selEnd > ASTNodes.getExclusiveEnd(rigth)) {
edit.addChild(new InsertEdit(rigth.getStartPosition(), String.valueOf('(')));
edit.addChild(new InsertEdit(selEnd, String.valueOf(')')));
}
}
};
proposals.add(proposal);
}
}
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class ModifierCorrectionSubProcessor method addNativeMethodProposals.
public static void addNativeMethodProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(astRoot);
if (selectedNode == null) {
return;
}
MethodDeclaration decl;
if (selectedNode instanceof SimpleName) {
decl = (MethodDeclaration) selectedNode.getParent();
} else if (selectedNode instanceof MethodDeclaration) {
decl = (MethodDeclaration) selectedNode;
} else {
return;
}
{
AST ast = astRoot.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
removeModifier(decl, rewrite, Modifier.NATIVE);
Block newBody = ast.newBlock();
rewrite.set(decl, MethodDeclaration.BODY_PROPERTY, newBody, null);
Type returnType = decl.getReturnType2();
if (returnType != null) {
Expression expr = ASTNodeFactory.newDefaultExpression(ast, returnType, decl.getExtraDimensions());
if (expr != null) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expr);
newBody.statements().add(returnStatement);
}
}
String label = CorrectionMessages.ModifierCorrectionSubProcessor_removenative_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_NATIVE, image);
proposals.add(proposal);
}
if (decl.getBody() != null) {
ASTRewrite rewrite = ASTRewrite.create(decl.getAST());
rewrite.remove(decl.getBody(), null);
String label = CorrectionMessages.ModifierCorrectionSubProcessor_removebody_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal2 = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_METHOD_BODY, image);
proposals.add(proposal2);
}
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class ModifierCorrectionSubProcessor method addOverridingDeprecatedMethodProposal.
public static void addOverridingDeprecatedMethodProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (!(selectedNode instanceof MethodDeclaration)) {
return;
}
boolean is50OrHigher = JavaModelUtil.is50OrHigher(cu.getJavaProject());
MethodDeclaration methodDecl = (MethodDeclaration) selectedNode;
AST ast = methodDecl.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
if (is50OrHigher) {
Annotation annot = ast.newMarkerAnnotation();
//$NON-NLS-1$
annot.setTypeName(ast.newName("Deprecated"));
rewrite.getListRewrite(methodDecl, methodDecl.getModifiersProperty()).insertFirst(annot, null);
}
Javadoc javadoc = methodDecl.getJavadoc();
if (javadoc != null || !is50OrHigher) {
if (!is50OrHigher) {
javadoc = ast.newJavadoc();
rewrite.set(methodDecl, MethodDeclaration.JAVADOC_PROPERTY, javadoc, null);
}
TagElement newTag = ast.newTagElement();
newTag.setTagName(TagElement.TAG_DEPRECATED);
JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
}
String label = CorrectionMessages.ModifierCorrectionSubProcessor_overrides_deprecated_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.OVERRIDES_DEPRECATED, image);
proposals.add(proposal);
}
use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.
the class ModifierCorrectionSubProcessor method addMethodRequiresBodyProposals.
public static void addMethodRequiresBodyProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
AST ast = context.getASTRoot().getAST();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (!(selectedNode instanceof MethodDeclaration)) {
return;
}
MethodDeclaration decl = (MethodDeclaration) selectedNode;
Modifier modifierNode;
{
ASTRewrite rewrite = ASTRewrite.create(ast);
modifierNode = removeModifier(decl, rewrite, Modifier.ABSTRACT);
Block body = ast.newBlock();
rewrite.set(decl, MethodDeclaration.BODY_PROPERTY, body, null);
if (!decl.isConstructor()) {
Type returnType = decl.getReturnType2();
if (returnType != null) {
Expression expression = ASTNodeFactory.newDefaultExpression(ast, returnType, decl.getExtraDimensions());
if (expression != null) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
body.statements().add(returnStatement);
}
}
}
String label = CorrectionMessages.ModifierCorrectionSubProcessor_addmissingbody_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_MISSING_BODY, image);
proposals.add(proposal);
}
IMethodBinding binding = decl.resolveBinding();
if (modifierNode == null && binding != null) {
String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertoabstract_description, getMethodLabel(binding));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
int included = binding.getDeclaringClass().isInterface() ? Modifier.NONE : Modifier.ABSTRACT;
int excluded = Modifier.STATIC | Modifier.DEFAULT;
ModifierChangeCorrectionProposal proposal = new ModifierChangeCorrectionProposal(label, cu, binding, decl, included, excluded, IProposalRelevance.ADD_ABSTRACT_MODIFIER, image);
proposals.add(proposal);
}
}
Aggregations