Micro tutorial on java reflection / RTTI


// given the name of a class, get a "Class" object that
// has all info about it
        Class c = Class.forName(classname);

// get the methods of this class
        Method[] methods = c.getMethods()

// select a particular method
	Method m1 = method[3];

// get the name of this method
        m1.getName()

// get the argument types of one of the methods,  also its return type
        Class[] arginfo = m1.getParameterTypes();
	int nargs = arginfo.length;
        Class rtnclass = m1.getReturnType();
  
// get the name of one of the arguments
        arginfo[i].getName();

// call the method
        Object[] args = new Object[nargs]
        Object val = m1.invoke(c, args);
// result of calling is in val

// get a typed variable for the result
	Float f; Integer i;
	if (val instanceof Float)
	    f = (Float)val;
        if (val instanceof Integer)
	    i = (Integer)val;