Type Safety: Unchecked Cast from Object to Arraylist<Myvariable>

How to address unchecked cast Object to ArrayList<Vehicle>

You were close. It is always safe to cast to ArrayList<?>:

Object obj = in.readObject();
ArrayList<?> ar = (ArrayList<?>) obj;

orders.clear();
for (Object x : ar) {
orders.add((Vehicle) x);
}

You might even want to be extra safe and cast to something more generalized, like Iterable:

Object obj = in.readObject();
Iterable<?> ar = (Iterable<?>) obj;

orders = new ArrayList<>();
for (Object x : ar) {
orders.add((Vehicle) x);
}

If you have control over the objects which were originally serialized, there is a way to avoid the loop entirely: Use an array instead of a Collection. Array types are always a safe cast (if they don’t have a generic type themselves):

Object obj = in.readObject();
orders = new ArrayList<>(Arrays.asList((Vehicle[]) obj));

Type safety: Unchecked cast from WebElement to List<WebElement>

WebElement ele = getObject(locator);
List<WebElement> ls = (List<WebElement>) ele;

You are casting List to WebElement and that is not valid. I believe in your getObject method you are using findElement method to identify the locator. Instead of that use findElements method to get List of WebElement

Try like below,

List<WebElement> ls = driver.findElements(By.xpath(locator));

Eclipse warning Type safety: Unchecked cast has got recursive solutions

Don't use clone. clone() is a badly designed method that requires too much knowledge of the object being cloned and is often implemented wrong by users anyway. (See Effective Java for more info about this)

The best way to copy the Map is to create a new map with the same data:

public SomeClass( HashMap<String, String> hashMap )
{
this.hashMap = new HashMap<String,String>(hashMap);
}

Also as an FYI, public SomeClass( private HashMap<String, String> hashMap ) doesn't compile. You can't have parameters with a private keyword. Removing "private" from the parameter works.

you're wasting memory with the new HashMap call

This was in the answer to the other post you referenced because their code was:

private Map<String, String> someMap = new HashMap<String, String>();
someMap = (HashMap<String, String>)...

Where someMap was created with a call to new Hashmap() but then that reference was immediately replaced with a different HashMap.

It is irrelevant to your problem so you can ignore it.

Removing Warnings (Type safety: Unchecked cast from capture#2-of ? extends Template to T)

You can remove some of them by not using raw types:

Class<?> templateClass = getGenericTypeArgument(this.getClass(), 0);
// ^
Constructor<? extends Template> constructor =
templateClass.getConstructor(new Class<?>[]{List.class, List.class});
// ^

It doesn't look to me like you can avoid the unchecked cast to T.

Is there a better solution?

It looks to me like you might be using the getGenericSuperclass().getActualTypeArguments() thing and you can remove all warnings by passing the Class<T> as a parameter to the object (shown here) instead.

How to address Type safety warning Unchecked cast from Collection to List<File> Java 6 without suppress annotation

Well, first of all, Collection is a superinterface of List, so unless you are sure that your implementation of listFiles() will always return a List, you shouldn't downcast (so as to avoid a ClassCastException). In most cases, if you need a list instead of a collection, it's wiser to simply create a new list from the returned collection, e.g.,

Collection<File> myCollection = FileUtils.listFiles(...);
List<File> myList = new ArrayList<File>(myCollection);

Alternatively, if all you want to do is convert the collection into an array, there's no need to create any list at all. All you have to do is:

Collection<File> myCollection = FileUtils.listFiles(...);
File[] myArray = myCollection.toArray(new File[myCollection.size()]);

If the compiler continues to give you warnings, then your only remaining problem lies with the listFiles() method itself; it's returning the raw type Collection instead of the generic type Collection<File>. To remove the associated warning, either update to the latest version of Commons IO, or simply insert the @SuppressWarnings("unchecked") annotation appropriately, e.g.,

@SuppressWarnings("unchecked")
Collection<File> myCollection = FileUtils.listFiles(...);

Explain why show Warning Unchecked cast source.readArrayList(data!!.javaClass.classLoader) as ArrayList<Data>?

Normally when you cast, the compiler inserts a runtime check to see that this cast is valid. For instance:

val s: String = f() as String

Will have a checkcast instruction in the resulting bytecode:

checkcast java/lang/String

This is a runtime check to see if the result of f() is actually a String.

But since generics are erased, an ArrayList<Data>? will just be an ArrayList? at runtime.

So the best a VM can do is check that the value you're casting is an ArrayList?, but it can't check if it's actually an ArrrayList<Data>?, and that's why the cast is called 'unchecked'.

So for:

val a: List<String> = f() as List<String>

You will get just get:

checkcast java/util/List

Notice here how String is missing.

However, that does not mean that the VM blindly treats any value you take from the list as a String, it usually just means that the cast to String happens somewhere else. For instance, when you take something out of the list:

val s: String = a[0]

This becomes:

iconst_0
invokeinterface java/util/List.get:(I)Ljava/lang/Object;
checkcast java/lang/String

Notice how there is a checkcast to String. That cast is implicitly inserted by the compiler, as if the code were actually:

val s: String = a[0] as String

Eclipse warning Type safety: Unchecked cast has got recursive solutions

Don't use clone. clone() is a badly designed method that requires too much knowledge of the object being cloned and is often implemented wrong by users anyway. (See Effective Java for more info about this)

The best way to copy the Map is to create a new map with the same data:

public SomeClass( HashMap<String, String> hashMap )
{
this.hashMap = new HashMap<String,String>(hashMap);
}

Also as an FYI, public SomeClass( private HashMap<String, String> hashMap ) doesn't compile. You can't have parameters with a private keyword. Removing "private" from the parameter works.

you're wasting memory with the new HashMap call

This was in the answer to the other post you referenced because their code was:

private Map<String, String> someMap = new HashMap<String, String>();
someMap = (HashMap<String, String>)...

Where someMap was created with a call to new Hashmap() but then that reference was immediately replaced with a different HashMap.

It is irrelevant to your problem so you can ignore it.



Related Topics



Leave a reply



Submit