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 {
    /**
     * Get the private field data value of the given instance of a class
     * 
     * @param objectInstance Object to get private member data from
     * @param fieldName Name of private member to get data from
     * @return Private member's data
     * 
     * @throws NoSuchFieldException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static Object getPrivateField(Object objectInstance, String fieldName)
            throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException // NOSONAR
    {
        Class<? extends Object> clazz = objectInstance.getClass();

        Field field = clazz.getDeclaredField(fieldName);

        field.setAccessible(true);

        return field.get(objectInstance);
    }
}