unchecked-cast

Unchecked cast warnings in generic method on parameter that must be used in a List

北战南征 提交于 2020-01-16 03:12:30
问题 In the code below, the type parameter D can be either a List<Byte> or a List<List<Byte>> (it is the third generic parameter in the Fields<?, ?, D> interface but still I might omit it there - but it is present also in the return type of the method). Can't seem to find a way to tell the compiler this - get Unchecked cast warnings in the lines marked //* : public static <D, K, T extends Enum<T> & Fields<?, ?, D>> List<EnumMap<T, D>> getEntries(InputStream is, Class<T> fields) throws IOException

Java - Is it safe to suppress unchecked cast warning with WatchEvent?

廉价感情. 提交于 2019-12-10 13:33:50
问题 I have the following test code: FileSystem fs = FileSystems.getDefault(); Path conf = fs.getPath("."); WatchKey key = null; try { WatchService watcher = fs.newWatchService(); conf.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY); while(true) { key = watcher.take(); // waits for (WatchEvent<?> event : key.pollEvents()) { WatchEvent.Kind<?> kind = event.kind(); if (StandardWatchEventKinds.OVERFLOW == kind) continue; WatchEvent<Path> ev = (WatchEvent<Path>)event; Path file = ev.context();

How to justify why an unchecked cast is okay, regarding Copyable getObjectCopy()

只谈情不闲聊 提交于 2019-12-02 00:07:22
问题 (This is a follow-up to my previous question.) I have an interface called Copyable , which has a single function Copyable getObjectCopy(); This is used by many other classes. Because this function always returns a Copyable , it results in unchecked casts. Example: @SuppressWarnings("unchecked") //Copy of itself is the same type. ValidateValue<L> vvo = (ValidateValue<O>)this_toCopy.getValidator().getObjectCopy(); vvBlkA = vvo; My question relates to Josh Bloch`s recommendation (in Effective

Type safety: Unchecked cast from Object to ArrayList<MyVariable>

心已入冬 提交于 2019-11-29 05:27:19
Here is a part of a program that sends an ArrayList from a server to a client. I want to remove the warning about the last line in this code: Client code: Socket s; (...) // A server is sending a list from the other side of the link. ois = new ObjectInputStream(s.getInputStream()); MyList = (ArrayList<MyVariable>) ois.readObject(); MyVariable is a Java class with some attributes. The server is creating an ArrayList and filling it with MyVariable variables as items. Then it sends the complete list to the client. I would like to know why do I have a warning there and how to code perfectly in

Type safety: Unchecked cast from Object to ArrayList<MyVariable>

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 22:58:19
问题 Here is a part of a program that sends an ArrayList from a server to a client. I want to remove the warning about the last line in this code: Client code: Socket s; (...) // A server is sending a list from the other side of the link. ois = new ObjectInputStream(s.getInputStream()); MyList = (ArrayList<MyVariable>) ois.readObject(); MyVariable is a Java class with some attributes. The server is creating an ArrayList and filling it with MyVariable variables as items. Then it sends the complete