From e37856e9fcdffb530ceb5182d5b17fb4076b5df3 Mon Sep 17 00:00:00 2001 From: Siva Velusamy Date: Tue, 1 Jul 2014 09:07:02 -0700 Subject: ADT: Fix NPE in InstrumentationRunnerValidator This CL adds a few checks to guard against NPEs that could occur if the manifest parsing failed (e.g. due to a missing manifest) Change-Id: I82a9b3b1fa3d651c13125798f70b61e5492e0838 --- .../junit/AndroidJUnitLaunchConfigurationTab.java | 21 ++++++++++----------- .../junit/InstrumentationRunnerValidator.java | 13 +++++++++++-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/junit/AndroidJUnitLaunchConfigurationTab.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/junit/AndroidJUnitLaunchConfigurationTab.java index 4f02fa9..038f0b9 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/junit/AndroidJUnitLaunchConfigurationTab.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/junit/AndroidJUnitLaunchConfigurationTab.java @@ -1017,18 +1017,17 @@ public class AndroidJUnitLaunchConfigurationTab extends AbstractLaunchConfigurat */ private void loadInstrumentations(IProject project) { try { - mInstrValidator = new InstrumentationRunnerValidator(project); - mInstrumentations = (mInstrValidator == null ? null : - mInstrValidator.getInstrumentationNames()); - if (mInstrumentations != null) { - mInstrumentationCombo.removeAll(); - for (String instrumentation : mInstrumentations) { - mInstrumentationCombo.add(instrumentation); + mInstrValidator = new InstrumentationRunnerValidator(project); + mInstrumentations = mInstrValidator.getInstrumentationNames(); + if (mInstrumentations.length > 0) { + mInstrumentationCombo.removeAll(); + for (String instrumentation : mInstrumentations) { + mInstrumentationCombo.add(instrumentation); + } + // the selection will be set when we update the ui from the current + // config object. + return; } - // the selection will be set when we update the ui from the current - // config object. - return; - } } catch (CoreException e) { AdtPlugin.logAndPrintError(e, project.getName(), LaunchMessages.AndroidJUnitTab_LoadInstrError_s, diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/junit/InstrumentationRunnerValidator.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/junit/InstrumentationRunnerValidator.java index 54fd207..820fed7 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/junit/InstrumentationRunnerValidator.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/junit/InstrumentationRunnerValidator.java @@ -16,6 +16,8 @@ package com.android.ide.eclipse.adt.internal.launch.junit; import com.android.SdkConstants; +import com.android.annotations.NonNull; +import com.android.annotations.Nullable; import com.android.ide.common.xml.ManifestData; import com.android.ide.common.xml.ManifestData.Instrumentation; import com.android.ide.common.xml.ManifestData.UsesLibrary; @@ -71,6 +73,12 @@ class InstrumentationRunnerValidator { } private void init(ManifestData manifestData) { + if (manifestData == null) { + mInstrumentationNames = new String[0]; + mHasRunnerLibrary = false; + return; + } + Instrumentation[] instrumentations = manifestData.getInstrumentations(); mInstrumentationNames = new String[instrumentations.length]; for (int i = 0; i < instrumentations.length; i++) { @@ -98,9 +106,9 @@ class InstrumentationRunnerValidator { /** * Return the set of instrumentation names for the Android project. * - * @return null if error occurred parsing instrumentations, otherwise returns array - * of instrumentation class names + * @return array of instrumentation class names, possibly empty */ + @NonNull String[] getInstrumentationNames() { return mInstrumentationNames; } @@ -111,6 +119,7 @@ class InstrumentationRunnerValidator { * @return fully qualified instrumentation class name. null if no valid * instrumentation can be found. */ + @Nullable String getValidInstrumentationTestRunner() { for (String instrumentation : getInstrumentationNames()) { if (validateInstrumentationRunner(instrumentation) == INSTRUMENTATION_OK) { -- cgit v1.1