Java Reflection Method Main Invoke invoke(String property, Object main)

Here you can find the source of invoke(String property, Object main)

Description

invoke

License

Open Source License

Declaration

private static Object invoke(String property, Object main) 

Method Source Code


//package com.java2s;
/* //from   w w w .j a va  2  s. co m
 * Copyright (C) 2014 GG-Net GmbH - Oliver G?nther
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    private static Object invoke(String property, Object main) {
        Method m;
        try {
            try {
                m = main.getClass()
                        .getMethod("get" + property.substring(0, 1).toUpperCase() + property.substring(1));
            } catch (NoSuchMethodException ex) {
                try {
                    // Trying "is" in the case of booleans
                    m = main.getClass()
                            .getMethod("is" + property.substring(0, 1).toUpperCase() + property.substring(1));
                } catch (NoSuchMethodException | SecurityException ex1) {
                    throw new RuntimeException("Exeption during invoke().getMethod()", ex1);
                }
            }
            m.setAccessible(true);
            return m.invoke(main);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
                | SecurityException ex) {
            throw new RuntimeException("Exeption during invoke()", ex);
        }
    }
}

Related

  1. invokeMain(Class clazz, String[] args)
  2. invokeMain(Class fooClass)
  3. invokeMain(Class mainClass, List args)
  4. invokeMain(ClassLoader classloader, String classname, String[] args)