我試圖清理一些遺留代碼,並有一些unchecked cast
警告我正在努力擺脫。困難的未經檢查的投射警告
我已經將提供警告的代碼解壓縮到下面的可編譯程序中。請注意,我刪除了大部分代碼以使其更小,因此所有這些代碼都可能不完整。它編譯,但運行它不會做任何事情。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
public class GenericsTest {
public static void main(String[] args) throws IOException {
Reader reader = new Reader();
List<String> stringEntries = reader.readAll(StringEntry.class);
}
public static class Reader {
public <T> ZipInputStream getInputStream(String fileName) throws ZipException, FileNotFoundException {
return new ZipInputStream(new FileInputStream(fileName));//_file.getInputStream(_paths.get(fileName));
}
public <T, TEntry extends Entry<T>> List<T> readAll(Class<TEntry> type) throws IOException {
List<T> list = new ArrayList<T>();
List<TEntry> entries = createEntries(type);
for (TEntry entry : entries) {
list.add(read(entry));
}
return list;
}
public <T> T read(Entry<T> entry) throws IOException {
ZipInputStream is = null;
try {
//is = _archive.getInputStream(entry.getName());
return entry.read(is);
} finally {
if (is != null) {
is.close();
}
}
}
public <TEntry extends Entry> List<TEntry> createEntries(Class<TEntry> type) throws ZipException {
List<TEntry> entries = new ArrayList<TEntry>();
List<String> paths = new ArrayList<String>();//getPaths(type);
for (String path : paths) {
entries.add(createEntry(type, path));
}
return entries;
}
public <TEntry extends Entry> TEntry createEntry(Class<TEntry> type, String folder) {
if (StringEntry.class.equals(type)) {
return (TEntry) new StringEntry(folder);
} else if (IntegerEntry.class.equals(type)) {
return (TEntry) new IntegerEntry(folder);
}
throw new IllegalArgumentException("Unknown type: " + type);
}
}
public static abstract class Entry<T> extends ZipEntry {
private T _data;
public Entry(T data, String folder, String name) {
super(folder + "/" + name);
_data = data;
}
protected abstract T read(InputStream is) throws IOException;
};
public static class StringEntry extends Entry<String> {
public StringEntry(String folder) {
super("Hallo world!", folder, "StringEntry");
}
@Override
protected String read(InputStream is) throws IOException {
throw new UnsupportedOperationException("Not supported yet.");
}
};
public static class IntegerEntry extends Entry<Integer> {
public IntegerEntry(String folder) {
super(42, folder, "IntegerEntry");
}
@Override
protected Integer read(InputStream is) throws IOException {
throw new UnsupportedOperationException("Not supported yet.");
}
};
}
編譯上述代碼給出以下警告
GenericsTest.java:57: warning: [unchecked] unchecked cast
found: GenericsTest.StringEntry
required: TEntry
return (TEntry) new StringEntry(folder);
GenericsTest.java:59: warning: [unchecked] unchecked cast
found: GenericsTest.IntegerEntry
required: TEntry
return (TEntry) new IntegerEntry(folder);
2 warnings
更改
public <TEntry extends Entry> TEntry createEntry...
到
public <TEntry extends Entry<T>> TEntry createEntry...
給出一個編譯器錯誤(cannot find symbol: class T
)。
我不想更改代碼太多,因爲它工作正常,所以我如何修復(而不是隱藏)代碼更改最少的警告?
我沒有了解Class.cast()!每天學習新東西:)謝謝! – ughzan 2012-02-15 07:09:53
@ughzan Class.cast()或多或少是如何擺脫未經檢查的強制轉換警告的主要方式。它的主要限制是你不能有'Entry'的類對象(相對於擴展它的'StringEntry'類),所以你不能投射它。 –
millimoose
2012-02-15 18:24:09