1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.util.reflection;
6
7import org.mockito.exceptions.base.MockitoException;
8
9import java.lang.reflect.Field;
10
11public class FieldReader {
12
13    final Object target;
14    final Field field;
15    final AccessibilityChanger changer = new AccessibilityChanger();
16
17    public FieldReader(Object target, Field field) {
18        this.target = target;
19        this.field = field;
20        changer.enableAccess(field);
21    }
22
23    public boolean isNull() {
24            return read() == null;
25    }
26
27    public Object read() {
28        try {
29            return field.get(target);
30        } catch (Exception e) {
31            throw new MockitoException("Cannot read state from field: " + field + ", on instance: " + target);
32        }
33    }
34}
35