reflection
Get Methods from an Object
This is an example of how to get methods of an Object. Getting the methods of an Object implies that you should:
- Get the array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, using
getMethods()
API method of Class. - Then get the array of Method objects reflecting all the methods declared by the class, using
getDeclaredMethods()
API method of Class. This includes public, protected, default (package) access, and private methods, but excludes inherited methods. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface declares no methods, or if this Class object represents a primitive type, an array class, or void.
Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.core; import java.lang.reflect.Method; public class GetMethodsFromAnObject { public static void main(String[] args) { Class<?> clazz = java.lang.ThreadLocal.class; Method[] methods; // list with all public member methods of the class or interface methods = clazz.getMethods(); for (int i=0; i<methods.length; i++) { System.out.println("Found public method: " + methods[i]); } // list with all member methods of the class or interface methods = clazz.getDeclaredMethods(); for (int i=0; i<methods.length; i++) { System.out.println("Found method: " + methods[i]); } } }
Output:
Found public method: public java.lang.Object java.lang.ThreadLocal.get()
Found public method: public void java.lang.ThreadLocal.remove()
Found public method: public void java.lang.ThreadLocal.set(java.lang.Object)
Found public method: public final void java.lang.Object.wait() throws java.lang.InterruptedException
Found public method: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
Found public method: public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
Found public method: public native int java.lang.Object.hashCode()
Found public method: public final native java.lang.Class java.lang.Object.getClass()
Found public method: public boolean java.lang.Object.equals(java.lang.Object)
Found public method: public java.lang.String java.lang.Object.toString()
Found public method: public final native void java.lang.Object.notify()
Found public method: public final native void java.lang.Object.notifyAll()
Found method: public java.lang.Object java.lang.ThreadLocal.get()
Found method: public void java.lang.ThreadLocal.remove()
Found method: static int java.lang.ThreadLocal.access$400(java.lang.ThreadLocal)
Found method: static java.lang.ThreadLocal$ThreadLocalMap java.lang.ThreadLocal.createInheritedMap(java.lang.ThreadLocal$ThreadLocalMap)
Found method: public void java.lang.ThreadLocal.set(java.lang.Object)
Found method: java.lang.Object java.lang.ThreadLocal.childValue(java.lang.Object)
Found method: void java.lang.ThreadLocal.createMap(java.lang.Thread,java.lang.Object)
Found method: java.lang.ThreadLocal$ThreadLocalMap java.lang.ThreadLocal.getMap(java.lang.Thread)
Found method: protected java.lang.Object java.lang.ThreadLocal.initialValue()
Found method: private static int java.lang.ThreadLocal.nextHashCode()
Found method: private java.lang.Object java.lang.ThreadLocal.setInitialValue()
This was an example of how to get methods of an Object in Java.