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

raphw / byte-buddy / #801

27 Oct 2025 09:37AM UTC coverage: 84.715% (-0.4%) from 85.118%
#801

push

raphw
Fix imports.

29586 of 34924 relevant lines covered (84.72%)

0.85 hits per line

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

92.45
/byte-buddy-dep/src/main/java/net/bytebuddy/matcher/LatentMatcher.java
1
/*
2
 * Copyright 2014 - Present Rafael Winterhalter
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package net.bytebuddy.matcher;
17

18
import net.bytebuddy.build.HashCodeAndEqualsPlugin;
19
import net.bytebuddy.build.SafeVarargsPlugin;
20
import net.bytebuddy.description.field.FieldDescription;
21
import net.bytebuddy.description.method.MethodDescription;
22
import net.bytebuddy.description.type.RecordComponentDescription;
23
import net.bytebuddy.description.type.TypeDescription;
24

25
import java.util.Arrays;
26
import java.util.List;
27

28
import static net.bytebuddy.matcher.ElementMatchers.any;
29
import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy;
30
import static net.bytebuddy.matcher.ElementMatchers.none;
31
import static net.bytebuddy.matcher.ElementMatchers.not;
32

33
/**
34
 * A latent matcher that resolves an {@link ElementMatcher} after supplying a type description.
35
 *
36
 * @param <T> The type of the matched element.
37
 */
38
public interface LatentMatcher<T> {
39

40
    /**
41
     * Resolves the element matcher this instance represents for the supplied type description.
42
     *
43
     * @param typeDescription The type description for which the represented matcher should be resolved.
44
     * @return An {@link ElementMatcher} that represents this matcher's resolved form.
45
     */
46
    ElementMatcher<? super T> resolve(TypeDescription typeDescription);
47

48
    /**
49
     * A latent matching methods that are declared by the resolved type.
50
     */
51
    enum ForSelfDeclaredMethod implements LatentMatcher<MethodDescription> {
1✔
52

53
        /**
54
         * Matches any method declared by the resolved type.
55
         */
56
        DECLARED(false),
1✔
57

58
        /**
59
         * Matches any method not declared by the resolved type.
60
         */
61
        NOT_DECLARED(true);
1✔
62

63
        /**
64
         * {@code true} if the matcher is inverted.
65
         */
66
        private final boolean inverted;
67

68
        /**
69
         * Creates a new latent matcher for a self-declared method.
70
         *
71
         * @param inverted {@code true} if the matcher is inverted.
72
         */
73
        ForSelfDeclaredMethod(boolean inverted) {
1✔
74
            this.inverted = inverted;
1✔
75
        }
1✔
76

77
        /**
78
         * {@inheritDoc}
79
         */
80
        @SuppressWarnings("unchecked")
81
        public ElementMatcher<? super MethodDescription> resolve(TypeDescription typeDescription) {
82
            // Casting is required by some Java 6 compilers.
83
            return (ElementMatcher<? super MethodDescription>) (inverted
1✔
84
                    ? not(isDeclaredBy(typeDescription))
1✔
85
                    : isDeclaredBy(typeDescription));
1✔
86
        }
87
    }
88

89
    /**
90
     * A latent matcher representing an already resolved {@link ElementMatcher}.
91
     *
92
     * @param <S> The type of the matched element.
93
     */
94
    @HashCodeAndEqualsPlugin.Enhance
95
    class Resolved<S> implements LatentMatcher<S> {
96

97
        /**
98
         * The resolved matcher.
99
         */
100
        private final ElementMatcher<? super S> matcher;
101

102
        /**
103
         * Creates a new resolved latent matcher.
104
         *
105
         * @param matcher The resolved matcher.
106
         */
107
        public Resolved(ElementMatcher<? super S> matcher) {
1✔
108
            this.matcher = matcher;
1✔
109
        }
1✔
110

111
        /**
112
         * {@inheritDoc}
113
         */
114
        public ElementMatcher<? super S> resolve(TypeDescription typeDescription) {
115
            return matcher;
1✔
116
        }
117
    }
118

119
    /**
120
     * A latent matcher where the field token is being attached to the supplied type description before matching.
121
     */
122
    @HashCodeAndEqualsPlugin.Enhance
123
    class ForFieldToken implements LatentMatcher<FieldDescription> {
124

125
        /**
126
         * A token representing the field being matched.
127
         */
128
        private final FieldDescription.Token token;
129

130
        /**
131
         * Creates a new latent matcher for a field token.
132
         *
133
         * @param token A token representing the field being matched.
134
         */
135
        public ForFieldToken(FieldDescription.Token token) {
1✔
136
            this.token = token;
1✔
137
        }
1✔
138

139
        /**
140
         * {@inheritDoc}
141
         */
142
        public ElementMatcher<? super FieldDescription> resolve(TypeDescription typeDescription) {
143
            return new ResolvedMatcher(token.asSignatureToken(typeDescription));
1✔
144
        }
145

146
        /**
147
         * A resolved matcher of a latent field matcher for a field token.
148
         */
149
        @HashCodeAndEqualsPlugin.Enhance
150
        protected static class ResolvedMatcher extends ElementMatcher.Junction.ForNonNullValues<FieldDescription> {
151

152
            /**
153
             * The signature token representing the matched field.
154
             */
155
            private final FieldDescription.SignatureToken signatureToken;
156

157
            /**
158
             * Creates a new resolved matcher.
159
             *
160
             * @param signatureToken The signature token representing the matched field.
161
             */
162
            protected ResolvedMatcher(FieldDescription.SignatureToken signatureToken) {
1✔
163
                this.signatureToken = signatureToken;
1✔
164
            }
1✔
165

166
            /**
167
             * {@inheritDoc}
168
             */
169
            protected boolean doMatch(FieldDescription target) {
170
                return target.asSignatureToken().equals(signatureToken);
1✔
171
            }
172
        }
173
    }
174

175
    /**
176
     * A latent matcher where the method token is being attached to the supplied type description before matching.
177
     */
178
    @HashCodeAndEqualsPlugin.Enhance
179
    class ForMethodToken implements LatentMatcher<MethodDescription> {
180

181
        /**
182
         * A token representing the method being matched.
183
         */
184
        private final MethodDescription.Token token;
185

186
        /**
187
         * Creates a new latent matcher for a method token.
188
         *
189
         * @param token A token representing the method being matched.
190
         */
191
        public ForMethodToken(MethodDescription.Token token) {
1✔
192
            this.token = token;
1✔
193
        }
1✔
194

195
        /**
196
         * {@inheritDoc}
197
         */
198
        public ElementMatcher<? super MethodDescription> resolve(TypeDescription typeDescription) {
199
            return new ResolvedMatcher(token.asSignatureToken(typeDescription));
1✔
200
        }
201

202
        /**
203
         * A resolved matcher of a latent method matcher for a method token.
204
         */
205
        @HashCodeAndEqualsPlugin.Enhance
206
        protected static class ResolvedMatcher extends ElementMatcher.Junction.ForNonNullValues<MethodDescription> {
207

208
            /**
209
             * The signature token representing the matched field.
210
             */
211
            private final MethodDescription.SignatureToken signatureToken;
212

213
            /**
214
             * Creates a new resolved matcher.
215
             *
216
             * @param signatureToken The signature token representing the matched field.
217
             */
218
            protected ResolvedMatcher(MethodDescription.SignatureToken signatureToken) {
1✔
219
                this.signatureToken = signatureToken;
1✔
220
            }
1✔
221

222
            /**
223
             * {@inheritDoc}
224
             */
225
            public boolean doMatch(MethodDescription target) {
226
                return target.asSignatureToken().equals(signatureToken);
1✔
227
            }
228
        }
229
    }
230

231
    /**
232
     * A latent matcher for a record component token.
233
     */
234
    @HashCodeAndEqualsPlugin.Enhance
235
    class ForRecordComponentToken implements LatentMatcher<RecordComponentDescription> {
236

237
        /**
238
         * The token being matched.
239
         */
240
        private final RecordComponentDescription.Token token;
241

242
        /**
243
         * Creates a latent matcher for a record component token.
244
         *
245
         * @param token The token being matched.
246
         */
247
        public ForRecordComponentToken(RecordComponentDescription.Token token) {
×
248
            this.token = token;
×
249
        }
×
250

251
        /**
252
         * {@inheritDoc}
253
         */
254
        public ElementMatcher<? super RecordComponentDescription> resolve(TypeDescription typeDescription) {
255
            return ElementMatchers.<RecordComponentDescription>named(token.getName());
×
256
        }
257
    }
258

259
    /**
260
     * A matcher that computes the conjunction of all supplied latent matchers.
261
     *
262
     * @param <S> The type of the matched element.
263
     */
264
    @HashCodeAndEqualsPlugin.Enhance
265
    class Conjunction<S> implements LatentMatcher<S> {
266

267
        /**
268
         * The matchers this conjunction represents.
269
         */
270
        private final List<? extends LatentMatcher<? super S>> matchers;
271

272
        /**
273
         * Creates a new conjunction of latent matchers.
274
         *
275
         * @param matcher The matchers this conjunction represents.
276
         */
277
        @SafeVarargsPlugin.Enhance
278
        @SuppressWarnings("unchecked") // In absence of @SafeVarargs
279
        public Conjunction(LatentMatcher<? super S>... matcher) {
280
            this(Arrays.asList(matcher));
1✔
281
        }
1✔
282

283
        /**
284
         * Creates a new conjunction of latent matchers.
285
         *
286
         * @param matchers The matchers this conjunction represents.
287
         */
288
        public Conjunction(List<? extends LatentMatcher<? super S>> matchers) {
1✔
289
            this.matchers = matchers;
1✔
290
        }
1✔
291

292
        /**
293
         * {@inheritDoc}
294
         */
295
        public ElementMatcher<? super S> resolve(TypeDescription typeDescription) {
296
            ElementMatcher.Junction<S> matcher = any();
1✔
297
            for (LatentMatcher<? super S> latentMatcher : matchers) {
1✔
298
                matcher = matcher.and(latentMatcher.resolve(typeDescription));
1✔
299
            }
1✔
300
            return matcher;
1✔
301
        }
302
    }
303

304
    /**
305
     * A matcher that computes the disjunction of all supplied latent matchers.
306
     *
307
     * @param <S> The type of the matched element.
308
     */
309
    @HashCodeAndEqualsPlugin.Enhance
310
    class Disjunction<S> implements LatentMatcher<S> {
311

312
        /**
313
         * The matchers this disjunction represents.
314
         */
315
        private final List<? extends LatentMatcher<? super S>> matchers;
316

317
        /**
318
         * Creates a new disjunction of latent matchers.
319
         *
320
         * @param matcher The matchers this disjunction represents.
321
         */
322
        @SafeVarargsPlugin.Enhance
323
        @SuppressWarnings("unchecked") // In absence of @SafeVarargs
324
        public Disjunction(LatentMatcher<? super S>... matcher) {
325
            this(Arrays.asList(matcher));
1✔
326
        }
1✔
327

328
        /**
329
         * Creates a new disjunction of latent matchers.
330
         *
331
         * @param matchers The matchers this disjunction represents.
332
         */
333
        public Disjunction(List<? extends LatentMatcher<? super S>> matchers) {
1✔
334
            this.matchers = matchers;
1✔
335
        }
1✔
336

337
        /**
338
         * {@inheritDoc}
339
         */
340
        public ElementMatcher<? super S> resolve(TypeDescription typeDescription) {
341
            ElementMatcher.Junction<S> matcher = none();
1✔
342
            for (LatentMatcher<? super S> latentMatcher : matchers) {
1✔
343
                matcher = matcher.or(latentMatcher.resolve(typeDescription));
1✔
344
            }
1✔
345
            return matcher;
1✔
346
        }
347
    }
348
}
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