Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.lang.reflect.Method;

public class Main {

    public static Object invokeGet(Object o, String fieldName) {

        Method method = getGetMethod(o.getClass(), fieldName);

        try {

            return method.invoke(o, new Object[0]);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }

    @SuppressWarnings("unchecked")
    public static Method getGetMethod(Class objectClass, String fieldName) {

        StringBuffer sb = new StringBuffer();

        sb.append("get");

        sb.append(fieldName.substring(0, 1).toUpperCase());

        sb.append(fieldName.substring(1));

        try {

            return objectClass.getMethod(sb.toString());

        } catch (Exception e) {

        }

        return null;

    }
}