Context.java

1
package fi.eis.libraries.di.context;
2
3
import fi.eis.libraries.di.Inject;
4
import fi.eis.libraries.di.logger.LogLevel;
5
import fi.eis.libraries.di.logger.SimpleLogger;
6
7
import java.lang.reflect.Field;
8
import java.util.ArrayList;
9
import java.util.Collections;
10
import java.util.List;
11
12
/**
13
 * A Context instance makes up of the DI context our library creates. It consists of one or more
14
 * Module instances that provide classes as required.
15
 *
16
 * @author eis
17
 */
18
public class Context {
19
20
    protected final SimpleLogger logger = new SimpleLogger(this.getClass());
21
22 2 1. <init> : Removed assignment to member variable modules → KILLED
2. <init> : removed call to java/util/ArrayList::<init> → KILLED
    protected final List<Module> modules = new ArrayList<>();
23
    public Context(Module... modules) {
24 1 1. <init> : removed call to java/util/Collections::addAll → KILLED
        Collections.addAll(this.modules, modules);
25
    }
26
    public <T> T get(Class<T> type) {
27
        logger.debug("context.get=" + type);
28
        T object = null;
29
        for (Module module : modules) {
30
31 4 1. get : removed conditional - replaced equality check with true → SURVIVED
2. get : negated conditional → KILLED
3. get : removed call to fi/eis/libraries/di/context/Module::has → KILLED
4. get : removed conditional - replaced equality check with false → KILLED
            if (module.has(type)) {
32
                logger.debug("has type " + type);
33 1 1. get : removed call to fi/eis/libraries/di/context/Module::get → KILLED
                object = module.get(type);
34
                break;
35
            }
36
        }
37 3 1. get : removed conditional - replaced equality check with false → SURVIVED
2. get : removed conditional - replaced equality check with true → KILLED
3. get : negated conditional → KILLED
        if (object == null) {
38 6 1. get : Substituted 2 with 3 → NO_COVERAGE
2. get : Substituted 1 with 0 → NO_COVERAGE
3. get : removed call to java/lang/String::format → NO_COVERAGE
4. get : Substituted 0 with 1 → NO_COVERAGE
5. get : replaced call to java/lang/String::format with argument → NO_COVERAGE
6. get : removed call to java/lang/IllegalArgumentException::<init> → NO_COVERAGE
            throw new IllegalArgumentException(String.format("Type %s was not found, looking at modules %s", type, modules));
39
        }
40 1 1. get : removed call to fi/eis/libraries/di/context/Context::resolveProperties → KILLED
        resolveProperties(object);
41 1 1. get : replaced return value with null for fi/eis/libraries/di/context/Context::get → KILLED
        return object;
42
    }
43
44
    private <T> void resolveProperties(T object) {
45
        logger.debug("resolveProperties=" + object);
46
        logger.debug("class=" + object.getClass());
47
48
        // @Injected fields
49 2 1. resolveProperties : removed call to java/lang/Object::getClass → KILLED
2. resolveProperties : removed call to java/lang/Class::getDeclaredFields → KILLED
        Field[] allFields = object.getClass().getDeclaredFields();
50
51
        for (Field field : allFields) {
52 4 1. resolveProperties : removed conditional - replaced equality check with true → SURVIVED
2. resolveProperties : removed conditional - replaced equality check with false → KILLED
3. resolveProperties : removed call to java/lang/reflect/Field::isAnnotationPresent → KILLED
4. resolveProperties : negated conditional → KILLED
            if(field.isAnnotationPresent(Inject.class)) {
53
                try {
54 1 1. resolveProperties : removed call to java/lang/reflect/Field::isAccessible → SURVIVED
                    boolean originallyAccessible = field.isAccessible();
55 2 1. resolveProperties : removed call to java/lang/reflect/Field::setAccessible → KILLED
2. resolveProperties : Substituted 1 with 0 → KILLED
                    field.setAccessible(true);
56 3 1. resolveProperties : removed call to java/lang/reflect/Field::getType → KILLED
2. resolveProperties : removed call to fi/eis/libraries/di/context/Context::get → KILLED
3. resolveProperties : removed call to java/lang/reflect/Field::set → KILLED
                    field.set(object, get(field.getType()));
57 3 1. resolveProperties : removed conditional - replaced equality check with false → SURVIVED
2. resolveProperties : removed conditional - replaced equality check with true → SURVIVED
3. resolveProperties : negated conditional → SURVIVED
                    if (!originallyAccessible) {
58 2 1. resolveProperties : Substituted 0 with 1 → SURVIVED
2. resolveProperties : removed call to java/lang/reflect/Field::setAccessible → SURVIVED
                        field.setAccessible(false);
59
                    }
60
                } catch (IllegalAccessException e) {
61 1 1. resolveProperties : removed call to java/lang/IllegalStateException::<init> → NO_COVERAGE
                    throw new IllegalStateException(e);
62
                }
63
            }
64
        }
65
    }
66
    public void setLogLevel(LogLevel level) {
67
        this.logger.setLogLevel(level);
68
    }
69
}

Mutations

22

1.1
Location : <init>
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
Removed assignment to member variable modules → KILLED

2.2
Location : <init>
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed call to java/util/ArrayList::<init> → KILLED

24

1.1
Location : <init>
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed call to java/util/Collections::addAll → KILLED

31

1.1
Location : get
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
negated conditional → KILLED

2.2
Location : get
Killed by : none
removed conditional - replaced equality check with true → SURVIVED
Covering tests

3.3
Location : get
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed call to fi/eis/libraries/di/context/Module::has → KILLED

4.4
Location : get
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed conditional - replaced equality check with false → KILLED

33

1.1
Location : get
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed call to fi/eis/libraries/di/context/Module::get → KILLED

37

1.1
Location : get
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed conditional - replaced equality check with true → KILLED

2.2
Location : get
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
negated conditional → KILLED

3.3
Location : get
Killed by : none
removed conditional - replaced equality check with false → SURVIVED
Covering tests

38

1.1
Location : get
Killed by : none
Substituted 2 with 3 → NO_COVERAGE

2.2
Location : get
Killed by : none
Substituted 1 with 0 → NO_COVERAGE

3.3
Location : get
Killed by : none
removed call to java/lang/String::format → NO_COVERAGE

4.4
Location : get
Killed by : none
Substituted 0 with 1 → NO_COVERAGE

5.5
Location : get
Killed by : none
replaced call to java/lang/String::format with argument → NO_COVERAGE

6.6
Location : get
Killed by : none
removed call to java/lang/IllegalArgumentException::<init> → NO_COVERAGE

40

1.1
Location : get
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed call to fi/eis/libraries/di/context/Context::resolveProperties → KILLED

41

1.1
Location : get
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
replaced return value with null for fi/eis/libraries/di/context/Context::get → KILLED

49

1.1
Location : resolveProperties
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed call to java/lang/Object::getClass → KILLED

2.2
Location : resolveProperties
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed call to java/lang/Class::getDeclaredFields → KILLED

52

1.1
Location : resolveProperties
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed conditional - replaced equality check with false → KILLED

2.2
Location : resolveProperties
Killed by : none
removed conditional - replaced equality check with true → SURVIVED
Covering tests

3.3
Location : resolveProperties
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed call to java/lang/reflect/Field::isAnnotationPresent → KILLED

4.4
Location : resolveProperties
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
negated conditional → KILLED

54

1.1
Location : resolveProperties
Killed by : none
removed call to java/lang/reflect/Field::isAccessible → SURVIVED
Covering tests

55

1.1
Location : resolveProperties
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed call to java/lang/reflect/Field::setAccessible → KILLED

2.2
Location : resolveProperties
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
Substituted 1 with 0 → KILLED

56

1.1
Location : resolveProperties
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed call to java/lang/reflect/Field::getType → KILLED

2.2
Location : resolveProperties
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed call to fi/eis/libraries/di/context/Context::get → KILLED

3.3
Location : resolveProperties
Killed by : fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest.testDi(fi.eis.libraries.di.test.moduleconfig.DIInheritanceTest)
removed call to java/lang/reflect/Field::set → KILLED

57

1.1
Location : resolveProperties
Killed by : none
removed conditional - replaced equality check with false → SURVIVED
Covering tests

2.2
Location : resolveProperties
Killed by : none
removed conditional - replaced equality check with true → SURVIVED Covering tests

3.3
Location : resolveProperties
Killed by : none
negated conditional → SURVIVED Covering tests

58

1.1
Location : resolveProperties
Killed by : none
Substituted 0 with 1 → SURVIVED
Covering tests

2.2
Location : resolveProperties
Killed by : none
removed call to java/lang/reflect/Field::setAccessible → SURVIVED Covering tests

61

1.1
Location : resolveProperties
Killed by : none
removed call to java/lang/IllegalStateException::<init> → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.25.4 support