Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javac "uses unchecked or unsafe operations" when the type is being specified

The following code:

public void addGrillaListener(Stage stageToClose,Grilla listener)
{
    GrillaHandler<WindowEvent> handy = new GrillaHandler<>(listener);
    if(stageToClose!=null)
    {
        stageToClose.addEventHandler(WindowEvent.WINDOW_HIDDEN,handy);
    }
}

causes the compiler to show that message. How can I avoid it?

Extra info:

  • Grilla is a Stage interface
  • GrillaHandler is a EventHandler subclass that takes a Grilla as a constructor parameter
  • Using JDK 7, GrillaHandler<> is allowed
  • The compiler message is rather unespecific - it states that this method uses unchecked or unsafe operations
  • Stage is a class provided by oracle, it's part of javafx

GrillaHandler:

public class GrillaHandler<T> implements EventHandler {

    private Grilla win;

    public GrillaHandler(Grilla win) {
        this.win=win;
    }

    @Override
    public void handle(Event t) {
        win.loadTable();
    }
}

Grilla:

public interface Grilla { 
    public void loadTable();
}
like image 447
Alvaro Avatar asked Feb 18 '23 22:02

Alvaro


1 Answers

Change code to

public class GrillaHandler<T extends Event> implements EventHandler<T>{ 
//...
}

The JavaFX EventHandler is a paremeterized type. You are missing that one in your declaration of the GrillaHandler. You are forced to provide a type argument in your class declaration or redeclare the type parameter, as you seem to require as per your declaration.

like image 106
Edwin Dalorzo Avatar answered Apr 27 '23 16:04

Edwin Dalorzo