• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / jmockit1 / 422

30 Oct 2025 09:23PM UTC coverage: 72.22% (+0.02%) from 72.198%
422

push

github

hazendaz
Combine catches

5674 of 8356 branches covered (67.9%)

Branch coverage included in aggregate %.

0 of 1 new or added line in 1 file covered. (0.0%)

52 existing lines in 8 files now uncovered.

11929 of 16018 relevant lines covered (74.47%)

0.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.54
/main/src/main/java/mockit/internal/reflection/ConstructorReflection.java
1
/*
2
 * Copyright (c) 2006 JMockit developers
3
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
4
 */
5
package mockit.internal.reflection;
6

7
import static mockit.internal.reflection.ParameterReflection.getParameterTypesDescription;
8
import static mockit.internal.reflection.ParameterReflection.indexOfFirstRealParameter;
9
import static mockit.internal.reflection.ParameterReflection.matchesParameterTypes;
10
import static mockit.internal.util.Utilities.ensureThatMemberIsAccessible;
11

12
import edu.umd.cs.findbugs.annotations.NonNull;
13
import edu.umd.cs.findbugs.annotations.Nullable;
14

15
import java.lang.reflect.Constructor;
16
import java.lang.reflect.InvocationTargetException;
17

18
import org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator;
19

20
public final class ConstructorReflection {
21
    private ConstructorReflection() {
22
    }
23

24
    @NonNull
25
    static <T> Constructor<T> findSpecifiedConstructor(@NonNull Class<?> theClass, @NonNull Class<?>[] paramTypes) {
26
        for (Constructor<?> declaredConstructor : theClass.getDeclaredConstructors()) {
1!
27
            Class<?>[] declaredParameterTypes = declaredConstructor.getParameterTypes();
1✔
28
            int firstRealParameter = indexOfFirstRealParameter(declaredParameterTypes, paramTypes);
1✔
29

30
            if (firstRealParameter >= 0
1✔
31
                    && matchesParameterTypes(declaredParameterTypes, paramTypes, firstRealParameter)) {
1✔
32
                // noinspection unchecked
33
                return (Constructor<T>) declaredConstructor;
1✔
34
            }
35
        }
36

37
        String paramTypesDesc = getParameterTypesDescription(paramTypes);
×
38

39
        throw new IllegalArgumentException(
×
40
                "Specified constructor not found: " + theClass.getSimpleName() + paramTypesDesc);
×
41
    }
42

43
    @NonNull
44
    public static <T> T invokeAccessible(@NonNull Constructor<T> constructor, @NonNull Object... initArgs) {
45
        if (!constructor.isAccessible()) {
1✔
46
            constructor.setAccessible(true);
1✔
47
        }
48
        try {
49
            return constructor.newInstance(initArgs);
1✔
50
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
×
51
            Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
×
52

53
            if (cause instanceof Error) {
×
UNCOV
54
                throw (Error) cause;
×
55
            }
56
            if (cause instanceof RuntimeException) {
×
UNCOV
57
                throw (RuntimeException) cause;
×
58
            }
59
            ThrowOfCheckedException.doThrow((Exception) cause);
×
UNCOV
60
            throw new IllegalStateException("Should never get here", cause);
×
61
        }
62
    }
63

64
    public static void newInstanceUsingCompatibleConstructor(@NonNull Class<?> aClass, @NonNull String argument)
65
            throws ReflectiveOperationException {
UNCOV
66
        Constructor<?> constructor = aClass.getDeclaredConstructor(String.class);
×
UNCOV
67
        ensureThatMemberIsAccessible(constructor);
×
68
        constructor.newInstance(argument);
×
69
    }
×
70

71
    @NonNull
72
    public static <T> T newInstanceUsingDefaultConstructor(@NonNull Class<T> aClass) {
73
        try {
74
            Constructor<T> constructor = aClass.getDeclaredConstructor();
1✔
75
            ensureThatMemberIsAccessible(constructor);
1✔
76
            return constructor.newInstance();
1✔
UNCOV
77
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) {
×
UNCOV
78
            throw new RuntimeException(e);
×
79
        } catch (InvocationTargetException e) {
×
80
            throw new RuntimeException(e.getTargetException());
×
81
        }
82
    }
83

84
    @Nullable
85
    public static <T> T newInstanceUsingDefaultConstructorIfAvailable(@NonNull Class<T> aClass) {
86
        try {
UNCOV
87
            Constructor<T> constructor = aClass.getDeclaredConstructor();
×
UNCOV
88
            return constructor.newInstance();
×
89
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException
×
90
                | InvocationTargetException ignore) {
91
            return null;
×
92
        }
93
    }
94

95
    @Nullable
96
    public static <T> T newInstanceUsingPublicConstructorIfAvailable(@NonNull Class<T> aClass,
97
            @NonNull Class<?>[] parameterTypes, @NonNull Object... initArgs) {
98
        Constructor<T> publicConstructor;
99
        try {
100
            publicConstructor = aClass.getConstructor(parameterTypes);
1✔
101
        } catch (NoSuchMethodException ignore) {
1✔
102
            return null;
1✔
103
        }
1✔
104

105
        return invokeAccessible(publicConstructor, initArgs);
1✔
106
    }
107

108
    @NonNull
109
    public static <T> T newInstanceUsingPublicDefaultConstructor(@NonNull Class<T> aClass) {
110
        Constructor<T> publicConstructor;
111
        try {
112
            publicConstructor = aClass.getConstructor();
1✔
UNCOV
113
        } catch (NoSuchMethodException e) {
×
UNCOV
114
            throw new RuntimeException(e);
×
115
        }
1✔
116

117
        return invokeAccessible(publicConstructor);
1✔
118
    }
119

120
    @NonNull
121
    public static <T> T newUninitializedInstance(@NonNull Class<T> aClass) {
122
        SunReflectionFactoryInstantiator<T> ref = new SunReflectionFactoryInstantiator<>(aClass);
1✔
123
        return ref.newInstance();
1✔
124
    }
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc