• 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

96.97
/main/src/main/java/mockit/internal/expectations/invocation/DelegatedResult.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.expectations.invocation;
7

8
import static mockit.internal.reflection.MethodReflection.JAVA_LANG;
9
import static mockit.internal.reflection.MethodReflection.findNonPrivateHandlerMethod;
10
import static mockit.internal.reflection.MethodReflection.invoke;
11

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

15
import java.lang.reflect.Method;
16
import java.util.concurrent.locks.ReentrantLock;
17

18
import mockit.Delegate;
19
import mockit.Invocation;
20
import mockit.asm.types.JavaType;
21
import mockit.internal.expectations.RecordAndReplayExecution;
22
import mockit.internal.reflection.ParameterReflection;
23
import mockit.internal.state.TestRun;
24
import mockit.internal.util.MethodFormatter;
25
import mockit.internal.util.TypeDescriptor;
26

27
final class DelegatedResult extends InvocationResult {
28
    private static final Object[] NO_ARGS = {};
1✔
29

30
    @NonNull
31
    private final ExpectedInvocation recordedInvocation;
32
    @NonNull
33
    private final Object targetObject;
34
    @NonNull
35
    private final Method methodToInvoke;
36
    @NonNull
37
    private final Class<?> targetReturnType;
38
    private final boolean hasInvocationParameter;
39
    private final int numberOfRegularParameters;
40

41
    DelegatedResult(@NonNull ExpectedInvocation recordedInvocation, @NonNull Delegate<?> delegate) {
1✔
42
        this.recordedInvocation = recordedInvocation;
1✔
43
        targetObject = delegate;
1✔
44
        methodToInvoke = findNonPrivateHandlerMethod(delegate);
1✔
45

46
        JavaType returnType = JavaType.getReturnType(recordedInvocation.getMethodNameAndDescription());
1✔
47
        targetReturnType = TypeDescriptor.getClassForType(returnType);
1✔
48

49
        Class<?>[] parameters = methodToInvoke.getParameterTypes();
1✔
50
        int n = parameters.length;
1✔
51

52
        hasInvocationParameter = n > 0 && parameters[0] == Invocation.class;
1✔
53
        numberOfRegularParameters = hasInvocationParameter ? n - 1 : n;
1✔
54
    }
1✔
55

56
    @Nullable
57
    @Override
58
    Object produceResult(@Nullable Object invokedObject, @NonNull ExpectedInvocation invocation,
59
            @NonNull InvocationConstraints constraints, @NonNull Object[] args) {
60
        Object[] delegateArgs = numberOfRegularParameters == 0 ? NO_ARGS : args;
1✔
61
        return hasInvocationParameter
1✔
62
                ? invokeMethodWithContext(invokedObject, invocation, constraints, args, delegateArgs)
1✔
63
                : executeMethodToInvoke(delegateArgs);
1✔
64
    }
65

66
    @Nullable
67
    private Object invokeMethodWithContext(@Nullable Object mockOrRealObject,
68
            @NonNull ExpectedInvocation expectedInvocation, @NonNull InvocationConstraints constraints,
69
            @NonNull Object[] invokedArgs, @NonNull Object[] delegateArgs) {
70
        Invocation delegateInvocation = new DelegateInvocation(mockOrRealObject, invokedArgs, expectedInvocation,
1✔
71
                constraints);
72
        Object[] delegateArgsWithInvocation = ParameterReflection.argumentsWithExtraFirstValue(delegateArgs,
1✔
73
                delegateInvocation);
74
        Object result = executeMethodToInvoke(delegateArgsWithInvocation);
1✔
75

76
        return expectedInvocation.isConstructor() && TestRun.getExecutingTest().isProceedingIntoRealImplementation()
1✔
77
                ? Void.class
1✔
78
                : result;
1✔
79
    }
80

81
    @Nullable
82
    private Object executeMethodToInvoke(@NonNull Object[] args) {
83
        ReentrantLock reentrantLock = RecordAndReplayExecution.RECORD_OR_REPLAY_LOCK;
1✔
84

85
        if (!reentrantLock.isHeldByCurrentThread()) {
1!
86
            return executeTargetMethod(args);
×
87
        }
88

89
        reentrantLock.unlock();
1✔
90

91
        try {
92
            return executeTargetMethod(args);
1✔
93
        } finally {
94
            // noinspection LockAcquiredButNotSafelyReleased
95
            reentrantLock.lock();
1✔
96
        }
97
    }
98

99
    @Nullable
100
    private Object executeTargetMethod(@NonNull Object[] args) {
101
        Object returnValue = invoke(targetObject, methodToInvoke, args);
1✔
102
        Class<?> fromReturnType = methodToInvoke.getReturnType();
1✔
103

104
        if (returnValue == null || targetReturnType.isInstance(returnValue)) {
1✔
105
            if (fromReturnType == void.class && fromReturnType != targetReturnType && targetReturnType.isPrimitive()) {
1✔
106
                String returnTypeName = JAVA_LANG.matcher(targetReturnType.getName()).replaceAll("");
1✔
107
                MethodFormatter methodDesc = new MethodFormatter(recordedInvocation.getClassDesc(),
1✔
108
                        recordedInvocation.getMethodNameAndDescription());
1✔
109
                String msg = "void return type incompatible with return type " + returnTypeName + " of " + methodDesc;
1✔
110
                throw new IllegalArgumentException(msg);
1✔
111
            }
112

113
            return returnValue;
1✔
114
        }
115

116
        ReturnTypeConversion typeConversion = new ReturnTypeConversion(recordedInvocation, targetReturnType,
1✔
117
                returnValue);
118
        return typeConversion.getConvertedValue();
1✔
119
    }
120
}
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