Java Reflection Method Static Invoke invokeStatic(final String className, final String methodName, final I argument)

Here you can find the source of invokeStatic(final String className, final String methodName, final I argument)

Description

invoke Static

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static <I, O> O invokeStatic(final String className, final String methodName, final I argument) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Method;

public class Main {
    @SuppressWarnings("unchecked")
    public static <I, O> O invokeStatic(final String className, final String methodName, final I argument) {
        try {// w  w w. jav  a  2s. c  o m
            final Class<?> clazz = Class.forName(className);
            final Class<?> argumentClass = argument.getClass();

            final Method method = clazz.getDeclaredMethod(methodName, argumentClass);
            return (O) method.invoke(null, argument);
        } catch (final Exception ex) {
            return handleReflectionException(ex);
        }
    }

    public static <T> T handleReflectionException(final Throwable ex) {
        if (ex instanceof RuntimeException) {
            throw (RuntimeException) ex;
        } else if (ex instanceof Error) {
            throw (Error) ex;
        }
        throw new RuntimeException(ex);
    }
}

Related

  1. invokeDefaultStaticMethod(Class expectedClass, String className, String methodName)
  2. invokeReflectStaticMethod(String methodName, Class cl, Object[] parameter, Class[] args)
  3. invokeStatic(Class clazz, String methodName, Class returnClass, Object... args)
  4. invokeStatic(final Class clazz, final String methodName, final Class[] argTypes, final Object[] args)
  5. invokeStatic(final ClassLoader loader, final String className, final String methodName, final Object... parameters)
  6. invokeStatic(String cname, String methodName, ClassLoader cl)
  7. invokeStaticClass(Class staticClass, String methodName, Object[] args, Class... parameterTypes)
  8. invokeStaticMethod(Class objClass, String methodName, Object args[])
  9. invokeStaticMethod(Class cls, String method, Class[] params, Object... args)