FieldAccessor.java

1
package fr.sii.ogham.core.util.bean;
2
3
import java.lang.reflect.Field;
4
5
import fr.sii.ogham.core.exception.util.InvalidPropertyException;
6
7
/**
8
 * Access the property of the bean using reflection. The property is accessed
9
 * through {@link Field}.
10
 * 
11
 * <p>
12
 * By default, if the field is not accessible, it makes it accessible and tries
13
 * to access it.
14
 * 
15
 * @author Aurélien Baudet
16
 * @param <T>
17
 *            The type of the value
18
 *
19
 */
20
public class FieldAccessor<T> implements Accessor<T> {
21
	private final Object bean;
22
	private final String name;
23
	private final boolean makeAccessible;
24
	private Field field;
25
26
	/**
27
	 * Use reflection to access the bean property named by <code>name</code>
28
	 * parameter.
29
	 * 
30
	 * <p>
31
	 * If the field is not accessible (private or protected), it automatically
32
	 * makes it accessible.
33
	 * 
34
	 * @param bean
35
	 *            the bean to get property value from
36
	 * @param name
37
	 *            the name of the property
38
	 */
39
	public FieldAccessor(Object bean, String name) {
40
		this(bean, name, true);
41
	}
42
43
	/**
44
	 * Use reflection to access the bean property named by <code>name</code>
45
	 * parameter.
46
	 * 
47
	 * <p>
48
	 * You can choose to force access to non accessible fields or not.
49
	 * 
50
	 * @param bean
51
	 *            the bean to get property value from
52
	 * @param name
53
	 *            the name of the property
54
	 * @param makeAccessible
55
	 *            make the field accessible if not
56
	 */
57
	public FieldAccessor(Object bean, String name, boolean makeAccessible) {
58
		super();
59
		this.bean = bean;
60
		this.name = name;
61
		this.makeAccessible = makeAccessible;
62
	}
63
64
	@Override
65
	@SuppressWarnings("unchecked")
66
	public T getValue() {
67
		try {
68 2 1. getValue : negated conditional → NO_COVERAGE
2. getValue : negated conditional → KILLED
			if (field == null) {
69 2 1. getValue : removed call to fr/sii/ogham/core/util/bean/FieldAccessor::initField → NO_COVERAGE
2. getValue : removed call to fr/sii/ogham/core/util/bean/FieldAccessor::initField → KILLED
				initField();
70
			}
71 2 1. getValue : replaced return value with null for fr/sii/ogham/core/util/bean/FieldAccessor::getValue → NO_COVERAGE
2. getValue : replaced return value with null for fr/sii/ogham/core/util/bean/FieldAccessor::getValue → KILLED
			return (T) field.get(bean);
72
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | ClassCastException e) {
73
			throw new InvalidPropertyException("Failed to get value for property '" + name + "' on bean '" + getClassName() + "'", bean, name, e);
74
		}
75
	}
76
77
	private void initField() throws NoSuchFieldException {
78
		field = bean.getClass().getDeclaredField(name);
79 4 1. initField : negated conditional → NO_COVERAGE
2. initField : negated conditional → NO_COVERAGE
3. initField : negated conditional → KILLED
4. initField : negated conditional → KILLED
		if (makeAccessible && !field.isAccessible()) {
80 2 1. initField : removed call to java/lang/reflect/Field::setAccessible → NO_COVERAGE
2. initField : removed call to java/lang/reflect/Field::setAccessible → KILLED
			field.setAccessible(true);
81
		}
82
	}
83
84
	private String getClassName() {
85 4 1. getClassName : replaced return value with "" for fr/sii/ogham/core/util/bean/FieldAccessor::getClassName → SURVIVED
2. getClassName : replaced return value with "" for fr/sii/ogham/core/util/bean/FieldAccessor::getClassName → NO_COVERAGE
3. getClassName : negated conditional → SURVIVED
4. getClassName : negated conditional → NO_COVERAGE
		return bean == null ? "null" : bean.getClass().getName();
86
	}
87
}

Mutations

68

1.1
Location : getValue
Killed by : oghamcore.ut.core.bean.util.FieldAccessorTest.makeAccessible(oghamcore.ut.core.bean.util.FieldAccessorTest)
negated conditional → KILLED

2.2
Location : getValue
Killed by : none
negated conditional → NO_COVERAGE

69

1.1
Location : getValue
Killed by : none
removed call to fr/sii/ogham/core/util/bean/FieldAccessor::initField → NO_COVERAGE

2.2
Location : getValue
Killed by : oghamcore.ut.core.bean.util.FieldAccessorTest.makeAccessible(oghamcore.ut.core.bean.util.FieldAccessorTest)
removed call to fr/sii/ogham/core/util/bean/FieldAccessor::initField → KILLED

71

1.1
Location : getValue
Killed by : oghamcore.ut.core.bean.util.FieldAccessorTest.makeAccessible(oghamcore.ut.core.bean.util.FieldAccessorTest)
replaced return value with null for fr/sii/ogham/core/util/bean/FieldAccessor::getValue → KILLED

2.2
Location : getValue
Killed by : none
replaced return value with null for fr/sii/ogham/core/util/bean/FieldAccessor::getValue → NO_COVERAGE

79

1.1
Location : initField
Killed by : oghamcore.ut.core.bean.util.FieldAccessorTest.makeAccessible(oghamcore.ut.core.bean.util.FieldAccessorTest)
negated conditional → KILLED

2.2
Location : initField
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : initField
Killed by : none
negated conditional → NO_COVERAGE

4.4
Location : initField
Killed by : oghamcore.ut.core.bean.util.FieldAccessorTest.makeAccessible(oghamcore.ut.core.bean.util.FieldAccessorTest)
negated conditional → KILLED

80

1.1
Location : initField
Killed by : oghamcore.ut.core.bean.util.FieldAccessorTest.makeAccessible(oghamcore.ut.core.bean.util.FieldAccessorTest)
removed call to java/lang/reflect/Field::setAccessible → KILLED

2.2
Location : initField
Killed by : none
removed call to java/lang/reflect/Field::setAccessible → NO_COVERAGE

85

1.1
Location : getClassName
Killed by : none
replaced return value with "" for fr/sii/ogham/core/util/bean/FieldAccessor::getClassName → SURVIVED

2.2
Location : getClassName
Killed by : none
replaced return value with "" for fr/sii/ogham/core/util/bean/FieldAccessor::getClassName → NO_COVERAGE

3.3
Location : getClassName
Killed by : none
negated conditional → SURVIVED

4.4
Location : getClassName
Killed by : none
negated conditional → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT OGHAM