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

raphw / byte-buddy / #752

20 Mar 2025 05:23PM UTC coverage: 85.175% (+0.005%) from 85.17%
#752

push

raphw
Add checksums

29243 of 34333 relevant lines covered (85.17%)

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.*;
29

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

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

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

50
        /**
51
         * Matches any method declared by the resolved type.
52
         */
53
        DECLARED(false),
1✔
54

55
        /**
56
         * Matches any method not declared by the resolved type.
57
         */
58
        NOT_DECLARED(true);
1✔
59

60
        /**
61
         * {@code true} if the matcher is inverted.
62
         */
63
        private final boolean inverted;
64

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

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

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

94
        /**
95
         * The resolved matcher.
96
         */
97
        private final ElementMatcher<? super S> matcher;
98

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

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

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

122
        /**
123
         * A token representing the field being matched.
124
         */
125
        private final FieldDescription.Token token;
126

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

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

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

149
            /**
150
             * The signature token representing the matched field.
151
             */
152
            private final FieldDescription.SignatureToken signatureToken;
153

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

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

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

178
        /**
179
         * A token representing the method being matched.
180
         */
181
        private final MethodDescription.Token token;
182

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

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

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

205
            /**
206
             * The signature token representing the matched field.
207
             */
208
            private final MethodDescription.SignatureToken signatureToken;
209

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

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

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

234
        /**
235
         * The token being matched.
236
         */
237
        private final RecordComponentDescription.Token token;
238

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

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

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

264
        /**
265
         * The matchers this conjunction represents.
266
         */
267
        private final List<? extends LatentMatcher<? super S>> matchers;
268

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

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

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

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

309
        /**
310
         * The matchers this disjunction represents.
311
         */
312
        private final List<? extends LatentMatcher<? super S>> matchers;
313

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

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

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