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 DrawAllocation. Closes #211 #227

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

luiscruz
Copy link
Collaborator

@luiscruz luiscruz commented Nov 2, 2016

No description provided.

* AutoRefactor - Eclipse plugin to automatically refactor Java code bases.
*
* Copyright (C) 2013-2016 Jean-Noël Rouvignac - initial API and implementation
* Copyright (C) 2016 Fabrice Tiercelin - Make sure we do not visit again modified nodes
Copy link
Owner

@JnRouvignac JnRouvignac Nov 4, 2016

Choose a reason for hiding this comment

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

Neither Fabrice nor I have worked on this yet :)

@@ -0,0 +1,90 @@
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,93 @@
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.

public boolean visit(MethodDeclaration node) {
if (isMethod(node, "android.widget.TextView", "onDraw", "android.graphics.Canvas")) {
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = this.ctx.getRefactorings();
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 unused b and r


public boolean isMethodBindingSubclassOf(ITypeBinding typeBinding, List<String> superClassStrings) {
ITypeBinding superClass = typeBinding;
while (superClass != null && !superClass.equals(ctx.getAST().resolveWellKnownType("java.lang.Object"))) {
Copy link
Owner

Choose a reason for hiding this comment

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

Please extract a variable for this out of the loop: ctx.getAST().resolveWellKnownType("java.lang.Object")
I suspect doing this repeatedly in the loop is not great performance wise.

if (className.contains("<")) {
className = className.split("<", 2)[0];
}
if (superClassStrings.contains(className)) {
Copy link
Owner

Choose a reason for hiding this comment

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

                if (superClassStrings.contains(superClass.getErasure().getName())) {

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

looks better 😅

if (initializer.getNodeType() == ASTNode.CAST_EXPRESSION) {
initializer = ((CastExpression) initializer).getExpression();
}
if (initializer.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
Copy link
Owner

Choose a reason for hiding this comment

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

Please use a switch:

                switch (initializer.getNodeType()) {
                case CAST_EXPRESSION:
                    initializer = ((CastExpression) initializer).getExpression();
                    break;

                case CLASS_INSTANCE_CREATION:
                    ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Switch will not work as expected. Since initializer is changed inside the first case, there are cases in which both blocks are executed.

Copy link
Owner

Choose a reason for hiding this comment

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

Indeed, this is not an else if.
Sorry for the noise.

InitializerVisitor initializerVisitor = new InitializerVisitor();
initializer.accept(initializerVisitor);
if (initializerVisitor.initializerCanBeExtracted) {
Statement declarationStatement = (Statement) getFirstAncestorOrNull(node,
Copy link
Owner

Choose a reason for hiding this comment

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

getAncestor()

// allocate object outside onDraw
r.insertBefore(b.move(declarationStatement), onDrawDeclaration);
// call collection.clear() in the end of
// onDraw
Copy link
Owner

Choose a reason for hiding this comment

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

move to previous line

// call collection.clear() in the end of
// onDraw
ASTNode clearNode = b.getAST()
.newExpressionStatement(b.invoke(node.getName().getIdentifier(), "clear"));
Copy link
Owner

Choose a reason for hiding this comment

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

                                    ASTNode clearNode = b.toStmt(b.invoke(node.getName().getIdentifier(), "clear"));

ASTNode clearNode = b.getAST()
.newExpressionStatement(b.invoke(node.getName().getIdentifier(), "clear"));
List bodyStatements = onDrawDeclaration.getBody().statements();
Statement lastStatement = (Statement) bodyStatements.get(bodyStatements.size() - 1);
Copy link
Owner

Choose a reason for hiding this comment

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

                                    List<Statement> bodyStatements = .statements(onDrawDeclaration.getBody());
                                    Statement lastStatement = bodyStatements.get(bodyStatements.size() - 1);


@Override
public boolean visit(MethodInvocation node) {
initializerCanBeExtracted = false;
Copy link
Owner

Choose a reason for hiding this comment

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

It is not checking anything on the MethodInvocation, this seems dubious?


@Override
public boolean visit(SimpleName node) {
initializerCanBeExtracted = false;
Copy link
Owner

Choose a reason for hiding this comment

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

It is not checking anything on the SimpleName, this seems dubious?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So, basically I don't want to refactor expressions like new Foo(bar) or new Foo(bar()). Since I'm moving these expressions to the outside of the method, I will do it only for default constructors or constructors with constants.

Copy link
Owner

Choose a reason for hiding this comment

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

This is worth a comment in the code IMO

@SuppressWarnings("unused")
public class AndroidDrawAllocationSample extends Button {

public AndroidDrawAllocationSample(Context context, AttributeSet attrs, int defStyle) {
Copy link
Owner

Choose a reason for hiding this comment

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

incorrect formatting

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 didn't get this one :\

if (initializer.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) initializer;
InitializerVisitor initializerVisitor = new InitializerVisitor();
initializer.accept(initializerVisitor);
Copy link
Owner

Choose a reason for hiding this comment

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

It would look more logical to me here to do:

                    classInstanceCreation.accept(initializerVisitor);

@Override
public boolean visit(SimpleType node) {
return DO_NOT_VISIT_SUBTREE;
}
Copy link
Owner

Choose a reason for hiding this comment

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

Why this?

I think you should just remove it, or else handle all the possible types (there are plenty ;) )

}
}
} else {
r.insertBefore(b.move(declarationStatement), onDrawDeclaration);
Copy link
Owner

Choose a reason for hiding this comment

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

return DO_NOT_VISIT_SUBTREE;

} else {
r.insertAfter(clearNode, lastStatement);
}
}
Copy link
Owner

Choose a reason for hiding this comment

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

return DO_NOT_VISIT_SUBTREE;


public boolean isMethodBindingSubclassOf(ITypeBinding typeBinding, List<String> superClassStrings) {
ITypeBinding superClass = typeBinding;
ITypeBinding objectType = ctx.getAST().resolveWellKnownType("java.lang.Object");
Copy link
Owner

Choose a reason for hiding this comment

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

You do not need that.
Just let superClass become null.

this.onDrawDeclaration = onDrawDeclaration;
}

public boolean isMethodBindingSubclassOf(ITypeBinding typeBinding, List<String> superClassStrings) {
Copy link
Owner

Choose a reason for hiding this comment

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

This is a type binding :)

new String("foo");
String s = new String("bar");

// This one should not be reported:
Copy link
Owner

Choose a reason for hiding this comment

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

Why not?

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
Copy link
Owner

Choose a reason for hiding this comment

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

These imports are never used:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

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