Search code examples
androidarraylistpermissionsandroid-permissionstoarray

Android Unchecked assignment


I am using location services in my app, and where I am setting the permissions, I have this warning: Unchecked assignment: 'java.util.ArrayList' to 'java.util.ArrayList

Code:

permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
    permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
    permissionsToRequest = findUnAskedPermissions(permissions);

The warning comes up on the last line.

I have defined the Arrays like this at the code start:

 ArrayList<String> permissions = new ArrayList<>();
ArrayList<String> permissionsToRequest= new ArrayList<>();
ArrayList<String> permissionsRejected = new ArrayList<>();

Also later in my code, where I ask for the permission (if not allowed by user):

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (permissionsToRequest.size() > 0) {
                requestPermissions(permissionsToRequest.toArray(new String[permissionsToRequest.size()]),
                        ALL_PERMISSIONS_RESULT);
                canGetLocation = false;
            }
        }

here the toArray causes the warning: Call to 'toArray()' with pre-sized array argument 'new String[permissionsToRequest.size()]

Here I can try to replace it with

toArray(new String[0])

but not sure if it is correct.


Solution

  • For the Unchecked assignment: 'java.util.ArrayList' to 'java.util.ArrayList warning I assume the return value of the findUnAskedPermissions method is just ArrayList and not ArrayList<String> (what it should be).

    As for the Array creation, there is an explanation in the inspection hint of IDEA why toArray(new String[0]) is preferred:

    There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]).

    In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation.

    There is an in-depth analysis of the different ways to create arrays that came to this conclusion:

    toArray(new T[0]) seems faster, safer, and contractually cleaner, and therefore should be the default choice now. Future VM optimizations may close this performance gap for toArray(new T[size]), rendering the current "believed to be optimal" usages on par with an actually optimal one. Further improvements in toArray APIs would follow the same logic as toArray(new T[0]) — the collection itself should create the appropriate storage.