Java tutorial
//package com.java2s; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Vector; public class Main { public static final int MAX = 1; public static final int MIN = 2; /** * Method getMax. * @param vector Vector * @param getter String * @return Object * @throws NoSuchMethodException * @throws IllegalAccessException * @throws InvocationTargetException */ public static Object getMax(Vector vector, String getter) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return getLimit(vector, getter, MAX); } /** * Method getLimit. * @param vector Vector * @param getter String * @param limitType int * @return Object * @throws NoSuchMethodException * @throws IllegalAccessException * @throws InvocationTargetException */ private static Object getLimit(Vector vector, String getter, int limitType) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Object limitObject = null; Object currentObject = null; Method getterMethod = null; Method compareMethod = null; Object vectorElement = null; Class[] clsParms = new Class[0]; Object[] objParms = new Object[0]; Class[] objectClass = new Class[1]; Object[] limitObjectArray = new Object[1]; int compareResult = 0; try { objectClass[0] = Class.forName("java.lang.Object"); } catch (Exception e) { } if (vector.size() > 0) { vectorElement = vector.elementAt(0); getterMethod = vectorElement.getClass().getMethod(getter, clsParms); limitObject = getterMethod.invoke(vectorElement, objParms); if ((limitObject == null) && (limitType == MIN)) { return null; } } for (int i = 1; i < vector.size(); i++) { vectorElement = vector.elementAt(i); if (vectorElement != null) { getterMethod = vectorElement.getClass().getMethod(getter, clsParms); currentObject = getterMethod.invoke(vectorElement, objParms); } else { currentObject = null; } if (currentObject != null) { if (limitObject != null) { compareMethod = currentObject.getClass().getMethod("compareTo", objectClass); limitObjectArray[0] = limitObject; compareResult = ((Integer) compareMethod.invoke(currentObject, limitObjectArray)).intValue(); } else { compareResult = 1; } } else { if (limitObject != null) { compareResult = -1; } else { compareResult = 0; } } if (((compareResult > 0) && (limitType == MAX)) || ((compareResult < 0) && (limitType == MIN))) { limitObject = currentObject; } } return limitObject; } }