use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.
the class IntroduceFactoryRefactoring method rewriteFactoryMethodCall.
/**
* Updates the constructor call.
*
* @param ctorCall the ClassInstanceCreation to be marked as replaced
* @param unitRewriter the AST rewriter
* @param gd the edit group to use
*/
private void rewriteFactoryMethodCall(ClassInstanceCreation ctorCall, ASTRewrite unitRewriter, TextEditGroup gd) {
AST ast = unitRewriter.getAST();
MethodInvocation factoryMethodCall = ast.newMethodInvocation();
ASTNode ctorCallParent = ctorCall.getParent();
StructuralPropertyDescriptor ctorCallLocation = ctorCall.getLocationInParent();
if (ctorCallLocation instanceof ChildListPropertyDescriptor) {
ListRewrite ctorCallParentListRewrite = unitRewriter.getListRewrite(ctorCallParent, (ChildListPropertyDescriptor) ctorCallLocation);
int index = ctorCallParentListRewrite.getOriginalList().indexOf(ctorCall);
ctorCall = (ClassInstanceCreation) ctorCallParentListRewrite.getRewrittenList().get(index);
} else {
ctorCall = (ClassInstanceCreation) unitRewriter.get(ctorCallParent, ctorCallLocation);
}
ListRewrite actualFactoryArgs = unitRewriter.getListRewrite(factoryMethodCall, MethodInvocation.ARGUMENTS_PROPERTY);
ListRewrite actualCtorArgs = unitRewriter.getListRewrite(ctorCall, ClassInstanceCreation.ARGUMENTS_PROPERTY);
// Need to use a qualified name for the factory method if we're not
// in the context of the class holding the factory.
AbstractTypeDeclaration callOwner = (AbstractTypeDeclaration) ASTNodes.getParent(ctorCall, AbstractTypeDeclaration.class);
ITypeBinding callOwnerBinding = callOwner.resolveBinding();
if (callOwnerBinding == null || !Bindings.equals(callOwner.resolveBinding(), fFactoryOwningClass.resolveBinding())) {
String qualifier = fImportRewriter.addImport(fFactoryOwningClass.resolveBinding());
factoryMethodCall.setExpression(ASTNodeFactory.newName(ast, qualifier));
}
factoryMethodCall.setName(ast.newSimpleName(fNewMethodName));
List<Expression> actualCtorArgsList = actualCtorArgs.getRewrittenList();
for (int i = 0; i < actualCtorArgsList.size(); i++) {
Expression actualCtorArg = actualCtorArgsList.get(i);
ASTNode movedArg;
if (ASTNodes.isExistingNode(actualCtorArg)) {
movedArg = unitRewriter.createMoveTarget(actualCtorArg);
} else {
unitRewriter.remove(actualCtorArg, null);
movedArg = actualCtorArg;
}
actualFactoryArgs.insertLast(movedArg, gd);
}
unitRewriter.replace(ctorCall, factoryMethodCall, gd);
}
use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.
the class PromoteTempToFieldRefactoring method addLocalDeclarationSplit.
private void addLocalDeclarationSplit(ASTRewrite rewrite) {
VariableDeclarationStatement tempDeclarationStatement = getTempDeclarationStatement();
ASTNode parentStatement = tempDeclarationStatement.getParent();
ListRewrite listRewrite;
if (parentStatement instanceof SwitchStatement) {
listRewrite = rewrite.getListRewrite(parentStatement, SwitchStatement.STATEMENTS_PROPERTY);
} else if (parentStatement instanceof Block) {
listRewrite = rewrite.getListRewrite(parentStatement, Block.STATEMENTS_PROPERTY);
} else {
// should not happen. VariableDeclaration's can not be in a control statement body
listRewrite = null;
Assert.isTrue(false);
}
int statementIndex = listRewrite.getOriginalList().indexOf(tempDeclarationStatement);
Assert.isTrue(statementIndex != -1);
Statement newStatement = createNewAssignmentStatement(rewrite);
List<VariableDeclarationFragment> fragments = tempDeclarationStatement.fragments();
int fragmentIndex = fragments.indexOf(fTempDeclarationNode);
Assert.isTrue(fragmentIndex != -1);
if (fragments.size() == 1) {
rewrite.replace(tempDeclarationStatement, newStatement, null);
return;
}
for (int i1 = fragmentIndex, n = fragments.size(); i1 < n; i1++) {
VariableDeclarationFragment fragment = fragments.get(i1);
rewrite.remove(fragment, null);
}
if (fragmentIndex == 0)
rewrite.remove(tempDeclarationStatement, null);
Assert.isTrue(tempHasInitializer());
listRewrite.insertAt(newStatement, statementIndex + 1, null);
if (fragmentIndex + 1 < fragments.size()) {
VariableDeclarationFragment firstFragmentAfter = fragments.get(fragmentIndex + 1);
VariableDeclarationFragment copyfirstFragmentAfter = (VariableDeclarationFragment) rewrite.createCopyTarget(firstFragmentAfter);
VariableDeclarationStatement statement = getAST().newVariableDeclarationStatement(copyfirstFragmentAfter);
Type type = (Type) rewrite.createCopyTarget(tempDeclarationStatement.getType());
statement.setType(type);
List<IExtendedModifier> modifiers = tempDeclarationStatement.modifiers();
if (modifiers.size() > 0) {
ListRewrite modifiersRewrite = rewrite.getListRewrite(tempDeclarationStatement, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
ASTNode firstModifier = (ASTNode) modifiers.get(0);
ASTNode lastModifier = (ASTNode) modifiers.get(modifiers.size() - 1);
ASTNode modifiersCopy = modifiersRewrite.createCopyTarget(firstModifier, lastModifier);
statement.modifiers().add(modifiersCopy);
}
for (int i = fragmentIndex + 2; i < fragments.size(); i++) {
VariableDeclarationFragment fragment = fragments.get(i);
VariableDeclarationFragment fragmentCopy = (VariableDeclarationFragment) rewrite.createCopyTarget(fragment);
statement.fragments().add(fragmentCopy);
}
listRewrite.insertAt(statement, statementIndex + 2, null);
}
}
use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project bayou by capergroup.
the class Visitor method postprocessGlobal.
/**
* Performs global post-processing of synthesized code:
* - Adds import declarations
*
* @param ast the owner of the document
* @param env environment that was used for synthesis
* @param document draft program document
* @throws BadLocationException if an error occurred when rewriting document
*/
private void postprocessGlobal(AST ast, Environment env, Document document) throws BadLocationException {
/* add imports */
ASTRewrite rewriter = ASTRewrite.create(ast);
ListRewrite lrw = rewriter.getListRewrite(cu, CompilationUnit.IMPORTS_PROPERTY);
Set<Class> toImport = new HashSet<>(env.imports);
// add all catch(...) types to imports
toImport.addAll(sketch.exceptionsThrown());
for (Class cls : toImport) {
while (cls.isArray()) cls = cls.getComponentType();
if (cls.isPrimitive() || cls.getPackage().getName().equals("java.lang"))
continue;
ImportDeclaration impDecl = cu.getAST().newImportDeclaration();
String className = cls.getName().replaceAll("\\$", "\\.");
impDecl.setName(cu.getAST().newName(className.split("\\.")));
lrw.insertLast(impDecl, null);
}
rewriter.rewriteAST(document, null).apply(document);
}
use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project eclipse-pmd by acanda.
the class UseUtilityClassQuickFix method addFinalIfNecessary.
private void addFinalIfNecessary(final TypeDeclaration typeDeclaration, final ASTRewrite rewrite) {
@SuppressWarnings("unchecked") final List<IExtendedModifier> modifiers = typeDeclaration.modifiers();
if (!Iterables.any(modifiers, isFinal())) {
final ListRewrite modifierRewrite = rewrite.getListRewrite(typeDeclaration, TypeDeclaration.MODIFIERS2_PROPERTY);
final Modifier modifier = (Modifier) typeDeclaration.getAST().createInstance(Modifier.class);
modifier.setKeyword(ModifierKeyword.FINAL_KEYWORD);
modifierRewrite.insertLast(modifier, null);
}
}
use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project eclipse-pmd by acanda.
the class UseUtilityClassQuickFix method addPrivateConstructor.
@SuppressWarnings("unchecked")
private void addPrivateConstructor(final TypeDeclaration typeDeclaration, final ASTRewrite rewrite) {
final AST ast = typeDeclaration.getAST();
final MethodDeclaration constructor = (MethodDeclaration) ast.createInstance(MethodDeclaration.class);
constructor.setConstructor(true);
final Modifier modifier = (Modifier) ast.createInstance(Modifier.class);
modifier.setKeyword(ModifierKeyword.PRIVATE_KEYWORD);
constructor.modifiers().add(modifier);
constructor.setName(ASTUtil.copy(typeDeclaration.getName()));
final Block body = (Block) ast.createInstance(Block.class);
constructor.setBody(body);
final ListRewrite statementRewrite = rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
final ASTNode comment = rewrite.createStringPlaceholder("// hide constructor of utility class", ASTNode.EMPTY_STATEMENT);
statementRewrite.insertFirst(comment, null);
final int position = findConstructorPosition(typeDeclaration);
final ListRewrite bodyDeclarationRewrite = rewrite.getListRewrite(typeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
bodyDeclarationRewrite.insertAt(constructor, position, null);
}
Aggregations