java2pyi 2.0.1

Java class files to Python stubs for mypy/pyright
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
pub mod types {
    use anyhow::Context;
    use bumpalo::Bump;

    #[derive(Debug, Clone, Copy)]
    pub enum BaseType {
        Byte,
        Char,
        Double,
        Float,
        Int,
        Long,
        Short,
        Boolean,
    }

    #[derive(Debug, Clone, Copy)]
    pub struct ObjectType<'a>(pub &'a [&'a str], pub &'a [SimpleObjectType<'a>]);

    #[derive(Debug, Clone, Copy)]
    pub struct SimpleObjectType<'a> {
        pub ident: &'a str,
        pub type_args: &'a [TypeArgument<'a>],
    }

    // public Map<T> var;
    //            ^
    //
    // public void example(Map<T> arg);
    //                         ^
    #[derive(Debug, Clone, Copy)]
    pub enum TypeArgument<'a> {
        Reference(ReferenceType<'a>),
        ParameterUnbound,
        ParameterUpperBound(ReferenceType<'a>),
        ParameterLowerBound(ReferenceType<'a>),
    }

    // Possible ReferenceType::Array values
    #[derive(Debug, Clone, Copy)]
    pub enum SignatureType<'a> {
        Base(BaseType),
        Reference(ReferenceType<'a>),
    }

    impl<'a> SignatureType<'a> {
        pub fn try_parse(bump: &'a Bump, signature: &'a str) -> anyhow::Result<Self> {
            let mut parser = super::Parser::new(bump, signature);
            let result = parser
                .parse_signature_type()
                .with_context(|| format!("failed to parse field signature {signature:?}"))?;
            parser.finish()?;
            Ok(result)
        }
    }

    #[derive(Debug, Clone, Copy)]
    pub enum ReferenceType<'a> {
        ParameterRef(&'a str),
        Object(ObjectType<'a>),
        Array(&'a SignatureType<'a>),
    }

    #[derive(Debug, Clone, Copy)]
    pub struct TypeParameter<'a> {
        pub name: &'a str,
        pub class_bound: Option<ReferenceType<'a>>,
        pub interface_bounds: &'a [ReferenceType<'a>],
    }

    #[derive(Debug, Clone, Copy)]
    pub struct MethodSignature<'a> {
        pub type_params: &'a [TypeParameter<'a>],
        pub args: &'a [SignatureType<'a>],
        pub return_type: Option<SignatureType<'a>>,
    }

    impl<'a> MethodSignature<'a> {
        pub fn try_parse(bump: &'a Bump, signature: &'a str) -> anyhow::Result<Self> {
            let mut parser = super::Parser::new(bump, signature);
            let result = parser
                .parse_method_signature()
                .with_context(|| format!("failed to parse method signature {signature:?}"))?;
            parser.finish()?;
            Ok(result)
        }
    }

    #[derive(Debug, Clone, Copy)]
    pub struct ClassSignature<'a> {
        pub type_params: &'a [TypeParameter<'a>],
        pub super_class: ObjectType<'a>,
        pub super_interfaces: &'a [ObjectType<'a>],
    }

    impl<'a> ClassSignature<'a> {
        pub fn try_parse(bump: &'a Bump, signature: &'a str) -> anyhow::Result<Self> {
            let mut parser = super::Parser::new(bump, signature);
            let result = parser
                .parse_class_signature()
                .with_context(|| format!("failed to parse class signature {signature:?}"))?;
            parser.finish()?;
            Ok(result)
        }
    }

    pub(super) fn bail_unexpected(pos: usize, input: &str, expected: &str) -> anyhow::Error {
        anyhow::anyhow!("expected {expected} at position {pos} in {input:?}")
    }
}

use bumpalo::Bump;
use bumpalo::collections::Vec as BumpVec;
use types::*;

struct Parser<'a> {
    bump: &'a Bump,
    input: &'a str,
    bytes: &'a [u8],
    pos: usize,
}

impl<'a> Parser<'a> {
    fn new(bump: &'a Bump, input: &'a str) -> Self {
        Self {
            bump,
            input,
            bytes: input.as_bytes(),
            pos: 0,
        }
    }

    fn finish(&self) -> anyhow::Result<()> {
        if self.pos != self.bytes.len() {
            anyhow::bail!(
                "trailing input at position {} in {:?}",
                self.pos,
                self.input
            );
        }
        Ok(())
    }

    fn peek(&self) -> Option<u8> {
        self.bytes.get(self.pos).copied()
    }

    fn bump(&mut self) -> Option<u8> {
        let c = self.peek()?;
        self.pos += 1;
        Some(c)
    }

    fn expect(&mut self, c: u8) -> anyhow::Result<()> {
        match self.bump() {
            Some(actual) if actual == c => Ok(()),
            _ => Err(types::bail_unexpected(
                self.pos,
                self.input,
                &format!("'{}'", c as char),
            )),
        }
    }

    fn is_separator(c: u8) -> bool {
        matches!(c, b'.' | b';' | b'[' | b'/' | b'<' | b'>' | b':')
    }

    fn parse_identifier(&mut self) -> anyhow::Result<&'a str> {
        let start = self.pos;

        while let Some(c) = self.peek() {
            if Self::is_separator(c) {
                break;
            }

            self.pos += 1;
        }

        if self.pos == start {
            return Err(types::bail_unexpected(self.pos, self.input, "identifier"));
        }

        Ok(&self.input[start..self.pos])
    }

    fn parse_base_type(&mut self) -> Option<BaseType> {
        let base_type = match self.peek()? {
            b'B' => BaseType::Byte,
            b'C' => BaseType::Char,
            b'D' => BaseType::Double,
            b'F' => BaseType::Float,
            b'I' => BaseType::Int,
            b'J' => BaseType::Long,
            b'S' => BaseType::Short,
            b'Z' => BaseType::Boolean,
            _ => return None,
        };

        self.pos += 1;
        Some(base_type)
    }

    fn parse_signature_type(&mut self) -> anyhow::Result<SignatureType<'a>> {
        if let Some(base_type) = self.parse_base_type() {
            return Ok(SignatureType::Base(base_type));
        }

        Ok(SignatureType::Reference(self.parse_reference_type()?))
    }

    fn parse_reference_type(&mut self) -> anyhow::Result<ReferenceType<'a>> {
        match self.peek() {
            Some(b'[') => {
                self.pos += 1;
                let inner = self.parse_signature_type()?;
                Ok(ReferenceType::Array(self.bump.alloc(inner)))
            }
            Some(b'L') => Ok(ReferenceType::Object(self.parse_object_type()?)),
            Some(b'T') => {
                self.pos += 1;
                let name = self.parse_identifier()?;
                self.expect(b';')?;
                Ok(ReferenceType::ParameterRef(name))
            }
            _ => Err(types::bail_unexpected(
                self.pos,
                self.input,
                "a reference type",
            )),
        }
    }

    fn parse_object_type(&mut self) -> anyhow::Result<ObjectType<'a>> {
        self.expect(b'L')?;

        let mut segments = BumpVec::new_in(self.bump);
        segments.push(self.parse_identifier()?);
        while self.peek() == Some(b'/') {
            self.pos += 1;
            segments.push(self.parse_identifier()?);
        }

        let ident = segments.pop().unwrap();
        let package = segments.into_bump_slice();

        let type_args = self.parse_optional_type_args()?;

        let mut types = BumpVec::new_in(self.bump);
        types.push(SimpleObjectType { ident, type_args });

        while self.peek() == Some(b'.') {
            self.pos += 1;
            let simple = self.parse_simple_object_type()?;
            types.push(simple);
        }

        self.expect(b';')?;

        Ok(ObjectType(package, types.into_bump_slice()))
    }

    fn parse_simple_object_type(&mut self) -> anyhow::Result<SimpleObjectType<'a>> {
        let ident = self.parse_identifier()?;
        let type_args = self.parse_optional_type_args()?;
        Ok(SimpleObjectType { ident, type_args })
    }

    fn parse_optional_type_args(&mut self) -> anyhow::Result<&'a [TypeArgument<'a>]> {
        if self.peek() != Some(b'<') {
            return Ok(&[]);
        }
        self.pos += 1;

        let mut args = BumpVec::new_in(self.bump);
        loop {
            let arg = self.parse_type_argument()?;
            args.push(arg);
            if self.peek() == Some(b'>') {
                self.pos += 1;
                break;
            }
        }

        Ok(args.into_bump_slice())
    }

    fn parse_type_argument(&mut self) -> anyhow::Result<TypeArgument<'a>> {
        match self.peek() {
            Some(b'*') => {
                self.pos += 1;
                Ok(TypeArgument::ParameterUnbound)
            }
            Some(b'+') => {
                self.pos += 1;
                Ok(TypeArgument::ParameterUpperBound(
                    self.parse_reference_type()?,
                ))
            }
            Some(b'-') => {
                self.pos += 1;
                Ok(TypeArgument::ParameterLowerBound(
                    self.parse_reference_type()?,
                ))
            }
            _ => Ok(TypeArgument::Reference(self.parse_reference_type()?)),
        }
    }

    fn parse_type_parameter(&mut self) -> anyhow::Result<TypeParameter<'a>> {
        let name = self.parse_identifier()?;
        self.expect(b':')?;

        let class_bound = match self.peek() {
            Some(b'L') | Some(b'T') | Some(b'[') => Some(self.parse_reference_type()?),
            _ => None,
        };

        let mut interface_bounds = BumpVec::new_in(self.bump);
        while self.peek() == Some(b':') {
            self.pos += 1;
            let bound = self.parse_reference_type()?;
            interface_bounds.push(bound);
        }

        Ok(TypeParameter {
            name,
            class_bound,
            interface_bounds: interface_bounds.into_bump_slice(),
        })
    }

    fn parse_type_parameters(&mut self) -> anyhow::Result<&'a [TypeParameter<'a>]> {
        if self.peek() != Some(b'<') {
            return Ok(&[]);
        }
        self.pos += 1;

        let mut params = BumpVec::new_in(self.bump);
        while self.peek() != Some(b'>') {
            let param = self.parse_type_parameter()?;
            params.push(param);
        }
        self.pos += 1;

        Ok(params.into_bump_slice())
    }

    fn parse_method_signature(&mut self) -> anyhow::Result<MethodSignature<'a>> {
        let type_params = self.parse_type_parameters()?;

        self.expect(b'(')?;
        let mut args = BumpVec::new_in(self.bump);
        while self.peek() != Some(b')') {
            let arg = self.parse_signature_type()?;
            args.push(arg);
        }
        self.pos += 1;

        let return_type = if self.peek() == Some(b'V') {
            self.pos += 1;
            None
        } else {
            Some(self.parse_signature_type()?)
        };

        while self.peek() == Some(b'^') {
            self.pos += 1;
            self.parse_reference_type()?;
        }

        Ok(MethodSignature {
            type_params,
            args: args.into_bump_slice(),
            return_type,
        })
    }

    fn parse_class_signature(&mut self) -> anyhow::Result<ClassSignature<'a>> {
        let type_params = self.parse_type_parameters()?;
        let super_class = self.parse_object_type()?;

        let mut super_interfaces = BumpVec::new_in(self.bump);
        while self.peek() == Some(b'L') {
            let iface = self.parse_object_type()?;
            super_interfaces.push(iface);
        }

        Ok(ClassSignature {
            type_params,
            super_class,
            super_interfaces: super_interfaces.into_bump_slice(),
        })
    }
}

#[cfg(test)]
mod tests {
    use bumpalo::Bump;

    use crate::ir::Type;

    use super::*;

    fn field(descriptor: &str) -> String {
        let bump = Bump::new();
        let descriptor = bump.alloc_str(descriptor);

        Type::from_sign(&bump, SignatureType::try_parse(&bump, descriptor).unwrap()).to_string()
    }

    #[test]
    fn field_descriptors_become_types() {
        assert_eq!(field("I"), "int");
        assert_eq!(field("[I"), "int[]");
        assert_eq!(field("Ljava/lang/String;"), "java.lang.String");
        assert_eq!(field("[[Ljava/lang/String;"), "java.lang.String[][]");
    }

    #[test]
    fn nested_names_become_path_steps() {
        assert_eq!(field("Ljava/util/Map$Entry;"), "java.util.Map.Entry");
        assert_eq!(field("LMain$1$Inner;"), "Main.Main_1.Inner");
    }

    #[test]
    fn anonymous_names_keep_the_flattened_identifier() {
        assert_eq!(field("LMain$1;"), "Main.Main_1");
        assert_eq!(field("LMain$1$2;"), "Main.Main_1.Main_1_2");
        assert_eq!(field("La/b/Main$1;"), "a.b.Main.Main_1");
    }

    #[test]
    fn obfuscated_names_are_accepted() {
        assert_eq!(field("La/b/1c;"), "a.b.1c");
    }

    #[test]
    fn type_arguments_stay_on_the_part_they_were_written_on() {
        assert_eq!(
            field("Ljava/util/Map<Ljava/lang/String;[I>;"),
            "java.util.Map<java.lang.String, int[]>"
        );
        assert_eq!(
            field("Ljava/util/Map<TK;TV;>.Entry<TK;TV;>;"),
            "java.util.Map<K, V>.Entry<K, V>"
        );
    }

    #[test]
    fn wildcards_keep_their_boundary() {
        assert_eq!(field("Ljava/util/List<*>;"), "java.util.List<?>");
        assert_eq!(
            field("Ljava/util/List<+Ljava/lang/Number;>;"),
            "java.util.List<? extends java.lang.Number>"
        );
        assert_eq!(
            field("Ljava/util/List<-Ljava/lang/Number;>;"),
            "java.util.List<? super java.lang.Number>"
        );
    }

    #[test]
    fn method_descriptors_split_into_arguments_and_a_return_type() {
        let bump = Bump::new();

        let signature = MethodSignature::try_parse(&bump, "(I[Ljava/lang/String;)V").unwrap();

        assert!(signature.type_params.is_empty());
        assert_eq!(
            signature
                .args
                .iter()
                .map(|arg| Type::from_sign(&bump, *arg).to_string())
                .collect::<Vec<_>>()
                .join(", "),
            "int, java.lang.String[]"
        );
        assert!(signature.return_type.is_none());
    }

    #[test]
    fn method_signatures_carry_type_parameters_and_throws() {
        let bump = Bump::new();

        let signature = MethodSignature::try_parse(
            &bump,
            "<T:Ljava/lang/Object;>(TT;)TT;^Ljava/io/IOException;",
        )
        .unwrap();

        assert_eq!(signature.type_params.len(), 1);
        assert_eq!(signature.type_params[0].name, "T");
        assert_eq!(Type::from_sign(&bump, signature.args[0]).to_string(), "T");
        assert_eq!(
            Type::from_sign(&bump, signature.return_type.unwrap()).to_string(),
            "T"
        );
    }

    #[test]
    fn class_signatures_split_into_bounds_and_supertypes() {
        let bump = Bump::new();

        let signature = ClassSignature::try_parse(
            &bump,
            "<K::Ljava/lang/Comparable<TK;>;>Ljava/lang/Object;Ljava/util/List<TK;>;",
        )
        .unwrap();

        assert_eq!(signature.type_params.len(), 1);
        assert_eq!(signature.type_params[0].name, "K");
        assert_eq!(
            Type::from_object(&bump, signature.super_class).to_string(),
            "java.lang.Object"
        );
        assert_eq!(
            Type::from_object(&bump, signature.super_interfaces[0]).to_string(),
            "java.util.List<K>"
        );
    }

    #[test]
    fn trailing_input_is_rejected() {
        let bump = Bump::new();

        assert!(SignatureType::try_parse(&bump, "II").is_err());
        assert!(SignatureType::try_parse(&bump, "Ljava/lang/String").is_err());
    }
}