Java Reflection Method Static Invoke invokeDefaultStaticMethod(Class expectedClass, String className, String methodName)

Here you can find the source of invokeDefaultStaticMethod(Class expectedClass, String className, String methodName)

Description

invoke Default Static Method

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static <T> T invokeDefaultStaticMethod(Class<T> expectedClass, String className, String methodName) 

Method Source Code

//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);
    }
}

Related

  1. invokeReflectStaticMethod(String methodName, Class cl, Object[] parameter, Class[] args)
  2. invokeStatic(Class clazz, String methodName, Class returnClass, Object... args)
  3. invokeStatic(final Class clazz, final String methodName, final Class[] argTypes, final Object[] args)
  4. invokeStatic(final ClassLoader loader, final String className, final String methodName, final Object... parameters)