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 {

    public static Object getProperty(Object o, String field) {
        try {
            Field f = o.getClass().getDeclaredField(field);
            f.setAccessible(true);
            String name = f.getName();
            name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase(Locale.US));
            Method m = o.getClass().getMethod("get" + name);
            // return f.get(o);
            return (Object) m.invoke(o);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("rawtypes")
    public static Field getDeclaredField(Class clazz, String name) {
        try {
            return clazz.getDeclaredField(name);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }
}