use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.
the class LocalCorrectionsSubProcessor method getUnnecessaryElseProposals.
public static void getUnnecessaryElseProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
CompilationUnit root = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(root);
if (selectedNode == null) {
return;
}
ASTNode parent = selectedNode.getParent();
if (parent instanceof ExpressionStatement) {
parent = parent.getParent();
}
if (!(parent instanceof IfStatement)) {
return;
}
IfStatement ifStatement = (IfStatement) parent;
ASTNode ifParent = ifStatement.getParent();
if (!(ifParent instanceof Block) && !(ifParent instanceof SwitchStatement) && !ASTNodes.isControlStatementBody(ifStatement.getLocationInParent())) {
return;
}
ASTRewrite rewrite = ASTRewrite.create(root.getAST());
ASTNode placeholder = QuickAssistProcessor.getCopyOfInner(rewrite, ifStatement.getElseStatement(), false);
if (placeholder == null) {
return;
}
rewrite.remove(ifStatement.getElseStatement(), null);
if (ifParent instanceof Block) {
ListRewrite listRewrite = rewrite.getListRewrite(ifParent, Block.STATEMENTS_PROPERTY);
listRewrite.insertAfter(placeholder, ifStatement, null);
} else if (ifParent instanceof SwitchStatement) {
ListRewrite listRewrite = rewrite.getListRewrite(ifParent, SwitchStatement.STATEMENTS_PROPERTY);
listRewrite.insertAfter(placeholder, ifStatement, null);
} else {
Block block = root.getAST().newBlock();
rewrite.replace(ifStatement, block, null);
block.statements().add(rewrite.createCopyTarget(ifStatement));
block.statements().add(placeholder);
}
String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeelse_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_ELSE, image);
proposals.add(proposal);
}
use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.
the class LocalCorrectionsSubProcessor method getInterfaceExtendsClassProposals.
public static void getInterfaceExtendsClassProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
CompilationUnit root = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(root);
if (selectedNode == null) {
return;
}
while (selectedNode.getParent() instanceof Type) {
selectedNode = selectedNode.getParent();
}
StructuralPropertyDescriptor locationInParent = selectedNode.getLocationInParent();
if (locationInParent != TypeDeclaration.SUPERCLASS_TYPE_PROPERTY) {
return;
}
TypeDeclaration typeDecl = (TypeDeclaration) selectedNode.getParent();
{
ASTRewrite rewrite = ASTRewrite.create(root.getAST());
ASTNode placeHolder = rewrite.createMoveTarget(selectedNode);
ListRewrite interfaces = rewrite.getListRewrite(typeDecl, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
interfaces.insertFirst(placeHolder, null);
String label = CorrectionMessages.LocalCorrectionsSubProcessor_extendstoimplements_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_EXTENDS_TO_IMPLEMENTS, image);
proposals.add(proposal);
}
{
ASTRewrite rewrite = ASTRewrite.create(root.getAST());
rewrite.set(typeDecl, TypeDeclaration.INTERFACE_PROPERTY, Boolean.TRUE, null);
String typeName = typeDecl.getName().getIdentifier();
String label = Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_classtointerface_description, BasicElementLabels.getJavaElementName(typeName));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_CLASS_TO_INTERFACE, image);
proposals.add(proposal);
}
}
use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.
the class AddUnimplementedMethodsOperation method rewriteAST.
/**
* {@inheritDoc}
*/
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {
IMethodBinding[] unimplementedMethods = getUnimplementedMethods(fTypeNode);
if (unimplementedMethods.length == 0)
return;
ImportRewriteContext context = new ContextSensitiveImportRewriteContext((CompilationUnit) fTypeNode.getRoot(), fTypeNode.getStartPosition(), cuRewrite.getImportRewrite());
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ICompilationUnit unit = cuRewrite.getCu();
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(unit.getJavaProject());
ListRewrite listRewrite;
if (fTypeNode instanceof AnonymousClassDeclaration) {
AnonymousClassDeclaration decl = (AnonymousClassDeclaration) fTypeNode;
listRewrite = rewrite.getListRewrite(decl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
settings.createComments = false;
} else if (fTypeNode instanceof AbstractTypeDeclaration) {
AbstractTypeDeclaration decl = (AbstractTypeDeclaration) fTypeNode;
listRewrite = rewrite.getListRewrite(decl, decl.getBodyDeclarationsProperty());
} else if (fTypeNode instanceof EnumConstantDeclaration) {
EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) fTypeNode;
AnonymousClassDeclaration anonymousClassDeclaration = enumConstantDeclaration.getAnonymousClassDeclaration();
if (anonymousClassDeclaration == null) {
anonymousClassDeclaration = rewrite.getAST().newAnonymousClassDeclaration();
rewrite.set(enumConstantDeclaration, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, anonymousClassDeclaration, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
}
listRewrite = rewrite.getListRewrite(anonymousClassDeclaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
settings.createComments = false;
} else {
//$NON-NLS-1$
Assert.isTrue(false, "Unknown type node");
return;
}
ImportRewrite imports = cuRewrite.getImportRewrite();
for (int i = 0; i < unimplementedMethods.length; i++) {
IMethodBinding curr = unimplementedMethods[i];
MethodDeclaration newMethodDecl = StubUtility2.createImplementationStub(unit, rewrite, imports, context, curr, curr.getDeclaringClass().getName(), settings, false);
listRewrite.insertLast(newMethodDecl, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
}
}
use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.
the class ConvertIterableLoopOperation method convert.
@Override
protected Statement convert(CompilationUnitRewrite cuRewrite, final TextEditGroup group, final LinkedProposalModel positionGroups) throws CoreException {
final AST ast = cuRewrite.getAST();
final ASTRewrite astRewrite = cuRewrite.getASTRewrite();
final ImportRewrite importRewrite = cuRewrite.getImportRewrite();
final ImportRemover remover = cuRewrite.getImportRemover();
fEnhancedForLoop = ast.newEnhancedForStatement();
String[] names = getVariableNameProposals();
String name;
if (fElementVariable != null) {
name = fElementVariable.getName();
} else {
name = names[0];
}
final LinkedProposalPositionGroup pg = positionGroups.getPositionGroup(name, true);
if (fElementVariable != null)
pg.addProposal(name, null, 10);
for (int i = 0; i < names.length; i++) {
pg.addProposal(names[i], null, 10);
}
final Statement body = getForStatement().getBody();
if (body != null) {
final ListRewrite list;
if (body instanceof Block) {
list = astRewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
for (final Iterator<Expression> iterator = fOccurrences.iterator(); iterator.hasNext(); ) {
final Statement parent = (Statement) ASTNodes.getParent(iterator.next(), Statement.class);
if (parent != null && list.getRewrittenList().contains(parent)) {
list.remove(parent, null);
remover.registerRemovedNode(parent);
}
}
} else {
list = null;
}
final String text = name;
body.accept(new ASTVisitor() {
private boolean replace(final Expression expression) {
final SimpleName node = ast.newSimpleName(text);
astRewrite.replace(expression, node, group);
remover.registerRemovedNode(expression);
pg.addPosition(astRewrite.track(node), false);
return false;
}
@Override
public final boolean visit(final MethodInvocation node) {
final IMethodBinding binding = node.resolveMethodBinding();
if (binding != null && (binding.getName().equals("next") || binding.getName().equals("nextElement"))) {
//$NON-NLS-1$ //$NON-NLS-2$
final Expression expression = node.getExpression();
if (expression instanceof Name) {
final IBinding result = ((Name) expression).resolveBinding();
if (result != null && result.equals(fIteratorVariable))
return replace(node);
} else if (expression instanceof FieldAccess) {
final IBinding result = ((FieldAccess) expression).resolveFieldBinding();
if (result != null && result.equals(fIteratorVariable))
return replace(node);
}
}
return super.visit(node);
}
@Override
public final boolean visit(final SimpleName node) {
if (fElementVariable != null) {
final IBinding binding = node.resolveBinding();
if (binding != null && binding.equals(fElementVariable)) {
final Statement parent = (Statement) ASTNodes.getParent(node, Statement.class);
if (parent != null && (list == null || list.getRewrittenList().contains(parent)))
pg.addPosition(astRewrite.track(node), false);
}
}
return false;
}
});
fEnhancedForLoop.setBody(getBody(cuRewrite, group, positionGroups));
}
final SingleVariableDeclaration declaration = ast.newSingleVariableDeclaration();
final SimpleName simple = ast.newSimpleName(name);
pg.addPosition(astRewrite.track(simple), true);
declaration.setName(simple);
final ITypeBinding elementType = getElementType(fIteratorVariable.getType());
declaration.setType(importType(elementType, getForStatement(), importRewrite, getRoot()));
if (fMakeFinal) {
ModifierRewrite.create(astRewrite, declaration).setModifiers(Modifier.FINAL, 0, group);
}
remover.registerAddedImport(elementType.getQualifiedName());
fEnhancedForLoop.setParameter(declaration);
fEnhancedForLoop.setExpression(getExpression(astRewrite));
for (Iterator<Expression> iterator = getForStatement().initializers().iterator(); iterator.hasNext(); ) {
ASTNode node = iterator.next();
if (node instanceof VariableDeclarationExpression) {
VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression) node;
remover.registerRemovedNode(variableDeclarationExpression.getType());
} else {
remover.registerRemovedNode(node);
}
}
for (Iterator<Expression> iterator = getForStatement().updaters().iterator(); iterator.hasNext(); ) {
ASTNode node = iterator.next();
remover.registerRemovedNode(node);
}
return fEnhancedForLoop;
}
use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.
the class StatementRewrite method handleOneMany.
@Override
protected void handleOneMany(ASTNode[] replacements, TextEditGroup description) {
AST ast = fToReplace[0].getAST();
// need to insert a block to not change structure
if (ASTNodes.isControlStatementBody(fDescriptor)) {
Block block = ast.newBlock();
ListRewrite statements = fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
for (int i = 0; i < replacements.length; i++) {
statements.insertLast(replacements[i], description);
}
fRewrite.replace(fToReplace[0], block, description);
} else {
ListRewrite container = fRewrite.getListRewrite(fToReplace[0].getParent(), (ChildListPropertyDescriptor) fDescriptor);
container.replace(fToReplace[0], replacements[0], description);
for (int i = 1; i < replacements.length; i++) {
container.insertAfter(replacements[i], replacements[i - 1], description);
}
}
}
Aggregations