例如:
javac Foo.java
Note: Foo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Run Code Online (Sandbox Code Playgroud) 在一个应用程序中,我正在使用SharedPrefernces保存/加载(序列化/反序列化)某些对象。
这是反序列化代码:
private void loadData() {
String str = sharedPreferences.getString(PREF_TAG, null);
byte[] bytes = Base64.decode(str, Base64.DEFAULT);
try {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream input = new ObjectInputStream(bais);
arrayOfObjects = (ArrayList<MyObject>) input.readObject();
} catch (Exception e) {
Log.i("BUG", "error decoding serialized objects: " + e.toString());
}
if (arrayOfObjects == null) {
Log.i("BUG", "serious problem!");
}
}
Run Code Online (Sandbox Code Playgroud)
但是每当我编译该项目时,该行:
arrayOfObjects = (ArrayList<MyObject>) input.readObject();
Run Code Online (Sandbox Code Playgroud)
引起警告,包含此方法的类“使用未经检查或不安全的操作”。
如何摆脱此警告或更改代码以更安全?