Java Reflection - Class.getMethods() Examples

Code Snippets Java Reflection Java 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public Method[] getMethods()
                    throws SecurityException

Returns an array of Method objects for the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

Returns:
the array of Method objects representing the public methods of this class

Examples


package com.logicbig.example.clazz;

import java.lang.reflect.Method;

public class GetMethodsExample {

public static void main(String... args) {
Class<GetMethodExample> c = GetMethodExample.class;
Method[] methods = c.getMethods();
for (Method method : methods) {
System.out.println(method);
}
}

public int calcInt(int i) {return 0;}

public void doSomething() {}

public static void aStaticMethod(String s) {}

private void aPrivateMethod() {}
}

Output

public void com.logicbig.example.clazz.GetMethodExample.doSomething()
public int com.logicbig.example.clazz.GetMethodExample.calcInt(int)
public static void com.logicbig.example.clazz.GetMethodExample.aStaticMethod(java.lang.String)
public static void com.logicbig.example.clazz.GetMethodExample.main(java.lang.String[]) throws java.lang.NoSuchMethodException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()




package com.logicbig.example.clazz;

import java.lang.reflect.Method;

public class GetMethodsExample2 {

public static void main(String... args) {
Class<Integer> c = int.class;
Method[] methods = c.getMethods();
printMethods(methods);

//arrays method, arrays is an object..
Class<int[]> c2 = int[].class;
methods = c2.getMethods();
printMethods(methods);
}

private static void printMethods(Method[] methods) {
System.out.println(methods.length);
for (Method method : methods) {
System.out.println(method);
}

}
}

Output

0
9
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()




package com.logicbig.example.clazz;

import java.lang.reflect.Method;

public class GetMethodsExample3 {

public static void main(String... args) throws NoSuchMethodException {
Class<Runnable> c = Runnable.class;
Method[] methods = c.getMethods();
for (Method method : methods) {
System.out.println(method);
}
}
}

Output

public abstract void java.lang.Runnable.run()




See Also