Java tutorial
//package com.java2s; //License from project: Apache License import java.lang.reflect.InvocationTargetException; public class Main { @SuppressWarnings("unchecked") public static <E> E callWithDefault(Object target, String methodName, E defaultValue) { try { //noinspection unchecked return (E) target.getClass().getMethod(methodName, (Class[]) null).invoke(target); } catch (NoSuchMethodException ignored) { } catch (IllegalAccessException ignored) { } catch (InvocationTargetException ignored) { } return defaultValue; } }