use of org.eclipse.jdt.core.dom.AST in project che by eclipse.
the class ConvertForLoopOperation method convertBody.
private void convertBody(Statement body, final IBinding indexBinding, final IBinding arrayBinding, final String parameterName, final ASTRewrite rewrite, final TextEditGroup editGroup, final LinkedProposalPositionGroup pg) {
final AST ast = body.getAST();
body.accept(new GenericVisitor() {
@Override
public boolean visit(ArrayAccess node) {
IBinding binding = getBinding(node.getArray());
if (arrayBinding.equals(binding)) {
IBinding index = getBinding(node.getIndex());
if (indexBinding.equals(index)) {
replaceAccess(node);
}
}
return super.visit(node);
}
private void replaceAccess(ASTNode node) {
if (fElementDeclaration != null && node.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.getParent();
IBinding targetBinding = fragment.getName().resolveBinding();
if (targetBinding != null) {
VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
if (statement.fragments().size() == 1) {
rewrite.remove(statement, editGroup);
} else {
ListRewrite listRewrite = rewrite.getListRewrite(statement, VariableDeclarationStatement.FRAGMENTS_PROPERTY);
listRewrite.remove(fragment, editGroup);
}
} else {
SimpleName name = ast.newSimpleName(parameterName);
rewrite.replace(node, name, editGroup);
pg.addPosition(rewrite.track(name), true);
}
} else {
SimpleName name = ast.newSimpleName(parameterName);
rewrite.replace(node, name, editGroup);
pg.addPosition(rewrite.track(name), true);
}
}
});
}
use of org.eclipse.jdt.core.dom.AST in project che by eclipse.
the class SelfEncapsulateFieldRefactoring method addGetterSetterChanges.
private List<TextEditGroup> addGetterSetterChanges(CompilationUnit root, ASTRewrite rewriter, String lineDelimiter, boolean usingLocalSetter, boolean usingLocalGetter) throws CoreException {
List<TextEditGroup> result = new ArrayList<TextEditGroup>(2);
AST ast = root.getAST();
FieldDeclaration decl = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, ASTNode.FIELD_DECLARATION);
int position = 0;
int numberOfMethods = 0;
List<BodyDeclaration> members = ASTNodes.getBodyDeclarations(decl.getParent());
for (Iterator<BodyDeclaration> iter = members.iterator(); iter.hasNext(); ) {
BodyDeclaration element = iter.next();
if (element.getNodeType() == ASTNode.METHOD_DECLARATION) {
if (fInsertionIndex == -1) {
break;
} else if (fInsertionIndex == numberOfMethods) {
position++;
break;
}
numberOfMethods++;
}
position++;
}
TextEditGroup description;
ListRewrite rewrite = fRewriter.getListRewrite(decl.getParent(), getBodyDeclarationsProperty(decl.getParent()));
if (!usingLocalGetter) {
description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_add_getter);
result.add(description);
rewrite.insertAt(createGetterMethod(ast, rewriter, lineDelimiter), position++, description);
}
if (!JdtFlags.isFinal(fField) && !usingLocalSetter) {
description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_add_setter);
result.add(description);
rewrite.insertAt(createSetterMethod(ast, rewriter, lineDelimiter), position, description);
}
if (!JdtFlags.isPrivate(fField))
result.add(makeDeclarationPrivate(rewriter, decl));
return result;
}
use of org.eclipse.jdt.core.dom.AST in project che by eclipse.
the class CallInliner method replaceCall.
private void replaceCall(RefactoringStatus status, String[] blocks, TextEditGroup textEditGroup) {
// Inline empty body
if (blocks.length == 0 && fTargetNode != null) {
if (fNeedsStatement) {
fRewrite.replace(fTargetNode, fTargetNode.getAST().newEmptyStatement(), textEditGroup);
} else {
fRewrite.remove(fTargetNode, textEditGroup);
}
} else {
ASTNode node = null;
for (int i = 0; i < blocks.length - 1; i++) {
node = fRewrite.createStringPlaceholder(blocks[i], ASTNode.RETURN_STATEMENT);
fListRewrite.insertAt(node, fInsertionIndex++, textEditGroup);
}
String block = blocks[blocks.length - 1];
// returned expression must be evaluated.
if (fContext.callMode == ASTNode.EXPRESSION_STATEMENT && fSourceProvider.hasReturnValue()) {
if (fSourceProvider.mustEvaluateReturnedExpression()) {
if (fSourceProvider.returnValueNeedsLocalVariable()) {
IMethodBinding invocation = Invocations.resolveBinding(fInvocation);
node = createLocalDeclaration(invocation.getReturnType(), fInvocationScope.createName(fSourceProvider.getMethodName(), true), (Expression) fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION));
} else {
node = fRewrite.getAST().newExpressionStatement((Expression) fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION));
}
} else {
node = null;
}
} else if (fTargetNode instanceof Expression) {
node = fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION);
// fixes bug #24941
if (needsExplicitCast(status)) {
AST ast = node.getAST();
CastExpression castExpression = ast.newCastExpression();
Type returnType = fImportRewrite.addImport(fSourceProvider.getReturnType(), ast);
castExpression.setType(returnType);
if (NecessaryParenthesesChecker.needsParentheses(fSourceProvider.getReturnExpressions().get(0), castExpression, CastExpression.EXPRESSION_PROPERTY)) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression((Expression) node);
node = parenthesized;
}
castExpression.setExpression((Expression) node);
node = castExpression;
if (NecessaryParenthesesChecker.needsParentheses(castExpression, fTargetNode.getParent(), fTargetNode.getLocationInParent())) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression((Expression) node);
node = parenthesized;
}
} else if (fSourceProvider.needsReturnedExpressionParenthesis(fTargetNode.getParent(), fTargetNode.getLocationInParent())) {
ParenthesizedExpression pExp = fTargetNode.getAST().newParenthesizedExpression();
pExp.setExpression((Expression) node);
node = pExp;
}
} else {
node = fRewrite.createStringPlaceholder(block, ASTNode.RETURN_STATEMENT);
}
// Now replace the target node with the source node
if (node != null) {
if (fTargetNode == null) {
fListRewrite.insertAt(node, fInsertionIndex++, textEditGroup);
} else {
fRewrite.replace(fTargetNode, node, textEditGroup);
}
} else {
if (fTargetNode != null) {
fRewrite.remove(fTargetNode, textEditGroup);
}
}
}
}
use of org.eclipse.jdt.core.dom.AST in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method addArgumentsForLocalsUsedInInnerClass.
private void addArgumentsForLocalsUsedInInnerClass(ClassInstanceCreation newClassCreation) {
IVariableBinding[] usedLocals = getUsedLocalVariables();
for (int i = 0; i < usedLocals.length; i++) {
final AST ast = fAnonymousInnerClassNode.getAST();
final IVariableBinding binding = usedLocals[i];
Name name = null;
if (binding.isEnumConstant())
name = ast.newQualifiedName(ast.newSimpleName(binding.getDeclaringClass().getName()), ast.newSimpleName(binding.getName()));
else
name = ast.newSimpleName(binding.getName());
newClassCreation.arguments().add(name);
}
}
use of org.eclipse.jdt.core.dom.AST in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method copyArguments.
private void copyArguments(CompilationUnitRewrite rewrite, ClassInstanceCreation newClassCreation) {
Iterator<Expression> iter = ((ClassInstanceCreation) fAnonymousInnerClassNode.getParent()).arguments().iterator();
if (!iter.hasNext())
return;
IMethodBinding superConstructorBinding = getSuperConstructorBinding();
ITypeBinding[] parameterTypes = superConstructorBinding.getParameterTypes();
List<Expression> arguments = newClassCreation.arguments();
ASTRewrite astRewrite = rewrite.getASTRewrite();
int last = parameterTypes.length - 1;
for (int i = 0; i < last; i++) {
arguments.add((Expression) astRewrite.createCopyTarget(iter.next()));
}
if (superConstructorBinding.isVarargs()) {
AST ast = astRewrite.getAST();
ArrayCreation arrayCreation = ast.newArrayCreation();
arrayCreation.setType((ArrayType) rewrite.getImportRewrite().addImport(parameterTypes[last], ast));
ArrayInitializer initializer = ast.newArrayInitializer();
arrayCreation.setInitializer(initializer);
arguments.add(arrayCreation);
arguments = initializer.expressions();
}
while (iter.hasNext()) {
arguments.add((Expression) astRewrite.createCopyTarget(iter.next()));
}
}
Aggregations