Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Recycle Rule. Closes #212 #225

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

luiscruz
Copy link
Collaborator

@luiscruz luiscruz commented Nov 1, 2016

No description provided.


@Override
public boolean visit(Assignment node) {
if (ASTHelper.isSameLocalVariable(node.getLeftHandSide(), cursorSimpleName)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change all uses of ASTHelper methods and fields to use static imports.


private static boolean isMethodIgnoringParameters(MethodInvocation node, String typeQualifiedName,
String[] methodNames) {
boolean isSameMethod;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move declaration to line 88.

}

private static boolean isMethodIgnoringParameters(MethodInvocation node, String typeQualifiedName,
String[] methodNames) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String[] => String...

if (isMethodIgnoringParameters(
node,
"android.database.sqlite.SQLiteDatabase",
new String[]{"query", "rawQuery", "queryWithFactory", "rawQueryWithFactory"})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After moving to variable arguments, I think you can get rid of this new String[] { ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the other ones in this method down below.

String recycleMethodName = methodNameToCleanupResource(node);
if (recycleMethodName != null) {
SimpleName cursorExpression = null;
ASTNode variableAssignmentNode = null;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not assign null:

            SimpleName cursorExpression;
            ASTNode variableAssignmentNode;

if (!closePresenceChecker.isClosePresent()) {
final ASTBuilder b = this.ctx.getASTBuilder();
MethodInvocation closeInvocation = b.invoke(b.copy(cursorExpression), recycleMethodName);
ExpressionStatement expressionStatement = b.getAST().newExpressionStatement(closeInvocation);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these 3 lines inside the then block of the next if statement.

* @param recycleMethodName Recycle method name
*/
public ClosePresenceChecker(SimpleName cursorSimpleName, String recycleMethodName) {
super();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

*/
public ClosePresenceChecker(SimpleName cursorSimpleName, String recycleMethodName) {
super();
this.closePresent = false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default value. Please remove.

public ClosePresenceChecker(SimpleName cursorSimpleName, String recycleMethodName) {
super();
this.closePresent = false;
this.lastCursorUse = null;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default value. Please remove.


/**
* @return the closePresent
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove empty javadoc.

@@ -0,0 +1,124 @@
package org.autorefactor.refactoring.rules.samples_in;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add correct copyright.

@@ -0,0 +1,141 @@
package org.autorefactor.refactoring.rules.samples_out;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add correct copyright.

import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import static org.autorefactor.refactoring.ASTHelper.*;
import static org.eclipse.jdt.core.dom.ASTNode.*;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import :)

public String getDescription() {
return "Many resources, such as TypedArrays, VelocityTrackers, etc., should be "
+ "recycled (with a recycle()/close() call) after use. " + "Inspired from "
+ "https://android.googlesource.com/platform/tools/base/+/master/lint/libs/lint-checks/src/main/"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this displayed to the user?

* TODO (low priority) check whether resources are being used after release.
* TODO add support for FragmentTransaction.beginTransaction(). It can use method
* chaining (which means local variable might not be present) and it can be released
* by two methods: commit() and commitAllowingStateLoss().
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a bit of work left :)


@Override
public String getName() {
return "RecycleRefactoring";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again this is displayed to the user "Android recycle" would be better.

final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
MethodInvocation closeInvocation = b.invoke(b.copy(cursorExpression), recycleMethodName);
ExpressionStatement expressionStatement = b.getAST().newExpressionStatement(closeInvocation);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b.toStmt()

return "Many resources, such as TypedArrays, VelocityTrackers, etc., should be "
+ "recycled (with a recycle()/close() call) after use. " + "Inspired from "
+ "https://android.googlesource.com/platform/tools/base/+/master/lint/libs/lint-checks/src/main/"
+ "java/com/android/tools/lint/checks/CleanupDetector.java";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code in this class is a lot nicer than CleanupDetector.java.

"ROUTE_ID=?",
new String[]{Long.toString(route_id)},
null, null, null);
cursor.close();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am very surprised this is not closed in a try / finally block.
Any reason for that?

Same comment for all others down below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the reason but it is not common in Android db queries:
https://www.codota.com/android/methods/android.database.sqlite.SQLiteDatabase/query

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently they are all bad practice:

Phew! I was worried for a second.
I think AutoRefactor should do the right thing here, i.e. use a try / finally block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants