diff options
author | Tor Norbye <tnorbye@google.com> | 2012-11-28 15:08:09 -0800 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2012-11-28 15:08:10 -0800 |
commit | 868df19f0356f750d9f41a373a3f6713f4fefe56 (patch) | |
tree | 357ee798360e4471f4ff3b0dec4bde3b4c859420 /lint/libs | |
parent | dbdf2bd7268af61a58b99d73ffed2915b6f65406 (diff) | |
parent | 3f73fd4068c4b50f10a55a84faefdb58e60af0a5 (diff) | |
download | sdk-868df19f0356f750d9f41a373a3f6713f4fefe56.zip sdk-868df19f0356f750d9f41a373a3f6713f4fefe56.tar.gz sdk-868df19f0356f750d9f41a373a3f6713f4fefe56.tar.bz2 |
Merge "40424: Wakelock Release check not quite correct"
Diffstat (limited to 'lint/libs')
4 files changed, 55 insertions, 0 deletions
diff --git a/lint/libs/lint_checks/src/com/android/tools/lint/checks/WakelockDetector.java b/lint/libs/lint_checks/src/com/android/tools/lint/checks/WakelockDetector.java index 1642d55..a8ce2f2 100644 --- a/lint/libs/lint_checks/src/com/android/tools/lint/checks/WakelockDetector.java +++ b/lint/libs/lint_checks/src/com/android/tools/lint/checks/WakelockDetector.java @@ -74,6 +74,7 @@ public class WakelockDetector extends Detector implements ClassScanner { private static final String WAKELOCK_OWNER = "android/os/PowerManager$WakeLock"; //$NON-NLS-1$ private static final String RELEASE_METHOD = "release"; //$NON-NLS-1$ private static final String ACQUIRE_METHOD = "acquire"; //$NON-NLS-1$ + private static final String IS_HELD_METHOD = "isHeld"; //$NON-NLS-1$ /** Print diagnostics during analysis (display flow control graph etc). * Make sure you add the asm-debug or asm-util jars to the runtime classpath @@ -239,6 +240,22 @@ public class WakelockDetector extends Detector implements ClassScanner { } } } + } else if (from.getOpcode() == Opcodes.IFEQ) { + JumpInsnNode jump = (JumpInsnNode) from; + if (jump.label == to) { + AbstractInsnNode prev = LintUtils.getPrevInstruction(from); + if (prev != null && prev.getType() == AbstractInsnNode.METHOD_INSN) { + MethodInsnNode method = (MethodInsnNode) prev; + if (method.name.equals(IS_HELD_METHOD) && + method.owner.equals(WAKELOCK_OWNER)) { + AbstractInsnNode next = LintUtils.getNextInstruction(from); + if (next != null) { + super.add(from, next); + return; + } + } + } + } } super.add(from, to); @@ -290,6 +307,8 @@ public class WakelockDetector extends Detector implements ClassScanner { return SEEN_TARGET; } else if (method.name.equals(ACQUIRE_METHOD) && method.owner.equals(WAKELOCK_OWNER)) { // OK + } else if (method.name.equals(IS_HELD_METHOD) && method.owner.equals(WAKELOCK_OWNER)) { + // OK } else { // Some non acquire/release method call: if this is not associated with a // try-catch block, it would mean the exception would exit the method, diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/WakelockDetectorTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/WakelockDetectorTest.java index aaf588c..f136dd9 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/WakelockDetectorTest.java +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/WakelockDetectorTest.java @@ -145,4 +145,16 @@ public class WakelockDetectorTest extends AbstractCheckTest { "bytecode/WakelockActivity7.class.data=>bin/classes/test/pkg/WakelockActivity7.class" )); } + + public void test8() throws Exception { + assertEquals( + "No warnings.", + + lintProject( + "bytecode/.classpath=>.classpath", + "bytecode/AndroidManifest.xml=>AndroidManifest.xml", + "bytecode/WakelockActivity8.java.txt=>src/test/pkg/WakelockActivity8.java", + "bytecode/WakelockActivity8.class.data=>bin/classes/test/pkg/WakelockActivity8.class" + )); + } } diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/bytecode/WakelockActivity8.class.data b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/bytecode/WakelockActivity8.class.data Binary files differnew file mode 100644 index 0000000..5bd3ae2 --- /dev/null +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/bytecode/WakelockActivity8.class.data diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/bytecode/WakelockActivity8.java.txt b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/bytecode/WakelockActivity8.java.txt new file mode 100644 index 0000000..a6307c5 --- /dev/null +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/bytecode/WakelockActivity8.java.txt @@ -0,0 +1,24 @@ +package test.pkg; + +import android.app.Activity; +import android.os.Bundle; +import android.os.PowerManager; +import android.os.PowerManager.WakeLock; + +import com.google.io.demo.R; + +public class WakelockActivity8 extends Activity { + private WakeLock mWakeLock; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + PowerManager manager = (PowerManager) getSystemService(POWER_SERVICE); + mWakeLock = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Test"); + mWakeLock.acquire(); + if (mWakeLock.isHeld()) { + mWakeLock.release(); + } + } +} |