• 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

86.05
/src/main/java/org/burningwave/reflection/ExecutableMemberCriteria.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
import java.lang.reflect.Executable;
32
import java.lang.reflect.Parameter;
33
import java.util.Arrays;
34
import java.util.List;
35
import java.util.function.BiPredicate;
36
import java.util.function.Predicate;
37

38
import org.burningwave.Classes;
39
import org.burningwave.TriPredicate;
40
import org.burningwave.reflection.Members.Handler;
41

42
/**
43
 * The base class of the criteria that select methods and constructors, which adds conditions on their parameters.
44
 *
45
 * @param <E> the type of the methods or constructors
46
 * @param <C> the type of the criteria
47
 * @param <T> the type of the test context
48
 */
49
@SuppressWarnings("unchecked")
50
public abstract class ExecutableMemberCriteria<
1 ✔
51
        E extends Executable,
52
        C extends ExecutableMemberCriteria<E, C, T>,
53
        T extends Criteria.TestContext<E, C>
54
> extends MemberCriteria<E, C, T> {
55

56
        /**
57
         * Adds a condition that is true if at least one of the parameters matches a predicate.
58
         *
59
         * @param predicate the predicate, which receives the parameters and the index of the parameter to test
60
         * @return this criteria
61
         */
62
        public C parameter(final BiPredicate<Parameter[], Integer> predicate) {
63
                this.predicate = concat(
1 ✔
64
                        this.predicate,
65
                        getPredicateWrapper(
1 ✔
66
                                (testContext, member) -> member.getParameters(),
1 ✔
67
                                (testContext, array, index) -> predicate.test(array, index)
1 ✔
68
                        )
69
                );
70
                return (C)this;
1 ✔
71
        }
72

73
        /**
74
         * Adds a condition that is true if at least one of the parameter types matches a predicate.
75
         *
76
         * @param predicate the predicate, which receives the types of the parameters and the index of the type to test
77
         * @return this criteria
78
         */
79
        public C parameterType(final BiPredicate<Class<?>[], Integer> predicate) {
80
                this.predicate = concat(
1 ✔
81
                        this.predicate,
82
                        getPredicateWrapper(
1 ✔
83
                                (testContext, member) -> member.getParameterTypes(),
1 ✔
84
                                (testContext, array, index) -> predicate.test(array, index)
1 ✔
85
                        )
86
                );
87
                return (C)this;
1 ✔
88
        }
89

90

91
        /**
92
         * Adds a condition on the types of the parameters.
93
         *
94
         * @param predicate the predicate, which receives the types of the parameters
95
         * @return this criteria
96
         */
97
        public C parameterTypes(final Predicate<Class<?>[]> predicate) {
98
                this.predicate = concat(
1 ✔
99
                        this.predicate,
100
                        (context, member) -> predicate.test(member.getParameterTypes())
1 ✔
101
                );
102
                return (C)this;
1 ✔
103
        }
104

105
        /**
106
         * Adds a condition that is true if the parameters accept arguments of the given types, that is if each parameter
107
         * type is the given type or a supertype of it; a {@code null} type matches any parameter and a variable arity
108
         * parameter accepts any number of arguments of its component type. Without types, the condition selects the members
109
         * without parameters.
110
         *
111
         * @param argumentsClasses the types of the arguments
112
         * @return this criteria
113
         */
114
        public C parameterTypesAreAssignableFrom(Class<?>... argumentsClasses) {
115
                return parameterTypesMatch(
1 ✔
116
                        (argClasses, paramTypes, innerIdx) ->
117
                                (argClasses.get(innerIdx) == null || Classes.INSTANCE.isAssignableFrom(paramTypes[innerIdx], argClasses.get(innerIdx))),
1 ✔
118
                        argumentsClasses
119
                );
120
        }
121

122
        /**
123
         * Adds a condition that is true if the parameters accept the given arguments, as
124
         * {@link #parameterTypesAreAssignableFrom(Class...)} does with the types of the arguments.
125
         *
126
         * @param arguments the arguments
127
         * @return this criteria
128
         */
129
        public C parameterTypesAreAssignableFromTypesOf(Object... arguments) {
130
                return parameterTypesAreAssignableFrom(Classes.INSTANCE.retrieveFrom(arguments));
×
131
        }
132

133

134
        /**
135
         * Adds a condition that is true if the types of the parameters are the given types, primitive types and their
136
         * wrappers being equal; a {@code null} type matches any parameter.
137
         *
138
         * @param argumentsClasses the types of the parameters
139
         * @return this criteria
140
         */
141
        public C parameterTypesExactlyMatch(Class<?>... argumentsClasses) {
142
                return parameterTypesMatch(
×
143
                        (argClasses, paramTypes, innerIdx) ->
144
                                (argClasses.get(innerIdx) == null || Classes.INSTANCE.getClassOrWrapper(paramTypes[innerIdx]).equals(Classes.INSTANCE.getClassOrWrapper(argClasses.get(innerIdx)))),
×
145
                        argumentsClasses
146
                );
147
        }
148

149

150
        /**
151
         * Adds a condition on the types of the given arguments: despite its name, it currently behaves as
152
         * {@link #parameterTypesAreAssignableFromTypesOf(Object...)}, so the parameter types can also be supertypes of the
153
         * types of the arguments.
154
         *
155
         * @param arguments the arguments
156
         * @return this criteria
157
         */
158
        public C parameterTypesExactlyMatchTypesOf(Object... arguments) {
159
                return parameterTypesAreAssignableFrom(Classes.INSTANCE.retrieveFrom(arguments));
×
160
        }
161

162
        private C parameterTypesMatch(TriPredicate<List<Class<?>>, Class<?>[], Integer> predicate, Class<?>... arguments) {
163
                if (arguments == null) {
1 ✔
164
                        arguments = new Class<?>[]{null};
×
165
                }
166
                Class<?>[] argumentsClasses = arguments;
1 ✔
167
                if (argumentsClasses != null && argumentsClasses.length > 0) {
1 ✔
168
                        List<Class<?>> argumentsClassesAsList = Arrays.asList(argumentsClasses);
1 ✔
169
                        for (int i = 0; i < argumentsClasses.length; i++) {
1 ✔
170
                                final int index = i;
1 ✔
171
                                this.predicate = concat(
1 ✔
172
                                        this.predicate,
173
                                        (context, member) -> {
174
                                                Parameter[] memberParameter = member.getParameters();
1 ✔
175
                                                if (memberParameter.length > 1 &&
1 ✔
176
                                                        memberParameter[memberParameter.length - 1].isVarArgs() &&
1 ✔
177
                                                        (memberParameter.length - 1) > argumentsClassesAsList.size()
1 ✔
178
                                                ) {
179
                                                        return false;
×
180
                                                }
181
                                                Class<?>[] memberParameterTypes = Handler.OfExecutable.retrieveParameterTypes(member, argumentsClassesAsList);
1 ✔
182
                                                if (argumentsClassesAsList.size() == memberParameterTypes.length) {
1 ✔
183
                                                        return predicate.test(argumentsClassesAsList, memberParameterTypes, index);
1 ✔
184
                                                } else {
185
                                                        return false;
1 ✔
186
                                                }
187
                                        }
188
                                );
189
                                if (index < arguments.length - 1) {
1 ✔
190
                                        and();
1 ✔
191
                                }
192
                        }
193
                } else {
1 ✔
194
                        parameterTypes(
1 ✔
195
                                parameters ->
196
                                parameters.length == 0
1 ✔
197
                        );
198
                }
199
                return (C)this;
1 ✔
200
        }
201
}
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