Using reflection to print out all methods


  import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Main{
  public static void main(String[] args) {
    Method[] methods = java.lang.Integer.class.getDeclaredMethods();

    for (int i = 0; i < methods.length; i++) {
      Method m = methods[i];
      Class retType = m.getReturnType();
      Class[] paramTypes = m.getParameterTypes();
      String name = m.getName();
      System.out.print(Modifier.toString(m.getModifiers()));
      System.out.print(" " + retType.getName() + " " + name + "(");
      for (int j = 0; j < paramTypes.length; j++) {
        if (j > 0)
          System.out.print(", ");
        System.out.print(paramTypes[j].getName());
      }
      System.out.println(");");
    }
  }
}
  

The output:


public int hashCode();
public static int reverseBytes(int);
public volatile int compareTo(java.lang.Object);
public int compareTo(java.lang.Integer);
public boolean equals(java.lang.Object);
public static java.lang.String toString(int, int);
public static java.lang.String toString(int);
public java.lang.String toString();
public static java.lang.String toHexString(int);
public static java.lang.Integer decode(java.lang.String);
static void getChars(int, int, [C);
public static java.lang.Integer valueOf(java.lang.String);
public static java.lang.Integer valueOf(java.lang.String, int);
public static java.lang.Integer valueOf(int);
public static int reverse(int);
public byte byteValue();
public double doubleValue();
public float floatValue();
public int intValue();
public long longValue();
public short shortValue();
public static int parseInt(java.lang.String);
public static int parseInt(java.lang.String, int);
public static int bitCount(int);
public static java.lang.Integer getInteger(java.lang.String);
public static java.lang.Integer getInteger(java.lang.String, int);
public static java.lang.Integer getInteger(java.lang.String, java.lang.Integer);
public static int highestOneBit(int);
public static int lowestOneBit(int);
public static int numberOfLeadingZeros(int);
public static int numberOfTrailingZeros(int);
public static int rotateLeft(int, int);
public static int rotateRight(int, int);
public static int signum(int);
static int stringSize(int);
public static java.lang.String toBinaryString(int);
public static java.lang.String toOctalString(int);
private static java.lang.String toUnsignedString(int, int);
Home 
  Java Book 
    Reflection  

Method:
  1. Method Reflection
  2. Using reflection to print out all methods
  3. Invoke a method