vendredi 17 février 2017

Java: check if a class exists and call a specific method if it exists

Is there a way to do the following? Check if a class exists (in the same package) and if it does exist, check if a particular method exists, and if so, calling it?

Say that I have class X. In some method of class X, I want to do the following:

if (class Y exists) { //Maybe use Class.forName("Y")?
  if ( Y has method a(String, String) ) {
    call Y.m("hello", "world");
  }
}

Is such a thing possible? And is doing such a thing reasonable? Thanks.





Aucun commentaire:

Enregistrer un commentaire