I need to get the list of public methods of a Java class, but I do not want to get the wait
, toString
, hasCode
, etc, that the Java class inherits. I just want to get the public methods of that class, not those inherited from their superclass.
Is there a method in the Java reflection
to get only those methods?
Right now this is my code:
String miclase;
Class<?> clase;
clase = Class.forName(miclase);
Method[] allMethods = clase.getMethods();
And I do not want to get the methods wait
, wait
, wait
, equals
, toString
, hasCode
, getClass
, notify
and notifyAll
UPDATE This is my code now after using the first of the answers and now it does NOT print the legacy methods or the private methods when I read the parameters of the methods:
String miclase;
Class<?> clase;
clase = Class.forName(miclase);
Method[] allMethods = clase.getDeclaredMethods();
for (Method method : allMethods){
if (Modifier.isPublic(method.getModifiers())) {
Parameter[] params = method.getParameters();
for (Parameter parametro : params) {
System.out.println("name: "+parametro.getName()+" type:" +parametro.getType());
}
} }