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 Field getFieldRecursiveLy(Class<?> clazz, String fieldName) {
        Field f = null;
        try {
            f = clazz.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            Class<?> superClazz;
            while ((superClazz = clazz.getSuperclass()) != null && superClazz != Object.class) {
                try {
                    f = superClazz.getDeclaredField(fieldName);
                    break;
                } catch (Exception e2) {
                    //ignore
                }
            }
        }
        if (f == null) {
            throw new RuntimeException(
                    "can't find the field , class = " + clazz.getName() + " , fieldName = " + fieldName);
        }
        f.setAccessible(true);
        return f;
    }
}