[英]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(). 我在下面的代码中使用ois.readObject()的List赋值抛出未经检查的类型转换警告。 Adding @SupressWarnings("unchecked") makes Android Studio give me an error saying "Annotations are not allowed here".
添加@SupressWarnings(“未选中”)会让Android Studio给我一个错误,说“这里不允许注释”。
Is my only option to restructure the entire class so List tweets is declared on that line? 我唯一的选择是重组整个类,以便在该行上声明List推文吗?
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. 这使得抑制范围变得紧张,并且不是特定于IDE的。
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")
. 因此,在这种情况下,您的选项是使用
@SupressWarnings("unchecked")
注释私有成员推文或onCreate
方法。 You can even annotate the entire class TweetListActivity
if you would like to suppress all unchecked warnings for that class (not recommended). 如果要禁止该类的所有未经检查的警告(不推荐),您甚至可以注释整个类
TweetListActivity
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.