• 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.9
/byte-buddy-benchmark/src/main/java/net/bytebuddy/benchmark/ClassByExtensionBenchmark.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.benchmark;
17

18
import javassist.util.proxy.MethodFilter;
19
import javassist.util.proxy.MethodHandler;
20
import javassist.util.proxy.ProxyFactory;
21
import net.bytebuddy.ByteBuddy;
22
import net.bytebuddy.benchmark.specimen.ExampleClass;
23
import net.bytebuddy.description.type.TypeDescription;
24
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
25
import net.bytebuddy.dynamic.scaffold.TypeValidation;
26
import net.bytebuddy.implementation.Implementation;
27
import net.bytebuddy.implementation.MethodDelegation;
28
import net.bytebuddy.implementation.SuperMethodCall;
29
import net.bytebuddy.implementation.bind.annotation.AllArguments;
30
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
31
import net.bytebuddy.implementation.bind.annotation.SuperCall;
32
import net.bytebuddy.implementation.bind.annotation.SuperMethod;
33
import net.bytebuddy.implementation.bind.annotation.This;
34
import net.bytebuddy.pool.TypePool;
35
import net.bytebuddy.utility.nullability.MaybeNull;
36
import net.sf.cglib.proxy.CallbackHelper;
37
import net.sf.cglib.proxy.Enhancer;
38
import net.sf.cglib.proxy.MethodInterceptor;
39
import net.sf.cglib.proxy.MethodProxy;
40
import net.sf.cglib.proxy.NoOp;
41
import org.openjdk.jmh.annotations.Benchmark;
42
import org.openjdk.jmh.annotations.BenchmarkMode;
43
import org.openjdk.jmh.annotations.Mode;
44
import org.openjdk.jmh.annotations.OutputTimeUnit;
45
import org.openjdk.jmh.annotations.Scope;
46
import org.openjdk.jmh.annotations.Setup;
47
import org.openjdk.jmh.annotations.State;
48

49
import java.lang.reflect.Method;
50
import java.net.URL;
51
import java.net.URLClassLoader;
52
import java.util.concurrent.Callable;
53
import java.util.concurrent.TimeUnit;
54

55
import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy;
56
import static net.bytebuddy.matcher.ElementMatchers.none;
57

58
/**
59
 * <p>
60
 * This benchmark dynamically creates a subclass of {@link ExampleClass} which overrides all methods to invoke the
61
 * direct super class's implementation. The benchmark furthermore creates an instance of this class since some
62
 * code generation frameworks rely on this property. Because this benchmark requires the creation of a subclass,
63
 * the JDK proxy is not included in this benchmark.
64
 * </p>
65
 * <p>
66
 * Note that this class defines all values that are accessed by benchmark methods as instance fields. This way, the JIT
67
 * compiler's capability of constant folding is limited in order to produce more comparable test results.
68
 * </p>
69
 */
70
@State(Scope.Thread)
71
@BenchmarkMode(Mode.AverageTime)
72
@OutputTimeUnit(TimeUnit.MICROSECONDS)
73
public class ClassByExtensionBenchmark {
1✔
74

75
    /**
76
     * The base class to be subclassed in all benchmarks.
77
     */
78
    public static final Class<? extends ExampleClass> BASE_CLASS = ExampleClass.class;
1✔
79

80
    /**
81
     * The base class to be subclassed in all benchmarks.
82
     */
83
    private Class<? extends ExampleClass> baseClass = BASE_CLASS;
1✔
84

85
    /**
86
     * The zero-length of the class loader's URL.
87
     */
88
    private int urlLength = 0;
1✔
89

90
    /**
91
     * Creates a new class loader. By using a fresh class loader for each creation, we avoid name space issues.
92
     * A class loader's creation is part of the benchmark but since any test creates a class loader exactly once,
93
     * the benchmark remains valid.
94
     *
95
     * @return A new class loader.
96
     */
97
    private ClassLoader newClassLoader() {
98
        return new URLClassLoader(new URL[urlLength]);
1✔
99
    }
100

101
    /**
102
     * An implementation to be used by {@link ClassByExtensionBenchmark#benchmarkByteBuddyWithProxyAndReusedDelegator()}.
103
     */
104
    @MaybeNull
105
    private Implementation proxyInterceptor;
106

107
    /**
108
     * An implementation to be used by {@link ClassByExtensionBenchmark#benchmarkByteBuddyWithAccessorAndReusedDelegator()}.
109
     */
110
    @MaybeNull
111
    private Implementation accessInterceptor;
112

113
    /**
114
     * An implementation to be used by {@link ClassByExtensionBenchmark#benchmarkByteBuddyWithPrefixAndReusedDelegator()}.
115
     */
116
    @MaybeNull
117
    private Implementation.Composable prefixInterceptor;
118

119
    /**
120
     * A description of {@link ClassByExtensionBenchmark#baseClass}.
121
     */
122
    @MaybeNull
123
    private TypeDescription baseClassDescription;
124

125
    /**
126
     * A description of {@link ByteBuddyProxyInterceptor}.
127
     */
128
    @MaybeNull
129
    private TypeDescription proxyClassDescription;
130

131
    /**
132
     * A description of {@link ByteBuddyAccessInterceptor}.
133
     */
134
    @MaybeNull
135
    private TypeDescription accessClassDescription;
136

137
    /**
138
     * A description of {@link ByteBuddyPrefixInterceptor}.
139
     */
140
    @MaybeNull
141
    private TypeDescription prefixClassDescription;
142

143
    /**
144
     * A method delegation to {@link ByteBuddyProxyInterceptor}.
145
     */
146
    @MaybeNull
147
    private Implementation proxyInterceptorDescription;
148

149
    /**
150
     * A method delegation to {@link ByteBuddyAccessInterceptor}.
151
     */
152
    @MaybeNull
153
    private Implementation accessInterceptorDescription;
154

155
    /**
156
     * A method delegation to {@link ByteBuddyPrefixInterceptor}.
157
     */
158
    @MaybeNull
159
    private Implementation.Composable prefixInterceptorDescription;
160

161
    /**
162
     * A setup method to create precomputed delegator.
163
     */
164
    @Setup
165
    public void setup() {
166
        proxyInterceptor = MethodDelegation.to(ByteBuddyProxyInterceptor.class);
1✔
167
        accessInterceptor = MethodDelegation.to(ByteBuddyAccessInterceptor.class);
1✔
168
        prefixInterceptor = MethodDelegation.to(ByteBuddyPrefixInterceptor.class);
1✔
169
        baseClassDescription = TypePool.Default.ofSystemLoader().describe(baseClass.getName()).resolve();
1✔
170
        proxyClassDescription = TypePool.Default.ofSystemLoader().describe(ByteBuddyProxyInterceptor.class.getName()).resolve();
1✔
171
        accessClassDescription = TypePool.Default.ofSystemLoader().describe(ByteBuddyAccessInterceptor.class.getName()).resolve();
1✔
172
        prefixClassDescription = TypePool.Default.ofSystemLoader().describe(ByteBuddyPrefixInterceptor.class.getName()).resolve();
1✔
173
        proxyInterceptorDescription = MethodDelegation.to(proxyClassDescription);
1✔
174
        accessInterceptorDescription = MethodDelegation.to(accessClassDescription);
1✔
175
        prefixInterceptorDescription = MethodDelegation.to(prefixClassDescription);
1✔
176
    }
1✔
177

178
    /**
179
     * Creates a baseline for the benchmark.
180
     *
181
     * @return A simple object that is not transformed.
182
     */
183
    @Benchmark
184
    public ExampleClass baseline() {
185
        return new ExampleClass();
1✔
186
    }
187

188
    /**
189
     * Performs a benchmark of a class extension using Byte Buddy. This benchmark creates proxy classes for the invocation
190
     * of super methods which requires the creation of auxiliary classes.
191
     *
192
     * @return The created instance, in order to avoid JIT removal.
193
     * @throws java.lang.Exception If the invocation causes an exception.
194
     */
195
    @Benchmark
196
    public ExampleClass benchmarkByteBuddyWithProxy() throws Exception {
197
        return new ByteBuddy()
1✔
198
                .with(TypeValidation.DISABLED)
1✔
199
                .ignore(none())
1✔
200
                .subclass(baseClass)
1✔
201
                .method(isDeclaredBy(baseClass)).intercept(MethodDelegation.to(ByteBuddyProxyInterceptor.class))
1✔
202
                .make()
1✔
203
                .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
1✔
204
                .getLoaded()
1✔
205
                .getDeclaredConstructor()
1✔
206
                .newInstance();
1✔
207
    }
208

209
    /**
210
     * Performs a benchmark of a class extension using Byte Buddy. This benchmark also uses the annotation-based approach
211
     * but creates delegation methods which do not require the creation of additional classes. This benchmark reuses a
212
     * precomputed delegator.
213
     *
214
     * @return The created instance, in order to avoid JIT removal.
215
     * @throws Exception If the invocation causes an exception.
216
     */
217
    @Benchmark
218
    public ExampleClass benchmarkByteBuddyWithProxyAndReusedDelegator() throws Exception {
219
        return new ByteBuddy()
1✔
220
                .with(TypeValidation.DISABLED)
1✔
221
                .ignore(none())
1✔
222
                .subclass(baseClass)
1✔
223
                .method(isDeclaredBy(baseClass)).intercept(proxyInterceptor)
1✔
224
                .make()
1✔
225
                .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
1✔
226
                .getLoaded()
1✔
227
                .getDeclaredConstructor()
1✔
228
                .newInstance();
1✔
229
    }
230

231
    /**
232
     * Performs a benchmark of a class extension using Byte Buddy. This benchmark creates proxy classes for the invocation
233
     * of super methods which requires the creation of auxiliary classes. This benchmark uses a type pool to compare against
234
     * usage of the reflection API.
235
     *
236
     * @return The created instance, in order to avoid JIT removal.
237
     * @throws java.lang.Exception If the invocation causes an exception.
238
     */
239
    @Benchmark
240
    public ExampleClass benchmarkByteBuddyWithProxyWithTypePool() throws Exception {
241
        return (ExampleClass) new ByteBuddy()
1✔
242
                .with(TypeValidation.DISABLED)
1✔
243
                .ignore(none())
1✔
244
                .subclass(baseClassDescription)
1✔
245
                .method(isDeclaredBy(baseClassDescription)).intercept(MethodDelegation.to(proxyClassDescription))
1✔
246
                .make()
1✔
247
                .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
1✔
248
                .getLoaded()
1✔
249
                .getDeclaredConstructor()
1✔
250
                .newInstance();
1✔
251
    }
252

253
    /**
254
     * Performs a benchmark of a class extension using Byte Buddy. This benchmark also uses the annotation-based approach
255
     * but creates delegation methods which do not require the creation of additional classes. This benchmark reuses a
256
     * precomputed delegator. This benchmark uses a type pool to compare against usage of the reflection API.
257
     *
258
     * @return The created instance, in order to avoid JIT removal.
259
     * @throws Exception If the invocation causes an exception.
260
     */
261
    @Benchmark
262
    public ExampleClass benchmarkByteBuddyWithProxyAndReusedDelegatorWithTypePool() throws Exception {
263
        return (ExampleClass) new ByteBuddy()
1✔
264
                .with(TypeValidation.DISABLED)
1✔
265
                .ignore(none())
1✔
266
                .subclass(baseClassDescription)
1✔
267
                .method(isDeclaredBy(baseClassDescription)).intercept(proxyInterceptorDescription)
1✔
268
                .make()
1✔
269
                .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
1✔
270
                .getLoaded()
1✔
271
                .getDeclaredConstructor()
1✔
272
                .newInstance();
1✔
273
    }
274

275
    /**
276
     * Performs a benchmark of a class extension using Byte Buddy. This benchmark also uses the annotation-based approach
277
     * but creates delegation methods which do not require the creation of additional classes.
278
     *
279
     * @return The created instance, in order to avoid JIT removal.
280
     * @throws Exception If the invocation causes an exception.
281
     */
282
    @Benchmark
283
    public ExampleClass benchmarkByteBuddyWithAccessor() throws Exception {
284
        return new ByteBuddy()
1✔
285
                .with(TypeValidation.DISABLED)
1✔
286
                .ignore(none())
1✔
287
                .subclass(baseClass)
1✔
288
                .method(isDeclaredBy(baseClass)).intercept(MethodDelegation.to(ByteBuddyAccessInterceptor.class))
1✔
289
                .make()
1✔
290
                .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
1✔
291
                .getLoaded()
1✔
292
                .getDeclaredConstructor()
1✔
293
                .newInstance();
1✔
294
    }
295

296
    /**
297
     * Performs a benchmark of a class extension using Byte Buddy. This benchmark also uses the annotation-based approach
298
     * but creates delegation methods which do not require the creation of additional classes. This benchmark reuses a
299
     * precomputed delegator.
300
     *
301
     * @return The created instance, in order to avoid JIT removal.
302
     * @throws Exception If the invocation causes an exception.
303
     */
304
    @Benchmark
305
    public ExampleClass benchmarkByteBuddyWithAccessorAndReusedDelegator() throws Exception {
306
        return new ByteBuddy()
1✔
307
                .with(TypeValidation.DISABLED)
1✔
308
                .ignore(none())
1✔
309
                .subclass(baseClass)
1✔
310
                .method(isDeclaredBy(baseClass)).intercept(accessInterceptor)
1✔
311
                .make()
1✔
312
                .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
1✔
313
                .getLoaded()
1✔
314
                .getDeclaredConstructor()
1✔
315
                .newInstance();
1✔
316
    }
317

318
    /**
319
     * Performs a benchmark of a class extension using Byte Buddy. This benchmark also uses the annotation-based approach
320
     * but creates delegation methods which do not require the creation of additional classes. This benchmark uses a type
321
     * pool to compare against usage of the reflection API.
322
     *
323
     * @return The created instance, in order to avoid JIT removal.
324
     * @throws Exception If the invocation causes an exception.
325
     */
326
    @Benchmark
327
    public ExampleClass benchmarkByteBuddyWithAccessorWithTypePool() throws Exception {
328
        return (ExampleClass) new ByteBuddy()
1✔
329
                .with(TypeValidation.DISABLED)
1✔
330
                .ignore(none())
1✔
331
                .subclass(baseClassDescription)
1✔
332
                .method(isDeclaredBy(baseClassDescription)).intercept(MethodDelegation.to(accessClassDescription))
1✔
333
                .make()
1✔
334
                .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
1✔
335
                .getLoaded()
1✔
336
                .getDeclaredConstructor()
1✔
337
                .newInstance();
1✔
338
    }
339

340
    /**
341
     * Performs a benchmark of a class extension using Byte Buddy. This benchmark also uses the annotation-based approach
342
     * but creates delegation methods which do not require the creation of additional classes. This benchmark reuses a
343
     * precomputed delegator. This benchmark uses a type pool to compare against usage of the reflection API.
344
     *
345
     * @return The created instance, in order to avoid JIT removal.
346
     * @throws Exception If the invocation causes an exception.
347
     */
348
    @Benchmark
349
    public ExampleClass benchmarkByteBuddyWithAccessorAndReusedDelegatorWithTypePool() throws Exception {
350
        return (ExampleClass) new ByteBuddy()
1✔
351
                .with(TypeValidation.DISABLED)
1✔
352
                .ignore(none())
1✔
353
                .subclass(baseClassDescription)
1✔
354
                .method(isDeclaredBy(baseClassDescription)).intercept(accessInterceptorDescription)
1✔
355
                .make()
1✔
356
                .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
1✔
357
                .getLoaded()
1✔
358
                .getDeclaredConstructor()
1✔
359
                .newInstance();
1✔
360
    }
361

362
    /**
363
     * Performs a benchmark of a class extension using Byte Buddy. This benchmark uses delegation but completes with a
364
     * hard-coded super method call.
365
     *
366
     * @return The created instance, in order to avoid JIT removal.
367
     * @throws Exception If the invocation causes an exception.
368
     */
369
    @Benchmark
370
    public ExampleClass benchmarkByteBuddyWithPrefix() throws Exception {
371
        return new ByteBuddy()
1✔
372
                .with(TypeValidation.DISABLED)
1✔
373
                .ignore(none())
1✔
374
                .subclass(baseClass)
1✔
375
                .method(isDeclaredBy(baseClass)).intercept(MethodDelegation.to(ByteBuddyPrefixInterceptor.class).andThen(SuperMethodCall.INSTANCE))
1✔
376
                .make()
1✔
377
                .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
1✔
378
                .getLoaded()
1✔
379
                .getDeclaredConstructor()
1✔
380
                .newInstance();
1✔
381
    }
382

383
    /**
384
     * Performs a benchmark of a class extension using Byte Buddy. This benchmark uses delegation but completes with a
385
     * hard-coded super method call. This benchmark reuses a precomputed delegator.
386
     *
387
     * @return The created instance, in order to avoid JIT removal.
388
     * @throws Exception If the invocation causes an exception.
389
     */
390
    @Benchmark
391
    public ExampleClass benchmarkByteBuddyWithPrefixAndReusedDelegator() throws Exception {
392
        return new ByteBuddy()
1✔
393
                .with(TypeValidation.DISABLED)
1✔
394
                .ignore(none())
1✔
395
                .subclass(baseClass)
1✔
396
                .method(isDeclaredBy(baseClass)).intercept(prefixInterceptor.andThen(SuperMethodCall.INSTANCE))
1✔
397
                .make()
1✔
398
                .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
1✔
399
                .getLoaded()
1✔
400
                .getDeclaredConstructor()
1✔
401
                .newInstance();
1✔
402
    }
403

404
    /**
405
     * Performs a benchmark of a class extension using Byte Buddy. This benchmark uses delegation but completes with a
406
     * hard-coded super method call. This benchmark uses a type pool to compare against usage of the reflection API.
407
     *
408
     * @return The created instance, in order to avoid JIT removal.
409
     * @throws Exception If the invocation causes an exception.
410
     */
411
    @Benchmark
412
    public ExampleClass benchmarkByteBuddyWithPrefixWithTypePool() throws Exception {
413
        return (ExampleClass) new ByteBuddy()
1✔
414
                .with(TypeValidation.DISABLED)
1✔
415
                .ignore(none())
1✔
416
                .subclass(baseClassDescription)
1✔
417
                .method(isDeclaredBy(baseClassDescription)).intercept(MethodDelegation.to(prefixClassDescription).andThen(SuperMethodCall.INSTANCE))
1✔
418
                .make()
1✔
419
                .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
1✔
420
                .getLoaded()
1✔
421
                .getDeclaredConstructor()
1✔
422
                .newInstance();
1✔
423
    }
424

425
    /**
426
     * Performs a benchmark of a class extension using Byte Buddy. This benchmark uses delegation but completes with a
427
     * hard-coded super method call. This benchmark reuses a precomputed delegator. This benchmark uses a type pool to
428
     * compare against usage of the reflection API.
429
     *
430
     * @return The created instance, in order to avoid JIT removal.
431
     * @throws Exception If the invocation causes an exception.
432
     */
433
    @Benchmark
434
    public ExampleClass benchmarkByteBuddyWithPrefixAndReusedDelegatorWithTypePool() throws Exception {
435
        return (ExampleClass) new ByteBuddy()
1✔
436
                .with(TypeValidation.DISABLED)
1✔
437
                .ignore(none())
1✔
438
                .subclass(baseClassDescription)
1✔
439
                .method(isDeclaredBy(baseClassDescription)).intercept(prefixInterceptorDescription.andThen(SuperMethodCall.INSTANCE))
1✔
440
                .make()
1✔
441
                .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
1✔
442
                .getLoaded()
1✔
443
                .getDeclaredConstructor()
1✔
444
                .newInstance();
1✔
445
    }
446

447
    /**
448
     * Performs a benchmark of a class extension using Byte Buddy. This benchmark uses a specialized interception
449
     * strategy which is easier to inline by the compiler.
450
     *
451
     * @return The created instance, in order to avoid JIT removal.
452
     * @throws java.lang.Exception If the invocation causes an exception.
453
     */
454
    @Benchmark
455
    public ExampleClass benchmarkByteBuddySpecialized() throws Exception {
456
        return new ByteBuddy()
1✔
457
                .with(TypeValidation.DISABLED)
1✔
458
                .ignore(none())
1✔
459
                .subclass(baseClass)
1✔
460
                .method(isDeclaredBy(baseClass)).intercept(SuperMethodCall.INSTANCE)
1✔
461
                .make()
1✔
462
                .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
1✔
463
                .getLoaded()
1✔
464
                .getDeclaredConstructor()
1✔
465
                .newInstance();
1✔
466
    }
467

468
    /**
469
     * Performs a benchmark of a class extension using cglib.
470
     *
471
     * @return The created instance, in order to avoid JIT removal.
472
     */
473
    @Benchmark
474
    public ExampleClass benchmarkCglib() {
475
        Enhancer enhancer = new Enhancer();
1✔
476
        enhancer.setUseCache(false);
1✔
477
        enhancer.setUseFactory(false);
1✔
478
        enhancer.setInterceptDuringConstruction(true);
1✔
479
        enhancer.setClassLoader(newClassLoader());
1✔
480
        enhancer.setSuperclass(baseClass);
1✔
481
        CallbackHelper callbackHelper = new CallbackHelper(baseClass, new Class<?>[0]) {
1✔
482
            protected Object getCallback(Method method) {
483
                if (method.getDeclaringClass() == baseClass) {
1✔
484
                    return new MethodInterceptor() {
1✔
485
                        public Object intercept(Object object,
486
                                                Method method,
487
                                                Object[] arguments,
488
                                                MethodProxy methodProxy) throws Throwable {
489
                            return methodProxy.invokeSuper(object, arguments);
1✔
490
                        }
491
                    };
492
                } else {
493
                    return NoOp.INSTANCE;
1✔
494
                }
495
            }
496
        };
497
        enhancer.setCallbackFilter(callbackHelper);
1✔
498
        enhancer.setCallbacks(callbackHelper.getCallbacks());
1✔
499
        return (ExampleClass) enhancer.create();
1✔
500
    }
501

502
    /**
503
     * Performs a benchmark of a class extension using javassist proxies.
504
     *
505
     * @return The created instance, in order to avoid JIT removal.
506
     * @throws java.lang.Exception If the invocation causes an exception.
507
     */
508
    @Benchmark
509
    public ExampleClass benchmarkJavassist() throws Exception {
510
        ProxyFactory proxyFactory = new ProxyFactory() {
1✔
511
            protected ClassLoader getClassLoader() {
512
                return newClassLoader();
1✔
513
            }
514
        };
515
        proxyFactory.setUseCache(false);
1✔
516
        proxyFactory.setUseWriteReplace(false);
1✔
517
        proxyFactory.setSuperclass(baseClass);
1✔
518
        proxyFactory.setFilter(new MethodFilter() {
1✔
519
            public boolean isHandled(Method method) {
520
                return method.getDeclaringClass() == baseClass;
1✔
521
            }
522
        });
523
        @SuppressWarnings("unchecked")
524
        Object instance = proxyFactory.createClass().getDeclaredConstructor().newInstance();
1✔
525
        ((javassist.util.proxy.Proxy) instance).setHandler(new MethodHandler() {
1✔
526
            public Object invoke(Object self,
527
                                 Method thisMethod,
528
                                 Method proceed,
529
                                 Object[] argument) throws Throwable {
530
                return proceed.invoke(self, argument);
1✔
531
            }
532
        });
533
        return (ExampleClass) instance;
1✔
534
    }
535

536
    /**
537
     * Instead of using the {@link net.bytebuddy.implementation.SuperMethodCall} implementation, we are using
538
     * a delegate in order to emulate the interception approach of other instrumentation libraries. Otherwise,
539
     * this benchmark would be biased in favor of Byte Buddy.
540
     */
541
    public static class ByteBuddyProxyInterceptor {
542

543
        /**
544
         * The interceptor's constructor is not supposed to be invoked.
545
         */
546
        private ByteBuddyProxyInterceptor() {
1✔
547
            throw new UnsupportedOperationException();
1✔
548
        }
549

550
        /**
551
         * Call the super method.
552
         *
553
         * @param zuper A proxy for invoking the super method.
554
         * @return The return value of the super method invocation.
555
         * @throws Exception As declared by {@link java.util.concurrent.Callable}'s contract.
556
         */
557
        @RuntimeType
558
        public static Object intercept(@SuperCall Callable<?> zuper) throws Exception {
559
            return zuper.call();
1✔
560
        }
561
    }
562

563
    /**
564
     * Instead of using the {@link net.bytebuddy.implementation.SuperMethodCall} implementation, we are creating
565
     * delegate methods that allow the invocation of the original code.
566
     */
567
    public static class ByteBuddyAccessInterceptor {
568

569
        /**
570
         * The interceptor's constructor is not supposed to be invoked.
571
         */
572
        private ByteBuddyAccessInterceptor() {
1✔
573
            throw new UnsupportedOperationException();
1✔
574
        }
575

576
        /**
577
         * Calls the super method.
578
         *
579
         * @param target    The target instance.
580
         * @param arguments The arguments to the method.
581
         * @param method    A method for invoking the original code.
582
         * @return The return value of the method.
583
         * @throws Exception If the super method call yields an exception.
584
         */
585
        @RuntimeType
586
        public static Object intercept(@This Object target, @AllArguments Object[] arguments, @SuperMethod(privileged = false) Method method) throws Exception {
587
            return method.invoke(target, arguments);
1✔
588
        }
589
    }
590

591
    /**
592
     * An interceptor that is invoked prior to a super method call.
593
     */
594
    public static class ByteBuddyPrefixInterceptor {
595

596
        /**
597
         * The interceptor's constructor is not supposed to be invoked.
598
         */
599
        private ByteBuddyPrefixInterceptor() {
×
600
            throw new UnsupportedOperationException();
×
601
        }
602

603
        /**
604
         * Invoked prior to a method call.
605
         */
606
        public static void intercept() {
607
            /* do nothing */
608
        }
1✔
609
    }
610
}
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