javaobject-property

How to satisfy Java compiler's unchecked warning for setting a SimpleObjectProperty


I have this class:

package mydate;

import java.util.Date;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;

public class MyDate {
    private final ObjectProperty<Date> theDate;

    public MyDate(Date d) { theDate = new SimpleObjectProperty<>(d); }

    public ObjectProperty theDateProperty() { return theDate; }

    public Date getTheDate() { return (Date)theDateProperty().get(); }
    //@SuppressWarnings("unchecked")
    public void setTheDate(Date value) { theDateProperty().set(value); }
}

This is a simple app that uses it:

package unchecked;
import java.util.Date;
import mydate.*;

public class Unchecked {

    public static void main(String[] args) {
        MyDate myDate = new MyDate(new Date(0));
        System.out.println(myDate.getTheDate().toString());
    }
}

I want to know if there is a way to satisfy the compiler about setTheDate. I can suppress the unchecked warning, but I'd like to know if there is something reasonable I can do in using ObjectProperty and SimpleObjectProperty to avoid the unchecked. I've tried a few things and googling comes close but not quite close enough for me to figure this out.


Solution

  • ObjectProperty<Date> is a parametrized type. You declared your instance variable type properly (ObjectProperty<Date>), but you didn't declare type parameter in parametized type returned from method theDateProperty(). This method should be declared this way:

    public ObjectProperty<Date> theDateProperty()

    After that, all warnings are gone.