use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class AndroidViewHolderCleanUp method visit.
@Override
public boolean visit(final MethodDeclaration node) {
Block body = node.getBody();
if (body != null && // $NON-NLS-1$//$NON-NLS-2$
ASTNodes.usesGivenSignature(// $NON-NLS-1$//$NON-NLS-2$
node, // $NON-NLS-1$//$NON-NLS-2$
"android.widget.Adapter", // $NON-NLS-1$//$NON-NLS-2$
"getView", // $NON-NLS-1$
int.class.getSimpleName(), // $NON-NLS-1$
"android.view.View", "android.view.ViewGroup")) {
// $NON-NLS-1$
GetViewVariableVisitor visitor = new GetViewVariableVisitor();
body.accept(visitor);
if (visitor.canApplyRefactoring()) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.AndroidViewHolderCleanUp_description);
TypeNameDecider typeNameDecider = new TypeNameDecider(visitor.viewVariableName);
// Transform tree
// Create If statement
SingleVariableDeclaration viewArg = (SingleVariableDeclaration) node.parameters().get(1);
Variable convertViewVar = new Variable(viewArg.getName().getIdentifier(), ast);
InfixExpression newInfixExpression = ast.newInfixExpression();
newInfixExpression.setLeftOperand(convertViewVar.varName());
newInfixExpression.setOperator(InfixExpression.Operator.EQUALS);
newInfixExpression.setRightOperand(ast.newNullLiteral());
InfixExpression condition = newInfixExpression;
Block thenBlock = ast.newBlock();
IfStatement newIfStatement = ast.newIfStatement();
newIfStatement.setExpression(condition);
newIfStatement.setThenStatement(thenBlock);
rewrite.insertBefore(newIfStatement, visitor.viewAssignmentStatement, group);
List<Statement> thenStatements = thenBlock.statements();
thenStatements.add(ast.newExpressionStatement(ast.newAssignment(convertViewVar.varName(), Assignment.Operator.ASSIGN, ast.createCopyTarget(visitor.getInflateExpression()))));
// Assign to local view variable when necessary
if (!"convertView".equals(visitor.viewVariableName.getIdentifier())) {
// $NON-NLS-1$
Statement assignConvertViewToView = null;
if (visitor.viewVariableDeclFragment != null) {
assignConvertViewToView = ast.declareStatement(ast.copyType(visitor.viewVariableName, typeNameDecider), ast.createCopyTarget(visitor.viewVariableName), convertViewVar.varName());
} else if (visitor.viewVariableAssignment != null) {
assignConvertViewToView = ast.newExpressionStatement(ast.newAssignment(ast.createCopyTarget(visitor.viewVariableName), Assignment.Operator.ASSIGN, convertViewVar.varName()));
}
if (assignConvertViewToView != null) {
rewrite.insertBefore(assignConvertViewToView, visitor.viewAssignmentStatement, group);
}
}
// Make sure method returns the view to be reused
if (visitor.returnStatement != null) {
rewrite.insertAfter(ast.newReturnStatement(ast.createCopyTarget(visitor.viewVariableName)), visitor.returnStatement, group);
rewrite.remove(visitor.returnStatement, group);
}
// Optimize findViewById calls
FindViewByIdVisitor findViewByIdVisitor = new FindViewByIdVisitor(visitor.viewVariableName);
body.accept(findViewByIdVisitor);
if (!findViewByIdVisitor.items.isEmpty()) {
// TODO JNR name conflict? Use VariableNameDecider
// $NON-NLS-1$ //$NON-NLS-2$
Variable viewHolderItemVar = new Variable("ViewHolderItem", "viewHolderItem", ast);
// Create ViewHolderItem class
rewrite.insertBefore(createViewHolderItemClass(findViewByIdVisitor, viewHolderItemVar.typeName(), typeNameDecider), node, group);
// Declare viewhHolderItem object
rewrite.insertFirst(body, Block.STATEMENTS_PROPERTY, viewHolderItemVar.declareStatement(), group);
// Initialize viewHolderItem
thenStatements.add(ast.newExpressionStatement(ast.newAssignment(viewHolderItemVar.varName(), Assignment.Operator.ASSIGN, ast.newClassInstanceCreation(viewHolderItemVar.type()))));
// Assign findViewById to ViewHolderItem
for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) {
// Ensure we are accessing convertView object
FieldAccess fieldAccess = ast.newFieldAccess(viewHolderItemVar.varName(), ast.newSimpleName(item.variable.getIdentifier()));
// FIXME This does not work: not sure why??
// rewrite.set(item.findViewByIdInvocation,
// MethodInvocation.EXPRESSION_PROPERTY, convertViewVar.varName());
item.findViewByIdInvocation.setExpression(convertViewVar.varName());
// FIXME For some reason ast.copy() does not do what we would like
thenStatements.add(ast.newExpressionStatement(ast.newAssignment(fieldAccess, Assignment.Operator.ASSIGN, ast.copySubtree(item.findViewByIdExpression))));
// Replace previous findViewById with accesses to viewHolderItem
rewrite.replace(item.findViewByIdExpression, ast.createCopyTarget(fieldAccess), group);
}
MethodInvocation setTagMethod = ast.newMethodInvocation();
// $NON-NLS-1$
setTagMethod.setExpression(ASTNodeFactory.newName(ast, "convertView"));
// $NON-NLS-1$
setTagMethod.setName(ast.newSimpleName("setTag"));
setTagMethod.arguments().add(viewHolderItemVar.varName());
// Store viewHolderItem in convertView
thenStatements.add(ast.newExpressionStatement(setTagMethod));
Block newBlock = ast.newBlock();
MethodInvocation getTagMethod = ast.newMethodInvocation();
// $NON-NLS-1$
getTagMethod.setExpression(ASTNodeFactory.newName(ast, "convertView"));
// $NON-NLS-1$
getTagMethod.setName(ast.newSimpleName("getTag"));
newBlock.statements().add(ast.newExpressionStatement(ast.newAssignment(viewHolderItemVar.varName(), Assignment.Operator.ASSIGN, ast.newCastExpression(viewHolderItemVar.type(), getTagMethod))));
// Retrieve viewHolderItem from convertView
newIfStatement.setElseStatement(newBlock);
}
rewrite.remove(visitor.viewAssignmentStatement, group);
return false;
}
}
return true;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class AnnotationCleanUp method visit.
@Override
public boolean visit(final NormalAnnotation node) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.AnnotationCleanUp_description);
List<MemberValuePair> values = node.values();
if (values.isEmpty()) {
rewrite.replace(node, ast.newMarkerAnnotation(ASTNodes.createMoveTarget(rewrite, node.getTypeName())), group);
return false;
}
if (values.size() == 1) {
MemberValuePair pair = values.get(0);
if ("value".equals(pair.getName().getIdentifier())) {
// $NON-NLS-1$
rewrite.replace(node, ast.newSingleMemberAnnotation(ASTNodes.createMoveTarget(rewrite, node.getTypeName()), ASTNodes.createMoveTarget(rewrite, pair.getValue())), group);
return false;
}
}
boolean result = true;
Map<String, IMethodBinding> elements = toElementsMap(node.resolveAnnotationBinding());
for (MemberValuePair pair : values) {
IMethodBinding elementBinding = elements.get(pair.getName().getIdentifier());
if (equal(elementBinding.getReturnType(), pair.getValue(), elementBinding.getDefaultValue())) {
rewrite.remove(pair, group);
result = false;
} else if (pair.getValue().getNodeType() == ASTNode.ARRAY_INITIALIZER) {
ArrayInitializer arrayInit = (ArrayInitializer) pair.getValue();
List<?> exprs = arrayInit.expressions();
if (exprs.size() == 1) {
rewrite.replace(arrayInit, ASTNodes.createMoveTarget(rewrite, (Expression) exprs.get(0)), group);
result = false;
}
}
}
return result;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class ArrayListRatherThanVectorCleanUp method refactorMethod.
@Override
protected void refactorMethod(final MethodInvocation originalMi, final MethodInvocation refactoredMi) {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
if (ASTNodes.usesGivenSignature(originalMi, Vector.class.getCanonicalName(), "addElement", Object.class.getCanonicalName())) {
// $NON-NLS-1$
// $NON-NLS-1$
refactoredMi.setName(ast.newSimpleName("add"));
} else if (ASTNodes.usesGivenSignature(originalMi, Vector.class.getCanonicalName(), "elementAt", int.class.getSimpleName())) {
// $NON-NLS-1$
// $NON-NLS-1$
refactoredMi.setName(ast.newSimpleName("get"));
} else if (ASTNodes.usesGivenSignature(originalMi, Vector.class.getCanonicalName(), "copyInto", Object[].class.getCanonicalName())) {
// $NON-NLS-1$
// $NON-NLS-1$
refactoredMi.setName(ast.newSimpleName("toArray"));
} else if (// $NON-NLS-1$
ASTNodes.usesGivenSignature(originalMi, Vector.class.getCanonicalName(), "removeElement", Object.class.getCanonicalName()) || ASTNodes.usesGivenSignature(originalMi, Vector.class.getCanonicalName(), "removeElementAt", int.class.getSimpleName())) {
// $NON-NLS-1$
// $NON-NLS-1$
refactoredMi.setName(ast.newSimpleName("remove"));
} else if (ASTNodes.usesGivenSignature(originalMi, Vector.class.getCanonicalName(), "removeAllElements")) {
// $NON-NLS-1$
// $NON-NLS-1$
refactoredMi.setName(ast.newSimpleName("clear"));
} else if (ASTNodes.usesGivenSignature(originalMi, Vector.class.getCanonicalName(), "insertElementAt", Object.class.getCanonicalName(), int.class.getSimpleName())) {
// $NON-NLS-1$
// $NON-NLS-1$
refactoredMi.setName(ast.newSimpleName("add"));
reorderArguments(refactoredMi);
} else if (ASTNodes.usesGivenSignature(originalMi, Vector.class.getCanonicalName(), "setElementAt", Object.class.getCanonicalName(), int.class.getSimpleName())) {
// $NON-NLS-1$
// $NON-NLS-1$
refactoredMi.setName(ast.newSimpleName("set"));
reorderArguments(refactoredMi);
}
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class AssignRatherThanTernaryFilterThenAssignAnywayCleanUp method replaceWithValue.
private void replaceWithValue(final ConditionalExpression visited, final Expression valuedExpression) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.AssignRatherThanTernaryFilterThenAssignAnywayCleanUp_description);
rewrite.replace(visited, ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, valuedExpression)), group);
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class BooleanCleanUp method newExpressionOrNull.
private Expression newExpressionOrNull(final ITypeBinding typeBinding, final Expression condition, final Expression thenExpression, final Expression elseExpression) {
Boolean thenLiteral = ASTNodes.getBooleanLiteral(thenExpression);
Boolean elseLiteral = ASTNodes.getBooleanLiteral(elseExpression);
if (areNegatedBooleanValues(thenExpression, elseExpression)) {
return getExpression(signExpression(condition, thenLiteral), typeBinding.getQualifiedName(), condition);
}
if ((ASTNodes.isPrimitive(thenExpression) || ASTNodes.isPrimitive(elseExpression)) && ASTNodes.hasType(typeBinding, boolean.class.getCanonicalName(), Boolean.class.getCanonicalName())) {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
// care
if (thenLiteral != null && elseLiteral == null) {
InfixExpression newInfixExpression = ast.newInfixExpression();
newInfixExpression.setLeftOperand(signExpression(condition, thenLiteral));
newInfixExpression.setOperator(getConditionalOperator(thenLiteral));
newInfixExpression.setRightOperand(ast.createCopyTarget(elseExpression));
return newInfixExpression;
}
if (thenLiteral == null && elseLiteral != null) {
InfixExpression newInfixExpression = ast.newInfixExpression();
newInfixExpression.setLeftOperand(signExpression(condition, !elseLiteral));
newInfixExpression.setOperator(getConditionalOperator(elseLiteral));
newInfixExpression.setRightOperand(ast.createCopyTarget(thenExpression));
return newInfixExpression;
}
}
return null;
}
Aggregations