Here you can find the source of invokeDefaultStaticMethod(Class
@SuppressWarnings("unchecked") public static <T> T invokeDefaultStaticMethod(Class<T> expectedClass, String className, String methodName)
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { @SuppressWarnings("unchecked") public static <T> T invokeDefaultStaticMethod(Class<T> expectedClass, String className, String methodName) { Class<?> cls = classForName(className); try {/*from w w w.java 2 s.c o m*/ Method m = cls.getMethod(methodName, new Class[] {}); T t = (T) m.invoke(null, new Object[] {}); if (t == null) { return t; } if (expectedClass.isAssignableFrom(t.getClass()) == false) { throw new RuntimeException("method: " + methodName + " from class: " + className + " does not return correct class type: " + expectedClass + " but: " + t.getClass()); } return t; } catch (NoSuchMethodException ex) { throw new RuntimeException("Cannot find method: " + methodName + " in class: " + className); } catch (IllegalArgumentException ex) { throw new RuntimeException(ex); } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } catch (InvocationTargetException ex) { Throwable nex = ex.getCause(); if (nex instanceof RuntimeException) { throw (RuntimeException) nex; } throw new RuntimeException(ex); } } public static Class<?> classForName(String className) { ClassLoader cl = Thread.currentThread().getContextClassLoader(); try { return Class.forName(className, true, cl); } catch (Throwable ex) { try { Class.forName(className, true, cl); } catch (Exception ex2) { } throw new RuntimeException("Failed to load class: " + className + "; " + ex.getMessage(), ex); } } public static <T> Class<T> classForName(String className, Class<T> ifacecls) { return (Class<T>) classForName(className); } }