Questions tagged [unchecked-cast]

Unchecked cast means that you are casting from a generic type to a non-qualified type, or the other way around.

51 questions
30
votes
5 answers

Type safety: Unchecked cast from Object to ArrayList

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…
luisgomezcaballero
  • 421
  • 1
  • 4
  • 7
23
votes
4 answers

How to check types of key and value if Object instanceof HashMap?

I have a method that accepts an Object. In one use case, the method accepts a HashMap and sets each value to the property of the corresponding key name. public void addHelper(Object object) { if (object instanceof HashMap) { …
Shaun Scovil
  • 3,905
  • 5
  • 39
  • 58
14
votes
4 answers

Unchecked cast to generic class implementing Map

I'm trying to understand why this code has an unchecked cast warning. The first two casts have no warning, but the third does: class StringMap extends HashMap { } class StringToIntegerMap extends HashMap { } Map
blurredd
  • 185
  • 8
7
votes
1 answer

Is there any way to handle unchecked cast warning without using Supress in Kotlin?

I am using ViewModelFactory in my android app to pass some data to my ViewModel from Fragment. I'm getting unchecked cast warning. If I'm using Supress the warning goes away. I was wondering is there any way to handle this without using…
miq0717
  • 103
  • 1
  • 8
7
votes
2 answers

Why doesn't the following codes cause "unchecked cast" warning?

I think that (String)x is an unchecked cast, but the compiler does not give any warning. Why does it happen? public static void main(String[] args) { Object x=new Object(); String y=(String)x; }
Zhu Li
  • 585
  • 6
  • 18
6
votes
1 answer

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

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); …
swahnee
  • 2,661
  • 2
  • 24
  • 34
5
votes
5 answers

Why is no unchecked cast warning given when downcasting Throwable to Exception?

Using this code: public class DowncastTest { public static void main(String[] args) { try { System.out.println(1); } catch (Exception ex) { Throwable cause = ex.getCause(); if (cause != null)…
skiwi
  • 66,971
  • 31
  • 131
  • 216
4
votes
1 answer

Unchecked cast on a Map (JSON converted to Map with Jackson)

In Java 8, I want to convert a JSON string to a map, and apply "complex" transformations to the keys. As an example, that "complex" transformation will simply be a lower case transformation. Values in the input JSON could be strings or nested JSON…
norbjd
  • 10,166
  • 4
  • 45
  • 80
4
votes
2 answers

Unchecked cast from X to generic type that extends X

I've been tasked with removing as many @SupressWarnings as possible in our codebase, and I'm not sure how to get around this particular issue. I have this external method that returns a Serializable object, and a generic type T extends Serializable…
mario_sunny
  • 1,412
  • 11
  • 29
4
votes
2 answers

How to address unchecked cast Object to ArrayList

For a class I was assigned to write code to read objects of the class Vehicle using ObjectInputStream (in). The objects are stored in an ArrayList called orders. SSCE: // Read all orders Object obj = in.readObject(); orders = (ArrayList)…
brienna
  • 1,415
  • 1
  • 18
  • 45
4
votes
2 answers

Inconsistent ClassCastException thrown for Raw Types

When executing the below code, the code is executed perfectly without any errors, but for a variable of type List , the return type of get() method should be Integer, but while executing this code, when I call x.get(0) a string is returned,…
Aman J
  • 452
  • 4
  • 15
3
votes
4 answers

Unsafe or unchecked operations warning

In an app I am using SharedPrefernces to save/load (serialize/deserialize) some objects. This is the deserialization code: private void loadData() { String str = sharedPreferences.getString(PREF_TAG, null); byte[] bytes =…
Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
3
votes
3 answers

Removing unchecked cast warnings with generics

I just got into generics with Java, so I set up a little project for myself. I wanted to make a Vector / Point where you could specify the Number (e.g. Double, Integer, Long, etc). I ended up getting a decent class object for it, however noticed…
VeeAyeInIn
  • 193
  • 1
  • 12
3
votes
2 answers

Unchecked cast warning with abstract method providing specific return value

I'm writing selenium tests for an app that has very standard pages that can easily be modeled by a generic structure as the base for the pages, with only a few base types (mostly list pages containing a list of records, and edit pages where one can…
Mobrockers
  • 2,128
  • 1
  • 16
  • 28
3
votes
2 answers

Java Reflection: (Type) field.get(object) - unchecked cast

Following code retrieves the value of a field using the Reflection API. As you can see in the provided image, this generates an unchecked cast warning. The warning can be suppressed using @SuppressWarnings("unchecked"). I was wondering if there is…
Jasper Catthoor
  • 595
  • 1
  • 6
  • 22
1
2 3 4