diff options
author | Tor Norbye <tnorbye@google.com> | 2012-03-26 10:04:57 -0600 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-04-02 08:39:47 -0700 |
commit | 38ea719c80b0bc3977087d9cd0609ccb22b4ceda (patch) | |
tree | 6ed7ead8218e966a46c29d059d09cee8be11d7e1 /lint/libs | |
parent | ff2c433c175d70a1ab96bc10274c5a869f3ac638 (diff) | |
download | sdk-38ea719c80b0bc3977087d9cd0609ccb22b4ceda.zip sdk-38ea719c80b0bc3977087d9cd0609ccb22b4ceda.tar.gz sdk-38ea719c80b0bc3977087d9cd0609ccb22b4ceda.tar.bz2 |
27629: DrawAllocation doesn't catch allocations done in View.layout()
Change-Id: I2784712f5b298e7103c12d04bc7ae7fbeefd9cdf
Diffstat (limited to 'lint/libs')
2 files changed, 74 insertions, 2 deletions
diff --git a/lint/libs/lint_checks/src/com/android/tools/lint/checks/JavaPerformanceDetector.java b/lint/libs/lint_checks/src/com/android/tools/lint/checks/JavaPerformanceDetector.java index 2c125b5..b15d7fa 100644 --- a/lint/libs/lint_checks/src/com/android/tools/lint/checks/JavaPerformanceDetector.java +++ b/lint/libs/lint_checks/src/com/android/tools/lint/checks/JavaPerformanceDetector.java @@ -44,7 +44,9 @@ import lombok.ast.If; import lombok.ast.MethodDeclaration; import lombok.ast.MethodInvocation; import lombok.ast.Node; +import lombok.ast.Select; import lombok.ast.StrictListAccessor; +import lombok.ast.This; import lombok.ast.Throw; import lombok.ast.TypeReference; import lombok.ast.TypeReferencePart; @@ -127,6 +129,7 @@ public class JavaPerformanceDetector extends Detector implements Detector.JavaSc private static final String ON_DRAW = "onDraw"; //$NON-NLS-1$ private static final String ON_LAYOUT = "onLayout"; //$NON-NLS-1$ private static final String ON_MEASURE = "onMeasure"; //$NON-NLS-1$ + private static final String LAYOUT = "layout"; //$NON-NLS-1$ /** Constructs a new {@link JavaPerformanceDetector} check */ public JavaPerformanceDetector() { @@ -346,6 +349,11 @@ public class JavaPerformanceDetector extends Detector implements Detector.JavaSc } else if (expression instanceof VariableReference) { VariableReference reference = (VariableReference) expression; variables.add(reference.astIdentifier().astValue()); + } else if (expression instanceof Select) { + Select select = (Select) expression; + if (select.astOperand() instanceof This) { + variables.add(select.astIdentifier().astValue()); + } } } @@ -354,7 +362,8 @@ public class JavaPerformanceDetector extends Detector implements Detector.JavaSc * where allocating objects is not allowed for performance reasons */ private boolean isBlockedAllocationMethod(MethodDeclaration node) { - return isOnDrawMethod(node) || isOnMeasureMethod(node) || isOnLayoutMethod(node); + return isOnDrawMethod(node) || isOnMeasureMethod(node) || isOnLayoutMethod(node) + || isLayoutMethod(node); } /** @@ -435,6 +444,34 @@ public class JavaPerformanceDetector extends Detector implements Detector.JavaSc } /** + * Returns true if this method looks like it's overriding android.view.View's + * {@code public void layout(int l, int t, int r, int b)} + */ + private static boolean isLayoutMethod(MethodDeclaration node) { + if (LAYOUT.equals(node.astMethodName().astValue())) { + StrictListAccessor<VariableDefinition, MethodDeclaration> parameters = + node.astParameters(); + if (parameters != null && parameters.size() == 4) { + Iterator<VariableDefinition> iterator = parameters.iterator(); + for (int i = 0; i < 4; i++) { + if (!iterator.hasNext()) { + return false; + } + VariableDefinition next = iterator.next(); + TypeReferencePart type = next.astTypeReference().astParts().last(); + if (!INT.equals(type.getTypeName())) { + return false; + } + } + return true; + } + } + + return false; + } + + + /** * Checks whether the given constructor call and type reference refers * to a HashMap constructor call that is eligible for replacement by a * SparseArray call instead @@ -482,7 +519,14 @@ public class JavaPerformanceDetector extends Detector implements Detector.JavaSc public boolean visitBinaryExpression(BinaryExpression node) { BinaryOperator operator = node.astOperator(); if (operator == BinaryOperator.ASSIGN || operator == BinaryOperator.OR_ASSIGN) { - mVariables.add(node.astLeft().toString()); + Expression left = node.astLeft(); + String variable; + if (left instanceof Select && ((Select) left).astOperand() instanceof This) { + variable = ((Select) left).astIdentifier().astValue(); + } else { + variable = left.toString(); + } + mVariables.add(variable); } return super.visitBinaryExpression(node); diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/src/test/pkg/JavaPerformanceTest.java.txt b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/src/test/pkg/JavaPerformanceTest.java.txt index daee930..55834ec 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/src/test/pkg/JavaPerformanceTest.java.txt +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/src/test/pkg/JavaPerformanceTest.java.txt @@ -157,4 +157,32 @@ public class JavaPerformanceTest extends Button { private boolean mAllowCrop; private Canvas mOverlayCanvas; private Bitmap mOverlay; + + @Override + public void layout(int l, int t, int r, int b) { + // Using "this." to reference fields + if (this.shader == null) + this.shader = new LinearGradient(0, 0, getWidth(), 0, GRADIENT_COLORS, null, + TileMode.REPEAT); + } + + @Override + public void layout(int l, int t, int r, int b) { + int width = getWidth(); + int height = getHeight(); + + if ((shader == null) || (lastWidth != width) || (lastHeight != height)) + { + lastWidth = width; + lastHeight = height; + + shader = new LinearGradient(0, 0, width, 0, GRADIENT_COLORS, null, TileMode.REPEAT); + } + } + + @Override + public void layout(int l, int t, int r, int b) { + if ((shader == null) || (lastWidth != getWidth()) || (lastHeight != getHeight())) { + } + } } |