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

burningwave / reflection / #97

27 Nov 2026 08:15PM UTC coverage: 74.528%. Remained the same
#97

push

Roberto-Gentili
Set the version to 4.6.0-SNAPSHOT. Releasing new version

790 of 1060 relevant lines covered (74.53%)

0.75 hits per line

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

81.93
/src/main/java/org/burningwave/reflection/Members.java
1
/*
2
 * This file is part of Burningwave Reflection.
3
 *
4
 * Author: Roberto Gentili
5
 *
6
 * Hosted at: https://github.com/burningwave/reflection
7
 *
8
 * --
9
 *
10
 * The MIT License (MIT)
11
 *
12
 * Copyright (c) 2022 Roberto Gentili
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
15
 * documentation files (the "Software"), to deal in the Software without restriction, including without
16
 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
17
 * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
18
 * conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all copies or substantial
21
 * portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
25
 * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
27
 * OR OTHER DEALINGS IN THE SOFTWARE.
28
 */
29
package org.burningwave.reflection;
30

31

32
import java.lang.invoke.MethodHandle;
33
import java.lang.invoke.MethodHandles;
34
import java.lang.reflect.AccessibleObject;
35
import java.lang.reflect.Array;
36
import java.lang.reflect.Executable;
37
import java.lang.reflect.Member;
38
import java.lang.reflect.Parameter;
39
import java.util.ArrayList;
40
import java.util.Arrays;
41
import java.util.Collection;
42
import java.util.Collections;
43
import java.util.Comparator;
44
import java.util.HashSet;
45
import java.util.LinkedHashSet;
46
import java.util.List;
47
import java.util.Optional;
48
import java.util.Set;
49
import java.util.TreeSet;
50
import java.util.function.BiFunction;
51
import java.util.function.BiPredicate;
52
import java.util.function.Consumer;
53
import java.util.function.Predicate;
54
import java.util.function.Supplier;
55
import java.util.stream.Collectors;
56
import java.util.stream.Stream;
57

58
import org.burningwave.Classes;
59
import org.burningwave.Throwables;
60
import org.burningwave.TriFunction;
61

62
/**
63
 * Finds the members of a class, filtered through a {@link MemberCriteria}, in the class and, depending on the criteria,
64
 * in its interfaces and superclasses. The members declared by each class are retrieved through {@link Facade}, so
65
 * through the JVM driver when it is enabled, which also returns the members that the reflection API filters out. It is
66
 * also the base of {@link Fields}, {@link Methods} and {@link Constructors}.
67
 */
68
@SuppressWarnings("unchecked")
69
public class Members {
70
        /**
71
         * The instance of this class.
72
         */
73
        public static final Members INSTANCE;
74
        static final String ALL_FOR_CLASS = "all for class";
75

76
        static {
77
                INSTANCE = new Members();
1 ✔
78
        }
1 ✔
79

80
        private Members() {}
1 ✔
81

82
        /**
83
         * Returns all the members that match a criteria: first the ones of the class, then the ones of its interfaces and
84
         * of its superclasses, up to the class where the criteria stops the scan. If the criteria has a result predicate
85
         * that the members found do not pass, the result is empty.
86
         *
87
         * @param <M> the type of the members
88
         * @param criteria the member criteria
89
         * @param classFrom the class where the search starts
90
         * @return the members found
91
         */
92
        public <M extends Member> Collection<M> findAll(MemberCriteria<M, ?, ?> criteria, Class<?> classFrom) {
93
                Collection<M> result = findAll(
1 ✔
94
                        classFrom,
95
                        classFrom,
96
                        criteria.getScanUpToPredicate(),
1 ✔
97
                        criteria.getMembersSupplier(),
1 ✔
98
                        criteria.getPredicateOrTruePredicateIfPredicateIsNull(),
1 ✔
99
                        new HashSet<>(),
100
                        new LinkedHashSet<>()
101
                );
102
                Predicate<Collection<M>> resultPredicate = criteria.getResultPredicate();
1 ✔
103
                return resultPredicate == null?
1 ✔
104
                                result :
105
                                resultPredicate.test(result)?
×
106
                                        result :
107
                                        new LinkedHashSet<>();
108
        }
109

110
        /**
111
         * Returns the first member that matches a criteria, in the order of {@link #findAll(MemberCriteria, Class)}.
112
         *
113
         * @param <M> the type of the member
114
         * @param criteria the member criteria
115
         * @param classFrom the class where the search starts
116
         * @return the member, or {@code null} if no member matches
117
         */
118
        public <M extends Member> M findFirst(MemberCriteria<M, ?, ?> criteria, Class<?> classFrom) {
119
                Predicate<Collection<M>> resultPredicate = criteria.getResultPredicate();
1 ✔
120
                if (resultPredicate == null) {
1 ✔
121
                        return findFirst(
1 ✔
122
                                classFrom,
123
                                classFrom,
124
                                criteria.getScanUpToPredicate(),
1 ✔
125
                                criteria.getMembersSupplier(),
1 ✔
126
                                criteria.getPredicateOrTruePredicateIfPredicateIsNull(),
1 ✔
127
                                new HashSet<>()
128
                        );
129
                } else {
130
                        Collection<M> result = findAll(
×
131
                                classFrom,
132
                                classFrom,
133
                                criteria.getScanUpToPredicate(),
×
134
                                criteria.getMembersSupplier(),
×
135
                                criteria.getPredicateOrTruePredicateIfPredicateIsNull(),
×
136
                                new HashSet<>(),
137
                                new LinkedHashSet<>()
138
                        );
139
                        return resultPredicate.test(result) ?
×
140
                                result.stream().findFirst().orElseGet(() -> null) :
×
141
                                null;
142
                }
143
        }
144

145
        /**
146
         * Returns the only member that matches a criteria. If more than one member matches, this method throws an
147
         * {@link Exception}, even though it does not declare it.
148
         *
149
         * @param <M> the type of the member
150
         * @param criteria the member criteria
151
         * @param classFrom the class where the search starts
152
         * @return the member, or {@code null} if no member matches
153
         */
154
        public <M extends Member> M findOne(MemberCriteria<M, ?, ?> criteria, Class<?> classFrom) {
155
                Collection<M> members = findAll(criteria, classFrom);
1 ✔
156
                if (members.size() > 1) {
1 ✔
157
                        Throwables.INSTANCE.throwException("More than one member found for class {}", classFrom.getName());
×
158
                }
159
                return members.stream().findFirst().orElse(null);
1 ✔
160
        }
161

162
        /**
163
         * Tells whether at least one member matches a criteria.
164
         *
165
         * @param <M> the type of the members
166
         * @param criteria the member criteria
167
         * @param classFrom the class where the search starts
168
         * @return {@code true} if a member matches
169
         */
170
        public <M extends Member> boolean match(MemberCriteria<M, ?, ?> criteria, Class<?> classFrom) {
171
                return findFirst(criteria, classFrom) != null;
1 ✔
172
        }
173

174
        private <M extends Member> Collection<M> findAll(
175
                Class<?> initialClsFrom,
176
                Class<?> currentScannedClass,
177
                BiPredicate<Class<?>, Class<?>> clsPredicate,
178
                BiFunction<Class<?>, Class<?>, M[]> memberSupplier,
179
                Predicate<M> predicate,
180
                Set<Class<?>> visitedInterfaces,
181
                Collection<M> collection
182
        ) {
183
                for (M member : memberSupplier.apply(initialClsFrom, currentScannedClass)) {
1 ✔
184
                        if (predicate.test(member)) {
1 ✔
185
                                collection.add(member);
1 ✔
186
                        }
187
                }
188
                for (Class<?> interf : currentScannedClass.getInterfaces()) {
1 ✔
189
                        if (!visitedInterfaces.add(interf)) {
1 ✔
190
                                continue;
1 ✔
191
                        }
192
                        collection = findAll((Class<?>) initialClsFrom, interf, clsPredicate, memberSupplier, predicate, visitedInterfaces, collection);
1 ✔
193
                        if (!(collection instanceof Set)) {
1 ✔
194
                                return collection;
×
195
                        }
196
                        if (clsPredicate.test(initialClsFrom, currentScannedClass)) {
1 ✔
197
                                return Collections.unmodifiableCollection(collection);
×
198
                        }
199
                }
200
                Class<?> superClass = currentScannedClass.getSuperclass();
1 ✔
201
                if (!(collection instanceof Set) || ((superClass = currentScannedClass.getSuperclass()) == null && currentScannedClass.isInterface())) {
1 ✔
202
                        return collection;
1 ✔
203
                }
204
                if (superClass == null || clsPredicate.test(initialClsFrom, currentScannedClass)) {
1 ✔
205
                        return Collections.unmodifiableCollection(collection);
1 ✔
206
                }
207
                return findAll(
1 ✔
208
                        initialClsFrom,
209
                        superClass,
210
                        clsPredicate,
211
                        memberSupplier,
212
                        predicate,
213
                        visitedInterfaces,
214
                        collection
215
                );
216
        }
217

218
        private <M extends Member> M findFirst(
219
                Class<?> initialClsFrom,
220
                Class<?> currentScannedClass,
221
                BiPredicate<Class<?>, Class<?>> clsPredicate,
222
                BiFunction<Class<?>, Class<?>, M[]>
223
                memberSupplier, Predicate<M> predicate,
224
                Set<Class<?>> visitedInterfaces
225
        ) {
226
                for (M member : memberSupplier.apply(initialClsFrom, currentScannedClass)) {
1 ✔
227
                        if (predicate.test(member)) {
1 ✔
228
                                return member;
1 ✔
229
                        }
230
                }
231
                for (Class<?> interf : currentScannedClass.getInterfaces()) {
×
232
                        if (!visitedInterfaces.add(interf)) {
×
233
                                continue;
×
234
                        }
235
                        M member = findFirst(initialClsFrom, interf, clsPredicate, memberSupplier, predicate, visitedInterfaces);
×
236
                        if (member != null || clsPredicate.test(initialClsFrom, currentScannedClass)) {
×
237
                                return member;
×
238
                        }
239
                }
240
                return
×
241
                        (clsPredicate.test(initialClsFrom, currentScannedClass) || currentScannedClass.getSuperclass() == null) ?
×
242
                                null :
243
                                findFirst(initialClsFrom, currentScannedClass.getSuperclass(), clsPredicate, memberSupplier, predicate, visitedInterfaces);
×
244
        }
245

246
        /**
247
         * The base class of the classes that handle a kind of members, such as {@link Fields}.
248
         *
249
         * @param <M> the type of the members
250
         * @param <C> the type of the member criteria
251
         */
252
        public static abstract class Handler<M extends Member, C extends MemberCriteria<M, C, ?>> {
1 ✔
253

254
                /**
255
                 * The base class of the classes that handle methods and constructors: it also finds their method handles.
256
                 *
257
                 * @param <E> the type of the executables
258
                 * @param <C> the type of the criteria
259
                 */
260
                public static abstract class OfExecutable<E extends Executable, C extends ExecutableMemberCriteria<E, C, ?>> extends Members.Handler<E, C> {
261

262
                        OfExecutable() {}
1 ✔
263

264
                        /**
265
                         * Returns the method handles of all the members that match a criteria.
266
                         *
267
                         * @param criteria the criteria
268
                         * @param clsFrom the class where the search starts
269
                         * @return the method handles
270
                         */
271
                        public Collection<MethodHandle> findAllDirectHandle(C criteria, Class<?> clsFrom) {
272
                                return findAll(
×
273
                                        criteria, clsFrom
274
                                ).stream().map(this::findDirectHandle).collect(Collectors.toSet());
×
275
                        }
276

277
                        /**
278
                         * Returns a method handle for a method or a constructor, found through a lookup with full access to its
279
                         * class, see {@link Facade#executeWithConsulter(Class, org.burningwave.ThrowingFunction)}, so that it can
280
                         * be invoked even if it is not accessible. The method handle of an instance method invokes that very
281
                         * method, even if a subclass overrides it. The method handles are cached.
282
                         *
283
                         * @param executable the method or the constructor
284
                         * @return the method handle
285
                         */
286
                        public MethodHandle findDirectHandle(E executable) {
287
                                return findDirectHandleBox(executable).getHandler();
1 ✔
288
                        }
289

290
                        /**
291
                         * Returns the method handle of the first member that matches a criteria.
292
                         *
293
                         * @param criteria the criteria
294
                         * @param clsFrom the class where the search starts
295
                         * @return the method handle, or {@code null} if no member matches
296
                         */
297
                        public MethodHandle findFirstDirectHandle(C criteria, Class<?> clsFrom) {
298
                                return Optional.ofNullable(findFirst(criteria, clsFrom)).map(this::findDirectHandle).orElseGet(() -> null);
×
299
                        }
300

301

302
                        /**
303
                         * Returns the method handle of the only member that matches a criteria: if more than one member matches,
304
                         * this method throws an {@link Exception}, even though it does not declare it.
305
                         *
306
                         * @param criteria the criteria
307
                         * @param clsFrom the class where the search starts
308
                         * @return the method handle, or {@code null} if no member matches
309
                         */
310
                        public MethodHandle findOneDirectHandle(C criteria, Class<?> clsFrom) {
311
                                return Optional.ofNullable(findOne(criteria, clsFrom)).map(this::findDirectHandle).orElseGet(() -> null);
×
312
                        }
313

314
                        Members.Handler.OfExecutable.Box<E> findDirectHandleBox(E executable) {
315
                                Class<?> targetClass = executable.getDeclaringClass();
1 ✔
316
                                String cacheKey = getCacheKey(targetClass, "equals " + retrieveNameForCaching(executable), executable.getParameterTypes());
1 ✔
317
                                return findDirectHandleBox(executable, cacheKey);
1 ✔
318
                        }
319

320
                        Members.Handler.OfExecutable.Box<E> checkAndGetExecutableBox(Members.Handler.OfExecutable.Box<E> executableBox) {
321
                                if (executableBox.getHandler() != null) {
1 ✔
322
                                        return executableBox;
1 ✔
323
                                }
324
                                return Throwables.INSTANCE.throwException(executableBox.getException());
×
325
                        }
326

327
                        Members.Handler.OfExecutable.Box<E> findDirectHandleBox(E executable, String cacheKey) {
328
                                return checkAndGetExecutableBox(
1 ✔
329
                                        (Members.Handler.OfExecutable.Box<E>)Cache.INSTANCE.uniqueKeyForExecutableAndMethodHandle.getOrUploadIfAbsent(
1 ✔
330
                                                cacheKey, () -> {
331
                                                        Class<?> methodDeclaringClass = executable.getDeclaringClass();
1 ✔
332
                                                        Collection<Members.Handler.OfExecutable.Box<E>> executableBoxes = new ArrayList<>();
1 ✔
333
                                                        try {
334
                                                                return (Members.Handler.OfExecutable.Box<E>)Facade.INSTANCE.executeWithConsulter(
1 ✔
335
                                                                        methodDeclaringClass,
336
                                                                        consulter -> {
337
                                                                                Throwable exception = null;
1 ✔
338
                                                                                MethodHandle methodHandle = null;
1 ✔
339
                                                                                try {
340
                                                                                        methodHandle = retrieveMethodHandle(consulter, executable);
1 ✔
341
                                                                                } catch (Throwable exc) {
1 ✔
342
                                                                                        exception = exc;
1 ✔
343
                                                                                }
1 ✔
344
                                                                                Members.Handler.OfExecutable.Box<E> executableBox = new Members.Handler.OfExecutable.Box<>(consulter,
1 ✔
345
                                                                                        executable,
346
                                                                                        methodHandle,
347
                                                                                        exception
348
                                                                                );
349
                                                                                executableBoxes.add(
1 ✔
350
                                                                                        executableBox
351
                                                                                );
352
                                                                                if (exception != null) {
1 ✔
353
                                                                                        throw exception;
1 ✔
354
                                                                                }
355
                                                                                return executableBox;
1 ✔
356
                                                                        }
357
                                                                ).getValue();
1 ✔
358
                                                        } catch (Throwable exc) {
1 ✔
359
                                                                return executableBoxes.iterator().next();
1 ✔
360
                                                        }
361
                                                }
362
                                        )
363
                                );
364
                        }
365

366
                        Object[] getArgumentArray(
367
                                E member,
368
                                TriFunction<E, Supplier<List<Object>>, Object[], List<Object>> argumentListSupplier,
369
                                Supplier<List<Object>> listSupplier,
370
                                Object... arguments
371
                        ) {
372
                                List<Object> argumentList = argumentListSupplier.apply(member, listSupplier, arguments);
1 ✔
373
                                return argumentList.toArray(new Object[argumentList.size()]);
1 ✔
374
                        }
375

376
                        List<Object> getArgumentListWithArrayForVarArgs(E member, Supplier<List<Object>> argumentListSupplier, Object... arguments) {
377
                                Parameter[] parameters = member.getParameters();
1 ✔
378
                                List<Object> argumentList = argumentListSupplier.get();
1 ✔
379
                                if (arguments != null) {
1 ✔
380
                                        if (parameters.length > 0 && parameters[parameters.length - 1].isVarArgs()) {
1 ✔
381
                                                for (int i = 0; i < arguments.length && i < parameters.length - 1; i++) {
1 ✔
382
                                                        argumentList.add(arguments[i]);
1 ✔
383
                                                }
384
                                                Parameter lastParameter = parameters[parameters.length -1];
1 ✔
385
                                                if (arguments.length == parameters.length) {
1 ✔
386
                                                        Object lastArgument = arguments[arguments.length -1];
1 ✔
387
                                                        if (lastArgument != null &&
1 ✔
388
                                                                lastArgument.getClass().isArray() &&
1 ✔
389
                                                                lastArgument.getClass().equals(lastParameter.getType())) {
1 ✔
390
                                                                argumentList.add(lastArgument);
1 ✔
391
                                                        } else {
392
                                                                Object array = Array.newInstance(lastParameter.getType().getComponentType(), 1);
1 ✔
393
                                                                Array.set(array, 0, lastArgument);
1 ✔
394
                                                                argumentList.add(array);
1 ✔
395
                                                        }
396
                                                } else if (arguments.length > parameters.length) {
1 ✔
397
                                                        Object array = Array.newInstance(lastParameter.getType().getComponentType(), arguments.length - (parameters.length - 1));
1 ✔
398
                                                        for (int i = parameters.length - 1, j = 0; i < arguments.length; i++, j++) {
1 ✔
399
                                                                Array.set(array, j, arguments[i]);
1 ✔
400
                                                        }
401
                                                        argumentList.add(array);
1 ✔
402
                                                } else if (arguments.length < parameters.length) {
1 ✔
403
                                                        argumentList.add(Array.newInstance(lastParameter.getType().getComponentType(),0));
1 ✔
404
                                                }
405
                                        } else if (arguments.length > 0) {
1 ✔
406
                                                for (Object argument : arguments) {
1 ✔
407
                                                        argumentList.add(argument);
1 ✔
408
                                                }
409
                                        }
410
                                } else {
411
                                        argumentList.add(null);
1 ✔
412
                                }
413
                                return argumentList;
1 ✔
414
                        }
415

416
                        List<Object> getFlatArgumentList(E member, Supplier<List<Object>> argumentListSupplier, Object... arguments) {
417
                                Parameter[] parameters = member.getParameters();
1 ✔
418
                                List<Object> argumentList = argumentListSupplier.get();
1 ✔
419
                                if (arguments != null) {
1 ✔
420
                                        if (parameters.length > 0 && parameters[parameters.length - 1].isVarArgs()) {
1 ✔
421
                                                for (int i = 0; i < arguments.length && i < parameters.length - 1; i++) {
1 ✔
422
                                                        argumentList.add(arguments[i]);
1 ✔
423
                                                }
424
                                                if (arguments.length == parameters.length) {
1 ✔
425
                                                        Parameter lastParameter = parameters[parameters.length -1];
1 ✔
426
                                                        Object lastArgument = arguments[arguments.length -1];
1 ✔
427
                                                        if (lastArgument != null &&
1 ✔
428
                                                                lastArgument.getClass().isArray() &&
1 ✔
429
                                                                lastArgument.getClass().equals(lastParameter.getType())) {
1 ✔
430
                                                                for (int i = 0; i < Array.getLength(lastArgument); i++) {
1 ✔
431
                                                                        argumentList.add(Array.get(lastArgument, i));
1 ✔
432
                                                                }
433
                                                        } else {
434
                                                                argumentList.add(lastArgument);
1 ✔
435
                                                        }
436
                                                } else if (arguments.length > parameters.length) {
1 ✔
437
                                                        for (int i = parameters.length - 1; i < arguments.length; i++) {
1 ✔
438
                                                                argumentList.add(arguments[i]);
1 ✔
439
                                                        }
440
                                                } else if (arguments.length < parameters.length) {
1 ✔
441
                                                        argumentList.add(null);
1 ✔
442
                                                }
443
                                        } else if (arguments.length > 0) {
1 ✔
444
                                                for (Object argument : arguments) {
1 ✔
445
                                                        argumentList.add(argument);
1 ✔
446
                                                }
447
                                        }
448
                                } else {
449
                                        argumentList.add(null);
1 ✔
450
                                }
451
                                return argumentList;
1 ✔
452
                        }
453

454
                        abstract MethodHandle retrieveMethodHandle(MethodHandles.Lookup consulter, E executable) throws NoSuchMethodException, IllegalAccessException;
455

456
                        abstract String retrieveNameForCaching(E executable);
457

458
                        static Class<?>[] retrieveParameterTypes(Executable member, List<Class<?>> argumentsClassesAsList) {
459
                                Parameter[] memberParameter = member.getParameters();
1 ✔
460
                                Class<?>[] memberParameterTypes = member.getParameterTypes();
1 ✔
461
                                if (memberParameter.length > 0 && memberParameter[memberParameter.length - 1].isVarArgs()) {
1 ✔
462
                                        Class<?> varArgsType =
1 ✔
463
                                                argumentsClassesAsList.size() > 0 &&
1 ✔
464
                                                argumentsClassesAsList.get(argumentsClassesAsList.size()-1) != null &&
1 ✔
465
                                                argumentsClassesAsList.get(argumentsClassesAsList.size()-1).isArray()?
1 ✔
466
                                                memberParameter[memberParameter.length - 1].getType():
1 ✔
467
                                                memberParameter[memberParameter.length - 1].getType().getComponentType();
1 ✔
468
                                        if (memberParameter.length == 1) {
1 ✔
469
                                                memberParameterTypes = new Class<?>[argumentsClassesAsList.size()];
1 ✔
470
                                                for (int j = 0; j < memberParameterTypes.length; j++) {
1 ✔
471
                                                        memberParameterTypes[j] = varArgsType;
1 ✔
472
                                                }
473
                                        } else if (memberParameter.length - 1 <= argumentsClassesAsList.size()) {
1 ✔
474
                                                memberParameterTypes = new Class<?>[argumentsClassesAsList.size()];
1 ✔
475
                                                for (int j = 0; j < memberParameterTypes.length; j++) {
1 ✔
476
                                                        if (j < (memberParameter.length - 1)) {
1 ✔
477
                                                                memberParameterTypes[j] = memberParameter[j].getType();
1 ✔
478
                                                        } else {
479
                                                                memberParameterTypes[j] = varArgsType;
1 ✔
480
                                                        }
481
                                                }
482
                                        }
483
                                }
484
                                return memberParameterTypes;
1 ✔
485
                        }
486

487
                        Collection<E> searchForExactMatch(Collection<E> members, Class<?>... arguments) {
488
                                List<Class<?>> argumentsClassesAsList = Arrays.asList(arguments);
1 ✔
489
                                //Collection<E> membersThatMatch = new LinkedHashSet<>();
490
                                Collection<E> membersThatMatch = new TreeSet<>(new Comparator<E>() {
1 ✔
491
                                        @Override
492
                                        public int compare(E executableOne, E executableTwo) {
493
                                                Parameter[] executableOneParameters = executableOne.getParameters();
1 ✔
494
                                                Parameter[] executableTwoParameters = executableTwo.getParameters();
1 ✔
495
                                                if (executableOneParameters.length == argumentsClassesAsList.size()) {
1 ✔
496
                                                        if (executableTwoParameters.length == argumentsClassesAsList.size()) {
1 ✔
497
                                                                if (executableOneParameters.length > 0 && executableOneParameters[executableOneParameters.length - 1].isVarArgs()) {
1 ✔
498
                                                                        if (executableTwoParameters.length > 0 && executableTwoParameters[executableTwoParameters.length - 1].isVarArgs()) {
1 ✔
499
                                                                                return 0;
1 ✔
500
                                                                        }
501
                                                                        return 1;
×
502
                                                                } else if (executableTwoParameters.length > 0 && executableTwoParameters[executableTwoParameters.length - 1].isVarArgs()) {
1 ✔
503
                                                                        return -1;
×
504
                                                                } else {
505
                                                                        return 0;
1 ✔
506
                                                                }
507
                                                        }
508
                                                        return -1;
×
509
                                                } else if (executableTwoParameters.length == argumentsClassesAsList.size()) {
×
510
                                                        return 1;
×
511
                                                }
512
                                                return 0;
×
513
                                        }
514

515
                                });
516

517
                                for (E executable : members) {
1 ✔
518
                                        Class<?>[] parameterTypes = retrieveParameterTypes(executable, argumentsClassesAsList);
1 ✔
519
                                        boolean exactMatch = true;
1 ✔
520
                                        for (int i = 0; i < parameterTypes.length; i++) {
1 ✔
521
                                                if (argumentsClassesAsList.get(i) != null &&
1 ✔
522
                                                        !Classes.INSTANCE.getClassOrWrapper(argumentsClassesAsList.get(i)).equals(Classes.INSTANCE.getClassOrWrapper(parameterTypes[i]))
1 ✔
523
                                                ) {
524
                                                        exactMatch = false;
1 ✔
525
                                                }
526
                                        }
527
                                        if (exactMatch) {
1 ✔
528
                                                membersThatMatch.add(executable);
1 ✔
529
                                        }
530
                                }
1 ✔
531
                                return membersThatMatch;
1 ✔
532
                        }
533

534
                        /**
535
                         * A method or a constructor together with its method handle, with the lookup that found it and with the
536
                         * exception thrown if the method handle could not be found.
537
                         *
538
                         * @param <E> the type of the executable
539
                         */
540
                        public static class Box<E extends Member> {
541
                                MethodHandles.Lookup consulter;
542
                                E executable;
543
                                MethodHandle handler;
544
                                Throwable exception;
545

546
                                Box(final MethodHandles.Lookup consulter, final E executable, final MethodHandle handler, final Throwable exception) {
547
                                        super();
1 ✔
548
                                        this.consulter = consulter;
1 ✔
549
                                        this.executable = executable;
1 ✔
550
                                        this.handler = handler;
1 ✔
551
                                        this.exception = exception;
1 ✔
552
                                }
1 ✔
553

554
                                /**
555
                                 * Returns the lookup used to find the method handle.
556
                                 *
557
                                 * @return the lookup
558
                                 */
559
                                public MethodHandles.Lookup getConsulter() {
560
                                        return consulter;
×
561
                                }
562

563
                                /**
564
                                 * Returns the method or the constructor.
565
                                 *
566
                                 * @return the method or the constructor
567
                                 */
568
                                public E getExecutable() {
569
                                        return executable;
1 ✔
570
                                }
571

572
                                /**
573
                                 * Returns the method handle.
574
                                 *
575
                                 * @return the method handle, or {@code null} if it could not be found
576
                                 */
577
                                public MethodHandle getHandler() {
578
                                        return handler;
1 ✔
579
                                }
580

581
                                /**
582
                                 * Returns the exception thrown while finding the method handle.
583
                                 *
584
                                 * @return the exception, or {@code null} if the method handle was found
585
                                 */
586
                                public Throwable getException() {
587
                                        return exception;
1 ✔
588
                                }
589

590
                        }
591

592
                }
593

594
                /**
595
                 * Returns all the members that match a criteria: see {@link Members#findAll(MemberCriteria, Class)}.
596
                 *
597
                 * @param criteria the member criteria
598
                 * @param classFrom the class where the search starts
599
                 * @return the members found
600
                 */
601
                public Collection<M> findAll(C criteria, Class<?> classFrom) {
602
                        return Members.INSTANCE.findAll(criteria, classFrom);
1 ✔
603
                }
604

605
                /**
606
                 * Returns all the members that match a criteria, after making them accessible through
607
                 * {@link #setAccessible(Member, boolean)}.
608
                 *
609
                 * @param criteria the member criteria
610
                 * @param targetClass the class where the search starts
611
                 * @return the members found
612
                 */
613
                public Collection<M> findAllAndMakeThemAccessible(
614
                        C criteria,
615
                        Class<?> targetClass
616
                ) {
617
                        return findAllAndApply(
1 ✔
618
                                criteria, targetClass, (member) -> {
619
                                        setAccessible(member, true);
1 ✔
620
                                }
1 ✔
621
                        );
622
                }
623

624
                /**
625
                 * Returns the first member that matches a criteria: see {@link Members#findFirst(MemberCriteria, Class)}.
626
                 *
627
                 * @param criteria the member criteria
628
                 * @param classFrom the class where the search starts
629
                 * @return the member, or {@code null} if no member matches
630
                 */
631
                public M findFirst(C criteria, Class<?> classFrom) {
632
                        return Members.INSTANCE.findFirst(criteria, classFrom);
×
633
                }
634

635
                /**
636
                 * Returns the only member that matches a criteria: see {@link Members#findOne(MemberCriteria, Class)}.
637
                 *
638
                 * @param criteria the member criteria
639
                 * @param classFrom the class where the search starts
640
                 * @return the member, or {@code null} if no member matches
641
                 */
642
                public M findOne(C criteria, Class<?> classFrom) {
643
                        return Members.INSTANCE.findOne(criteria, classFrom);
×
644
                }
645

646
                /**
647
                 * Tells whether at least one member matches a criteria.
648
                 *
649
                 * @param criteria the member criteria
650
                 * @param classFrom the class where the search starts
651
                 * @return {@code true} if a member matches
652
                 */
653
                public boolean match(C criteria, Class<?> classFrom) {
654
                        return Members.INSTANCE.match(criteria, classFrom);
×
655
                }
656

657
                /**
658
                 * Sets the accessible flag of a member through
659
                 * {@link Facade#setAccessible(java.lang.reflect.AccessibleObject, boolean)}, which uses the JVM driver, when it
660
                 * is enabled, so it works also when the module system would not allow it.
661
                 *
662
                 * @param member the member
663
                 * @param flag the value of the flag
664
                 */
665
                public void setAccessible(M member, boolean flag) {
666
                        Facade.INSTANCE.setAccessible(((AccessibleObject)member), flag);
1 ✔
667
                }
1 ✔
668

669
                Collection<M> findAllAndApply(C criteria, Class<?> targetClass, Consumer<M>... consumers) {
670
                        Collection<M> members = findAll(criteria, targetClass);
1 ✔
671
                        Optional.ofNullable(consumers).ifPresent(cnsms ->
1 ✔
672
                                members.stream().forEach(member ->
1 ✔
673
                                        Stream.of(cnsms).filter(consumer ->
1 ✔
674
                                                consumer != null
1 ✔
675
                                        ).forEach(consumer -> {
1 ✔
676
                                                consumer.accept(member);
1 ✔
677
                                        })
1 ✔
678
                                )
679
                        );
680
                        return members;
1 ✔
681
                }
682

683
                M findOneAndApply(C criteria, Class<?> targetClass, Consumer<M>... consumers) {
684
                        M member = findOne(criteria, targetClass);
×
685
                        Optional.ofNullable(consumers).ifPresent(cnsms ->
×
686
                                Optional.ofNullable(member).ifPresent(mmb ->
×
687
                                        Stream.of(cnsms).filter(consumer ->
×
688
                                                consumer != null
×
689
                                        ).forEach(consumer -> {
×
690
                                                consumer.accept(mmb);
×
691
                                        })
×
692
                                )
693
                        );
694
                        return member;
×
695
                }
696

697
                String getCacheKey(Class<?> targetClass, String groupName, Class<?>... arguments) {
698
                        if (arguments == null) {
1 ✔
699
                                arguments = new Class<?>[] {null};
1 ✔
700
                        }
701
                        String argumentsKey = "";
1 ✔
702
                        if (arguments != null && arguments.length > 0) {
1 ✔
703
                                StringBuffer argumentsKeyStringBuffer = new StringBuffer();
1 ✔
704
                                Stream.of(arguments).forEach(cls ->
1 ✔
705
                                        argumentsKeyStringBuffer.append("/" + Optional.ofNullable(cls).map(Class::getName).orElseGet(() ->"null"))
1 ✔
706
                                );
707
                                argumentsKey = argumentsKeyStringBuffer.toString();
1 ✔
708
                        }
709
                        String cacheKey = "/" + targetClass.getName() + "@" + targetClass.hashCode() +
1 ✔
710
                                "/" + groupName +
711
                                argumentsKey;
712
                        return cacheKey;
1 ✔
713
                }
714
        }
715

716
}
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