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

hazendaz / jmockit1 / 462

08 Nov 2025 01:40AM UTC coverage: 72.263%. Remained the same
462

push

github

hazendaz
[ci] Code cleanup

5679 of 8364 branches covered (67.9%)

Branch coverage included in aggregate %.

2 of 3 new or added lines in 1 file covered. (66.67%)

11941 of 16019 relevant lines covered (74.54%)

0.75 hits per line

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

67.02
/main/src/main/java/mockit/internal/reflection/MethodReflection.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 java.lang.reflect.Modifier.isPrivate;
8
import static java.lang.reflect.Modifier.isStatic;
9

10
import static mockit.internal.reflection.ParameterReflection.*;
11

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

15
import java.lang.reflect.InvocationTargetException;
16
import java.lang.reflect.Method;
17
import java.util.regex.Pattern;
18

19
import mockit.Delegate;
20
import mockit.internal.util.StackTrace;
21
import mockit.internal.util.Utilities;
22

23
public final class MethodReflection {
24
    @NonNull
25
    public static final Pattern JAVA_LANG = Pattern.compile("java.lang.", Pattern.LITERAL);
1✔
26

27
    private MethodReflection() {
28
    }
29

30
    @Nullable
31
    public static <T> T invoke(@NonNull Class<?> theClass, @Nullable Object targetInstance, @NonNull String methodName,
32
            @NonNull Class<?>[] paramTypes, @NonNull Object... methodArgs) {
33
        Method method = findSpecifiedMethod(theClass, methodName, paramTypes);
×
NEW
34
        return invoke(targetInstance, method, methodArgs);
×
35
    }
36

37
    @NonNull
38
    private static Method findSpecifiedMethod(@NonNull Class<?> theClass, @NonNull String methodName,
39
            @NonNull Class<?>[] paramTypes) {
40
        while (true) {
41
            Method declaredMethod = findSpecifiedMethodInGivenClass(theClass, methodName, paramTypes);
1✔
42

43
            if (declaredMethod != null) {
1!
44
                return declaredMethod;
1✔
45
            }
46

47
            Class<?> superClass = theClass.getSuperclass();
×
48

49
            if (superClass == null || superClass == Object.class) {
×
50
                String paramTypesDesc = getParameterTypesDescription(paramTypes);
×
51
                throw new IllegalArgumentException("Specified method not found: " + methodName + paramTypesDesc);
×
52
            }
53

54
            // noinspection AssignmentToMethodParameter
55
            theClass = superClass;
×
56
        }
×
57
    }
58

59
    @Nullable
60
    private static Method findSpecifiedMethodInGivenClass(@NonNull Class<?> theClass, @NonNull String methodName,
61
            @NonNull Class<?>[] paramTypes) {
62
        for (Method declaredMethod : theClass.getDeclaredMethods()) {
1!
63
            if (declaredMethod.getName().equals(methodName)) {
1✔
64
                Class<?>[] declaredParameterTypes = declaredMethod.getParameterTypes();
1✔
65
                int firstRealParameter = indexOfFirstRealParameter(declaredParameterTypes, paramTypes);
1✔
66

67
                if (firstRealParameter >= 0
1✔
68
                        && matchesParameterTypes(declaredMethod.getParameterTypes(), paramTypes, firstRealParameter)) {
1✔
69
                    return declaredMethod;
1✔
70
                }
71
            }
72
        }
73

74
        return null;
×
75
    }
76

77
    @Nullable
78
    public static <T> T invokePublicIfAvailable(@NonNull Class<?> aClass, @Nullable Object targetInstance,
79
            @NonNull String methodName, @NonNull Class<?>[] parameterTypes, @NonNull Object... methodArgs) {
80
        Method publicMethod;
81
        try {
82
            publicMethod = aClass.getMethod(methodName, parameterTypes);
1✔
83
        } catch (NoSuchMethodException ignore) {
×
84
            return null;
×
85
        }
1✔
86

87
        return invoke(targetInstance, publicMethod, methodArgs);
1✔
88
    }
89

90
    @Nullable
91
    public static <T> T invokeWithCheckedThrows(@NonNull Class<?> theClass, @Nullable Object targetInstance,
92
            @NonNull String methodName, @NonNull Class<?>[] paramTypes, @NonNull Object... methodArgs)
93
            throws Throwable {
94
        Method method = findSpecifiedMethod(theClass, methodName, paramTypes);
1✔
95
        return invokeWithCheckedThrows(targetInstance, method, methodArgs);
1✔
96
    }
97

98
    @Nullable
99
    public static <T> T invoke(@Nullable Object targetInstance, @NonNull Method method, @NonNull Object... methodArgs) {
100
        Utilities.ensureThatMemberIsAccessible(method);
1✔
101

102
        try {
103
            // noinspection unchecked
104
            return (T) method.invoke(targetInstance, methodArgs);
1✔
105
        } catch (IllegalAccessException e) {
×
106
            throw new RuntimeException(e);
×
107
        } catch (IllegalArgumentException e) {
1✔
108
            StackTrace.filterStackTrace(e);
1✔
109
            throw new IllegalArgumentException("Failure to invoke method: " + method, e);
1✔
110
        } catch (InvocationTargetException e) {
1✔
111
            Throwable cause = e.getCause();
1✔
112

113
            if (cause instanceof Error) {
1!
114
                throw (Error) cause;
×
115
            } else if (cause instanceof RuntimeException) {
1!
116
                throw (RuntimeException) cause;
1✔
117
            } else {
118
                ThrowOfCheckedException.doThrow((Exception) cause);
×
119
                return null;
×
120
            }
121
        }
122
    }
123

124
    @Nullable
125
    public static <T> T invokeWithCheckedThrows(@Nullable Object targetInstance, @NonNull Method method,
126
            @NonNull Object... methodArgs) throws Throwable {
127
        Utilities.ensureThatMemberIsAccessible(method);
1✔
128

129
        try {
130
            // noinspection unchecked
131
            return (T) method.invoke(targetInstance, methodArgs);
1✔
132
        } catch (IllegalArgumentException e) {
1✔
133
            StackTrace.filterStackTrace(e);
1✔
134
            throw new IllegalArgumentException("Failure to invoke method: " + method, e);
1✔
135
        } catch (InvocationTargetException e) {
1✔
136
            throw e.getCause();
1✔
137
        }
138
    }
139

140
    @Nullable
141
    public static <T> T invoke(@NonNull Class<?> theClass, @Nullable Object targetInstance, @NonNull String methodName,
142
            @NonNull Object... methodArgs) {
143
        boolean staticMethod = targetInstance == null;
×
144
        Class<?>[] argTypes = getArgumentTypesFromArgumentValues(methodArgs);
×
145
        Method method = staticMethod ? findCompatibleStaticMethod(theClass, methodName, argTypes)
×
146
                : findCompatibleMethod(theClass, methodName, argTypes);
×
147

148
        if (staticMethod && !isStatic(method.getModifiers())) {
×
149
            throw new IllegalArgumentException(
×
150
                    "Attempted to invoke non-static method without an instance to invoke it on");
151
        }
152

153
        T result = invoke(targetInstance, method, methodArgs);
×
154
        return result;
×
155
    }
156

157
    @NonNull
158
    private static Method findCompatibleStaticMethod(@NonNull Class<?> theClass, @NonNull String methodName,
159
            @NonNull Class<?>[] argTypes) {
160
        Method methodFound = findCompatibleMethodInClass(theClass, methodName, argTypes);
×
161

162
        if (methodFound != null) {
×
163
            return methodFound;
×
164
        }
165

166
        String argTypesDesc = getParameterTypesDescription(argTypes);
×
167
        throw new IllegalArgumentException("No compatible static method found: " + methodName + argTypesDesc);
×
168
    }
169

170
    @NonNull
171
    public static Method findCompatibleMethod(@NonNull Class<?> theClass, @NonNull String methodName,
172
            @NonNull Class<?>[] argTypes) {
173
        Method methodFound = findCompatibleMethodIfAvailable(theClass, methodName, argTypes);
1✔
174

175
        if (methodFound != null) {
1!
176
            return methodFound;
1✔
177
        }
178

179
        String argTypesDesc = getParameterTypesDescription(argTypes);
×
180
        throw new IllegalArgumentException("No compatible method found: " + methodName + argTypesDesc);
×
181
    }
182

183
    @Nullable
184
    private static Method findCompatibleMethodIfAvailable(@NonNull Class<?> theClass, @NonNull String methodName,
185
            @NonNull Class<?>[] argTypes) {
186
        Method methodFound = null;
1✔
187

188
        while (true) {
189
            Method compatibleMethod = findCompatibleMethodInClass(theClass, methodName, argTypes);
1✔
190

191
            if (compatibleMethod != null && (methodFound == null
1!
192
                    || hasMoreSpecificTypes(compatibleMethod.getParameterTypes(), methodFound.getParameterTypes()))) {
×
193
                methodFound = compatibleMethod;
1✔
194
            }
195

196
            Class<?> superClass = theClass.getSuperclass();
1✔
197

198
            if (superClass == null || superClass == Object.class) {
1!
199
                break;
1✔
200
            }
201

202
            // noinspection AssignmentToMethodParameter
203
            theClass = superClass;
1✔
204
        }
1✔
205

206
        return methodFound;
1✔
207
    }
208

209
    @Nullable
210
    private static Method findCompatibleMethodInClass(@NonNull Class<?> theClass, @NonNull String methodName,
211
            @NonNull Class<?>[] argTypes) {
212
        Method found = null;
1✔
213
        Class<?>[] foundParamTypes = null;
1✔
214

215
        for (Method declaredMethod : theClass.getDeclaredMethods()) {
1✔
216
            if (declaredMethod.getName().equals(methodName)) {
1✔
217
                Class<?>[] declaredParamTypes = declaredMethod.getParameterTypes();
1✔
218
                int firstRealParameter = indexOfFirstRealParameter(declaredParamTypes, argTypes);
1✔
219

220
                if (firstRealParameter >= 0
1✔
221
                        && (matchesParameterTypes(declaredParamTypes, argTypes, firstRealParameter)
1✔
222
                                || acceptsArgumentTypes(declaredParamTypes, argTypes, firstRealParameter))
1!
223
                        && (foundParamTypes == null || hasMoreSpecificTypes(declaredParamTypes, foundParamTypes))) {
×
224
                    found = declaredMethod;
1✔
225
                    foundParamTypes = declaredParamTypes;
1✔
226
                }
227
            }
228
        }
229

230
        return found;
1✔
231
    }
232

233
    @NonNull
234
    public static Method findNonPrivateHandlerMethod(@NonNull Object handler) {
235
        Class<?> handlerClass = handler.getClass();
1✔
236
        Method nonPrivateMethod;
237

238
        do {
239
            nonPrivateMethod = findNonPrivateHandlerMethod(handlerClass);
1✔
240

241
            if (nonPrivateMethod != null) {
1✔
242
                break;
1✔
243
            }
244

245
            handlerClass = handlerClass.getSuperclass();
1✔
246
        } while (handlerClass != null && handlerClass != Object.class);
1!
247

248
        if (nonPrivateMethod == null) {
1✔
249
            throw new IllegalArgumentException("No non-private instance method found");
1✔
250
        }
251

252
        return nonPrivateMethod;
1✔
253
    }
254

255
    @Nullable
256
    private static Method findNonPrivateHandlerMethod(@NonNull Class<?> handlerClass) {
257
        Method[] declaredMethods = handlerClass.getDeclaredMethods();
1✔
258
        Method found = null;
1✔
259

260
        for (Method declaredMethod : declaredMethods) {
1✔
261
            int methodModifiers = declaredMethod.getModifiers();
1✔
262

263
            if (!isPrivate(methodModifiers) && !isStatic(methodModifiers)) {
1✔
264
                if (found != null) {
1✔
265
                    String methodType = Delegate.class.isAssignableFrom(handlerClass) ? "delegate"
1!
266
                            : "invocation handler";
1✔
267
                    throw new IllegalArgumentException("More than one candidate " + methodType + " method found: "
1✔
268
                            + methodSignature(found) + ", " + methodSignature(declaredMethod));
1✔
269
                }
270

271
                found = declaredMethod;
1✔
272
            }
273
        }
274

275
        return found;
1✔
276
    }
277

278
    @NonNull
279
    private static String methodSignature(@NonNull Method method) {
280
        String signature = JAVA_LANG.matcher(method.toGenericString()).replaceAll("");
1✔
281
        int p = signature.lastIndexOf('(');
1✔
282
        int q = signature.lastIndexOf('.', p);
1✔
283

284
        return signature.substring(q + 1);
1✔
285
    }
286
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc