Skip to content

Android Recycle Rule. Closes #212#225

Open
luiscruz wants to merge 12 commits intoJnRouvignac:masterfrom
luiscruz:recycle
Open

Android Recycle Rule. Closes #212#225
luiscruz wants to merge 12 commits intoJnRouvignac:masterfrom
luiscruz:recycle

Conversation

@luiscruz
Copy link
Copy Markdown
Collaborator

@luiscruz luiscruz commented Nov 1, 2016

No description provided.


@Override
public boolean visit(Assignment node) {
if (ASTHelper.isSameLocalVariable(node.getLeftHandSide(), cursorSimpleName)) {
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@Fabrice-TIERCELIN Fabrice-TIERCELIN force-pushed the master branch 6 times, most recently from aebd2b8 to 0d63a43 Compare September 14, 2019 17:41
@Fabrice-TIERCELIN Fabrice-TIERCELIN force-pushed the master branch 6 times, most recently from 87b5052 to 67259b6 Compare October 3, 2019 17:57
@Fabrice-TIERCELIN Fabrice-TIERCELIN force-pushed the master branch 18 times, most recently from a7cdf4a to e446e46 Compare October 7, 2019 16:31
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.

3 participants