Java Reflection Method Get from Object getMethodValue(Method method, Object instance)

Here you can find the source of getMethodValue(Method method, Object instance)

Description

get Method Value

License

Open Source License

Declaration

public static Object getMethodValue(Method method, Object instance) 

Method Source Code

//package com.java2s;
/*// w  w  w  .  j  a v a 2 s  .c o  m
 * JBoss, Home of Professional Open Source
 * Copyright 2010-2016, Red Hat, Inc. and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.lang.reflect.Method;

public class Main {
    public static Object getMethodValue(Method method, Object instance) {
        Object result;
        boolean accessible = method.isAccessible();
        if (!accessible) {
            method.setAccessible(true);
        }
        try {
            result = method.invoke(instance);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        if (!accessible) {
            method.setAccessible(false);
        }
        return result;
    }
}

Related

  1. getMethodsAnnotatedWith(Object target, Class annotation)
  2. getMethodsAnnotatedWithValue(Class anno, Object o, String name, Object value)
  3. getMethodsByStartsWithName(String name, Object o)
  4. getMethodsForObject(Object o2, String[] passedMethods)
  5. getMethodsIncludingHierarchy(Object comp, Class annotation)
  6. getMethodValue(Object base, Method method)
  7. getMethodValue(Object target, String methodName)
  8. getMethodVector(Object object)
  9. getMethodWithExactName(Class clazz, String name)