Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;

public class Main {
    public static <T> T getMember(Object object, String member, Class<T> klass)
            throws NoSuchFieldException, IllegalAccessException {
        Field field = getField(object.getClass(), member);
        field.setAccessible(true);
        return (T) field.get(object);
    }

    public static Field getField(Class<?> klass, String member) throws NoSuchFieldException {
        try {
            return klass.getDeclaredField(member);
        } catch (NoSuchFieldException e) {
            if (klass.getSuperclass() != null)
                return getField(klass.getSuperclass(), member);
            else
                throw new NoSuchFieldException(String.format("Class does not contain member %s!", member));
        }
    }
}