I'm using reflection discover a method satisfying some conditions and to invoke the found method.
Check following code. Using Groovy..
class TestClass<T>{
T hello(){
return null
}
}
class TestSubClass extends TestClass<List<String>>{
List<String> hello(){
return null
}
}
TestSubClass.methods.each{
if(it.name.contains("hello")){
println it
}
}
which prints out
public java.util.List TestSubClass.hello() // <-- most relevant method for a user of this class
public java.lang.Object TestSubClass.hello()
public java.lang.Object TestSubClass.super$2$hello()
Java reflection is returning multiple declarations of same method based on inheritance/generics, which is understandable. However, I'd like to prune them out and find the most appropriate method in the context of usability i.e. I think the above commented method would be the most appropriate one.
Note: above is a simplified example. The real logic is not about finding methods based on naming.
Aucun commentaire:
Enregistrer un commentaire