-
Notifications
You must be signed in to change notification settings - Fork 37
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
base: master
Are you sure you want to change the base?
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.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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"))) { |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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())) {
?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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:
...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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")); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
incorrect formatting
There was a problem hiding this comment.
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 :\
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.
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; | ||
} |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); | ||
} | ||
} |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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;
52183d4
to
b649b3d
Compare
b649b3d
to
b25b9db
Compare
a395432
to
7cc3557
Compare
3e0fe43
to
90aac00
Compare
e9111cd
to
d11ddab
Compare
a1b70c6
to
cc358af
Compare
b87707a
to
74160ad
Compare
2f35a94
to
e5cce10
Compare
4f9c5c6
to
86bde66
Compare
No description provided.