1 | package fr.sii.ogham.core.util.bean; | |
2 | ||
3 | import static fr.sii.ogham.core.util.bean.BeanWrapperUtils.getReadMethod; | |
4 | ||
5 | import java.lang.reflect.InvocationTargetException; | |
6 | import java.lang.reflect.Method; | |
7 | ||
8 | import fr.sii.ogham.core.exception.util.InvalidPropertyException; | |
9 | ||
10 | /** | |
11 | * Access object property using reflection | |
12 | * | |
13 | * @author Aurélien Baudet | |
14 | * | |
15 | * @param <T> | |
16 | * The type of the value | |
17 | */ | |
18 | public class ReadMethodAccessor<T> implements Accessor<T> { | |
19 | private static final Object[] NULL_ARGUMENTS = {}; | |
20 | private final Object bean; | |
21 | private final String name; | |
22 | private final Method readMethod; | |
23 | private final Accessor<T> defaultAccessor; | |
24 | ||
25 | /** | |
26 | * Initialize the accessor with the provided bean and property name. | |
27 | * | |
28 | * <p> | |
29 | * The read {@link Method} is searched by reflection and using the property | |
30 | * name. | |
31 | * | |
32 | * <p> | |
33 | * If no read method defined for the property, the {@link FieldAccessor} is | |
34 | * used. | |
35 | * | |
36 | * @param bean | |
37 | * the bean that will be accessed | |
38 | * @param name | |
39 | * the name of the property to access | |
40 | */ | |
41 | public ReadMethodAccessor(Object bean, String name) { | |
42 | this(bean, name, getReadMethod(bean, name), new FieldAccessor<>(bean, name)); | |
43 | } | |
44 | ||
45 | /** | |
46 | * Initialize the accessor with the provided bean and property name. | |
47 | * | |
48 | * <p> | |
49 | * The read {@link Method} is directly provided in order to avoid reflection | |
50 | * scanning. | |
51 | * | |
52 | * <p> | |
53 | * If no read method defined for the property, the {@link FieldAccessor} is | |
54 | * used. | |
55 | * | |
56 | * @param bean | |
57 | * the bean that will be accessed | |
58 | * @param name | |
59 | * the name of the property to access | |
60 | * @param readMethod | |
61 | * the getter method obtained through reflection that is used to | |
62 | * access the property | |
63 | */ | |
64 | public ReadMethodAccessor(Object bean, String name, Method readMethod) { | |
65 | this(bean, name, readMethod, new FieldAccessor<>(bean, name)); | |
66 | } | |
67 | ||
68 | /** | |
69 | * Initialize the accessor with the provided bean and property name. | |
70 | * | |
71 | * <p> | |
72 | * The read {@link Method} is directly provided in order to avoid reflection | |
73 | * scanning. | |
74 | * | |
75 | * <p> | |
76 | * If no read method defined for the property, the | |
77 | * <code>defaultAccessor</code> parameter is used. | |
78 | * | |
79 | * @param bean | |
80 | * the bean that will be accessed | |
81 | * @param name | |
82 | * the name of the property to access | |
83 | * @param readMethod | |
84 | * the getter method obtained through reflection that is used to | |
85 | * access the property. May be null if no read method (no getter) | |
86 | * exists in the bean class but the property exists | |
87 | * @param defaultAccessor | |
88 | * the default accessor if read method is null. May be null if | |
89 | * only read method is used | |
90 | */ | |
91 | public ReadMethodAccessor(Object bean, String name, Method readMethod, Accessor<T> defaultAccessor) { | |
92 | super(); | |
93 | this.bean = bean; | |
94 | this.name = name; | |
95 | this.readMethod = readMethod; | |
96 | this.defaultAccessor = defaultAccessor; | |
97 | } | |
98 | ||
99 | @Override | |
100 | @SuppressWarnings("unchecked") | |
101 | public T getValue() { | |
102 |
6
1. getValue : negated conditional → SURVIVED 2. getValue : negated conditional → NO_COVERAGE 3. getValue : negated conditional → NO_COVERAGE 4. getValue : negated conditional → TIMED_OUT 5. getValue : negated conditional → KILLED 6. getValue : negated conditional → KILLED |
if (readMethod == null && defaultAccessor == null) { |
103 | throw new InvalidPropertyException("Can't get value for property '" + name + "' on bean '" + getClassName() + "': no getter and no default accessor provided.", bean, name); | |
104 | } | |
105 |
8
1. getValue : negated conditional → NO_COVERAGE 2. getValue : negated conditional → TIMED_OUT 3. getValue : negated conditional → KILLED 4. getValue : negated conditional → KILLED 5. getValue : negated conditional → KILLED 6. getValue : negated conditional → KILLED 7. getValue : negated conditional → KILLED 8. getValue : negated conditional → KILLED |
if (readMethod == null) { |
106 |
2
1. getValue : replaced return value with null for fr/sii/ogham/core/util/bean/ReadMethodAccessor::getValue → NO_COVERAGE 2. getValue : replaced return value with null for fr/sii/ogham/core/util/bean/ReadMethodAccessor::getValue → KILLED |
return defaultAccessor.getValue(); |
107 | } | |
108 | ||
109 | try { | |
110 |
8
1. getValue : replaced return value with null for fr/sii/ogham/core/util/bean/ReadMethodAccessor::getValue → NO_COVERAGE 2. getValue : replaced return value with null for fr/sii/ogham/core/util/bean/ReadMethodAccessor::getValue → TIMED_OUT 3. getValue : replaced return value with null for fr/sii/ogham/core/util/bean/ReadMethodAccessor::getValue → KILLED 4. getValue : replaced return value with null for fr/sii/ogham/core/util/bean/ReadMethodAccessor::getValue → KILLED 5. getValue : replaced return value with null for fr/sii/ogham/core/util/bean/ReadMethodAccessor::getValue → KILLED 6. getValue : replaced return value with null for fr/sii/ogham/core/util/bean/ReadMethodAccessor::getValue → KILLED 7. getValue : replaced return value with null for fr/sii/ogham/core/util/bean/ReadMethodAccessor::getValue → KILLED 8. getValue : replaced return value with null for fr/sii/ogham/core/util/bean/ReadMethodAccessor::getValue → KILLED |
return (T) readMethod.invoke(bean, NULL_ARGUMENTS); |
111 | } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassCastException e) { | |
112 | throw new InvalidPropertyException("Failed to get value for property '" + name + "' on bean '" + getClassName() + "'", bean, name, e); | |
113 | } | |
114 | } | |
115 | ||
116 | private String getClassName() { | |
117 |
4
1. getClassName : replaced return value with "" for fr/sii/ogham/core/util/bean/ReadMethodAccessor::getClassName → NO_COVERAGE 2. getClassName : replaced return value with "" for fr/sii/ogham/core/util/bean/ReadMethodAccessor::getClassName → SURVIVED 3. getClassName : negated conditional → NO_COVERAGE 4. getClassName : negated conditional → SURVIVED |
return bean == null ? "null" : bean.getClass().getName(); |
118 | } | |
119 | ||
120 | } | |
Mutations | ||
102 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
105 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
106 |
1.1 2.2 |
|
110 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
117 |
1.1 2.2 3.3 4.4 |