javagenericstableviewjavafx-2unchecked-conversion

Unchecked Warning for ChangeListener<? super TableRow>-Parameter


In a JavaFX application I attached a ChangeListener to a TableCell's tableRowProperty, which is of type ChangeListener<? super TableRow> (and TableRow<T> is generic too).

What I did was the following:

public final class PairingResultEditingCell extends TableCell<Pairing, Result> {

    private final ChoiceBox<Result> choiceField;

    // Unchecked casts and raw types are needed to wire the
    // tableRowProperty changed listener
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private PairingResultEditingCell() {

        super();
        this.choiceField = new ChoiceBox<Result>();
        // ReadOnlyObjectProperty<TableRow> javafx.scene.control.TableCell.tableRowProperty()
        this.tableRowProperty()
            // this cast is the actual source of the warnings
            // rawtype of TableRow<T>: ChangeListener<? super TableRow>
            .addListener((ChangeListener<? super TableRow>) new ChangeListener<TableRow<Result>>() {

                @Override
                public void changed(
                        final ObservableValue<? extends TableRow<Result>> observable,
                        final TableRow<Result> oldValue,
                        final TableRow<Result> newValue) {
                    choiceField.setVisible(newValue.getItem() != null);
                }
            });
    }
}

I need two suppress two sorts of warnings to do this: @SuppressWarnings({ "unchecked", "rawtypes" }). The rawtype warning appears to be Eclipse only. The Jenkins CI server, however, refuses to compile the code because of the former (and I cannot change its configuration).

Is there a way to do this without unchecked casts and raw types? I tried an inner class implementing the interface, but I got stuck. I'm also struggling with Java's ? super MyClass syntax in general.


Solution

  • I don't get any warnings with the following code:

    public final class PairingResultEditingCell extends TableCell<Pairing, Result> {
    
        private final ChoiceBox<Result> choiceField;
    
        private PairingResultEditingCell() {
    
            super();
            this.choiceField = new ChoiceBox<Result>();
    
            ReadOnlyObjectProperty<TableRow> roop= this.tableRowProperty();
            this.tableRowProperty().addListener(new ChangeListener<TableRow>() {
                @Override
                public void changed(ObservableValue<? extends TableRow> observable, TableRow oldValue, TableRow newValue) {
                    choiceField.setVisible(newValue.getItem() != null);
                }
            });
        }
    }