Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.lang.reflect.Field;

public class Main {
    /**
     * Set the private field data value of the given instance of a class
     * 
     * @param objectInstance Object to set private member data to
     * @param fieldName Name of private member to set data for
     * @param value Private member's data to set
     * 
     * @throws NoSuchFieldException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static void setPrivateField(Object objectInstance, String fieldName, Object value)
            throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException // NOSONAR
    {
        Class<? extends Object> clazz = objectInstance.getClass();

        Field field = clazz.getDeclaredField(fieldName);

        field.setAccessible(true);

        field.set(objectInstance, value);
    }
}