4

I am trying to use an AsyncTask with generics in an abstract fragment in the following manner:

protected abstract void doSomething(T item);

...

public void test(int position)
{
    AsyncTask<T, Void, Void> exampleTask = new AsyncTask<T, Void, Void>()
    {
        @Override
        protected Void doInBackground(T... params)
        {
            doSomething(params[0]);

            return null;
        }
    };

    exampleTask.execute(mAdapter.get(position));
}

where mAdapter uses generics as well, and get is defined as:

public T get(int position)
{
    return mItemList.get(position);
}

The idea here is that I would like to use a generic adapter and a generic asynctask to perform a common call (and have the fragments which extend this abstract fragment implement doSomething).

This totally works from what my testing has shown, but it displays two warnings that lead me to believe that I am not doing things the "correct" way.

First, doInBackground has the following warning appear:

Possible heap pollution from parameterized varag type. This inspection reports all methods with variable arity which can be annotated as @SafeVarags. @SafeVarags annotation suppresses unchecked warnings about parameterized array creation at call sites.

Second, exampleTask.execute has the following warning appear:

Unchecked generics array creation for varargs parameter. Signals places where an unchecked warning is issued by the compiler, for example:

void f(HashMap) {
    map.put("key", "value");
}

I'd like to remove the warnings and make sure I'm doing this correctly, any thoughts?

Zach
  • 3,909
  • 6
  • 25
  • 50
  • Have the same problem, reviewing this https://stackoverflow.com/questions/12462079/potential-heap-pollution-via-varargs-parameter does not provide much clarity for me. Did you ever resolve this problem? – CybeX Dec 18 '18 at 20:26
  • @KGCybeX yes - I stopped using AsyncTasks! – Zach Dec 18 '18 at 20:36

0 Answers0