| 1 | package fi.eis.libraries.di.context.configclass; | |
| 2 | ||
| 3 | import fi.eis.libraries.di.DependencyInjection; | |
| 4 | import fi.eis.libraries.di.context.Context; | |
| 5 | import fi.eis.libraries.di.logger.LogLevel; | |
| 6 | ||
| 7 | import java.lang.reflect.InvocationTargetException; | |
| 8 | import java.lang.reflect.Method; | |
| 9 | import java.util.ArrayList; | |
| 10 | import java.util.Arrays; | |
| 11 | import java.util.Collections; | |
| 12 | import java.util.Comparator; | |
| 13 | import java.util.HashMap; | |
| 14 | import java.util.List; | |
| 15 | import java.util.Map; | |
| 16 | ||
| 17 | /** | |
| 18 | * A Configuration Class is a Spring-style class that contains the required configuration | |
| 19 | * to do dependency injection. It contains the definition of dependency associations so that | |
| 20 | * it can be used to figure out which class depends on what. | |
| 21 | * | |
| 22 | * This class contains the logic to handle such classes, so we create a dependency injection context | |
| 23 | * based on such class or several classes. This class goes through all the methods in a | |
| 24 | * configuration class, creating the needed dependencies and instantiating the configured classes | |
| 25 | * with their dependencies. | |
| 26 | */ | |
| 27 | public class ConfigurationClassContext extends Context { | |
| 28 |
2
1. <init> : removed call to java/util/HashMap::<init> → KILLED 2. <init> : Removed assignment to member variable classObjectMap → KILLED |
private final Map<Class, Object> classObjectMap = new HashMap<>(); |
| 29 | ||
| 30 | public ConfigurationClassContext(Class... configurationClasses) { | |
| 31 | this(LogLevel.NONE, configurationClasses); | |
| 32 | } | |
| 33 |
1
1. <init> : Substituted 0 with 1 → KILLED |
public ConfigurationClassContext(LogLevel logLevel, Class... configurationClassInstances) { |
| 34 |
1
1. <init> : removed call to fi/eis/libraries/di/context/configclass/ConfigurationClassContext::setLogLevel → SURVIVED |
setLogLevel(logLevel); |
| 35 | ||
| 36 | try { | |
| 37 |
1
1. <init> : removed call to java/util/ArrayList::<init> → KILLED |
List<Map.Entry<Object, Method>> confClassCreationMethodTuples = new ArrayList<>(); |
| 38 | for (Class configurationClass: configurationClassInstances){ | |
| 39 |
1
1. <init> : removed call to java/lang/Class::newInstance → KILLED |
Object configurationClassInstance = configurationClass.newInstance(); |
| 40 | ||
| 41 |
1
1. <init> : removed call to java/lang/Class::getMethods → KILLED |
for (Method m : configurationClass.getMethods()) { |
| 42 |
4
1. <init> : removed call to java/lang/reflect/Method::getDeclaringClass → KILLED 2. <init> : removed conditional - replaced equality check with true → KILLED 3. <init> : removed conditional - replaced equality check with false → KILLED 4. <init> : negated conditional → KILLED |
if (m.getDeclaringClass() != Object.class) { |
| 43 | logger.debug("Got method " + m); | |
| 44 |
2
1. <init> : removed call to fi/eis/libraries/di/context/configclass/ConfigurationClassContext::tuple → KILLED 2. <init> : removed call to java/util/List::add → KILLED |
confClassCreationMethodTuples.add(tuple(configurationClassInstance, m)); |
| 45 | } | |
| 46 | } | |
| 47 | } | |
| 48 |
1
1. <init> : removed call to java/util/List::sort → KILLED |
confClassCreationMethodTuples.sort(ConfClassCreationMethodTuplesComparator); |
| 49 | logger.debug("tuples: " + confClassCreationMethodTuples); | |
| 50 | ||
| 51 | for (Map.Entry<Object,Method> confClassCreationMethodTuple: confClassCreationMethodTuples) { | |
| 52 |
1
1. <init> : removed call to java/util/Map$Entry::getValue → KILLED |
Method method = confClassCreationMethodTuple.getValue(); |
| 53 |
3
1. <init> : replaced call to fi/eis/libraries/di/context/configclass/ConfigurationClassContext::newInstance with argument → KILLED 2. <init> : removed call to java/util/Map$Entry::getKey → KILLED 3. <init> : removed call to fi/eis/libraries/di/context/configclass/ConfigurationClassContext::newInstance → KILLED |
Object instance = newInstance(confClassCreationMethodTuple.getKey(), method); |
| 54 |
3
1. <init> : removed call to java/lang/reflect/Method::getReturnType → KILLED 2. <init> : removed call to java/util/Map::put → KILLED 3. <init> : replaced call to java/util/Map::put with argument → KILLED |
classObjectMap.put(method.getReturnType(), instance); |
| 55 | logger.debug("instantiated and stored instance for class " + method.getReturnType()); | |
| 56 | } | |
| 57 | } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { | |
| 58 |
1
1. <init> : removed call to java/lang/IllegalArgumentException::<init> → NO_COVERAGE |
throw new IllegalArgumentException(e); |
| 59 | } | |
| 60 | logger.debug("class-instance module: " + DependencyInjection.module(classObjectMap)); | |
| 61 |
2
1. <init> : removed call to java/util/List::add → KILLED 2. <init> : removed call to fi/eis/libraries/di/DependencyInjection::module → KILLED |
super.modules.add(DependencyInjection.module(classObjectMap)) ; |
| 62 | } | |
| 63 | ||
| 64 | private static Map.Entry<Object,Method> tuple(Object object, Method method) { | |
| 65 |
2
1. tuple : replaced return value with null for fi/eis/libraries/di/context/configclass/ConfigurationClassContext::tuple → KILLED 2. tuple : removed call to java/util/AbstractMap$SimpleEntry::<init> → KILLED |
return new HashMap.SimpleEntry<>(object, method); |
| 66 | } | |
| 67 | private static final Comparator<Map.Entry<Object,Method>> ConfClassCreationMethodTuplesComparator = | |
| 68 |
3
1. lambda$static$0 : removed call to java/lang/reflect/Method::getParameterTypes → KILLED 2. lambda$static$0 : replaced int return with 0 for fi/eis/libraries/di/context/configclass/ConfigurationClassContext::lambda$static$0 → KILLED 3. lambda$static$0 : removed call to java/util/Map$Entry::getValue → KILLED |
Comparator.comparingInt(o -> o.getValue().getParameterTypes().length); |
| 69 | ||
| 70 | private Object newInstance(Object configurationClass, Method method) throws InstantiationException, | |
| 71 | IllegalAccessException, NoSuchMethodException, InvocationTargetException { | |
| 72 |
1
1. newInstance : removed call to java/lang/reflect/Method::getParameterTypes → KILLED |
Class[] parameterTypes = method.getParameterTypes(); |
| 73 | Object[] paramArr = new Object[parameterTypes.length]; | |
| 74 |
1
1. newInstance : Substituted 0 with 1 → KILLED |
int i = 0; |
| 75 | ||
| 76 | for(Class c : parameterTypes) { | |
| 77 |
4
1. newInstance : Removed increment 1 → SURVIVED 2. newInstance : Changed increment from 1 to -1 → SURVIVED 3. newInstance : replaced call to java/util/Map::get with argument → KILLED 4. newInstance : removed call to java/util/Map::get → KILLED |
paramArr[i++] = classObjectMap.get(c); |
| 78 | } | |
| 79 |
3
1. newInstance : replaced return value with null for fi/eis/libraries/di/context/configclass/ConfigurationClassContext::newInstance → KILLED 2. newInstance : replaced call to java/lang/reflect/Method::invoke with argument → KILLED 3. newInstance : removed call to java/lang/reflect/Method::invoke → KILLED |
return method.invoke(configurationClass, paramArr); |
| 80 | } | |
| 81 | } | |
Mutations | ||
| 28 |
1.1 2.2 |
|
| 33 |
1.1 |
|
| 34 |
1.1 |
|
| 37 |
1.1 |
|
| 39 |
1.1 |
|
| 41 |
1.1 |
|
| 42 |
1.1 2.2 3.3 4.4 |
|
| 44 |
1.1 2.2 |
|
| 48 |
1.1 |
|
| 52 |
1.1 |
|
| 53 |
1.1 2.2 3.3 |
|
| 54 |
1.1 2.2 3.3 |
|
| 58 |
1.1 |
|
| 61 |
1.1 2.2 |
|
| 65 |
1.1 2.2 |
|
| 68 |
1.1 2.2 3.3 |
|
| 72 |
1.1 |
|
| 74 |
1.1 |
|
| 77 |
1.1 2.2 3.3 4.4 |
|
| 79 |
1.1 2.2 3.3 |