Getting the Pair class methods

The public methods of a class are accessible via the Class.getMethods() method. This method returns an array of Method:

Method[] methods = clazz.getMethods();
// public boolean modern.challenge.Pair.equals(java.lang.Object)
// public int modern.challenge.Pair.hashCode()
// public int modern.challenge.Pair.compareTo(java.lang.Object)
// ...
System.out.println("Methods: " + Arrays.toString(methods));

For fetching the actual name of the methods, we can quickly provide a helper method:

public static List<String> getMethodNames(Method[] methods) {

return Arrays.stream(methods)
.map(Method::getName)
.collect(Collectors.toList());
}

Now, we only retrieve the names of the methods:

List<String> methodsName = getMethodNames(methods);

// equals, hashCode, compareTo, wait, wait,
// wait, toString, getClass, notify, notifyAll
System.out.println("Methods names: " + methodsName);
For fetching all the declared methods (for example, private and protected), call getDeclaredMethods(). For searching for a certain method, call getMethod​(String name, Class<?>... parameterTypes) or getDeclaredMethod​(String name, Class<?>... parameterTypes).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.61.170