Method Class | toGenericString() method in Java
Last Updated :
06 Apr, 2023
The java.lang.reflect.Method.toGenericString() method of Method class returns a string which gives the details of Method, including details of type parameters of the method.
Syntax:
public String toGenericString()
Return Value: This method returns a string which gives the details of Method, including details of type parameters of the method.
Below programs illustrates toGenericString() method of Method class:
Example 1:
Java
import java.lang.reflect.Method;
public class GFG {
public final void paint(Object... values)
{
String message = "A Computer Science portal for geeks";
}
public static void main(String args[])
{
try {
Method[] methods = GFG. class .getMethods();
for (Method method : methods) {
if (method.getName().equals("main")
|| method.getName().equals("paint")) {
System.out.println(method.toGenericString());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
Output:
public static void GFG.main(java.lang.String[])
public final void GFG.paint(java.lang.Object...)
Example 2: Explanation: In this Method at first java.util.concurrent.CountDownLatch Class Object is created. After creating Class Object of java.util.concurrent.CountDownLatch Class a list of Method Objects is created by calling getMethods() of class Object. Iterate through Method list and print method details using toGenericString().
Java
import java.lang.reflect.Method;
import java.util.concurrent.CountDownLatch;
public class GFG {
public static void main(String args[])
{
try {
Class c = CountDownLatch. class ;
Method[] methods = c.getMethods();
System.out.println("Methods of CountDownLatch: ");
for (Method m : methods) {
System.out.println("Method: "
+ m.toGenericString());
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
Output:
Methods of CountDownLatch:
Method: public boolean java.util.concurrent.CountDownLatch.await(long,java.util.concurrent.TimeUnit) throws java.lang.InterruptedException
Method: public void java.util.concurrent.CountDownLatch.await() throws java.lang.InterruptedException
Method: public void java.util.concurrent.CountDownLatch.countDown()
Method: public long java.util.concurrent.CountDownLatch.getCount()
Method: public java.lang.String java.util.concurrent.CountDownLatch.toString()
Method: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
Method: public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
Method: public final void java.lang.Object.wait() throws java.lang.InterruptedException
Method: public boolean java.lang.Object.equals(java.lang.Object)
Method: public native int java.lang.Object.hashCode()
Method: public final native java.lang.Class java.lang.Object.getClass()
Method: public final native void java.lang.Object.notify()
Method: public final native void java.lang.Object.notifyAll()
Explanation: Output of this program also showing results for method objects other than methods defined in class CountDownLatch like wait, equals, toString, hashCode, getClass, notify, notifyAll.These methods are inherited from superclass name Object of java.lang package. Difference between toGenericString() and toString()
- toGenericString() returns a string describing this Method, including its generic type parameters.
- toString() returns a string describing this Method.
- toString() method does not include generic types.
Reference:
Share your thoughts in the comments
Please Login to comment...