• 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

98.86
/byte-buddy-dep/src/main/java/net/bytebuddy/utility/visitor/MetadataAwareClassVisitor.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.utility.visitor;
17

18
import net.bytebuddy.utility.nullability.MaybeNull;
19
import org.objectweb.asm.AnnotationVisitor;
20
import org.objectweb.asm.Attribute;
21
import org.objectweb.asm.ClassVisitor;
22
import org.objectweb.asm.FieldVisitor;
23
import org.objectweb.asm.MethodVisitor;
24
import org.objectweb.asm.RecordComponentVisitor;
25
import org.objectweb.asm.TypePath;
26

27
/**
28
 * A class visitor that traces invocations of visitation methods and notifies if a nest host or outer class was not visited.
29
 */
30
public abstract class MetadataAwareClassVisitor extends ClassVisitor {
31

32
    /**
33
     * {@code true} if the nest host was not yet visited.
34
     */
35
    private boolean triggerNestHost;
36

37
    /**
38
     * {@code true} if the outer class was not yet visited.
39
     */
40
    private boolean triggerOuterClass;
41

42
    /**
43
     * {@code true} if the attribute visitation is not yet completed.
44
     */
45
    private boolean triggerAttributes;
46

47
    /**
48
     * Creates a metadata aware class visitor.
49
     *
50
     * @param api          The API version.
51
     * @param classVisitor The class visitor to delegate to.
52
     */
53
    protected MetadataAwareClassVisitor(int api, ClassVisitor classVisitor) {
54
        super(api, classVisitor);
1✔
55
        triggerNestHost = true;
1✔
56
        triggerOuterClass = true;
1✔
57
        triggerAttributes = true;
1✔
58
    }
1✔
59

60
    /**
61
     * Invoked if the nest host was not visited.
62
     */
63
    protected void onNestHost() {
64
        /* do nothing */
65
    }
1✔
66

67
    /**
68
     * Invoked if the outer class was not visited.
69
     */
70
    protected void onOuterType() {
71
        /* do nothing */
72
    }
1✔
73

74
    /**
75
     * Invoked if the attribute visitation is about to complete.
76
     */
77
    protected void onAfterAttributes() {
78
        /* do nothing */
79
    }
×
80

81
    /**
82
     * Considers triggering a nest host visitation.
83
     */
84
    private void considerTriggerNestHost() {
85
        if (triggerNestHost) {
1✔
86
            triggerNestHost = false;
1✔
87
            onNestHost();
1✔
88
        }
89
    }
1✔
90

91
    /**
92
     * Considers triggering an outer class visitation.
93
     */
94
    private void considerTriggerOuterClass() {
95
        if (triggerOuterClass) {
1✔
96
            triggerOuterClass = false;
1✔
97
            onOuterType();
1✔
98
        }
99
    }
1✔
100

101
    /**
102
     * Considers triggering the after attribute visitation.
103
     */
104
    private void considerTriggerAfterAttributes() {
105
        if (triggerAttributes) {
1✔
106
            triggerAttributes = false;
1✔
107
            onAfterAttributes();
1✔
108
        }
109
    }
1✔
110

111
    @Override
112
    public final void visitNestHost(String nestHost) {
113
        triggerNestHost = false;
1✔
114
        onVisitNestHost(nestHost);
1✔
115
    }
1✔
116

117
    /**
118
     * An order-sensitive invocation of {@link ClassVisitor#visitNestHost(String)}.
119
     *
120
     * @param nestHost The internal name of the nest host.
121
     */
122
    protected void onVisitNestHost(String nestHost) {
123
        super.visitNestHost(nestHost);
1✔
124
    }
1✔
125

126
    @Override
127
    public final void visitOuterClass(String owner, @MaybeNull String name, @MaybeNull String descriptor) {
128
        considerTriggerNestHost();
1✔
129
        triggerOuterClass = false;
1✔
130
        onVisitOuterClass(owner, name, descriptor);
1✔
131
    }
1✔
132

133
    /**
134
     * An order-sensitive invocation of {@link ClassVisitor#visitOuterClass(String, String, String)}.
135
     *
136
     * @param owner      The outer class's internal name.
137
     * @param name       The outer method's name or {@code null} if it does not exist.
138
     * @param descriptor The outer method's descriptor or {@code null} if it does not exist.
139
     */
140
    protected void onVisitOuterClass(String owner, @MaybeNull String name, @MaybeNull String descriptor) {
141
        super.visitOuterClass(owner, name, descriptor);
1✔
142
    }
1✔
143

144
    @Override
145
    public final void visitPermittedSubclass(String permittedSubclass) {
146
        considerTriggerNestHost();
1✔
147
        considerTriggerOuterClass();
1✔
148
        considerTriggerAfterAttributes();
1✔
149
        onVisitPermittedSubclass(permittedSubclass);
1✔
150
    }
1✔
151

152
    /**
153
     * An order-sensitive invocation of {@code ClassVisitor#visitPermittedSubclass}.
154
     *
155
     * @param permittedSubclass The internal name of the permitted subclass.
156
     */
157
    protected void onVisitPermittedSubclass(String permittedSubclass) {
158
        super.visitPermittedSubclass(permittedSubclass);
1✔
159
    }
1✔
160

161
    @Override
162
    @MaybeNull
163
    public RecordComponentVisitor visitRecordComponent(String name, String descriptor, @MaybeNull String signature) {
164
        considerTriggerNestHost();
1✔
165
        considerTriggerOuterClass();
1✔
166
        considerTriggerAfterAttributes();
1✔
167
        return onVisitRecordComponent(name, descriptor, signature);
1✔
168
    }
169

170
    /**
171
     * An order-sensitive invocation of {@link ClassVisitor#visitRecordComponent(String, String, String)}.
172
     *
173
     * @param name       The record component's name.
174
     * @param descriptor The record component's descriptor.
175
     * @param signature  The record component's generic signature or {@code null} if the record component's type is non-generic.
176
     * @return The record component visitor or {@code null} if the component should not be visited.
177
     */
178
    @MaybeNull
179
    protected RecordComponentVisitor onVisitRecordComponent(String name, String descriptor, @MaybeNull String signature) {
180
        return super.visitRecordComponent(name, descriptor, signature);
1✔
181
    }
182

183
    @Override
184
    @MaybeNull
185
    public final AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
186
        considerTriggerNestHost();
1✔
187
        considerTriggerOuterClass();
1✔
188
        return onVisitAnnotation(descriptor, visible);
1✔
189
    }
190

191
    /**
192
     * An order-sensitive invocation of {@link ClassVisitor#visitAnnotation(String, boolean)}.
193
     *
194
     * @param descriptor The annotation type's descriptor.
195
     * @param visible    {@code true} if the annotation is visible at runtime.
196
     * @return An annotation visitor or {@code null} if the annotation should be ignored.
197
     */
198
    @MaybeNull
199
    protected AnnotationVisitor onVisitAnnotation(String descriptor, boolean visible) {
200
        return super.visitAnnotation(descriptor, visible);
1✔
201
    }
202

203
    @Override
204
    @MaybeNull
205
    public final AnnotationVisitor visitTypeAnnotation(int typeReference, TypePath typePath, String descriptor, boolean visible) {
206
        considerTriggerNestHost();
1✔
207
        considerTriggerOuterClass();
1✔
208
        return onVisitTypeAnnotation(typeReference, typePath, descriptor, visible);
1✔
209
    }
210

211
    /**
212
     * An order-sensitive invocation of {@link ClassVisitor#visitTypeAnnotation(int, TypePath, String, boolean)}.
213
     *
214
     * @param typeReference The type reference of the type annotation.
215
     * @param typePath      The type path of the type annotation.
216
     * @param descriptor    The descriptor of the annotation type.
217
     * @param visible       {@code true} if the annotation is visible at runtime.
218
     * @return An annotation visitor or {@code null} if the annotation should be ignored.
219
     */
220
    @MaybeNull
221
    protected AnnotationVisitor onVisitTypeAnnotation(int typeReference, TypePath typePath, String descriptor, boolean visible) {
222
        return super.visitTypeAnnotation(typeReference, typePath, descriptor, visible);
1✔
223
    }
224

225
    @Override
226
    public final void visitAttribute(Attribute attribute) {
227
        considerTriggerNestHost();
1✔
228
        considerTriggerOuterClass();
1✔
229
        onVisitAttribute(attribute);
1✔
230
    }
1✔
231

232
    /**
233
     * An order-sensitive invocation of {@link ClassVisitor#visitAttribute(Attribute)}.
234
     *
235
     * @param attribute The attribute to visit.
236
     */
237
    protected void onVisitAttribute(Attribute attribute) {
238
        super.visitAttribute(attribute);
1✔
239
    }
1✔
240

241
    @Override
242
    public final void visitNestMember(String nestMember) {
243
        considerTriggerNestHost();
1✔
244
        considerTriggerOuterClass();
1✔
245
        considerTriggerAfterAttributes();
1✔
246
        onVisitNestMember(nestMember);
1✔
247
    }
1✔
248

249
    /**
250
     * An order-sensitive invocation of {@link ClassVisitor#visitNestMember(String)}.
251
     *
252
     * @param nestMember The internal name of the nest member.
253
     */
254
    protected void onVisitNestMember(String nestMember) {
255
        super.visitNestMember(nestMember);
1✔
256
    }
1✔
257

258
    @Override
259
    public final void visitInnerClass(String name, @MaybeNull String outerName, @MaybeNull String innerName, int modifiers) {
260
        considerTriggerNestHost();
1✔
261
        considerTriggerOuterClass();
1✔
262
        considerTriggerAfterAttributes();
1✔
263
        onVisitInnerClass(name, outerName, innerName, modifiers);
1✔
264
    }
1✔
265

266
    /**
267
     * An order-sensitive invocation of {@link ClassVisitor#visitInnerClass(String, String, String, int)}.
268
     *
269
     * @param internalName      The internal name of the inner class.
270
     * @param outerName The internal name of the outer class or {@code null} for a member class.
271
     * @param innerName The inner class's simple name or {@code null} for an anonymous class.
272
     * @param modifiers The inner class's source code modifiers.
273
     */
274
    protected void onVisitInnerClass(String internalName, @MaybeNull String outerName, @MaybeNull String innerName, int modifiers) {
275
        super.visitInnerClass(internalName, outerName, innerName, modifiers);
1✔
276
    }
1✔
277

278
    @Override
279
    @MaybeNull
280
    public final FieldVisitor visitField(int modifiers, String internalName, String descriptor, @MaybeNull String signature, @MaybeNull Object value) {
281
        considerTriggerNestHost();
1✔
282
        considerTriggerOuterClass();
1✔
283
        considerTriggerAfterAttributes();
1✔
284
        return onVisitField(modifiers, internalName, descriptor, signature, value);
1✔
285
    }
286

287
    /**
288
     * An order-sensitive invocation of {@link ClassVisitor#visitField(int, String, String, String, Object)}.
289
     *
290
     * @param modifiers    The field's modifiers.
291
     * @param internalName The field's internal name.
292
     * @param descriptor   The field type's descriptor.
293
     * @param signature    The field's generic signature or {@code null} if the field is not generic.
294
     * @param value        The field's default value or {@code null} if no such value exists.
295
     * @return A field visitor to visit the field or {@code null} to ignore it.
296
     */
297
    @MaybeNull
298
    protected FieldVisitor onVisitField(int modifiers, String internalName, String descriptor, @MaybeNull String signature, @MaybeNull Object value) {
299
        return super.visitField(modifiers, internalName, descriptor, signature, value);
1✔
300
    }
301

302
    @Override
303
    @MaybeNull
304
    public final MethodVisitor visitMethod(int modifiers, String internalName, String descriptor, @MaybeNull String signature, @MaybeNull String[] exception) {
305
        considerTriggerNestHost();
1✔
306
        considerTriggerOuterClass();
1✔
307
        considerTriggerAfterAttributes();
1✔
308
        return onVisitMethod(modifiers, internalName, descriptor, signature, exception);
1✔
309
    }
310

311
    /**
312
     * An order-sensitive invocation of {@link ClassVisitor#visitMethod(int, String, String, String, String[])}.
313
     *
314
     * @param modifiers    The method's modifiers.
315
     * @param internalName The method's internal name.
316
     * @param descriptor   The field type's descriptor.
317
     * @param signature    The method's generic signature or {@code null} if the method is not generic.
318
     * @param exception    The method's declared exceptions or {@code null} if no exceptions are declared.
319
     * @return A method visitor to visit the method or {@code null} to ignore it.
320
     */
321
    @MaybeNull
322
    protected MethodVisitor onVisitMethod(int modifiers, String internalName, String descriptor, @MaybeNull String signature, @MaybeNull String[] exception) {
323
        return super.visitMethod(modifiers, internalName, descriptor, signature, exception);
1✔
324
    }
325

326
    @Override
327
    public final void visitEnd() {
328
        considerTriggerNestHost();
1✔
329
        considerTriggerOuterClass();
1✔
330
        considerTriggerAfterAttributes();
1✔
331
        onVisitEnd();
1✔
332
    }
1✔
333

334
    /**
335
     * An order-sensitive invocation of {@link ClassVisitor#visitEnd()}.
336
     */
337
    protected void onVisitEnd() {
338
        super.visitEnd();
1✔
339
    }
1✔
340
}
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