Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Field;

public class Main {
    public static void writeValue(Object src, String fieldName, Object value) throws Exception {
        Field field = findField(src, fieldName);
        field.setAccessible(true);
        field.set(src, value);
    }

    public static Field findField(Object src, String fieldName) throws Exception {
        return findField(src.getClass(), fieldName);
    }

    public static Field findField(Class src, String fieldName) throws Exception {
        try {
            return src.getDeclaredField(fieldName);
        } catch (Exception e) {
            if (src.getSuperclass() != null) {
                return findField(src.getSuperclass(), fieldName);
            } else {
                throw e;
            }
        }
    }
}