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

hazendaz / jmockit1 / 496

15 Nov 2025 05:33PM UTC coverage: 72.192% (-0.008%) from 72.2%
496

push

github

web-flow
Merge pull request #412 from hazendaz/renovate/major-spring-core

Update spring core to v7 (major)

5677 of 8360 branches covered (67.91%)

Branch coverage included in aggregate %.

11922 of 16018 relevant lines covered (74.43%)

0.74 hits per line

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

84.62
/main/src/main/java/mockit/internal/faking/FakeState.java
1
/*
2
 * MIT License
3
 * Copyright (c) 2006-2025 JMockit developers
4
 * See LICENSE file for full license text.
5
 */
6
package mockit.internal.faking;
7

8
import edu.umd.cs.findbugs.annotations.NonNull;
9
import edu.umd.cs.findbugs.annotations.Nullable;
10

11
import java.lang.reflect.Member;
12
import java.lang.reflect.Method;
13

14
import mockit.internal.faking.FakeMethods.FakeMethod;
15
import mockit.internal.reflection.MethodReflection;
16
import mockit.internal.reflection.RealMethodOrConstructor;
17
import mockit.internal.util.ClassLoad;
18

19
final class FakeState {
20
    private static final ClassLoader THIS_CL = FakeState.class.getClassLoader();
1✔
21

22
    @NonNull
23
    final FakeMethod fakeMethod;
24
    @Nullable
25
    private Method actualFakeMethod;
26
    @Nullable
27
    private Member realMethodOrConstructor;
28
    @Nullable
29
    private Object realClass;
30

31
    // Current fake invocation state:
32
    private int invocationCount;
33
    @Nullable
34
    private ThreadLocal<FakeInvocation> proceedingInvocation;
35

36
    // Helper field just for synchronization:
37
    @NonNull
38
    private final Object invocationCountLock;
39

40
    FakeState(@NonNull FakeMethod fakeMethod) {
1✔
41
        this.fakeMethod = fakeMethod;
1✔
42
        invocationCountLock = new Object();
1✔
43

44
        if (fakeMethod.canBeReentered()) {
1✔
45
            makeReentrant();
1✔
46
        }
47
    }
1✔
48

49
    FakeState(@NonNull FakeState fakeState) {
×
50
        fakeMethod = fakeState.fakeMethod;
×
51
        actualFakeMethod = fakeState.actualFakeMethod;
×
52
        realMethodOrConstructor = fakeState.realMethodOrConstructor;
×
53
        invocationCountLock = new Object();
×
54

55
        if (fakeState.proceedingInvocation != null) {
×
56
            makeReentrant();
×
57
        }
58
    }
×
59

60
    @NonNull
61
    Class<?> getRealClass() {
62
        return fakeMethod.getRealClass();
1✔
63
    }
64

65
    private void makeReentrant() {
66
        proceedingInvocation = new ThreadLocal<>();
1✔
67
    }
1✔
68

69
    boolean update() {
70
        if (proceedingInvocation != null) {
1✔
71
            FakeInvocation invocation = proceedingInvocation.get();
1✔
72

73
            if (invocation != null && invocation.proceeding) {
1✔
74
                invocation.proceeding = false;
1✔
75
                return false;
1✔
76
            }
77
        }
78

79
        synchronized (invocationCountLock) {
1✔
80
            invocationCount++;
1✔
81
        }
1✔
82

83
        return true;
1✔
84
    }
85

86
    int getTimesInvoked() {
87
        synchronized (invocationCountLock) {
1✔
88
            return invocationCount;
1✔
89
        }
90
    }
91

92
    @NonNull
93
    Member getRealMethodOrConstructor(@NonNull String fakedClassDesc, @NonNull String fakedMethodName,
94
            @NonNull String fakedMethodDesc) {
95
        Class<?> fakedClass = ClassLoad.loadFromLoader(THIS_CL, fakedClassDesc.replace('/', '.'));
1✔
96
        return getRealMethodOrConstructor(fakedClass, fakedMethodName, fakedMethodDesc);
1✔
97
    }
98

99
    @NonNull
100
    Member getRealMethodOrConstructor(@NonNull Class<?> fakedClass, @NonNull String fakedMethodName,
101
            @NonNull String fakedMethodDesc) {
102
        Member member = realMethodOrConstructor;
1✔
103

104
        if (member == null || !fakedClass.equals(realClass)) {
1✔
105
            String memberName = "$init".equals(fakedMethodName) ? "<init>" : fakedMethodName;
1✔
106

107
            RealMethodOrConstructor realMember;
108
            try {
109
                realMember = new RealMethodOrConstructor(fakedClass, memberName, fakedMethodDesc);
1✔
110
            } catch (NoSuchMethodException e) {
×
111
                throw new RuntimeException(e);
×
112
            }
1✔
113

114
            member = realMember.getMember();
1✔
115

116
            if (!fakeMethod.isAdvice) {
1✔
117
                realMethodOrConstructor = member;
1✔
118
                realClass = fakedClass;
1✔
119
            }
120
        }
121

122
        return member;
1✔
123
    }
124

125
    boolean shouldProceedIntoRealImplementation(@Nullable Object fake, @NonNull String classDesc) {
126
        if (proceedingInvocation != null) {
1✔
127
            FakeInvocation pendingInvocation = proceedingInvocation.get();
1✔
128

129
            // noinspection RedundantIfStatement
130
            if (pendingInvocation != null && pendingInvocation.isMethodInSuperclass(fake, classDesc)) {
1✔
131
                return true;
1✔
132
            }
133
        }
134

135
        return false;
1✔
136
    }
137

138
    void prepareToProceed(@NonNull FakeInvocation invocation) {
139
        if (proceedingInvocation == null) {
1✔
140
            throw new UnsupportedOperationException("Cannot proceed into abstract/interface method");
1✔
141
        }
142

143
        if (fakeMethod.isForNativeMethod()) {
1✔
144
            throw new UnsupportedOperationException("Cannot proceed into real implementation of native method");
1✔
145
        }
146

147
        FakeInvocation previousInvocation = proceedingInvocation.get();
1✔
148

149
        if (previousInvocation != null) {
1✔
150
            invocation.setPrevious(previousInvocation);
1✔
151
        }
152

153
        proceedingInvocation.set(invocation);
1✔
154
    }
1✔
155

156
    void prepareToProceedFromNonRecursiveFake(@NonNull FakeInvocation invocation) {
157
        assert proceedingInvocation != null;
1!
158
        proceedingInvocation.set(invocation);
1✔
159
    }
1✔
160

161
    void clearProceedIndicator() {
162
        assert proceedingInvocation != null;
1!
163
        FakeInvocation currentInvocation = proceedingInvocation.get();
1✔
164
        FakeInvocation previousInvocation = (FakeInvocation) currentInvocation.getPrevious();
1✔
165
        proceedingInvocation.set(previousInvocation);
1✔
166
    }
1✔
167

168
    @NonNull
169
    Method getFakeMethod(@NonNull Class<?> fakeClass, @NonNull Class<?>[] parameterTypes) {
170
        if (actualFakeMethod == null) {
1✔
171
            actualFakeMethod = MethodReflection.findCompatibleMethod(fakeClass, fakeMethod.name, parameterTypes);
1✔
172
        }
173

174
        return actualFakeMethod;
1✔
175
    }
176
}
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