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

hazendaz / jmockit1 / 115

26 Apr 2025 08:34PM UTC coverage: 73.247% (-0.03%) from 73.277%
115

push

github

web-flow
Merge pull request #331 from hazendaz/paths

Cleanup build now that requiring higher jdk to build

5621 of 8178 branches covered (68.73%)

Branch coverage included in aggregate %.

11880 of 15715 relevant lines covered (75.6%)

0.76 hits per line

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

93.24
/main/src/main/java/mockit/internal/injection/InterfaceResolution.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.injection;
6

7
import static mockit.internal.reflection.MethodReflection.invoke;
8
import static mockit.internal.util.Utilities.getClassType;
9

10
import edu.umd.cs.findbugs.annotations.NonNull;
11
import edu.umd.cs.findbugs.annotations.Nullable;
12

13
import java.lang.reflect.Method;
14
import java.lang.reflect.ParameterizedType;
15
import java.lang.reflect.Type;
16
import java.lang.reflect.WildcardType;
17
import java.util.Map.Entry;
18
import java.util.NavigableMap;
19
import java.util.TreeMap;
20

21
final class InterfaceResolution {
22
    @NonNull
23
    private final NavigableMap<ParameterizedType, Method> interfaceResolutionMethods;
24

25
    InterfaceResolution() {
1✔
26
        interfaceResolutionMethods = new TreeMap<>((t1, t2) -> {
1✔
27
            if (t1 == t2) {
1✔
28
                return 0;
1✔
29
            }
30

31
            Type targetType1 = t1.getActualTypeArguments()[0];
1✔
32
            Type targetType2 = t2.getActualTypeArguments()[0];
1✔
33

34
            if (targetType1 == targetType2) {
1!
35
                return 0;
×
36
            }
37

38
            if (targetType1 instanceof WildcardType) {
1✔
39
                if (targetType2 instanceof WildcardType) {
1!
40
                    return compareTypesFromResolutionMethods((WildcardType) targetType1, (WildcardType) targetType2);
1✔
41
                }
42

43
                return 1;
×
44
            }
45

46
            return -1;
1✔
47
        });
48
    }
1✔
49

50
    private static int compareTypesFromResolutionMethods(@NonNull WildcardType type1, @NonNull WildcardType type2) {
51
        Type upperBound1 = type1.getUpperBounds()[0];
1✔
52
        Class<?> classOfUpperBound1 = getClassType(upperBound1);
1✔
53

54
        Type upperBound2 = type2.getUpperBounds()[0];
1✔
55
        Class<?> classOfUpperBound2 = getClassType(upperBound2);
1✔
56

57
        if (classOfUpperBound1.isAssignableFrom(classOfUpperBound2)) {
1✔
58
            return 1;
1✔
59
        }
60

61
        if (classOfUpperBound2.isAssignableFrom(classOfUpperBound1)) {
1✔
62
            return -1;
1✔
63
        }
64

65
        return classOfUpperBound1.getName().compareTo(classOfUpperBound2.getName());
1✔
66
    }
67

68
    boolean canResolveInterfaces() {
69
        return !interfaceResolutionMethods.isEmpty();
1!
70
    }
71

72
    void addInterfaceResolutionMethod(@NonNull ParameterizedType interfaceType, @NonNull Method resolutionMethod) {
73
        interfaceResolutionMethods.put(interfaceType, resolutionMethod);
1✔
74
    }
1✔
75

76
    @Nullable
77
    Class<?> resolveInterface(@NonNull Class<?> anInterface, @NonNull Object testClassInstance) {
78
        if (interfaceResolutionMethods.isEmpty()) {
1✔
79
            return null;
1✔
80
        }
81

82
        for (Entry<ParameterizedType, Method> typeAndMethod : interfaceResolutionMethods.entrySet()) {
1✔
83
            ParameterizedType acceptedType = typeAndMethod.getKey();
1✔
84
            Method method = typeAndMethod.getValue();
1✔
85
            Type targetType = acceptedType.getActualTypeArguments()[0];
1✔
86

87
            if (targetType == anInterface || targetType instanceof WildcardType
1✔
88
                    && satisfiesUpperBounds(anInterface, (WildcardType) targetType)) {
1✔
89
                Class<?> implementationClass = invoke(testClassInstance, method, anInterface);
1✔
90

91
                if (implementationClass != null) {
1✔
92
                    return implementationClass;
1✔
93
                }
94
            }
95
        }
1✔
96

97
        return null;
1✔
98
    }
99

100
    private static boolean satisfiesUpperBounds(@NonNull Class<?> interfaceType, @NonNull WildcardType targetType) {
101
        for (Type upperBound : targetType.getUpperBounds()) {
1✔
102
            Class<?> classOfUpperBound = getClassType(upperBound);
1✔
103

104
            if (!classOfUpperBound.isAssignableFrom(interfaceType)) {
1✔
105
                return false;
1✔
106
            }
107
        }
108

109
        return true;
1✔
110
    }
111
}
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