简体   繁体   中英

How to suppress unchecked typecast warning with generics not at declaration?

I have my code below which throws an unchecked typecast warning with the List assignment from ois.readObject(). Adding @SupressWarnings("unchecked") makes Android Studio give me an error saying "Annotations are not allowed here".

Is my only option to restructure the entire class so List tweets is declared on that line?

package com.fredliu.hellotwitter;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.fredliu.hellotwitter.models.Tweet;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class TweetListActivity extends BaseListActivityWithMenu {
    private List<Tweet> tweets;
    private static final String cacheFile = "tweetCache.ser";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tweet_list);
        try {
            FileInputStream fis = openFileInput(cacheFile);
            ObjectInputStream ois = new ObjectInputStream(fis);
            @SuppressWarnings(value="unchecked")
            tweets = (List<Tweet>) ois.readObject();

            ois.close();
            fis.close();
            Log.d("FREDFRED", "Tweets cache read from!");
        } catch (Exception e) {
            Log.e("FREDFRED", "Something horrible has happened when reading from the Tweets cache");
        }

        if (tweets == null || tweets.isEmpty()) {
            Toast.makeText(getApplicationContext(), "No stored Tweets found!", Toast.LENGTH_LONG).show();
            tweets = new ArrayList<>();
            for (int i = 1; i <= 20; i++) {
                Tweet t = new Tweet();
                t.setTitle("Title " + i);
                t.setBody("Body " + i);
                tweets.add(t);
            }
        }


        try {
            FileOutputStream fos = openFileOutput(cacheFile, MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(tweets);
            Log.d("FREDFRED", "Tweets successfully written to cache!");
        } catch (FileNotFoundException e) {
            Log.e("FREDFRED", "Tweet cache file not found" + e);
        } catch (IOException e) {
            Log.e("FREDFRED", "Object not found when writing Tweet to cache");
        }


        ArrayAdapter a = new TweetAdapter(this, tweets);
        setListAdapter(a);


    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent i = new Intent(this, TweetDetailActivity.class);
        startActivity(i);
        Toast.makeText(getApplicationContext(), "Position " + position + ", ID " + id, Toast.LENGTH_LONG).show();
    }
}

If you want to suppress a warning for a single statement, you can just insert the following:

// noinspection <checktype>

So for your case, for "unchecked", just change it to:

// noinspection unchecked
tweets = (List<Tweet>) ois.readObject();

EDIT: Another alternative would be to create some standalone method that performs the cast, and then apply the annotation to that method. Something like:

@SuppressWarnings("unchecked")
public static List<Tweet> asTweetList(ObjectInputStream ois) {
    return (List<Tweet>) ois.readObject();
}

and then use that instead:

FileInputStream fis = openFileInput(cacheFile);
ObjectInputStream ois = new ObjectInputStream(fis);
List<Tweet> tweets = asTweetList(ois);

That keeps the suppression scope tight, as well as not being IDE-specific.

Annotations can not be attached to expressions, statements, or blocks. So your options in this case are to annotate the private member tweets, or the method onCreate with @SupressWarnings("unchecked") . You can even annotate the entire class TweetListActivity if you would like to suppress all unchecked warnings for that class (not recommended).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM