sqry-classpath 8.0.1

JVM classpath analysis for sqry - bytecode parsing, build system resolution, and graph integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Lambda/method-reference target extraction from the `BootstrapMethods`
//! attribute.
//!
//! JVM compilers (javac, kotlinc, scalac, etc.) compile lambda expressions and
//! method references into `invokedynamic` instructions whose bootstrap method
//! is [`java/lang/invoke/LambdaMetafactory.metafactory`][metafactory] (or
//! `altMetafactory`). The third bootstrap argument (index 2) of such entries
//! is a `CONSTANT_MethodHandle_info` that points to the **actual target
//! method** being captured.
//!
//! This module extracts those targets from a parsed [`cafebabe::ClassFile`] and
//! returns them as [`LambdaTargetStub`] records for inclusion in the class
//! stub.
//!
//! [metafactory]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/invoke/LambdaMetafactory.html

use cafebabe::attributes::AttributeData;
use cafebabe::constant_pool::{
    BootstrapArgument, MethodHandle, ReferenceKind as CafeReferenceKind,
};

use crate::stub::model::{LambdaTargetStub, ReferenceKind};

use super::constants::class_name_to_fqn;

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// The internal class name of `LambdaMetafactory`.
const LAMBDA_METAFACTORY_CLASS: &str = "java/lang/invoke/LambdaMetafactory";

/// The standard bootstrap method name for lambda expressions.
const METAFACTORY_METHOD: &str = "metafactory";

/// The alternative bootstrap method name for complex lambda expressions
/// (serialisable lambdas, intersection-type target, etc.).
const ALT_METAFACTORY_METHOD: &str = "altMetafactory";

/// Index of the implementation method handle within the bootstrap arguments.
/// For `LambdaMetafactory.metafactory`, the arguments are:
///   0 — samMethodType  (`MethodType`)
///   1 — implMethod     (`MethodHandle`) — **sometimes**
///   2 — implMethod     (`MethodHandle`) — **standard position**
///
/// Per the JVM spec and `LambdaMetafactory` javadoc, argument index 2 is the
/// implementation `MethodHandle`.
const IMPL_METHOD_ARG_INDEX: usize = 2;

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Extract lambda/method-reference targets from a parsed class file.
///
/// Iterates class-level attributes, finds the `BootstrapMethods` attribute, and
/// filters entries whose bootstrap method handle points to
/// `LambdaMetafactory.metafactory` or `altMetafactory`. For each matching
/// entry, the third bootstrap argument (a `MethodHandle`) is converted to a
/// [`LambdaTargetStub`].
///
/// # Returns
///
/// A `Vec` of targets, potentially empty if no `BootstrapMethods` attribute
/// exists or none of the entries are `LambdaMetafactory` invocations.
///
/// Non-`LambdaMetafactory` bootstrap entries are silently skipped. Malformed
/// entries (e.g., fewer than 3 arguments, wrong argument type at index 2) are
/// logged as warnings and skipped.
#[must_use]
#[allow(clippy::needless_continue)] // Continue clarifies control flow
pub fn extract_lambda_targets(class: &cafebabe::ClassFile<'_>) -> Vec<LambdaTargetStub> {
    let mut targets = Vec::new();

    for attr in &class.attributes {
        if let AttributeData::BootstrapMethods(entries) = &attr.data {
            for (idx, entry) in entries.iter().enumerate() {
                // Check if the bootstrap method points to LambdaMetafactory.
                if !is_lambda_metafactory(&entry.method) {
                    continue;
                }

                // Extract the implementation MethodHandle from argument index 2.
                match extract_impl_handle(idx, &entry.arguments) {
                    Some(stub) => targets.push(stub),
                    #[allow(clippy::needless_continue)] // Continue at end of loop for clarity
                    None => continue,
                }
            }
        }
    }

    targets
}

// ---------------------------------------------------------------------------
// Internals
// ---------------------------------------------------------------------------

/// Check whether a bootstrap method handle points to `LambdaMetafactory`.
///
/// The handle's class name must be `java/lang/invoke/LambdaMetafactory` and its
/// method name must be `metafactory` or `altMetafactory`.
fn is_lambda_metafactory(handle: &MethodHandle<'_>) -> bool {
    handle.class_name.as_ref() == LAMBDA_METAFACTORY_CLASS
        && (handle.member_ref.name.as_ref() == METAFACTORY_METHOD
            || handle.member_ref.name.as_ref() == ALT_METAFACTORY_METHOD)
}

/// Extract the implementation `MethodHandle` from bootstrap arguments and
/// convert it to a [`LambdaTargetStub`].
///
/// Returns `None` (with a warning log) if the arguments are too few or the
/// third argument is not a `MethodHandle`.
fn extract_impl_handle(
    bootstrap_idx: usize,
    arguments: &[BootstrapArgument<'_>],
) -> Option<LambdaTargetStub> {
    if arguments.len() <= IMPL_METHOD_ARG_INDEX {
        log::warn!(
            "BootstrapMethods entry {bootstrap_idx}: expected at least {} arguments, \
             found {}; skipping",
            IMPL_METHOD_ARG_INDEX + 1,
            arguments.len(),
        );
        return None;
    }

    match &arguments[IMPL_METHOD_ARG_INDEX] {
        BootstrapArgument::MethodHandle(handle) => {
            #[allow(clippy::manual_let_else)] // Match-based error handling for clarity
            let reference_kind = if let Some(kind) = convert_reference_kind(handle.kind) {
                kind
            } else {
                log::warn!(
                    "BootstrapMethods entry {bootstrap_idx}: \
                     unsupported reference kind {:?}; skipping",
                    handle.kind,
                );
                return None;
            };

            Some(LambdaTargetStub {
                owner_fqn: class_name_to_fqn(handle.class_name.as_ref()),
                method_name: handle.member_ref.name.to_string(),
                method_descriptor: handle.member_ref.descriptor.to_string(),
                reference_kind,
            })
        }
        other => {
            log::warn!(
                "BootstrapMethods entry {bootstrap_idx}: expected MethodHandle at \
                 argument index {IMPL_METHOD_ARG_INDEX}, found {kind}; skipping",
                kind = bootstrap_arg_kind_name(other),
            );
            None
        }
    }
}

/// Convert a cafebabe [`CafeReferenceKind`] to our model [`ReferenceKind`].
#[allow(clippy::unnecessary_wraps)] // Result for API consistency
fn convert_reference_kind(kind: CafeReferenceKind) -> Option<ReferenceKind> {
    Some(match kind {
        CafeReferenceKind::GetField => ReferenceKind::GetField,
        CafeReferenceKind::GetStatic => ReferenceKind::GetStatic,
        CafeReferenceKind::PutField => ReferenceKind::PutField,
        CafeReferenceKind::PutStatic => ReferenceKind::PutStatic,
        CafeReferenceKind::InvokeVirtual => ReferenceKind::InvokeVirtual,
        CafeReferenceKind::InvokeStatic => ReferenceKind::InvokeStatic,
        CafeReferenceKind::InvokeSpecial => ReferenceKind::InvokeSpecial,
        CafeReferenceKind::NewInvokeSpecial => ReferenceKind::NewInvokeSpecial,
        CafeReferenceKind::InvokeInterface => ReferenceKind::InvokeInterface,
    })
}

/// Return a human-readable name for a [`BootstrapArgument`] variant (for log
/// messages).
fn bootstrap_arg_kind_name(arg: &BootstrapArgument<'_>) -> &'static str {
    match arg {
        BootstrapArgument::LiteralConstant(_) => "LiteralConstant",
        BootstrapArgument::ClassInfo(_) => "ClassInfo",
        BootstrapArgument::MethodHandle(_) => "MethodHandle",
        BootstrapArgument::MethodType(_) => "MethodType",
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use cafebabe::attributes::{AttributeData, AttributeInfo, BootstrapMethodEntry};
    use cafebabe::constant_pool::{
        BootstrapArgument, MemberKind, MethodHandle, NameAndType,
        ReferenceKind as CafeReferenceKind,
    };
    use std::borrow::Cow;

    // -- Test helpers ---------------------------------------------------------

    /// Build a `MethodHandle` pointing to `LambdaMetafactory.metafactory`.
    fn metafactory_handle<'a>() -> MethodHandle<'a> {
        MethodHandle {
            kind: CafeReferenceKind::InvokeStatic,
            class_name: Cow::Borrowed(LAMBDA_METAFACTORY_CLASS),
            member_kind: MemberKind::Method,
            member_ref: NameAndType {
                name: Cow::Borrowed(METAFACTORY_METHOD),
                descriptor: Cow::Borrowed(
                    "(Ljava/lang/invoke/MethodHandles$Lookup;\
                     Ljava/lang/String;\
                     Ljava/lang/invoke/MethodType;\
                     Ljava/lang/invoke/MethodType;\
                     Ljava/lang/invoke/MethodHandle;\
                     Ljava/lang/invoke/MethodType;\
                     )Ljava/lang/invoke/CallSite;",
                ),
            },
        }
    }

    /// Build a `MethodHandle` pointing to `LambdaMetafactory.altMetafactory`.
    fn alt_metafactory_handle<'a>() -> MethodHandle<'a> {
        MethodHandle {
            kind: CafeReferenceKind::InvokeStatic,
            class_name: Cow::Borrowed(LAMBDA_METAFACTORY_CLASS),
            member_kind: MemberKind::Method,
            member_ref: NameAndType {
                name: Cow::Borrowed(ALT_METAFACTORY_METHOD),
                descriptor: Cow::Borrowed(
                    "(Ljava/lang/invoke/MethodHandles$Lookup;\
                     Ljava/lang/String;\
                     Ljava/lang/invoke/MethodType;\
                     [Ljava/lang/Object;\
                     )Ljava/lang/invoke/CallSite;",
                ),
            },
        }
    }

    /// Build a non-lambda bootstrap handle (e.g., `StringConcatFactory`).
    fn string_concat_handle<'a>() -> MethodHandle<'a> {
        MethodHandle {
            kind: CafeReferenceKind::InvokeStatic,
            class_name: Cow::Borrowed("java/lang/invoke/StringConcatFactory"),
            member_kind: MemberKind::Method,
            member_ref: NameAndType {
                name: Cow::Borrowed("makeConcatWithConstants"),
                descriptor: Cow::Borrowed(
                    "(Ljava/lang/invoke/MethodHandles$Lookup;\
                     Ljava/lang/String;\
                     Ljava/lang/invoke/MethodType;\
                     Ljava/lang/String;\
                     [Ljava/lang/Object;\
                     )Ljava/lang/invoke/CallSite;",
                ),
            },
        }
    }

    /// Build the standard 3-argument list for a `LambdaMetafactory` entry.
    ///
    /// Arguments: [`MethodType(sam_descriptor)`, MethodType(instantiated),
    /// MethodHandle(impl)].
    fn lambda_bootstrap_args<'a>(
        impl_kind: CafeReferenceKind,
        impl_class: &'a str,
        impl_name: &'a str,
        impl_descriptor: &'a str,
    ) -> Vec<BootstrapArgument<'a>> {
        vec![
            // arg 0: SAM method type
            BootstrapArgument::MethodType(Cow::Borrowed("(Ljava/lang/Object;)Ljava/lang/Object;")),
            // arg 1: instantiated method type
            BootstrapArgument::MethodType(Cow::Borrowed("(Ljava/lang/String;)Ljava/lang/String;")),
            // arg 2: implementation method handle
            BootstrapArgument::MethodHandle(MethodHandle {
                kind: impl_kind,
                class_name: Cow::Borrowed(impl_class),
                member_kind: MemberKind::Method,
                member_ref: NameAndType {
                    name: Cow::Borrowed(impl_name),
                    descriptor: Cow::Borrowed(impl_descriptor),
                },
            }),
        ]
    }

    /// Parse a real class file and extract lambda targets. This requires
    /// building a `ClassFile` from bytes, which is the full integration path.
    /// These unit tests use the component function directly with constructed
    /// bootstrap entries instead.

    // -- Test 1: No BootstrapMethods attribute → empty result -----------------

    #[test]
    fn no_bootstrap_methods_returns_empty() {
        // Simulate a class with no BootstrapMethods attribute by calling the
        // extraction logic directly on empty attribute lists.
        let attrs: Vec<AttributeInfo<'_>> = vec![];
        let targets = extract_lambda_targets_from_attrs(&attrs);
        assert!(targets.is_empty(), "Expected empty targets");
    }

    // -- Test 2: Lambda target from stream().map(String::toUpperCase) ---------

    #[test]
    fn lambda_target_from_method_reference() {
        let entries = vec![BootstrapMethodEntry {
            method: metafactory_handle(),
            arguments: lambda_bootstrap_args(
                CafeReferenceKind::InvokeVirtual,
                "java/lang/String",
                "toUpperCase",
                "()Ljava/lang/String;",
            ),
        }];

        let attrs = vec![AttributeInfo {
            name: Cow::Borrowed("BootstrapMethods"),
            data: AttributeData::BootstrapMethods(entries),
        }];

        let targets = extract_lambda_targets_from_attrs(&attrs);

        assert_eq!(targets.len(), 1);
        assert_eq!(targets[0].owner_fqn, "java.lang.String");
        assert_eq!(targets[0].method_name, "toUpperCase");
        assert_eq!(targets[0].method_descriptor, "()Ljava/lang/String;");
        assert_eq!(targets[0].reference_kind, ReferenceKind::InvokeVirtual);
    }

    // -- Test 3: Method reference target correctly identified -----------------

    #[test]
    fn method_reference_with_invoke_static() {
        let entries = vec![BootstrapMethodEntry {
            method: metafactory_handle(),
            arguments: lambda_bootstrap_args(
                CafeReferenceKind::InvokeStatic,
                "java/lang/Integer",
                "parseInt",
                "(Ljava/lang/String;)I",
            ),
        }];

        let attrs = vec![AttributeInfo {
            name: Cow::Borrowed("BootstrapMethods"),
            data: AttributeData::BootstrapMethods(entries),
        }];

        let targets = extract_lambda_targets_from_attrs(&attrs);

        assert_eq!(targets.len(), 1);
        assert_eq!(targets[0].owner_fqn, "java.lang.Integer");
        assert_eq!(targets[0].method_name, "parseInt");
        assert_eq!(targets[0].method_descriptor, "(Ljava/lang/String;)I");
        assert_eq!(targets[0].reference_kind, ReferenceKind::InvokeStatic);
    }

    // -- Test 4: Non-LambdaMetafactory bootstrap entries skipped --------------

    #[test]
    fn non_lambda_metafactory_skipped() {
        let entries = vec![BootstrapMethodEntry {
            method: string_concat_handle(),
            arguments: vec![BootstrapArgument::LiteralConstant(
                cafebabe::constant_pool::LiteralConstant::String(Cow::Borrowed("\u{1}Hello \u{1}")),
            )],
        }];

        let attrs = vec![AttributeInfo {
            name: Cow::Borrowed("BootstrapMethods"),
            data: AttributeData::BootstrapMethods(entries),
        }];

        let targets = extract_lambda_targets_from_attrs(&attrs);
        assert!(
            targets.is_empty(),
            "Non-LambdaMetafactory should be skipped"
        );
    }

    // -- Test 5: Multiple lambda targets in one class -------------------------

    #[test]
    fn multiple_lambda_targets() {
        let entries = vec![
            // Entry 0: String::toUpperCase method reference
            BootstrapMethodEntry {
                method: metafactory_handle(),
                arguments: lambda_bootstrap_args(
                    CafeReferenceKind::InvokeVirtual,
                    "java/lang/String",
                    "toUpperCase",
                    "()Ljava/lang/String;",
                ),
            },
            // Entry 1: StringConcatFactory (not lambda — should be skipped)
            BootstrapMethodEntry {
                method: string_concat_handle(),
                arguments: vec![],
            },
            // Entry 2: Constructor reference (NewInvokeSpecial)
            BootstrapMethodEntry {
                method: metafactory_handle(),
                arguments: lambda_bootstrap_args(
                    CafeReferenceKind::NewInvokeSpecial,
                    "java/util/ArrayList",
                    "<init>",
                    "()V",
                ),
            },
            // Entry 3: altMetafactory — serialisable lambda
            BootstrapMethodEntry {
                method: alt_metafactory_handle(),
                arguments: lambda_bootstrap_args(
                    CafeReferenceKind::InvokeStatic,
                    "com/example/Service",
                    "lambda$process$0",
                    "(Ljava/lang/Object;)V",
                ),
            },
        ];

        let attrs = vec![AttributeInfo {
            name: Cow::Borrowed("BootstrapMethods"),
            data: AttributeData::BootstrapMethods(entries),
        }];

        let targets = extract_lambda_targets_from_attrs(&attrs);

        // 3 lambda entries (indices 0, 2, 3); index 1 is StringConcatFactory.
        assert_eq!(
            targets.len(),
            3,
            "Expected 3 lambda targets, got {}",
            targets.len()
        );

        assert_eq!(targets[0].owner_fqn, "java.lang.String");
        assert_eq!(targets[0].method_name, "toUpperCase");
        assert_eq!(targets[0].reference_kind, ReferenceKind::InvokeVirtual);

        assert_eq!(targets[1].owner_fqn, "java.util.ArrayList");
        assert_eq!(targets[1].method_name, "<init>");
        assert_eq!(targets[1].reference_kind, ReferenceKind::NewInvokeSpecial);

        assert_eq!(targets[2].owner_fqn, "com.example.Service");
        assert_eq!(targets[2].method_name, "lambda$process$0");
        assert_eq!(targets[2].reference_kind, ReferenceKind::InvokeStatic);
    }

    // -- Test 6: Reference kind correctly mapped for all variants -------------

    #[test]
    fn reference_kind_mapping_exhaustive() {
        let cafe_to_model = [
            (CafeReferenceKind::GetField, ReferenceKind::GetField),
            (CafeReferenceKind::GetStatic, ReferenceKind::GetStatic),
            (CafeReferenceKind::PutField, ReferenceKind::PutField),
            (CafeReferenceKind::PutStatic, ReferenceKind::PutStatic),
            (
                CafeReferenceKind::InvokeVirtual,
                ReferenceKind::InvokeVirtual,
            ),
            (CafeReferenceKind::InvokeStatic, ReferenceKind::InvokeStatic),
            (
                CafeReferenceKind::InvokeSpecial,
                ReferenceKind::InvokeSpecial,
            ),
            (
                CafeReferenceKind::NewInvokeSpecial,
                ReferenceKind::NewInvokeSpecial,
            ),
            (
                CafeReferenceKind::InvokeInterface,
                ReferenceKind::InvokeInterface,
            ),
        ];

        for (cafe_kind, expected_model_kind) in &cafe_to_model {
            let result = convert_reference_kind(*cafe_kind);
            assert_eq!(
                result,
                Some(*expected_model_kind),
                "Mapping failed for {cafe_kind:?}"
            );
        }
    }

    // -- Test 7: Malformed entry — too few arguments --------------------------

    #[test]
    fn too_few_arguments_skipped_with_warning() {
        // Only 2 arguments instead of the required 3.
        let entries = vec![BootstrapMethodEntry {
            method: metafactory_handle(),
            arguments: vec![
                BootstrapArgument::MethodType(Cow::Borrowed(
                    "(Ljava/lang/Object;)Ljava/lang/Object;",
                )),
                BootstrapArgument::MethodType(Cow::Borrowed(
                    "(Ljava/lang/String;)Ljava/lang/String;",
                )),
            ],
        }];

        let attrs = vec![AttributeInfo {
            name: Cow::Borrowed("BootstrapMethods"),
            data: AttributeData::BootstrapMethods(entries),
        }];

        let targets = extract_lambda_targets_from_attrs(&attrs);
        assert!(targets.is_empty(), "Malformed entry should be skipped");
    }

    // -- Test 8: Wrong argument type at index 2 -------------------------------

    #[test]
    fn wrong_argument_type_at_index_2_skipped() {
        // Argument index 2 is a MethodType instead of a MethodHandle.
        let entries = vec![BootstrapMethodEntry {
            method: metafactory_handle(),
            arguments: vec![
                BootstrapArgument::MethodType(Cow::Borrowed(
                    "(Ljava/lang/Object;)Ljava/lang/Object;",
                )),
                BootstrapArgument::MethodType(Cow::Borrowed(
                    "(Ljava/lang/String;)Ljava/lang/String;",
                )),
                BootstrapArgument::MethodType(Cow::Borrowed("()V")),
            ],
        }];

        let attrs = vec![AttributeInfo {
            name: Cow::Borrowed("BootstrapMethods"),
            data: AttributeData::BootstrapMethods(entries),
        }];

        let targets = extract_lambda_targets_from_attrs(&attrs);
        assert!(
            targets.is_empty(),
            "Wrong type at index 2 should be skipped"
        );
    }

    // -- Test 9: Interface method reference -----------------------------------

    #[test]
    fn interface_method_reference() {
        let entries = vec![BootstrapMethodEntry {
            method: metafactory_handle(),
            arguments: lambda_bootstrap_args(
                CafeReferenceKind::InvokeInterface,
                "java/util/List",
                "size",
                "()I",
            ),
        }];

        let attrs = vec![AttributeInfo {
            name: Cow::Borrowed("BootstrapMethods"),
            data: AttributeData::BootstrapMethods(entries),
        }];

        let targets = extract_lambda_targets_from_attrs(&attrs);

        assert_eq!(targets.len(), 1);
        assert_eq!(targets[0].owner_fqn, "java.util.List");
        assert_eq!(targets[0].method_name, "size");
        assert_eq!(targets[0].reference_kind, ReferenceKind::InvokeInterface);
    }

    // -- Test 10: FQN conversion from internal format -------------------------

    #[test]
    fn fqn_conversion_internal_to_dotted() {
        let entries = vec![BootstrapMethodEntry {
            method: metafactory_handle(),
            arguments: lambda_bootstrap_args(
                CafeReferenceKind::InvokeStatic,
                "com/example/deeply/nested/ServiceImpl",
                "handle",
                "(Ljava/lang/Object;)V",
            ),
        }];

        let attrs = vec![AttributeInfo {
            name: Cow::Borrowed("BootstrapMethods"),
            data: AttributeData::BootstrapMethods(entries),
        }];

        let targets = extract_lambda_targets_from_attrs(&attrs);

        assert_eq!(targets.len(), 1);
        assert_eq!(
            targets[0].owner_fqn,
            "com.example.deeply.nested.ServiceImpl"
        );
    }

    // -- Test helper: extract from attributes without a full ClassFile ---------

    /// Helper that mirrors `extract_lambda_targets` but operates on a bare
    /// attribute slice so tests don't need to construct a full `ClassFile`.
    fn extract_lambda_targets_from_attrs(attrs: &[AttributeInfo<'_>]) -> Vec<LambdaTargetStub> {
        let mut targets = Vec::new();
        for attr in attrs {
            if let AttributeData::BootstrapMethods(entries) = &attr.data {
                for (idx, entry) in entries.iter().enumerate() {
                    if !is_lambda_metafactory(&entry.method) {
                        continue;
                    }
                    if let Some(stub) = extract_impl_handle(idx, &entry.arguments) {
                        targets.push(stub);
                    }
                }
            }
        }
        targets
    }
}