• 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

88.57
/src/main/java/org/burningwave/reflection/MemberCriteria.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.reflect.Member;
33
import java.util.Collection;
34
import java.util.function.BiFunction;
35
import java.util.function.BiPredicate;
36
import java.util.function.Function;
37
import java.util.function.Predicate;
38

39
import org.burningwave.TriPredicate;
40

41

42
/**
43
 * The base class of the criteria that select the members of a class, such as {@link FieldCriteria} and
44
 * {@link MethodCriteria}. Besides the conditions on the members, it sets how far the search goes up the class
45
 * hierarchy, which classes it skips and a condition on the whole result. By default the search scans the whole
46
 * hierarchy, up to {@link Object}.
47
 *
48
 * @param <M> the type of the members
49
 * @param <C> the type of the criteria
50
 * @param <T> the type of the test context
51
 */
52
@SuppressWarnings("unchecked")
53
public abstract class MemberCriteria<M extends Member, C extends MemberCriteria<M, C, T>, T extends Criteria.TestContext<M, C>> extends Criteria<M, C, T> {
1 ✔
54
        private static Member[] EMPTY_MEMBERS_ARRAY = {};
1 ✔
55
        Predicate<Collection<M>> resultPredicate;
56
        TriPredicate<C, Class<?>, Class<?>> scanUpToPredicate;
57
        TriPredicate<C, Class<?>, Class<?>> skipClassPredicate;
58

59

60
        /**
61
         * Creates a copy of this criteria, with the same conditions, the same limit of the scan of the hierarchy, the same
62
         * skipped classes and the same condition on the result.
63
         *
64
         * @return the copy
65
         */
66
        @Override
67
        public C createCopy() {
68
                C copy = super.createCopy();
1 ✔
69
                copy.scanUpToPredicate = this.scanUpToPredicate;
1 ✔
70
                copy.skipClassPredicate = this.skipClassPredicate;
1 ✔
71
                copy.resultPredicate = this.resultPredicate;
1 ✔
72
                return copy;
1 ✔
73
        }
74

75
        /**
76
         * Adds a condition on the name of the member.
77
         *
78
         * @param predicate the predicate, which receives the name of the member
79
         * @return this criteria
80
         */
81
        public C name(final Predicate<String> predicate) {
82
                this.predicate = concat(
1 ✔
83
                        this.predicate,
84
                        (context, member) ->
85
                                predicate.test(member.getName())
1 ✔
86
                );
87
                return (C)this;
1 ✔
88
        }
89

90
        /**
91
         * Sets a condition on all the members found: if they do not pass it, the search returns no member.
92
         *
93
         * @param resultPredicate the condition on the members found
94
         * @return this criteria
95
         */
96
        public C result(Predicate<Collection<M>> resultPredicate) {
97
                this.resultPredicate = resultPredicate;
×
98
                return (C)this;
×
99
        }
100

101

102
        /**
103
         * Makes the search skip the members of the classes that match a predicate. More calls skip the classes that match
104
         * any of the predicates.
105
         *
106
         * @param predicate the predicate, which receives the class where the search started and the class being scanned
107
         * @return this criteria
108
         */
109
        public C skip(BiPredicate<Class<?>, Class<?>> predicate) {
110
                if (skipClassPredicate != null) {
1 ✔
111
                        skipClassPredicate = skipClassPredicate.or((criteria, initialClassFrom, currentClass) ->
×
112
                                predicate.test(initialClassFrom, currentClass)
×
113
                        );
114
                } else {
115
                        skipClassPredicate = (criteria, initialClassFrom, currentClass) ->
1 ✔
116
                                predicate.test(initialClassFrom, currentClass);
1 ✔
117
                }
118
                return (C)this;
1 ✔
119
        }
120

121
        /**
122
         * Combines the conditions of two criteria, as the base class does, and also their limits of the scan of the
123
         * hierarchy and their skipped classes: if both criteria limit the scan, the new criteria stops where either of them
124
         * would stop, otherwise it scans the whole hierarchy; it skips the classes that either criteria skips.
125
         *
126
         * @param leftCriteria the first criteria
127
         * @param rightCriteria the second criteria
128
         * @param binaryOperator the logic operation, which combines the predicate of the first criteria with the one of the
129
         * second
130
         * @param targetCriteria the criteria that receives the result
131
         * @return the criteria that received the result
132
         */
133
        @Override
134
        protected C logicOperation(C leftCriteria, C rightCriteria,
135
                        Function<BiPredicate<T, M>, Function<BiPredicate<? super T, ? super M>, BiPredicate<T, M>>> binaryOperator,
136
                        C targetCriteria) {
137
                C newCriteria = super.logicOperation(leftCriteria, rightCriteria, binaryOperator, targetCriteria);
1 ✔
138
                newCriteria.scanUpToPredicate =
1 ✔
139
                        leftCriteria.scanUpToPredicate != null?
140
                                rightCriteria.scanUpToPredicate != null?
141
                                        leftCriteria.scanUpToPredicate.or(rightCriteria.scanUpToPredicate) :
1 ✔
142
                                        null :
143
                                null;
144
                newCriteria.skipClassPredicate =
1 ✔
145
                        leftCriteria.skipClassPredicate != null?
146
                                rightCriteria.skipClassPredicate != null?
147
                                        leftCriteria.skipClassPredicate.or(rightCriteria.skipClassPredicate) :
1 ✔
148
                                        leftCriteria.skipClassPredicate :
149
                                rightCriteria.skipClassPredicate;
150
//                newCriteria.resultPredicate =
151
//                        leftCriteria.resultPredicate != null?
152
//                                rightCriteria.resultPredicate != null?
153
//                                        leftCriteria.resultPredicate.or(rightCriteria.resultPredicate) :
154
//                                        leftCriteria.resultPredicate :
155
//                                rightCriteria.resultPredicate;
156
                return newCriteria;
1 ✔
157
        }
158

159
        BiFunction<Class<?>, Class<?>, M[]> getMembersSupplier() {
160
                return (initialClassFrom, currentClass) ->
1 ✔
161
                        !(skipClassPredicate != null && skipClassPredicate.test((C)this, initialClassFrom, currentClass)) ?
1 ✔
162
                                getMembersSupplierFunction().apply(currentClass) :
1 ✔
163
                                (M[]) EMPTY_MEMBERS_ARRAY;
164
        }
165

166
        abstract Function<Class<?>, M[]> getMembersSupplierFunction();
167

168
        Predicate<Collection<M>> getResultPredicate() {
169
                return this.resultPredicate;
1 ✔
170
        }
171

172
        BiPredicate<Class<?>, Class<?>> getScanUpToPredicate() {
173
                return scanUpToPredicate != null?
1 ✔
174
                        (initialClassFrom, currentClass) -> this.scanUpToPredicate.test((C)this, initialClassFrom, currentClass):
1 ✔
175
                        (initialClassFrom, currentClass) -> currentClass.getName().equals(Object.class.getName()
1 ✔
176
                );
177
        }
178

179
        C scanUpTo(BiPredicate<Class<?>, Class<?>> predicate) {
180
                this.scanUpToPredicate = (criteria, initialClassFrom, currentClass) -> predicate.test(initialClassFrom, currentClass);
1 ✔
181
                return (C)this;
1 ✔
182
        }
183

184
        C scanUpTo(Predicate<Class<?>> predicate) {
185
                this.scanUpToPredicate = (criteria, initialClassFrom, currentClass) -> predicate.test(currentClass);
1 ✔
186
                return (C)this;
1 ✔
187
        }
188

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