Skip to content

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.

@Fabrice-TIERCELIN Fabrice-TIERCELIN force-pushed the master branch 6 times, most recently from 3e0fe43 to 90aac00 Compare April 28, 2020 18:28
@Fabrice-TIERCELIN Fabrice-TIERCELIN force-pushed the master branch 2 times, most recently from e9111cd to d11ddab Compare June 14, 2020 07:44
@Fabrice-TIERCELIN Fabrice-TIERCELIN force-pushed the master branch 8 times, most recently from a1b70c6 to cc358af Compare October 10, 2020 18:24
@Fabrice-TIERCELIN Fabrice-TIERCELIN force-pushed the master branch 5 times, most recently from b87707a to 74160ad Compare January 3, 2021 05:34
@Fabrice-TIERCELIN Fabrice-TIERCELIN force-pushed the master branch 3 times, most recently from 2f35a94 to e5cce10 Compare May 2, 2021 13:19
@Fabrice-TIERCELIN Fabrice-TIERCELIN force-pushed the master branch 6 times, most recently from 4f9c5c6 to 86bde66 Compare May 28, 2021 06:11
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