RecordInstantiator.java

1
package com.ancientprogramming.fixedformat4j.format.impl;
2
3
import com.ancientprogramming.fixedformat4j.exception.FixedFormatException;
4
5
import java.lang.reflect.Constructor;
6
7
import static java.lang.String.format;
8
9
/**
10
 * Creates instances of {@code @Record}-annotated classes via reflection,
11
 * including support for static nested classes and non-static inner classes.
12
 *
13
 * @author Jacob von Eyben - <a href="https://eybenconsult.com">https://eybenconsult.com</a>
14
 * @since 1.6.0
15
 */
16
class RecordInstantiator {
17
18
  <T> T instantiate(Class<T> fixedFormatRecordClass) {
19
    try {
20
      Constructor<T> constructor = fixedFormatRecordClass.getDeclaredConstructor();
21 1 1. instantiate : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/RecordInstantiator::instantiate → KILLED
      return constructor.newInstance();
22
    } catch (NoSuchMethodException e) {
23
      Class<?> declaringClass = fixedFormatRecordClass.getDeclaringClass();
24 1 1. instantiate : negated conditional → KILLED
      if (declaringClass != null) {
25 1 1. instantiate : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/RecordInstantiator::instantiate → KILLED
        return instantiateInnerClass(fixedFormatRecordClass, declaringClass, e);
26
      }
27
      throw new FixedFormatException(format(
28
          "%s is missing a default constructor which is nessesary to be loaded through %s",
29
          fixedFormatRecordClass.getName(), getClass().getName()));
30
    } catch (Exception e) {
31
      throw new FixedFormatException(format("unable to create instance of %s", fixedFormatRecordClass.getName()), e);
32
    }
33
  }
34
35
  private <T> T instantiateInnerClass(Class<T> innerClass, Class<?> declaringClass, Exception outerException) {
36
    Object declaringClassInstance;
37
    try {
38
      Constructor<?> declaringClassConstructor = declaringClass.getDeclaredConstructor();
39
      declaringClassInstance = declaringClassConstructor.newInstance();
40
    } catch (NoSuchMethodException dex) {
41
      throw new FixedFormatException(format(
42
          "Trying to create instance of innerclass %s, but the declaring class %s is missing a default constructor which is nessesary to be loaded through %s",
43
          innerClass.getName(), declaringClass.getName(), getClass().getName()));
44
    } catch (Exception de) {
45
      throw new FixedFormatException(format(
46
          "unable to create instance of declaring class %s, which is needed to instansiate %s",
47
          declaringClass.getName(), innerClass.getName()), outerException);
48
    }
49
50
    try {
51
      Constructor<T> constructor = innerClass.getDeclaredConstructor(declaringClass);
52 1 1. instantiateInnerClass : replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/RecordInstantiator::instantiateInnerClass → KILLED
      return constructor.newInstance(declaringClassInstance);
53
    } catch (NoSuchMethodException ex) {
54
      throw new FixedFormatException(format(
55
          "%s is missing a default constructor which is nessesary to be loaded through %s",
56
          innerClass.getName(), getClass().getName()));
57
    } catch (Exception ex) {
58
      throw new FixedFormatException(format("unable to create instance of %s", innerClass.getName()), outerException);
59
    }
60
  }
61
}

Mutations

21

1.1
Location : instantiate
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestRecordInstantiator.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestRecordInstantiator]/[method:instantiate_plainClass_returnsNewInstance()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/RecordInstantiator::instantiate → KILLED

24

1.1
Location : instantiate
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestRecordInstantiator.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestRecordInstantiator]/[method:instantiate_innerClass_returnsNewInstance()]
negated conditional → KILLED

25

1.1
Location : instantiate
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestRecordInstantiator.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestRecordInstantiator]/[method:instantiate_innerClass_returnsNewInstance()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/RecordInstantiator::instantiate → KILLED

52

1.1
Location : instantiateInnerClass
Killed by : com.ancientprogramming.fixedformat4j.format.impl.TestRecordInstantiator.[engine:junit-jupiter]/[class:com.ancientprogramming.fixedformat4j.format.impl.TestRecordInstantiator]/[method:instantiate_innerClass_returnsNewInstance()]
replaced return value with null for com/ancientprogramming/fixedformat4j/format/impl/RecordInstantiator::instantiateInnerClass → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.1