SimpleBeanReadWrapper.java

1
package fr.sii.ogham.core.util.bean;
2
3
import static fr.sii.ogham.core.util.bean.BeanWrapperUtils.getClassName;
4
import static fr.sii.ogham.core.util.bean.BeanWrapperUtils.getReadMethods;
5
import static fr.sii.ogham.core.util.bean.BeanWrapperUtils.isInvalid;
6
7
import java.lang.reflect.Method;
8
import java.util.ArrayList;
9
import java.util.Collection;
10
import java.util.HashMap;
11
import java.util.Iterator;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.Map.Entry;
15
16
import fr.sii.ogham.core.exception.util.InvalidPropertyException;
17
18
/**
19
 * Simple implementation that wraps a bean in order to access the properties of
20
 * the bean.
21
 * 
22
 * This implementation delegates the access to the properties to
23
 * {@link Accessor}s.
24
 * 
25
 * @author Aurélien Baudet
26
 *
27
 */
28
public class SimpleBeanReadWrapper implements BeanReadWrapper {
29
	private final Object bean;
30
	private final Map<String, Accessor<Object>> accessors;
31
	private final boolean failOnMissingProperty;
32
33
	/**
34
	 * Builds the map of accessors for each bean property.
35
	 * 
36
	 * If a property doesn't exist, an {@link InvalidPropertyException} is
37
	 * thrown.
38
	 * 
39
	 * @param bean
40
	 *            the bean that may have properties to access later
41
	 */
42
	public SimpleBeanReadWrapper(Object bean) {
43
		this(bean, true);
44
	}
45
46
	/**
47
	 * Builds the map of accessors for each bean property.
48
	 * 
49
	 * @param bean
50
	 *            the bean that may have properties to access later
51
	 * @param failOnMissingProperty
52
	 *            if false null is returned if the property doesn't exist, if
53
	 *            true an {@link InvalidPropertyException} is thrown if the
54
	 *            property doesn't exist
55
	 */
56
	public SimpleBeanReadWrapper(Object bean, boolean failOnMissingProperty) {
57
		super();
58
		this.bean = bean;
59
		this.accessors = new HashMap<>();
60
		this.failOnMissingProperty = failOnMissingProperty;
61 1 1. <init> : removed call to fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::initialize → RUN_ERROR
		initialize(bean);
62
	}
63
64
	@Override
65
	public Object getPropertyValue(String name) {
66 1 1. getPropertyValue : negated conditional → RUN_ERROR
		if (getWrappedBean() == null) {
67
			return null;
68
		}
69
70
		Accessor<Object> accessor = accessors.get(name);
71 2 1. getPropertyValue : negated conditional → RUN_ERROR
2. getPropertyValue : negated conditional → RUN_ERROR
		if (failOnMissingProperty && accessor == null) {
72
			throw new InvalidPropertyException("No accessor for property '" + name + "' on bean '" + getClassName(bean) + "'", bean, name);
73
		}
74
75 2 1. getPropertyValue : negated conditional → RUN_ERROR
2. getPropertyValue : replaced return value with null for fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::getPropertyValue → RUN_ERROR
		return accessor == null ? null : accessor.getValue();
76
	}
77
78
	@Override
79
	public List<String> getProperties() {
80 1 1. getProperties : replaced return value with Collections.emptyList for fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::getProperties → RUN_ERROR
		return new ArrayList<>(accessors.keySet());
81
	}
82
83
	@Override
84
	public Object getWrappedBean() {
85 1 1. getWrappedBean : replaced return value with null for fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::getWrappedBean → RUN_ERROR
		return bean;
86
	}
87
88
	@SuppressWarnings("unchecked")
89
	private void initialize(Object bean) {
90 1 1. initialize : negated conditional → RUN_ERROR
		if (bean == null) {
91
			return;
92
		}
93
94 1 1. initialize : negated conditional → RUN_ERROR
		if (isInvalid(bean)) {
95
			throw new IllegalArgumentException(getClassName(bean) + " values can't be used as bean");
96 1 1. initialize : negated conditional → RUN_ERROR
		} else if (bean instanceof Collection) {
97 1 1. initialize : removed call to fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::initializeCollection → RUN_ERROR
			initializeCollection((Collection<Object>) bean, accessors);
98 1 1. initialize : negated conditional → RUN_ERROR
		} else if (bean instanceof Map) {
99 1 1. initialize : removed call to fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::initializeMap → RUN_ERROR
			initializeMap((Map<Object, Object>) bean, accessors);
100
		} else {
101 1 1. initialize : removed call to fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::initializeBean → RUN_ERROR
			initializeBean(bean, accessors);
102
		}
103
	}
104
105
	private static void initializeCollection(Collection<Object> collection, Map<String, Accessor<Object>> accessors) {
106
		int i = 0;
107 2 1. initializeCollection : Changed increment from 1 to -1 → RUN_ERROR
2. initializeCollection : negated conditional → RUN_ERROR
		for (Iterator<Object> it = collection.iterator(); it.hasNext(); i++) {
108
			accessors.put(Integer.toString(i), new DirectAccessor<>(it.next()));
109
		}
110
	}
111
112
	private static void initializeMap(Map<Object, Object> map, Map<String, Accessor<Object>> accessors) {
113
		for (Entry<Object, Object> entry : map.entrySet()) {
114
			accessors.put(entry.getKey().toString(), new DirectAccessor<>(entry.getValue()));
115
		}
116
	}
117
118
	private static void initializeBean(Object bean, Map<String, Accessor<Object>> accessors) {
119
		Map<String, Method> readMethods = getReadMethods(bean);
120
		for(Entry<String, Method> entry : readMethods.entrySet()) {
121
			String name = entry.getKey();
122
			Method readMethod = entry.getValue();
123 1 1. initializeBean : negated conditional → RUN_ERROR
			if (readMethod != null) {
124
				accessors.put(name, new ReadMethodAccessor<>(bean, name, readMethod));
125
			}
126
		}
127
	}
128
}

Mutations

61

1.1
Location : <init>
Killed by :
removed call to fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::initialize → RUN_ERROR

66

1.1
Location : getPropertyValue
Killed by :
negated conditional → RUN_ERROR

71

1.1
Location : getPropertyValue
Killed by :
negated conditional → RUN_ERROR

2.2
Location : getPropertyValue
Killed by :
negated conditional → RUN_ERROR

75

1.1
Location : getPropertyValue
Killed by :
negated conditional → RUN_ERROR

2.2
Location : getPropertyValue
Killed by :
replaced return value with null for fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::getPropertyValue → RUN_ERROR

80

1.1
Location : getProperties
Killed by :
replaced return value with Collections.emptyList for fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::getProperties → RUN_ERROR

85

1.1
Location : getWrappedBean
Killed by :
replaced return value with null for fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::getWrappedBean → RUN_ERROR

90

1.1
Location : initialize
Killed by :
negated conditional → RUN_ERROR

94

1.1
Location : initialize
Killed by :
negated conditional → RUN_ERROR

96

1.1
Location : initialize
Killed by :
negated conditional → RUN_ERROR

97

1.1
Location : initialize
Killed by :
removed call to fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::initializeCollection → RUN_ERROR

98

1.1
Location : initialize
Killed by :
negated conditional → RUN_ERROR

99

1.1
Location : initialize
Killed by :
removed call to fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::initializeMap → RUN_ERROR

101

1.1
Location : initialize
Killed by :
removed call to fr/sii/ogham/core/util/bean/SimpleBeanReadWrapper::initializeBean → RUN_ERROR

107

1.1
Location : initializeCollection
Killed by :
Changed increment from 1 to -1 → RUN_ERROR

2.2
Location : initializeCollection
Killed by :
negated conditional → RUN_ERROR

123

1.1
Location : initializeBean
Killed by :
negated conditional → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1