• 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

28.26
/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/RecordComponentRegistry.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.dynamic.scaffold;
17

18
import net.bytebuddy.build.HashCodeAndEqualsPlugin;
19
import net.bytebuddy.description.type.RecordComponentDescription;
20
import net.bytebuddy.description.type.TypeDescription;
21
import net.bytebuddy.dynamic.Transformer;
22
import net.bytebuddy.implementation.attribute.RecordComponentAttributeAppender;
23
import net.bytebuddy.matcher.ElementMatcher;
24
import net.bytebuddy.matcher.LatentMatcher;
25
import net.bytebuddy.utility.nullability.MaybeNull;
26

27
import java.util.ArrayList;
28
import java.util.Collections;
29
import java.util.HashMap;
30
import java.util.List;
31
import java.util.Map;
32

33
/**
34
 * A record component registry represents an extendable collection of record components which are identified by their names that are mapped
35
 * to a given {@link RecordComponentAttributeAppender}. Record components
36
 * can be uniquely identified by their name for a given type since record components are never inherited.
37
 * <p>&nbsp;</p>
38
 * This registry is the counterpart of a {@link MethodRegistry}.
39
 * However, a record component registry is implemented simpler since it does not have to deal with complex signatures or
40
 * inheritance. For the sake of consistency, the record component registry follows however a similar pattern without introducing
41
 * unnecessary complexity.
42
 */
43
public interface RecordComponentRegistry {
44

45
    /**
46
     * Prepends the given record component definition to this record component registry, i.e. this configuration is applied first.
47
     *
48
     * @param matcher                                 The matcher to identify any record component that this definition concerns.
49
     * @param recordComponentAttributeAppenderFactory The record component attribute appender factory to apply on any matched record component.
50
     * @param transformer                             The record component transformer to apply to any matched record component.
51
     * @return An adapted version of this method registry.
52
     */
53
    RecordComponentRegistry prepend(LatentMatcher<? super RecordComponentDescription> matcher,
54
                                    RecordComponentAttributeAppender.Factory recordComponentAttributeAppenderFactory,
55
                                    Transformer<RecordComponentDescription> transformer);
56

57
    /**
58
     * Prepares the record component registry for a given instrumented type.
59
     *
60
     * @param instrumentedType The instrumented type.
61
     * @return A prepared record component registry.
62
     */
63
    Compiled compile(TypeDescription instrumentedType);
64

65
    /**
66
     * Represents a compiled record component registry.
67
     */
68
    interface Compiled extends TypeWriter.RecordComponentPool {
69

70
        /**
71
         * A no-op record component registry that does not register annotations for any record component.
72
         */
73
        enum NoOp implements Compiled {
×
74

75
            /**
76
             * The singleton instance.
77
             */
78
            INSTANCE;
×
79

80
            /**
81
             * {@inheritDoc}
82
             */
83
            public Record target(RecordComponentDescription recordComponentDescription) {
84
                return new Record.ForImplicitRecordComponent(recordComponentDescription);
×
85
            }
86
        }
87
    }
88

89
    /**
90
     * An immutable default implementation of a record component registry.
91
     */
92
    @HashCodeAndEqualsPlugin.Enhance
93
    class Default implements RecordComponentRegistry {
94

95
        /**
96
         * This registries entries.
97
         */
98
        private final List<Entry> entries;
99

100
        /**
101
         * Creates a new empty default record component registry.
102
         */
103
        public Default() {
104
            this(Collections.<Entry>emptyList());
1✔
105
        }
1✔
106

107
        /**
108
         * Creates a new default record component registry.
109
         *
110
         * @param entries The entries of the record component registry.
111
         */
112
        private Default(List<Entry> entries) {
1✔
113
            this.entries = entries;
1✔
114
        }
1✔
115

116
        /**
117
         * {@inheritDoc}
118
         */
119
        public RecordComponentRegistry prepend(LatentMatcher<? super RecordComponentDescription> matcher,
120
                                               RecordComponentAttributeAppender.Factory recordComponentAttributeAppenderFactory,
121
                                               Transformer<RecordComponentDescription> transformer) {
122
            List<Entry> entries = new ArrayList<Entry>(this.entries.size() + 1);
×
123
            entries.add(new Entry(matcher, recordComponentAttributeAppenderFactory, transformer));
×
124
            entries.addAll(this.entries);
×
125
            return new Default(entries);
×
126
        }
127

128
        /**
129
         * {@inheritDoc}
130
         */
131
        public RecordComponentRegistry.Compiled compile(TypeDescription instrumentedType) {
132
            List<Compiled.Entry> entries = new ArrayList<Compiled.Entry>(this.entries.size());
1✔
133
            Map<RecordComponentAttributeAppender.Factory, RecordComponentAttributeAppender> recordComponentAttributeAppenders = new HashMap<RecordComponentAttributeAppender.Factory, RecordComponentAttributeAppender>();
1✔
134
            for (Entry entry : this.entries) {
1✔
135
                RecordComponentAttributeAppender recordComponentAttributeAppender = recordComponentAttributeAppenders.get(entry.getRecordComponentAttributeAppender());
×
136
                if (recordComponentAttributeAppender == null) {
×
137
                    recordComponentAttributeAppender = entry.getRecordComponentAttributeAppender().make(instrumentedType);
×
138
                    recordComponentAttributeAppenders.put(entry.getRecordComponentAttributeAppender(), recordComponentAttributeAppender);
×
139
                }
140
                entries.add(new Compiled.Entry(entry.resolve(instrumentedType), recordComponentAttributeAppender, entry.getTransformer()));
×
141
            }
×
142
            return new Compiled(instrumentedType, entries);
1✔
143
        }
144

145
        /**
146
         * An entry of the default record component registry.
147
         */
148
        @HashCodeAndEqualsPlugin.Enhance
149
        protected static class Entry implements LatentMatcher<RecordComponentDescription> {
150

151
            /**
152
             * The matcher to identify any record component that this definition concerns.
153
             */
154
            private final LatentMatcher<? super RecordComponentDescription> matcher;
155

156
            /**
157
             * The record component attribute appender factory to apply on any matched record component.
158
             */
159
            private final RecordComponentAttributeAppender.Factory recordComponentAttributeAppender;
160

161
            /**
162
             * The record component transformer to apply to any matched record component.
163
             */
164
            private final Transformer<RecordComponentDescription> transformer;
165

166
            /**
167
             * Creates a new entry.
168
             *
169
             * @param matcher                          The matcher to identify any record component that this definition concerns.
170
             * @param recordComponentAttributeAppender The record component attribute appender factory to apply on any matched record component.
171
             * @param transformer                      The record component transformer to apply to any matched record component.
172
             */
173
            protected Entry(LatentMatcher<? super RecordComponentDescription> matcher,
174
                            RecordComponentAttributeAppender.Factory recordComponentAttributeAppender,
175
                            Transformer<RecordComponentDescription> transformer) {
×
176
                this.matcher = matcher;
×
177
                this.recordComponentAttributeAppender = recordComponentAttributeAppender;
×
178
                this.transformer = transformer;
×
179
            }
×
180

181
            /**
182
             * Returns the record component attribute appender factory to apply on any matched record component.
183
             *
184
             * @return The record component attribute appender factory to apply on any matched record component.
185
             */
186
            protected RecordComponentAttributeAppender.Factory getRecordComponentAttributeAppender() {
187
                return recordComponentAttributeAppender;
×
188
            }
189

190
            /**
191
             * Returns the record component transformer to apply to any matched record component.
192
             *
193
             * @return The record component transformer to apply to any matched record component.
194
             */
195
            protected Transformer<RecordComponentDescription> getTransformer() {
196
                return transformer;
×
197
            }
198

199
            /**
200
             * {@inheritDoc}
201
             */
202
            public ElementMatcher<? super RecordComponentDescription> resolve(TypeDescription typeDescription) {
203
                return matcher.resolve(typeDescription);
×
204
            }
205
        }
206

207
        /**
208
         * A compiled default record component registry.
209
         */
210
        @HashCodeAndEqualsPlugin.Enhance
211
        protected static class Compiled implements RecordComponentRegistry.Compiled {
212

213
            /**
214
             * The instrumented type for which this registry was compiled for.
215
             */
216
            private final TypeDescription instrumentedType;
217

218
            /**
219
             * The entries of this compiled record component registry.
220
             */
221
            private final List<Entry> entries;
222

223
            /**
224
             * Creates a new compiled record component registry.
225
             *
226
             * @param instrumentedType The instrumented type for which this registry was compiled for.
227
             * @param entries          The entries of this compiled record component registry.
228
             */
229
            protected Compiled(TypeDescription instrumentedType, List<Entry> entries) {
1✔
230
                this.instrumentedType = instrumentedType;
1✔
231
                this.entries = entries;
1✔
232
            }
1✔
233

234
            /**
235
             * {@inheritDoc}
236
             */
237
            public Record target(RecordComponentDescription recordComponentDescription) {
238
                for (Entry entry : entries) {
×
239
                    if (entry.matches(recordComponentDescription)) {
×
240
                        return entry.bind(instrumentedType, recordComponentDescription);
×
241
                    }
242
                }
×
243
                return new Record.ForImplicitRecordComponent(recordComponentDescription);
×
244
            }
245

246
            /**
247
             * An entry of a compiled record component registry.
248
             */
249
            @HashCodeAndEqualsPlugin.Enhance
250
            protected static class Entry implements ElementMatcher<RecordComponentDescription> {
251

252
                /**
253
                 * The matcher to identify any record component that this definition concerns.
254
                 */
255
                private final ElementMatcher<? super RecordComponentDescription> matcher;
256

257
                /**
258
                 * The record component attribute appender to apply on any matched record component.
259
                 */
260
                private final RecordComponentAttributeAppender recordComponentAttributeAppender;
261

262
                /**
263
                 * The record component transformer to apply to any matched record component.
264
                 */
265
                private final Transformer<RecordComponentDescription> transformer;
266

267
                /**
268
                 * Creates a new entry.
269
                 *
270
                 * @param matcher                          The matcher to identify any record component that this definition concerns.
271
                 * @param recordComponentAttributeAppender The record component attribute appender to apply on any matched record component.
272
                 * @param transformer                      The record component transformer to apply to any matched record component.
273
                 */
274
                protected Entry(ElementMatcher<? super RecordComponentDescription> matcher,
275
                                RecordComponentAttributeAppender recordComponentAttributeAppender,
276
                                Transformer<RecordComponentDescription> transformer) {
×
277
                    this.matcher = matcher;
×
278
                    this.recordComponentAttributeAppender = recordComponentAttributeAppender;
×
279
                    this.transformer = transformer;
×
280
                }
×
281

282
                /**
283
                 * Binds this entry to the provided record component description.
284
                 *
285
                 * @param instrumentedType           The instrumented type for which this entry applies.
286
                 * @param recordComponentDescription The record component description to be bound to this entry.
287
                 * @return A record representing the binding of this entry to the provided record component.
288
                 */
289
                protected Record bind(TypeDescription instrumentedType, RecordComponentDescription recordComponentDescription) {
290
                    return new Record.ForExplicitRecordComponent(recordComponentAttributeAppender, transformer.transform(instrumentedType, recordComponentDescription));
×
291
                }
292

293
                /**
294
                 * {@inheritDoc}
295
                 */
296
                public boolean matches(@MaybeNull RecordComponentDescription target) {
297
                    return matcher.matches(target);
×
298
                }
299
            }
300
        }
301
    }
302
}
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