2

I have this compiler warning : Use of unchecked or unsafe operation on the above code

new InsertAllTask(essenceDao).execute(essenceList)

but I don't know why, I tried to run with -Xlint params no more informations.

The code works but warning is still there.

Hope you can help

Entire task :

public static class DeleteAllTask extends AsyncTask<Void, Void, Void> {
    private final EssenceDao essenceDao;
    private final List<Essence> essenceList;

    DeleteAllTask(EssenceDao essenceDao, List<Essence> list) {
        this.essenceDao = essenceDao;
        this.essenceList = list;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        essenceDao.deleteAllEssence();
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        if (essenceList != null) {
            // Insert new list
            new InsertAllTask(essenceDao).execute(essenceList);
        }
    }
}

The InsertTask :

public static class InsertAllTask extends AsyncTask<List<Essence>, Void, Void> {
    private final EssenceDao essenceDao;

    InsertAllTask(EssenceDao essenceDao) {
        this.essenceDao = essenceDao;
    }

    @Override
    @SafeVarargs
    protected final Void doInBackground(List<Essence>... essences) {
        essenceDao.insertEssence(essences[0]);
        return null;
    }
}
Yeheshuah
  • 1,216
  • 1
  • 13
  • 28
lupaulus
  • 358
  • 5
  • 22
  • 1
    Is class `InsertAllTask` generic? – Abra May 23 '20 at 12:05
  • ```public static class InsertAllTask extends AsyncTask, Void, Void>```same as DeleteAllTask – lupaulus May 23 '20 at 12:08
  • Error : ```new InsertAllTask<>(essenceDao).execute(essenceList); ^ reason: cannot use '<>' with non-generic class InsertAllTask``` – lupaulus May 23 '20 at 12:12
  • 1
    Maybe it's off-topic, but can you clarify - using AsyncTask is your strong position (you have to maintain legacy, you have no time for refactoring and something like that) or you are open to other methods of switching execution to background, but just don't know enough what are they? – sergiy tikhonov May 23 '20 at 12:43
  • I use `AsyncTasks` to manipulate the DAO interface of my **room model**, it's pretty usefull and I don't have any problem unless this warning. – lupaulus May 23 '20 at 13:01
  • @Abra can this be linked with the `SafeVarargs` – lupaulus May 23 '20 at 13:11
  • 1
    You have marked `doInBackground` with `@SafeVarargs` but you are invoking the inherited `execute` method, which apperantly doesn’t have the annotation. – Holger May 26 '20 at 15:16
  • Without the @SafeVarargs it doesn't compile – lupaulus May 26 '20 at 15:25
  • 2
    I don’t understand your response. I did not suggest to remove the existing annotation. I said the inherited `execute` method doesn’t have it. Since the method is outside your responsibility and can’t be overridden, there is nothing you can do about it. Since [this page](https://developer.android.com/reference/android/os/AsyncTask#execute(Params...)) says, the whole class deprecated, it might be even unlikely that the Android developers fix this. – Holger May 26 '20 at 16:01
  • Ok, to Solve my warning, I need to change architecture of my async task. Thanks @Holger, for the help and the ressource. – lupaulus May 26 '20 at 16:06
  • I didn t notice it was deprecated – lupaulus May 26 '20 at 16:07
  • 3
    There would be one possible fix. Instead of implementing `AsyncTask, Void, Void>` and only using the first parameter of the varargs method, you could implement `AsyncTask` and use all arguments, e.g. `doInBackground(Essence... essences) { essenceDao.insertEssence(Arrays.asList(essences)); return null; }`. But then, the caller also needs to pass an array, i.e. `new InsertAllTask(essenceDao) .execute(essenceList.toArray(new Essence[0]));` – Holger May 26 '20 at 16:09
  • 1
    Does [it](https://stackoverflow.com/questions/197986/what-causes-javac-to-issue-the-uses-unchecked-or-unsafe-operations-warning) answer your question? – Yeheshuah Jun 02 '20 at 02:55
  • Did you try to *compile* with `-XLint:unchecked`? – user207421 Jun 02 '20 at 06:09

1 Answers1

0

Since you are trying to suppress the warning (-Xlint) without fix the warning source, you could add this annotation to your method which contains the unsafe code:

    @SuppressWarnings({"unused", "rawtypes"})

Example

The above code:

    public class TestUnsafe {
      public static void main(String[] args) {
        List myList = new ArrayList();
      }
    }

Show these warnings when I try to compile:

javac TestUnsafe.java -Xlint

    TestUnsafe.java:7: warning: [rawtypes] found raw type: List
        List myList = new ArrayList();
        ^
      missing type arguments for generic class List<E>
      where E is a type-variable:
        E extends Object declared in interface List
    TestUnsafe.java:7: warning: [rawtypes] found raw type: ArrayList
        List myList = new ArrayList();
                          ^
      missing type arguments for generic class ArrayList<E>
      where E is a type-variable:
        E extends Object declared in class ArrayList
    2 warnings

But, using a couple of annotations:

    public class TestUnsafe {
      @SuppressWarnings({"unused", "rawtypes"})
      public static void main(String[] args) {
        List myList = new ArrayList();
      }
    }

javac TestUnsafe.java -Xlint returns an empty compile log.

Share us a minimal reproducible example to find the right annotations or fix it!

Krishna Sony
  • 1,286
  • 13
  • 27
JRichardsz
  • 14,356
  • 6
  • 59
  • 94