Here you can find the source of invoke(Optional
public static Optional<Object> invoke(Optional<Method> method, Class<?> clazz, Object... args)
//package com.java2s; //License from project: Apache License import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Optional; public class Main { public static Optional<Object> invoke(Optional<Method> method, Object o, Object... args) { if (method.isPresent()) return Optional.of(invoke(method.get(), o, args)); return Optional.empty(); }//from w ww. j a va2 s . co m public static Optional<Object> invoke(Optional<Method> method, Class<?> clazz, Object... args) { try { return invoke(method, clazz.newInstance(), args); } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } return Optional.empty(); } public static Object invoke(Method method, Object o, Object... args) { try { return method.invoke(o, args); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } return null; } public static Object invoke(Method method, Class<?> clazz, Object... args) { try { return invoke(method, clazz.newInstance(), args); } catch (InstantiationException | IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }