Java Reference
In-Depth Information
public static String arrayListToString(ArrayList<String> list, String saparator) {
String[] tempArray = new String[list.size()];
tempArray = list.toArray(tempArray);
String str = String.join(saparator, tempArray);
return str;
}
}
Reflecting on Methods
The following four methods in the Class class can be used to get information about the methods of a class:
Method[] getMethods()
Method[] getDeclaredMethods()
Method getMethod(String name, Class... parameterTypes)
Method getDeclaredMethod(String name, Class... parameterTypes)
The getMethods() method returns all the accessible public methods of the class. The accessible public methods
include any public method declared in the class or inherited from the superclass. The getDeclaredMethods()
method returns all the methods declared only in the class. It does not return any methods that are inherited from the
superclass. The other two methods, getMethod() and getDeclaredMethod() , are used to get the Method object if you
know the name of the method and its parameter types.
The getReturnType() method of the Method class returns the Class object, which contains information about
the return type of the method.
Listing 3-7 illustrates how to get information about the methods of a class. You can uncomment the code in
the main() method to print all methods in the Person class—declared in the Person class and inherited from the
Object class.
Listing 3-7. Reflecting on Methods of a Class
// MethodReflection.java
package com.jdojo.reflection;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class MethodReflection {
public static void main(String[] args) {
Class<Person> c = Person.class;
// Get the declared methods
ArrayList<String> methodsDesciption = getDeclaredMethodsList(c);
System.out.println("Declared Methods for " + c.getName());
for (String desc : methodsDesciption) {
System.out.println(desc);
}
 
Search WWH ::




Custom Search