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
3
votes
1 answer

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

(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…
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
2
votes
1 answer

Type casting to a class nested inside a generic class

package LinkedList; public class Linkedlist { private int size; Node head; Node tail; public Linkedlist() {// default constructor size = 0; head = null; tail = null; } public class Node {// Node…
2
votes
1 answer

How to match on unknown generic type without warnings

I have a function that might receive input of any Java/Scala type as argument: def foo(arbitraryInput: Object): Option[Object] = { arbitraryInput match { case map: Map[Object, Object] => map.get("foo") // ... case _ =>…
DWR
  • 888
  • 1
  • 8
  • 15
2
votes
4 answers

Unchecked cast from generic type to the same type

The following code generates a warning Unchecked cast: 'T' to 'U' in IntelliJ IDEA: interface A {} class B { void f() { final T t = null; final U u = (U) t; } } This doesn't make sense to me, since…
Nickkk
  • 2,261
  • 1
  • 25
  • 34
2
votes
1 answer

Unchecked cast prblem

I have an auto complete adapter but i'm getting this warning: Unchecked cast: 'java.lang.Object' to 'java.util.ArrayList' This is the code for my filter where i'm getting it: private final Filter nameFilter = new Filter() { @Override public…
Liraz Shaka Amir
  • 587
  • 9
  • 26
2
votes
2 answers

What should I do with unchecked cast for criteria.list()?

I have a criteria that is returning a list of strings List. I have following in the method return criteria.list(); But the code shows Type safety: Unchecked cast from List to List To avoid adding…
Jack
  • 6,430
  • 27
  • 80
  • 151
2
votes
1 answer

Unchecked cast: 'java.io.Serializable' to 'java.util.ArrayList'

I am getting an unchecked cast warning and I am not sure if it is safe to suppress it. I am putting an ArrayList inside a Bundle. This Bundle is then put in my Intent as following: Intent mIntent = new…
LimpSquid
  • 327
  • 6
  • 17
1
vote
1 answer

Java generic method: return specific type (without unchecked cast warning)

Given a collection c with elements of type T (or T's subtypes) return some element e of type R from c so that R is a subtype of T. Motivation: I want to use specific methods of R on e. UPDATE: From the aioobe's answer added to the requirements…
Nick Legend
  • 789
  • 1
  • 7
  • 21
1
vote
1 answer

How to design a method that has no `unchecked cast` warning in java

I have a class named Myclass, which is just a wrapper of a HashMap, I want to be able to store the possible key/value pair listed below: KEY_A -> MyClassA KEY_LIST_B -> List KEY_C -> List Here is my code : public class Main { …
Abdo21
  • 498
  • 4
  • 14
1
vote
1 answer

Is there a convenient way to explicitly do an unsafe cast?

So as we know, Java doesn't have reified generics. That means that if you do this: Object someObject; Transporter transporter = (Transporter) someObject; This produces an unchecked warning because you're now at risk that some of the…
Hakanai
  • 12,010
  • 10
  • 62
  • 132
1
vote
2 answers

Java unchecked cast to type parameter warning despite being instance of

Consider you have this List: private final List inMemoryElements; that contains a bunch of objects of subclasses of AbstractXmlElement and you want to add a method to filter objects of a specific subclass from that…
Robin Friedli
  • 88
  • 1
  • 6
1
vote
1 answer

How to solve Unchecked cast in android without suppressing the warning

How do I solve this issue without using SuppressWarning("uncheckedcast"), this there any way to check the object before casting it or any other way to solve this issue. The problem is inside onLoadFinished(). I am using the data for different…
1
vote
1 answer

Making an UnorderedVector from an UnorderedListADT, but getting an unchecked cast error

I'm very confused at the moment when dealing with this concept of casting objects and whatnot, but I believe I am very close to being finished. If you could please take a look at my code and clue me in on what I could do to stop receiving this…
ggx7
  • 49
  • 8
1
vote
0 answers

Java: return type of concrete method of abstract class becomes raw

The following code gives two warnings when compiled with -Xlint. One of them states that type C is required but C was found, even though the return type is very clearly C. I found a solution that gets rid of both warnings, but I…
Justin Thyme
  • 19
  • 1
  • 2
1
vote
1 answer

Java: Generic type casting, how can I fix this so I don't suppress the unchecked warning

private static transient JavaPlugin myPlugin = null; public SomeClass(JavaPlugin plugin) { if (myPlugin == null) myPlugin = plugin; } public T getPlugin(Class plugin) { return (T)myPlugin; // return casted version…
Nahydrin
  • 13,197
  • 12
  • 59
  • 101