Java Reflection Method Static Invoke invokeStaticMethod(Class cls, String name)

Here you can find the source of invokeStaticMethod(Class cls, String name)

Description

invoke Static Method

License

Apache License

Declaration

public static <T> T invokeStaticMethod(Class<?> cls, String name) throws Exception 

Method Source Code

//package com.java2s;
/*/*from   w  w w.  j  a va 2s .  com*/
 * Copyright ? 2016 Oleg Cherednik
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations
 * under the License.
 */

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static <T> T invokeStaticMethod(Class<?> cls, String name) throws Exception {
        return invokeStaticMethod(cls, name, null);
    }

    public static <T> T invokeStaticMethod(Class<?> cls, String name, Class<?> type, Object value)
            throws Exception {
        return invokeStaticMethod(cls, name, new Class<?>[] { type }, value);
    }

    public static <T> T invokeStaticMethod(Class<?> cls, String name, Class<?> type1, Class<?> type2, Object value1,
            Object value2) throws Exception {
        return invokeStaticMethod(cls, name, new Class<?>[] { type1, type2 }, value1, value2);
    }

    public static <T> T invokeStaticMethod(Class<?> cls, String name, Class<?> type1, Class<?> type2,
            Class<?> type3, Object value1, Object value2, Object value3) throws Exception {
        return invokeStaticMethod(cls, name, new Class<?>[] { type1, type2, type3 }, value1, value2, value3);
    }

    public static <T> T invokeStaticMethod(Class<?> cls, String name, Class<?>[] types, Object... values)
            throws Exception {
        try {
            Method method = getMethod(cls, name, types);
            method.setAccessible(true);
            return (T) method.invoke(null, values);
        } catch (InvocationTargetException e) {
            throw (Exception) e.getTargetException();
        }
    }

    private static Method getMethod(Class<?> cls, String name, Class<?>... types) throws NoSuchMethodException {
        Method method = null;

        while (method == null && cls != null) {
            try {
                method = cls.getDeclaredMethod(name, types);
            } catch (NoSuchMethodException ignored) {
                cls = cls.getSuperclass();
            }
        }

        if (method == null)
            throw new NoSuchMethodException();

        return method;
    }
}

Related

  1. invokeStatic(String cname, String methodName, ClassLoader cl)
  2. invokeStaticClass(Class staticClass, String methodName, Object[] args, Class... parameterTypes)
  3. invokeStaticMethod(Class objClass, String methodName, Object args[])
  4. invokeStaticMethod(Class cls, String method, Class[] params, Object... args)
  5. invokeStaticMethod(Class cls, String methodName, Object... args)
  6. invokeStaticMethod(Class klass, String methodName)
  7. invokeStaticMethod(Class objectType, String name, Class paramTypes[], Object args[])
  8. invokeStaticMethod(Class pClass, String pMethod, Object pParam)
  9. invokeStaticMethod(final Class cls, final String methodName)