Android DrawAllocation. Closes #211#227
Conversation
e3ecac5 to
951e781
Compare
| * 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 |
There was a problem hiding this comment.
Neither Fabrice nor I have worked on this yet :)
| @@ -0,0 +1,90 @@ | |||
| package org.autorefactor.refactoring.rules.samples_in; | |||
There was a problem hiding this comment.
Please add correct copyright.
| @@ -0,0 +1,93 @@ | |||
| package org.autorefactor.refactoring.rules.samples_out; | |||
There was a problem hiding this comment.
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(); |
|
|
||
| public boolean isMethodBindingSubclassOf(ITypeBinding typeBinding, List<String> superClassStrings) { | ||
| ITypeBinding superClass = typeBinding; | ||
| while (superClass != null && !superClass.equals(ctx.getAST().resolveWellKnownType("java.lang.Object"))) { |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
if (superClassStrings.contains(superClass.getErasure().getName())) {?
| if (initializer.getNodeType() == ASTNode.CAST_EXPRESSION) { | ||
| initializer = ((CastExpression) initializer).getExpression(); | ||
| } | ||
| if (initializer.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) { |
There was a problem hiding this comment.
Please use a switch:
switch (initializer.getNodeType()) {
case CAST_EXPRESSION:
initializer = ((CastExpression) initializer).getExpression();
break;
case CLASS_INSTANCE_CREATION:
...There was a problem hiding this comment.
Switch will not work as expected. Since initializer is changed inside the first case, there are cases in which both blocks are executed.
There was a problem hiding this comment.
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, |
| // allocate object outside onDraw | ||
| r.insertBefore(b.move(declarationStatement), onDrawDeclaration); | ||
| // call collection.clear() in the end of | ||
| // onDraw |
| // call collection.clear() in the end of | ||
| // onDraw | ||
| ASTNode clearNode = b.getAST() | ||
| .newExpressionStatement(b.invoke(node.getName().getIdentifier(), "clear")); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
List<Statement> bodyStatements = .statements(onDrawDeclaration.getBody());
Statement lastStatement = bodyStatements.get(bodyStatements.size() - 1);|
|
||
| @Override | ||
| public boolean visit(MethodInvocation node) { | ||
| initializerCanBeExtracted = false; |
There was a problem hiding this comment.
It is not checking anything on the MethodInvocation, this seems dubious?
|
|
||
| @Override | ||
| public boolean visit(SimpleName node) { | ||
| initializerCanBeExtracted = false; |
There was a problem hiding this comment.
It is not checking anything on the SimpleName, this seems dubious?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
This is worth a comment in the code IMO
| @SuppressWarnings("unused") | ||
| public class AndroidDrawAllocationSample extends Button { | ||
|
|
||
| public AndroidDrawAllocationSample(Context context, AttributeSet attrs, int defStyle) { |
There was a problem hiding this comment.
I didn't get this one :\
d76e438 to
94112d3
Compare
| if (initializer.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) { | ||
| ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) initializer; | ||
| InitializerVisitor initializerVisitor = new InitializerVisitor(); | ||
| initializer.accept(initializerVisitor); |
There was a problem hiding this comment.
It would look more logical to me here to do:
classInstanceCreation.accept(initializerVisitor);| @Override | ||
| public boolean visit(SimpleType node) { | ||
| return DO_NOT_VISIT_SUBTREE; | ||
| } |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
return DO_NOT_VISIT_SUBTREE;| } else { | ||
| r.insertAfter(clearNode, lastStatement); | ||
| } | ||
| } |
There was a problem hiding this comment.
return DO_NOT_VISIT_SUBTREE;|
|
||
| public boolean isMethodBindingSubclassOf(ITypeBinding typeBinding, List<String> superClassStrings) { | ||
| ITypeBinding superClass = typeBinding; | ||
| ITypeBinding objectType = ctx.getAST().resolveWellKnownType("java.lang.Object"); |
There was a problem hiding this comment.
You do not need that.
Just let superClass become null.
| this.onDrawDeclaration = onDrawDeclaration; | ||
| } | ||
|
|
||
| public boolean isMethodBindingSubclassOf(ITypeBinding typeBinding, List<String> superClassStrings) { |
| new String("foo"); | ||
| String s = new String("bar"); | ||
|
|
||
| // This one should not be reported: |
| import android.content.Context; | ||
| import android.graphics.Bitmap; | ||
| import android.graphics.BitmapFactory; | ||
| import android.graphics.Canvas; |
There was a problem hiding this comment.
These imports are never used:
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;52183d4 to
b649b3d
Compare
b649b3d to
b25b9db
Compare
a395432 to
7cc3557
Compare
aebd2b8 to
0d63a43
Compare
87b5052 to
67259b6
Compare
e446e46 to
4f0cb47
Compare
No description provided.