use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class AndroidWakeLockCleanUp method createWakelockReleaseStatement.
private Statement createWakelockReleaseStatement(final MethodInvocation methodInvocation) {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
IfStatement newIfStatement = ast.newIfStatement();
MethodInvocation isHeldMethod = ast.newMethodInvocation();
isHeldMethod.setExpression(ast.copyExpression(methodInvocation));
// $NON-NLS-1$
isHeldMethod.setName(ast.newSimpleName("isHeld"));
newIfStatement.setExpression(ast.not(isHeldMethod));
Block newBlock = ast.newBlock();
MethodInvocation releaseMethod = ast.newMethodInvocation();
releaseMethod.setExpression(ast.copyExpression(methodInvocation));
// $NON-NLS-1$
releaseMethod.setName(ast.newSimpleName("release"));
newBlock.statements().add(ast.newExpressionStatement(releaseMethod));
newIfStatement.setThenStatement(newBlock);
return newIfStatement;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class AbstractEnumCollectionReplacementCleanUp method handleAssignment.
private boolean handleAssignment(final ClassInstanceCreation node, final Assignment a, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
Expression leftHandSide = a.getLeftHandSide();
if (isTargetType(leftHandSide.resolveTypeBinding())) {
ITypeBinding[] typeArguments = leftHandSide.resolveTypeBinding().getTypeArguments();
if (typeArguments.length > 0 && typeArguments[0].isEnum()) {
TypeNameDecider typeNameDecider = new TypeNameDecider(leftHandSide);
ASTNodeFactory ast = cuRewrite.getASTBuilder();
Type[] types = new Type[typeArguments.length];
for (int i = 0; i < types.length; i++) {
types[i] = ast.toType(typeArguments[i], typeNameDecider);
}
return maybeReplace(node, classesToUseWithImport, importsToAdd, types);
}
}
return true;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class AbstractUnitTestCleanUp method maybeRefactorToEquality.
private boolean maybeRefactorToEquality(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final ASTNode nodeToReplace, final MethodInvocation originalMethod, final boolean isAssertEquals, final Expression actualValue, final Expression expectedValue, final Expression failureMessage, final boolean isRewriteNeeded) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
// $NON-NLS-1$
TextEditGroup group = new TextEditGroup("");
if (ASTNodes.is(actualValue, NullLiteral.class)) {
ASTNodes.replaceButKeepComment(rewrite, nodeToReplace, invokeMethodOrStatement(nodeToReplace, invokeAssertNull(classesToUseWithImport, importsToAdd, originalMethod, isAssertEquals, expectedValue, failureMessage)), group);
return false;
}
if (ASTNodes.is(expectedValue, NullLiteral.class)) {
ASTNodes.replaceButKeepComment(rewrite, nodeToReplace, invokeMethodOrStatement(nodeToReplace, invokeAssertNull(classesToUseWithImport, importsToAdd, originalMethod, isAssertEquals, actualValue, failureMessage)), group);
return false;
}
Expression copyOfExpected;
Expression copyOfActual;
boolean localIsRewriteNeeded;
if ((ASTNodes.isHardCoded(actualValue) || isVariableNamedExpected(actualValue)) && !ASTNodes.isHardCoded(expectedValue) && !isVariableNamedExpected(expectedValue)) {
copyOfExpected = ast.createCopyTarget(ASTNodes.getUnparenthesedExpression(actualValue));
copyOfActual = ast.createCopyTarget(ASTNodes.getUnparenthesedExpression(expectedValue));
localIsRewriteNeeded = true;
} else {
copyOfExpected = ast.createCopyTarget(ASTNodes.getUnparenthesedExpression(expectedValue));
copyOfActual = ast.createCopyTarget(ASTNodes.getUnparenthesedExpression(actualValue));
localIsRewriteNeeded = isRewriteNeeded;
}
if (localIsRewriteNeeded) {
Expression delta = null;
if (ASTNodes.hasType(actualValue, double.class.getCanonicalName()) && ASTNodes.hasType(expectedValue, double.class.getCanonicalName())) {
// $NON-NLS-1$
delta = ast.newNumberLiteral(".0");
} else if (ASTNodes.hasType(actualValue, float.class.getCanonicalName()) && ASTNodes.hasType(expectedValue, float.class.getCanonicalName())) {
// $NON-NLS-1$
delta = ast.newNumberLiteral(".0F");
}
MethodInvocation newAssert = invokeMethod(classesToUseWithImport, importsToAdd, originalMethod, getAssertName(isAssertEquals, "Equals"), copyOfActual, copyOfExpected, delta, // $NON-NLS-1$
failureMessage);
ASTNodes.replaceButKeepComment(rewrite, nodeToReplace, invokeMethodOrStatement(nodeToReplace, newAssert), group);
return false;
}
return true;
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class SingleDeclarationsRatherThanMultiDeclarationCleanUp method refactorMultiDeclaration.
private void refactorMultiDeclaration(final ASTNode visited, final List<IExtendedModifier> modifiers, final Type type, final List<VariableDeclarationFragment> fragments, final Javadoc docComment) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.SingleDeclarationsRatherThanMultiDeclarationCleanUp_description);
for (int i = fragments.size() - 1; 0 <= i; i--) {
VariableDeclarationFragment fragment = fragments.get(i);
SimpleName copyOfFragment = ASTNodes.createMoveTarget(rewrite, fragment.getName());
Type copyOfType = ast.createCopyTarget(type);
Expression copyOfInitializer;
if (fragment.getInitializer() != null) {
copyOfInitializer = ASTNodes.createMoveTarget(rewrite, fragment.getInitializer());
} else {
copyOfInitializer = null;
}
VariableDeclarationFragment newFragment = ast.newVariableDeclarationFragment(copyOfFragment, copyOfInitializer);
newFragment.extraDimensions().addAll(ASTNodes.createMoveTarget(rewrite, fragment.extraDimensions()));
ASTNode newNode;
if (visited instanceof VariableDeclarationStatement) {
VariableDeclarationStatement newStatement = ast.newVariableDeclarationStatement(copyOfType, newFragment);
updateModifiers(modifiers, newStatement.modifiers());
newNode = newStatement;
} else {
FieldDeclaration newField = ast.newFieldDeclaration(copyOfType, newFragment);
if (docComment != null) {
newField.setJavadoc(ast.createCopyTarget(docComment));
}
updateModifiers(modifiers, newField.modifiers());
newNode = newField;
}
if (i > 0) {
rewrite.insertAfter(newNode, visited, group);
} else {
ASTNodes.replaceButKeepComment(rewrite, visited, newNode, group);
}
}
}
use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.
the class StringBuilderCleanUp method replaceWithString.
private void replaceWithString(final Type type, final Expression initializer, final List<Pair<ITypeBinding, Expression>> allAppendedStrings, final List<Statement> statementsToRemove, final List<MethodInvocation> toStringToRefactor) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.StringBuilderCleanUp_description);
rewrite.replace(initializer, createStringConcats(allAppendedStrings), group);
ASTNodes.replaceButKeepComment(rewrite, type, ast.type(String.class.getSimpleName()), group);
for (Statement statementToRemove : statementsToRemove) {
ASTNodes.removeButKeepComment(rewrite, statementToRemove, group);
}
for (MethodInvocation readToRefactor : toStringToRefactor) {
rewrite.replace(readToRefactor, ASTNodes.createMoveTarget(rewrite, readToRefactor.getExpression()), group);
}
}
Aggregations