BeanWrapperUtils.java

1
package fr.sii.ogham.core.util.bean;
2
3
import static org.apache.commons.lang3.ClassUtils.isPrimitiveOrWrapper;
4
5
import java.beans.BeanInfo;
6
import java.beans.IntrospectionException;
7
import java.beans.Introspector;
8
import java.beans.PropertyDescriptor;
9
import java.lang.reflect.Method;
10
import java.util.ArrayList;
11
import java.util.HashMap;
12
import java.util.List;
13
import java.util.Map;
14
15
import fr.sii.ogham.core.exception.util.BeanWrapperException;
16
17
/**
18
 * Some utility methods for bean wrapper management
19
 * 
20
 * @author Aurélien Baudet
21
 *
22
 */
23
public final class BeanWrapperUtils {
24
25
	private static final List<Class<?>> INVALID_TYPES = new ArrayList<>();
26
	static {
27
		INVALID_TYPES.add(String.class);
28
		INVALID_TYPES.add(Number.class);
29
	}
30
31
	/**
32
	 * Check if the bean type can be wrapped or not.
33
	 * 
34
	 * @param bean
35
	 *            the bean instance
36
	 * @return false if null or valid type, true if primitive type or string
37
	 */
38
	@SuppressWarnings("squid:S2250")
39
	public static boolean isInvalid(Object bean) {
40 1 1. isInvalid : negated conditional → RUN_ERROR
		if (bean == null) {
41 1 1. isInvalid : replaced boolean return with true for fr/sii/ogham/core/util/bean/BeanWrapperUtils::isInvalid → NO_COVERAGE
			return false;
42
		}
43 2 1. isInvalid : replaced boolean return with true for fr/sii/ogham/core/util/bean/BeanWrapperUtils::isInvalid → RUN_ERROR
2. isInvalid : negated conditional → RUN_ERROR
		return isPrimitiveOrWrapper(bean.getClass()) 
44 1 1. isInvalid : negated conditional → RUN_ERROR
				|| INVALID_TYPES.contains(bean.getClass())
45 1 1. isInvalid : negated conditional → RUN_ERROR
				|| isInstanceOfInvalid(bean.getClass());
46
	}
47
48
	/**
49
	 * Get the class name of the bean even if null.
50
	 * 
51
	 * @param bean
52
	 *            the bean instance
53
	 * @return the class of the bean
54
	 */
55
	public static String getClassName(Object bean) {
56 2 1. getClassName : replaced return value with "" for fr/sii/ogham/core/util/bean/BeanWrapperUtils::getClassName → RUN_ERROR
2. getClassName : negated conditional → RUN_ERROR
		return bean == null ? "null" : bean.getClass().getName();
57
	}
58
59
	/**
60
	 * Get the whole list of read {@link Method}s using introspection.
61
	 * 
62
	 * @param bean
63
	 *            the bean to introspect
64
	 * @return the map of bean property getters (indexed by property name)
65
	 */
66
	public static Map<String, Method> getReadMethods(Object bean) {
67
		Class<? extends Object> beanClass = bean.getClass();
68
		try {
69
			Map<String, Method> readMethods = new HashMap<>();
70
			final BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
71
			final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
72 1 1. getReadMethods : negated conditional → RUN_ERROR
			if (propertyDescriptors != null) {
73 1 1. getReadMethods : removed call to fr/sii/ogham/core/util/bean/BeanWrapperUtils::putReadMethods → RUN_ERROR
				putReadMethods(readMethods, propertyDescriptors);
74
			}
75 1 1. getReadMethods : replaced return value with Collections.emptyMap for fr/sii/ogham/core/util/bean/BeanWrapperUtils::getReadMethods → RUN_ERROR
			return readMethods;
76
		} catch (final IntrospectionException e) {
77
			throw new BeanWrapperException("Failed to initialize bean wrapper on " + beanClass, e);
78
		}
79
	}
80
81
	/**
82
	 * Get a read {@link Method} for a particular property.
83
	 * 
84
	 * @param bean
85
	 *            the bean instance
86
	 * @param name
87
	 *            the name of the property
88
	 * @return the getter method for the property
89
	 */
90
	public static Method getReadMethod(Object bean, String name) {
91 1 1. getReadMethod : replaced return value with null for fr/sii/ogham/core/util/bean/BeanWrapperUtils::getReadMethod → RUN_ERROR
		return getReadMethods(bean).get(name);
92
	}
93
94
	private static void putReadMethods(Map<String, Method> readMethods, final PropertyDescriptor[] propertyDescriptors) {
95
		for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) {
96 1 1. putReadMethods : negated conditional → RUN_ERROR
			if (propertyDescriptor != null) {
97
				final String name = propertyDescriptor.getName();
98
				final Method readMethod = propertyDescriptor.getReadMethod();
99
100 1 1. putReadMethods : negated conditional → RUN_ERROR
				if (readMethod != null) {
101
					readMethods.put(name, readMethod);
102
				}
103
			}
104
		}
105
	}
106
107
	private static boolean isInstanceOfInvalid(Class<?> clazz) {
108
		for(Class<?> c : INVALID_TYPES) {
109 1 1. isInstanceOfInvalid : negated conditional → RUN_ERROR
			if(c.isAssignableFrom(clazz)) {
110 1 1. isInstanceOfInvalid : replaced boolean return with false for fr/sii/ogham/core/util/bean/BeanWrapperUtils::isInstanceOfInvalid → RUN_ERROR
				return true;
111
			}
112
		}
113 1 1. isInstanceOfInvalid : replaced boolean return with true for fr/sii/ogham/core/util/bean/BeanWrapperUtils::isInstanceOfInvalid → RUN_ERROR
		return false;
114
	}
115
116
	private BeanWrapperUtils() {
117
		super();
118
	}
119
120
}

Mutations

40

1.1
Location : isInvalid
Killed by :
negated conditional → RUN_ERROR

41

1.1
Location : isInvalid
Killed by :
replaced boolean return with true for fr/sii/ogham/core/util/bean/BeanWrapperUtils::isInvalid → NO_COVERAGE

43

1.1
Location : isInvalid
Killed by :
replaced boolean return with true for fr/sii/ogham/core/util/bean/BeanWrapperUtils::isInvalid → RUN_ERROR

2.2
Location : isInvalid
Killed by :
negated conditional → RUN_ERROR

44

1.1
Location : isInvalid
Killed by :
negated conditional → RUN_ERROR

45

1.1
Location : isInvalid
Killed by :
negated conditional → RUN_ERROR

56

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

2.2
Location : getClassName
Killed by :
negated conditional → RUN_ERROR

72

1.1
Location : getReadMethods
Killed by :
negated conditional → RUN_ERROR

73

1.1
Location : getReadMethods
Killed by :
removed call to fr/sii/ogham/core/util/bean/BeanWrapperUtils::putReadMethods → RUN_ERROR

75

1.1
Location : getReadMethods
Killed by :
replaced return value with Collections.emptyMap for fr/sii/ogham/core/util/bean/BeanWrapperUtils::getReadMethods → RUN_ERROR

91

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

96

1.1
Location : putReadMethods
Killed by :
negated conditional → RUN_ERROR

100

1.1
Location : putReadMethods
Killed by :
negated conditional → RUN_ERROR

109

1.1
Location : isInstanceOfInvalid
Killed by :
negated conditional → RUN_ERROR

110

1.1
Location : isInstanceOfInvalid
Killed by :
replaced boolean return with false for fr/sii/ogham/core/util/bean/BeanWrapperUtils::isInstanceOfInvalid → RUN_ERROR

113

1.1
Location : isInstanceOfInvalid
Killed by :
replaced boolean return with true for fr/sii/ogham/core/util/bean/BeanWrapperUtils::isInstanceOfInvalid → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1