public static < T > void callIfExists ( T object , String methodName ) throws SecurityException , IllegalAccessException , IllegalArgumentException , InvocationTargetException { try { Method method = object . getClass ( ) . getMethod ( methodName ) ; method . invoke ( object ) ; } catch ( NoSuchMethodException e ) { } }
Call a method if it exists . Use very sparingly and generally prefer interfaces .