Fix ClassCastException from toArray().

toArray() returns Object[], which can't be cast to Foo[]. Switch to
toArray(Foo[]) instead.

Fixed one instance which caused test breakage, and then all the others
I could find.

Also make sure PackageInstallerSessionTest runs in presubmit. It's a
cheap unit test, and tests are more useful when they run. (This is
what I was trying to do when I discovered the test was currently
failing.)

Fixes: 148858333
Bug: 137951074
Test: atest PackageInstallerSessionTest
Change-Id: Ie0142d46f1a34cf2072aaf81b3a5f8aa76d0c1f2
This commit is contained in:
Alan Stokes
2020-02-04 17:35:15 +00:00
parent 2f6b69c5e7
commit ee59d5462c
3 changed files with 12 additions and 7 deletions

View File

@@ -38,6 +38,8 @@ import android.util.Log;
import android.util.LruCache;
import android.util.Slog;
import libcore.util.EmptyArray;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
@@ -301,7 +303,7 @@ public class ValidateNotificationPeople implements NotificationSignalExtractor {
for (String person: second) {
people.add(person);
}
return (String[]) people.toArray();
return people.toArray(EmptyArray.STRING);
}
@Nullable

View File

@@ -129,6 +129,7 @@ import com.android.server.pm.dex.DexManager;
import com.android.server.security.VerityUtils;
import libcore.io.IoUtils;
import libcore.util.EmptyArray;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@@ -212,7 +213,8 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
private static final String ATTR_SIGNATURE = "signature";
private static final String PROPERTY_NAME_INHERIT_NATIVE = "pi.inherit_native_on_dont_kill";
private static final int[] EMPTY_CHILD_SESSION_ARRAY = {};
private static final int[] EMPTY_CHILD_SESSION_ARRAY = EmptyArray.INT;
private static final FileInfo[] EMPTY_FILE_INFO_ARRAY = {};
private static final String SYSTEM_DATA_LOADER_PACKAGE = "android";
@@ -375,8 +377,6 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
// TODO(b/146080380): merge file list with Callback installation.
private IncrementalFileStorages mIncrementalFileStorages;
private static final String[] EMPTY_STRING_ARRAY = new String[]{};
private static final FileFilter sAddedApkFilter = new FileFilter() {
@Override
public boolean accept(File file) {
@@ -719,7 +719,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
if (!isDataLoaderInstallation()) {
String[] result = stageDir.list();
if (result == null) {
result = EMPTY_STRING_ARRAY;
result = EmptyArray.STRING;
}
return result;
}
@@ -3096,7 +3096,8 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
}
if (grantedRuntimePermissions.size() > 0) {
params.grantedRuntimePermissions = (String[]) grantedRuntimePermissions.toArray();
params.grantedRuntimePermissions =
grantedRuntimePermissions.toArray(EmptyArray.STRING);
}
if (whitelistedRestrictedPermissions.size() > 0) {
@@ -3115,7 +3116,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
FileInfo[] fileInfosArray = null;
if (!files.isEmpty()) {
fileInfosArray = (FileInfo[]) files.toArray();
fileInfosArray = files.toArray(EMPTY_FILE_INFO_ARRAY);
}
InstallSource installSource = InstallSource.create(installInitiatingPackageName,

View File

@@ -24,6 +24,7 @@ import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
import static org.xmlpull.v1.XmlPullParser.START_TAG;
import android.content.pm.PackageInstaller;
import android.platform.test.annotations.Presubmit;
import android.util.AtomicFile;
import android.util.Slog;
import android.util.Xml;
@@ -57,6 +58,7 @@ import java.util.Arrays;
import java.util.List;
@RunWith(AndroidJUnit4.class)
@Presubmit
public class PackageInstallerSessionTest {
@Rule
public TemporaryFolder mTemporaryFolder = new TemporaryFolder();