[ Back | Previous | Next ]

How to invoke bean properties from Method?

Package:
java.lang.reflect.*
Product:
JDK
Release:
1.1.x
Related Links:
General
Field
Method
Comment:
/**
 * getValueAt method comment.
 */
public Object getValueAt(int arg1, int arg2) {
	if (getResource() == null) {
		System.out.println("No resource defined...");
	}
	Object aRow = (Object) getData().elementAt(arg1);
	//
	if (aRow != null) {

		Field[] f = aRow.getClass().getDeclaredFields();
		String fieldName = null;
		try {
			fieldName = (String) ((Object[]) getResource().findObjectList("c" + arg2))[1];
		} catch (Exception e) {
			return "NoFieldName";
		}
		//for (int j = 0; j < f.length; j++) {
		//	dbg("fields: " + f[j] + ": " + f[j].getType());
		//	dbg
		//}
		//dbg("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
		//
		try {
			Class c = aRow.getClass();
			Object[] args_value = null;
			Class[] args_class = null;
			try {
				// this fails!
				Method m = c.getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), args_class);
				try {
					return m.invoke(aRow, args_value);
				} catch (Exception e) {
					System.err.println("Failed to invoke: " + e.toString());
				}
			} catch (Exception e) {
				System.err.println("Failed to get method: " + e.toString());
			}
		} catch (Exception e) {
			System.err.println("Error: " + e.toString());
		}
		return getData().elementAt(arg2);
	}
	return null;
}
1