Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import java.util.Locale;

public class Main {
    /**
     * @param type
     * @param object
     * @param field
     * @return
     */
    public static <T> Object getFieldValue(Class<T> type, T object, Field field) {
        try {
            Method m = type.getMethod(getGetIsPrefix(field) + getFirstLetterUppercased(field.getName()),
                    (Class<?>[]) null);
            return m.invoke(object, (Object[]) null);
        } catch (Exception e) {
        }

        return null;
    }

    /**
     * @param field
     * @return
     */
    public static String getGetIsPrefix(Field field) {
        if (field.getType() == Boolean.class || field.getType() == boolean.class)
            return "is";
        else
            return "get";
    }

    /**
     * @param s
     * @return
     */
    public static String getFirstLetterUppercased(String s) {
        StringBuilder sb = new StringBuilder(s.substring(0, 1).toUpperCase(Locale.getDefault()));
        sb.append(s.substring(1));
        return sb.toString();
    }
}