Skip to content

Android DrawAllocation. Closes #211#227

Open
luiscruz wants to merge 1 commit intoJnRouvignac:masterfrom
luiscruz:drawallocation
Open

Android DrawAllocation. Closes #211#227
luiscruz wants to merge 1 commit intoJnRouvignac:masterfrom
luiscruz:drawallocation

Conversation

@luiscruz
Copy link
Copy Markdown
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
Copy Markdown
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
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,93 @@
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.

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
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 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
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 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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;

@Fabrice-TIERCELIN Fabrice-TIERCELIN force-pushed the master branch 5 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 19 times, most recently from e446e46 to 4f0cb47 Compare October 7, 2019 16:43
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