use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class AbstractPrimitiveRatherThanWrapperCleanUp method refactorWrapper.
private void refactorWrapper(final VariableDeclarationStatement visited, final Expression initializer, final List<MethodInvocation> toStringMethods, final List<MethodInvocation> compareToMethods, final List<MethodInvocation> primitiveValueMethods) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
// $NON-NLS-1$
TextEditGroup group = new TextEditGroup("");
String parsingMethodName = getParsingMethodName(getWrapperFullyQualifiedName());
if (initializer instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) initializer;
if (ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", getPrimitiveTypeName())) {
// $NON-NLS-1$
rewrite.replace(methodInvocation, ASTNodes.createMoveTarget(rewrite, (Expression) methodInvocation.arguments().get(0)), group);
}
if (// $NON-NLS-1$
ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", String.class.getCanonicalName()) || ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", String.class.getCanonicalName(), int.class.getSimpleName())) {
// $NON-NLS-1$
rewrite.set(methodInvocation, MethodInvocation.NAME_PROPERTY, ast.newSimpleName(parsingMethodName), group);
}
} else if (initializer instanceof ClassInstanceCreation) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) initializer;
List<Expression> classInstanceCreationArguments = classInstanceCreation.arguments();
if (classInstanceCreationArguments.size() == 1 && parsingMethodName != null && !Character.class.getCanonicalName().equals(getWrapperFullyQualifiedName()) && ASTNodes.hasType(classInstanceCreation, getWrapperFullyQualifiedName())) {
Expression arg0 = classInstanceCreationArguments.get(0);
if (ASTNodes.hasType(arg0, String.class.getCanonicalName())) {
MethodInvocation newMethodInvocation = ast.newMethodInvocation();
newMethodInvocation.setExpression(rewrite.createCopyTarget(((SimpleType) visited.getType()).getName()));
newMethodInvocation.setName(ast.newSimpleName(parsingMethodName));
newMethodInvocation.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(arg0)));
ASTNodes.replaceButKeepComment(rewrite, initializer, newMethodInvocation, group);
}
}
}
for (MethodInvocation primitiveValueMethod : primitiveValueMethods) {
rewrite.replace(primitiveValueMethod, ASTNodes.createMoveTarget(rewrite, primitiveValueMethod.getExpression()), group);
}
for (MethodInvocation toStringMethod : toStringMethods) {
Type wrapperType = rewrite.createCopyTarget(visited.getType());
rewrite.insertFirst(toStringMethod, MethodInvocation.ARGUMENTS_PROPERTY, ASTNodes.createMoveTarget(rewrite, toStringMethod.getExpression()), group);
rewrite.set(toStringMethod, MethodInvocation.EXPRESSION_PROPERTY, wrapperType, group);
}
for (MethodInvocation compareToMethod : compareToMethods) {
Type wrapperType = rewrite.createCopyTarget(visited.getType());
rewrite.insertFirst(compareToMethod, MethodInvocation.ARGUMENTS_PROPERTY, ASTNodes.createMoveTarget(rewrite, compareToMethod.getExpression()), group);
rewrite.set(compareToMethod, MethodInvocation.EXPRESSION_PROPERTY, wrapperType, group);
// $NON-NLS-1$
rewrite.replace(compareToMethod.getName(), ast.newSimpleName("compare"), group);
}
Type primitiveType = ast.type(getPrimitiveTypeName());
ASTNodes.replaceButKeepComment(rewrite, visited.getType(), primitiveType, group);
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class AndroidWakeLockCleanUp method visit.
@Override
public boolean visit(final MethodInvocation node) {
if (ASTNodes.usesGivenSignature(node, "android.os.PowerManager.WakeLock", "release")) {
// $NON-NLS-1$ //$NON-NLS-2$
// Check whether it is being called in onDestroy()
MethodDeclaration enclosingMethod = ASTNodes.getTypedAncestor(node, MethodDeclaration.class);
if (ASTNodes.usesGivenSignature(enclosingMethod, "android.app.Activity", "onDestroy")) {
// $NON-NLS-1$ //$NON-NLS-2$
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.AndroidWakeLockCleanUp_description);
TypeDeclaration typeDeclaration = ASTNodes.getTypedAncestor(enclosingMethod, TypeDeclaration.class);
// $NON-NLS-1$
MethodDeclaration onPauseMethod = findMethod(typeDeclaration, "onPause");
if (onPauseMethod != null && node.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
rewrite.remove(node.getParent(), group);
rewrite.insertLast(onPauseMethod.getBody(), Block.STATEMENTS_PROPERTY, createWakelockReleaseStatement(node), group);
} else {
// Add the missing onPause() method to the class.
rewrite.insertAfter(createOnPauseMethodDeclaration(), enclosingMethod, group);
}
return false;
}
} else if (ASTNodes.usesGivenSignature(node, "android.os.PowerManager.WakeLock", "acquire")) {
// $NON-NLS-1$ //$NON-NLS-2$
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.AndroidWakeLockCleanUp_description);
TypeDeclaration typeDeclaration = ASTNodes.getTypedAncestor(node, TypeDeclaration.class);
ReleasePresenceChecker releasePresenceChecker = new ReleasePresenceChecker();
if (!releasePresenceChecker.findOrDefault(typeDeclaration, false)) {
// $NON-NLS-1$
MethodDeclaration onPauseMethod = findMethod(typeDeclaration, "onPause");
if (onPauseMethod != null && node.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
rewrite.insertLast(onPauseMethod.getBody(), Block.STATEMENTS_PROPERTY, createWakelockReleaseStatement(node), group);
} else {
rewrite.insertLast(typeDeclaration, typeDeclaration.getBodyDeclarationsProperty(), createOnPauseMethodDeclaration(), group);
}
return false;
}
}
return true;
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class AbstractUnitTestCleanUp method invokeAssertNull.
private MethodInvocation invokeAssertNull(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final MethodInvocation originalMethod, final boolean isPositive, final Expression actual, final Expression failureMessage) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
// $NON-NLS-1$
String methodName = getAssertName(isPositive, "Null");
Expression copyOfActual = ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(actual));
return invokeMethod(classesToUseWithImport, importsToAdd, originalMethod, methodName, copyOfActual, null, null, failureMessage);
}
use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.
the class AbstractUnitTestCleanUp method refactorToAssertTrueOrFalse.
private void refactorToAssertTrueOrFalse(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final ASTNode nodeToReplace, final MethodInvocation originalMethod, final Expression failureMessage, final Expression condition, final boolean isAssertTrue) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
// $NON-NLS-1$
TextEditGroup group = new TextEditGroup("");
// $NON-NLS-1$ //$NON-NLS-2$
String methodName = isAssertTrue ? "assertTrue" : "assertFalse";
ASTNodes.replaceButKeepComment(rewrite, nodeToReplace, invokeMethodOrStatement(nodeToReplace, invokeMethod(classesToUseWithImport, importsToAdd, originalMethod, methodName, ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(condition)), null, null, failureMessage)), group);
}
use of org.autorefactor.jdt.core.dom.ASTRewrite 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;
}
Aggregations