I have a class that stores certain events and gives out clones, but Eclipse (my IDE) gives me unchecked clone warnings:
import java.util.*;
import java.awt.event.*;
public PlayerEvents {
private final ArrayList<KeyEvent> keyevents;
private final ArrayList<MouseEvent> mouseevents;
@SuppressWarnings("unchecked") // I have to do this to keep the IDE quiet
public PlayerEvents(ArrayList<KeyEvent> keyevents, ArrayList<MouseEvent> mouseevents) {
this.keyevents = (ArrayList<KeyEvent>) keyevents.clone(); // Type safety: Unchecked cast from Object to ArrayList<KeyEvent>
this.mouseevents = (ArrayList<MouseEvent>) mouseevents.clone(); // Type safety: Unchecked cast from Object to ArrayList<KeyEvent>
}
// More code...
}
When would a cloned object not be its source, i.e. when will a cloned keyevent
not be the type of keyevent
?
Edit: Furthermore, why does the .clone() object give an Object? Does this have to do with the limitations of the Java language?
I'm using Eclipse Mars (4.5.2) with Java 1.8.0_91.