Skip to main content

jvm_assembler/builder/
mod.rs

1use crate::program::{JvmAccessFlags, JvmExceptionHandler, JvmInstruction, JvmMethod, JvmProgram, JvmVersion};
2
3/// JVM program builder
4pub struct JvmProgramBuilder {
5    program: JvmProgram,
6}
7
8impl JvmProgramBuilder {
9    /// Creates a new JVM program builder with the given class name
10    pub fn new(name: impl Into<String>) -> Self {
11        Self { program: JvmProgram::new(name.into()) }
12    }
13
14    /// Sets the JVM version for the program
15    pub fn with_version(mut self, major: u16, minor: u16) -> Self {
16        self.program.version = JvmVersion { major, minor };
17        self
18    }
19
20    /// Sets the access flags for the program
21    pub fn with_access_flags(mut self, flags: JvmAccessFlags) -> Self {
22        self.program.access_flags = flags;
23        self
24    }
25
26    /// Sets the program as public
27    pub fn with_public(mut self) -> Self {
28        self.program.access_flags.is_public = true;
29        self
30    }
31
32    /// Sets the super class for the program
33    pub fn with_super_class(mut self, super_class: impl Into<String>) -> Self {
34        self.program.super_class = Some(super_class.into());
35        self
36    }
37
38    /// Adds an interface to the program
39    pub fn add_interface(mut self, interface: impl Into<String>) -> Self {
40        self.program.interfaces.push(interface.into());
41        self
42    }
43
44    /// Sets the source file for the program
45    pub fn with_source_file(mut self, source_file: impl Into<String>) -> Self {
46        self.program.source_file = Some(source_file.into());
47        self
48    }
49
50    /// Adds a method to the program using a builder closure
51    pub fn add_method<F>(mut self, name: impl Into<String>, descriptor: impl Into<String>, f: F) -> Self
52    where
53        F: FnOnce(JvmMethodBuilder) -> JvmMethodBuilder,
54    {
55        let builder = JvmMethodBuilder::new(name.into(), descriptor.into());
56        let method = f(builder).build();
57        self.program.add_method(method);
58        self
59    }
60
61    /// Builds and returns the JVM program
62    pub fn build(self) -> JvmProgram {
63        self.program
64    }
65}
66
67/// JVM method builder
68pub struct JvmMethodBuilder {
69    method: JvmMethod,
70}
71
72impl JvmMethodBuilder {
73    /// Creates a new JVM method builder with the given name and descriptor
74    pub fn new(name: String, descriptor: String) -> Self {
75        Self { method: JvmMethod::new(name, descriptor) }
76    }
77
78    /// Sets the method as public
79    pub fn with_public(mut self) -> Self {
80        self.method.access_flags.is_public = true;
81        self
82    }
83
84    /// Sets the method as private
85    pub fn with_private(mut self) -> Self {
86        self.method.access_flags.is_private = true;
87        self
88    }
89
90    /// Sets the method as protected
91    pub fn with_protected(mut self) -> Self {
92        self.method.access_flags.is_protected = true;
93        self
94    }
95
96    /// Sets the method as static
97    pub fn with_static(mut self) -> Self {
98        self.method.access_flags.is_static = true;
99        self
100    }
101
102    /// Sets the method as final
103    pub fn with_final(mut self) -> Self {
104        self.method.access_flags.is_final = true;
105        self
106    }
107
108    /// Sets the method as synchronized
109    pub fn with_synchronized(mut self) -> Self {
110        self.method.access_flags.is_synchronized = true;
111        self
112    }
113
114    /// Sets the maximum stack size for the method
115    pub fn with_max_stack(mut self, max_stack: u16) -> Self {
116        self.method.max_stack = max_stack;
117        self
118    }
119
120    /// Sets the maximum number of local variables for the method
121    pub fn with_max_locals(mut self, max_locals: u16) -> Self {
122        self.method.max_locals = max_locals;
123        self
124    }
125
126    /// Adds an instruction to the method
127    pub fn add_instruction(mut self, instruction: JvmInstruction) -> Self {
128        self.method.add_instruction(instruction);
129        self
130    }
131
132    /// Adds an exception handler to the method
133    pub fn add_exception_handler(mut self, handler: JvmExceptionHandler) -> Self {
134        self.method.add_exception_handler(handler);
135        self
136    }
137
138    /// Adds a thrown exception to the method
139    pub fn add_exception(mut self, exception: impl Into<String>) -> Self {
140        self.method.add_exception(exception.into());
141        self
142    }
143
144    // Convenience methods for common instructions
145    /// Adds a NOP instruction
146    pub fn nop(self) -> Self {
147        self.add_instruction(JvmInstruction::Nop)
148    }
149    /// Adds an ACONST_NULL instruction
150    pub fn aconst_null(self) -> Self {
151        self.add_instruction(JvmInstruction::AconstNull)
152    }
153    /// Adds an ICONST_M1 instruction
154    pub fn iconst_m1(self) -> Self {
155        self.add_instruction(JvmInstruction::IconstM1)
156    }
157    /// Adds an ICONST_0 instruction
158    pub fn iconst_0(self) -> Self {
159        self.add_instruction(JvmInstruction::Iconst0)
160    }
161    /// Adds an ICONST_1 instruction
162    pub fn iconst_1(self) -> Self {
163        self.add_instruction(JvmInstruction::Iconst1)
164    }
165    /// Adds an ICONST_2 instruction
166    pub fn iconst_2(self) -> Self {
167        self.add_instruction(JvmInstruction::Iconst2)
168    }
169    /// Adds an ICONST_3 instruction
170    pub fn iconst_3(self) -> Self {
171        self.add_instruction(JvmInstruction::Iconst3)
172    }
173    /// Adds an ICONST_4 instruction
174    pub fn iconst_4(self) -> Self {
175        self.add_instruction(JvmInstruction::Iconst4)
176    }
177    /// Adds an ICONST_5 instruction
178    pub fn iconst_5(self) -> Self {
179        self.add_instruction(JvmInstruction::Iconst5)
180    }
181    /// Adds an LCONST_0 instruction
182    pub fn lconst_0(self) -> Self {
183        self.add_instruction(JvmInstruction::Lconst0)
184    }
185    /// Adds an LCONST_1 instruction
186    pub fn lconst_1(self) -> Self {
187        self.add_instruction(JvmInstruction::Lconst1)
188    }
189    /// Adds an FCONST_0 instruction
190    pub fn fconst_0(self) -> Self {
191        self.add_instruction(JvmInstruction::Fconst0)
192    }
193    /// Adds an FCONST_1 instruction
194    pub fn fconst_1(self) -> Self {
195        self.add_instruction(JvmInstruction::Fconst1)
196    }
197    /// Adds an FCONST_2 instruction
198    pub fn fconst_2(self) -> Self {
199        self.add_instruction(JvmInstruction::Fconst2)
200    }
201    /// Adds a DCONST_0 instruction
202    pub fn dconst_0(self) -> Self {
203        self.add_instruction(JvmInstruction::Dconst0)
204    }
205    /// Adds a DCONST_1 instruction
206    pub fn dconst_1(self) -> Self {
207        self.add_instruction(JvmInstruction::Dconst1)
208    }
209
210    /// Adds a BIPUSH instruction
211    pub fn bipush(self, value: i8) -> Self {
212        self.add_instruction(JvmInstruction::Bipush { value })
213    }
214    /// Adds a SIPUSH instruction
215    pub fn sipush(self, value: i16) -> Self {
216        self.add_instruction(JvmInstruction::Sipush { value })
217    }
218    /// Adds an LDC instruction
219    pub fn ldc(self, symbol: impl Into<String>) -> Self {
220        self.add_instruction(JvmInstruction::Ldc { symbol: symbol.into() })
221    }
222
223    /// Adds an ILOAD instruction
224    pub fn iload(self, index: u16) -> Self {
225        self.add_instruction(JvmInstruction::Iload { index })
226    }
227    /// Adds an LLOAD instruction
228    pub fn lload(self, index: u16) -> Self {
229        self.add_instruction(JvmInstruction::Lload { index })
230    }
231    /// Adds an FLOAD instruction
232    pub fn fload(self, index: u16) -> Self {
233        self.add_instruction(JvmInstruction::Fload { index })
234    }
235    /// Adds a DLOAD instruction
236    pub fn dload(self, index: u16) -> Self {
237        self.add_instruction(JvmInstruction::Dload { index })
238    }
239    /// Adds an ALOAD instruction
240    pub fn aload(self, index: u16) -> Self {
241        self.add_instruction(JvmInstruction::Aload { index })
242    }
243    /// Adds an ILOAD_0 instruction
244    pub fn iload_0(self) -> Self {
245        self.add_instruction(JvmInstruction::Iload0)
246    }
247    /// Adds an ILOAD_1 instruction
248    pub fn iload_1(self) -> Self {
249        self.add_instruction(JvmInstruction::Iload1)
250    }
251    /// Adds an ILOAD_2 instruction
252    pub fn iload_2(self) -> Self {
253        self.add_instruction(JvmInstruction::Iload2)
254    }
255    /// Adds an ILOAD_3 instruction
256    pub fn iload_3(self) -> Self {
257        self.add_instruction(JvmInstruction::Iload3)
258    }
259    /// Adds an LLOAD_0 instruction
260    pub fn lload_0(self) -> Self {
261        self.add_instruction(JvmInstruction::Lload0)
262    }
263    /// Adds an LLOAD_1 instruction
264    pub fn lload_1(self) -> Self {
265        self.add_instruction(JvmInstruction::Lload1)
266    }
267    /// Adds an LLOAD_2 instruction
268    pub fn lload_2(self) -> Self {
269        self.add_instruction(JvmInstruction::Lload2)
270    }
271    /// Adds an LLOAD_3 instruction
272    pub fn lload_3(self) -> Self {
273        self.add_instruction(JvmInstruction::Lload3)
274    }
275    /// Adds an FLOAD_0 instruction
276    pub fn fload_0(self) -> Self {
277        self.add_instruction(JvmInstruction::Fload0)
278    }
279    /// Adds an FLOAD_1 instruction
280    pub fn fload_1(self) -> Self {
281        self.add_instruction(JvmInstruction::Fload1)
282    }
283    /// Adds an FLOAD_2 instruction
284    pub fn fload_2(self) -> Self {
285        self.add_instruction(JvmInstruction::Fload2)
286    }
287    /// Adds an FLOAD_3 instruction
288    pub fn fload_3(self) -> Self {
289        self.add_instruction(JvmInstruction::Fload3)
290    }
291    /// Adds a DLOAD_0 instruction
292    pub fn dload_0(self) -> Self {
293        self.add_instruction(JvmInstruction::Dload0)
294    }
295    /// Adds a DLOAD_1 instruction
296    pub fn dload_1(self) -> Self {
297        self.add_instruction(JvmInstruction::Dload1)
298    }
299    /// Adds a DLOAD_2 instruction
300    pub fn dload_2(self) -> Self {
301        self.add_instruction(JvmInstruction::Dload2)
302    }
303    /// Adds a DLOAD_3 instruction
304    pub fn dload_3(self) -> Self {
305        self.add_instruction(JvmInstruction::Dload3)
306    }
307    /// Adds an ALOAD_0 instruction
308    pub fn aload_0(self) -> Self {
309        self.add_instruction(JvmInstruction::Aload0)
310    }
311    /// Adds an ALOAD_1 instruction
312    pub fn aload_1(self) -> Self {
313        self.add_instruction(JvmInstruction::Aload1)
314    }
315    /// Adds an ALOAD_2 instruction
316    pub fn aload_2(self) -> Self {
317        self.add_instruction(JvmInstruction::Aload2)
318    }
319    /// Adds an ALOAD_3 instruction
320    pub fn aload_3(self) -> Self {
321        self.add_instruction(JvmInstruction::Aload3)
322    }
323
324    /// Adds an ISTORE instruction
325    pub fn istore(self, index: u16) -> Self {
326        self.add_instruction(JvmInstruction::Istore { index })
327    }
328    /// Adds an LSTORE instruction
329    pub fn lstore(self, index: u16) -> Self {
330        self.add_instruction(JvmInstruction::Lstore { index })
331    }
332    /// Adds an FSTORE instruction
333    pub fn fstore(self, index: u16) -> Self {
334        self.add_instruction(JvmInstruction::Fstore { index })
335    }
336    /// Adds a DSTORE instruction
337    pub fn dstore(self, index: u16) -> Self {
338        self.add_instruction(JvmInstruction::Dstore { index })
339    }
340    /// Adds an ASTORE instruction
341    pub fn astore(self, index: u16) -> Self {
342        self.add_instruction(JvmInstruction::Astore { index })
343    }
344    /// Adds an ISTORE_0 instruction
345    pub fn istore_0(self) -> Self {
346        self.add_instruction(JvmInstruction::Istore0)
347    }
348    /// Adds an ISTORE_1 instruction
349    pub fn istore_1(self) -> Self {
350        self.add_instruction(JvmInstruction::Istore1)
351    }
352    /// Adds an ISTORE_2 instruction
353    pub fn istore_2(self) -> Self {
354        self.add_instruction(JvmInstruction::Istore2)
355    }
356    /// Adds an ISTORE_3 instruction
357    pub fn istore_3(self) -> Self {
358        self.add_instruction(JvmInstruction::Istore3)
359    }
360    /// Adds an LSTORE_0 instruction
361    pub fn lstore_0(self) -> Self {
362        self.add_instruction(JvmInstruction::Lstore0)
363    }
364    /// Adds an LSTORE_1 instruction
365    pub fn lstore_1(self) -> Self {
366        self.add_instruction(JvmInstruction::Lstore1)
367    }
368    /// Adds an LSTORE_2 instruction
369    pub fn lstore_2(self) -> Self {
370        self.add_instruction(JvmInstruction::Lstore2)
371    }
372    /// Adds an LSTORE_3 instruction
373    pub fn lstore_3(self) -> Self {
374        self.add_instruction(JvmInstruction::Lstore3)
375    }
376    /// Adds an FSTORE_0 instruction
377    pub fn fstore_0(self) -> Self {
378        self.add_instruction(JvmInstruction::Fstore0)
379    }
380    /// Adds an FSTORE_1 instruction
381    pub fn fstore_1(self) -> Self {
382        self.add_instruction(JvmInstruction::Fstore1)
383    }
384    /// Adds an FSTORE_2 instruction
385    pub fn fstore_2(self) -> Self {
386        self.add_instruction(JvmInstruction::Fstore2)
387    }
388    /// Adds an FSTORE_3 instruction
389    pub fn fstore_3(self) -> Self {
390        self.add_instruction(JvmInstruction::Fstore3)
391    }
392    /// Adds a DSTORE_0 instruction
393    pub fn dstore_0(self) -> Self {
394        self.add_instruction(JvmInstruction::Dstore0)
395    }
396    /// Adds a DSTORE_1 instruction
397    pub fn dstore_1(self) -> Self {
398        self.add_instruction(JvmInstruction::Dstore1)
399    }
400    /// Adds a DSTORE_2 instruction
401    pub fn dstore_2(self) -> Self {
402        self.add_instruction(JvmInstruction::Dstore2)
403    }
404    /// Adds a DSTORE_3 instruction
405    pub fn dstore_3(self) -> Self {
406        self.add_instruction(JvmInstruction::Dstore3)
407    }
408    /// Adds an ASTORE_0 instruction
409    pub fn astore_0(self) -> Self {
410        self.add_instruction(JvmInstruction::Astore0)
411    }
412    /// Adds an ASTORE_1 instruction
413    pub fn astore_1(self) -> Self {
414        self.add_instruction(JvmInstruction::Astore1)
415    }
416    /// Adds an ASTORE_2 instruction
417    pub fn astore_2(self) -> Self {
418        self.add_instruction(JvmInstruction::Astore2)
419    }
420    /// Adds an ASTORE_3 instruction
421    pub fn astore_3(self) -> Self {
422        self.add_instruction(JvmInstruction::Astore3)
423    }
424
425    /// Adds an IADD instruction
426    pub fn iadd(self) -> Self {
427        self.add_instruction(JvmInstruction::Iadd)
428    }
429    /// Adds an LADD instruction
430    pub fn ladd(self) -> Self {
431        self.add_instruction(JvmInstruction::Ladd)
432    }
433    /// Adds an FADD instruction
434    pub fn fadd(self) -> Self {
435        self.add_instruction(JvmInstruction::Fadd)
436    }
437    /// Adds a DADD instruction
438    pub fn dadd(self) -> Self {
439        self.add_instruction(JvmInstruction::Dadd)
440    }
441    /// Adds an ISUB instruction
442    pub fn isub(self) -> Self {
443        self.add_instruction(JvmInstruction::Isub)
444    }
445    /// Adds an LSUB instruction
446    pub fn lsub(self) -> Self {
447        self.add_instruction(JvmInstruction::Lsub)
448    }
449    /// Adds an FSUB instruction
450    pub fn fsub(self) -> Self {
451        self.add_instruction(JvmInstruction::Fsub)
452    }
453    /// Adds a DSUB instruction
454    pub fn dsub(self) -> Self {
455        self.add_instruction(JvmInstruction::Dsub)
456    }
457    /// Adds an IMUL instruction
458    pub fn imul(self) -> Self {
459        self.add_instruction(JvmInstruction::Imul)
460    }
461    /// Adds an LMUL instruction
462    pub fn lmul(self) -> Self {
463        self.add_instruction(JvmInstruction::Lmul)
464    }
465    /// Adds an FMUL instruction
466    pub fn fmul(self) -> Self {
467        self.add_instruction(JvmInstruction::Fmul)
468    }
469    /// Adds a DMUL instruction
470    pub fn dmul(self) -> Self {
471        self.add_instruction(JvmInstruction::Dmul)
472    }
473    /// Adds an IDIV instruction
474    pub fn idiv(self) -> Self {
475        self.add_instruction(JvmInstruction::Idiv)
476    }
477    /// Adds an LDIV instruction
478    pub fn ldiv(self) -> Self {
479        self.add_instruction(JvmInstruction::Ldiv)
480    }
481    /// Adds an FDIV instruction
482    pub fn fdiv(self) -> Self {
483        self.add_instruction(JvmInstruction::Fdiv)
484    }
485    /// Adds a DDIV instruction
486    pub fn ddiv(self) -> Self {
487        self.add_instruction(JvmInstruction::Ddiv)
488    }
489    /// Adds an IREM instruction
490    pub fn irem(self) -> Self {
491        self.add_instruction(JvmInstruction::Irem)
492    }
493    /// Adds an LREM instruction
494    pub fn lrem(self) -> Self {
495        self.add_instruction(JvmInstruction::Lrem)
496    }
497    /// Adds an FREM instruction
498    pub fn frem(self) -> Self {
499        self.add_instruction(JvmInstruction::Frem)
500    }
501    /// Adds a DREM instruction
502    pub fn drem(self) -> Self {
503        self.add_instruction(JvmInstruction::Drem)
504    }
505    /// Adds an INEG instruction
506    pub fn ineg(self) -> Self {
507        self.add_instruction(JvmInstruction::Ineg)
508    }
509    /// Adds an LNEG instruction
510    pub fn lneg(self) -> Self {
511        self.add_instruction(JvmInstruction::Lneg)
512    }
513    /// Adds an FNEG instruction
514    pub fn fneg(self) -> Self {
515        self.add_instruction(JvmInstruction::Fneg)
516    }
517    /// Adds a DNEG instruction
518    pub fn dneg(self) -> Self {
519        self.add_instruction(JvmInstruction::Dneg)
520    }
521
522    /// Adds an ISHL instruction
523    pub fn ishl(self) -> Self {
524        self.add_instruction(JvmInstruction::Ishl)
525    }
526    /// Adds an LSHL instruction
527    pub fn lshl(self) -> Self {
528        self.add_instruction(JvmInstruction::Lshl)
529    }
530    /// Adds an ISHR instruction
531    pub fn ishr(self) -> Self {
532        self.add_instruction(JvmInstruction::Ishr)
533    }
534    /// Adds an LSHR instruction
535    pub fn lshr(self) -> Self {
536        self.add_instruction(JvmInstruction::Lshr)
537    }
538    /// Adds an IUSHR instruction
539    pub fn iushr(self) -> Self {
540        self.add_instruction(JvmInstruction::Iushr)
541    }
542    /// Adds an LUSHR instruction
543    pub fn lushr(self) -> Self {
544        self.add_instruction(JvmInstruction::Lushr)
545    }
546    /// Adds an IAND instruction
547    pub fn iand(self) -> Self {
548        self.add_instruction(JvmInstruction::Iand)
549    }
550    /// Adds a LAND instruction
551    pub fn land(self) -> Self {
552        self.add_instruction(JvmInstruction::Land)
553    }
554    /// Adds an IOR instruction
555    pub fn ior(self) -> Self {
556        self.add_instruction(JvmInstruction::Ior)
557    }
558    /// Adds a LOR instruction
559    pub fn lor(self) -> Self {
560        self.add_instruction(JvmInstruction::Lor)
561    }
562    /// Adds an IXOR instruction
563    pub fn ixor(self) -> Self {
564        self.add_instruction(JvmInstruction::Ixor)
565    }
566    /// Adds an LXOR instruction
567    pub fn lxor(self) -> Self {
568        self.add_instruction(JvmInstruction::Lxor)
569    }
570
571    /// Adds an LCMP instruction
572    pub fn lcmp(self) -> Self {
573        self.add_instruction(JvmInstruction::Lcmp)
574    }
575    /// Adds an FCMPL instruction
576    pub fn fcmpl(self) -> Self {
577        self.add_instruction(JvmInstruction::Fcmpl)
578    }
579    /// Adds an FCMPG instruction
580    pub fn fcmpg(self) -> Self {
581        self.add_instruction(JvmInstruction::Fcmpg)
582    }
583    /// Adds a DCMPL instruction
584    pub fn dcmpl(self) -> Self {
585        self.add_instruction(JvmInstruction::Dcmpl)
586    }
587    /// Adds a DCMPG instruction
588    pub fn dcmpg(self) -> Self {
589        self.add_instruction(JvmInstruction::Dcmpg)
590    }
591
592    /// Adds an IINC instruction
593    pub fn iinc(self, index: u16, value: i16) -> Self {
594        self.add_instruction(JvmInstruction::Iinc { index, value })
595    }
596
597    /// Adds a GETSTATIC instruction
598    pub fn getstatic(self, class: impl Into<String>, name: impl Into<String>, desc: impl Into<String>) -> Self {
599        self.add_instruction(JvmInstruction::Getstatic {
600            class_name: class.into(),
601            field_name: name.into(),
602            descriptor: desc.into(),
603        })
604    }
605
606    /// Adds a PUTSTATIC instruction
607    pub fn putstatic(self, class: impl Into<String>, name: impl Into<String>, desc: impl Into<String>) -> Self {
608        self.add_instruction(JvmInstruction::Putstatic {
609            class_name: class.into(),
610            field_name: name.into(),
611            descriptor: desc.into(),
612        })
613    }
614
615    /// Adds a GETFIELD instruction
616    pub fn getfield(self, class: impl Into<String>, name: impl Into<String>, desc: impl Into<String>) -> Self {
617        self.add_instruction(JvmInstruction::Getfield {
618            class_name: class.into(),
619            field_name: name.into(),
620            descriptor: desc.into(),
621        })
622    }
623
624    /// Adds a PUTFIELD instruction
625    pub fn putfield(self, class: impl Into<String>, name: impl Into<String>, desc: impl Into<String>) -> Self {
626        self.add_instruction(JvmInstruction::Putfield {
627            class_name: class.into(),
628            field_name: name.into(),
629            descriptor: desc.into(),
630        })
631    }
632
633    /// Adds an INVOKEVIRTUAL instruction
634    pub fn invokevirtual(self, class: impl Into<String>, name: impl Into<String>, desc: impl Into<String>) -> Self {
635        self.add_instruction(JvmInstruction::Invokevirtual {
636            class_name: class.into(),
637            method_name: name.into(),
638            descriptor: desc.into(),
639        })
640    }
641
642    /// Adds an INVOKESPECIAL instruction
643    pub fn invokespecial(self, class: impl Into<String>, name: impl Into<String>, desc: impl Into<String>) -> Self {
644        self.add_instruction(JvmInstruction::Invokespecial {
645            class_name: class.into(),
646            method_name: name.into(),
647            descriptor: desc.into(),
648        })
649    }
650
651    /// Adds an INVOKESTATIC instruction
652    pub fn invokestatic(self, class: impl Into<String>, name: impl Into<String>, desc: impl Into<String>) -> Self {
653        self.add_instruction(JvmInstruction::Invokestatic {
654            class_name: class.into(),
655            method_name: name.into(),
656            descriptor: desc.into(),
657        })
658    }
659
660    /// Adds an INVOKEINTERFACE instruction
661    pub fn invokeinterface(self, class: impl Into<String>, name: impl Into<String>, desc: impl Into<String>) -> Self {
662        self.add_instruction(JvmInstruction::Invokeinterface {
663            class_name: class.into(),
664            method_name: name.into(),
665            descriptor: desc.into(),
666        })
667    }
668
669    /// Adds a NEW instruction
670    pub fn new_instance(self, class: impl Into<String>) -> Self {
671        self.add_instruction(JvmInstruction::New { class_name: class.into() })
672    }
673
674    /// Adds a DUP instruction
675    pub fn dup(self) -> Self {
676        self.add_instruction(JvmInstruction::Dup)
677    }
678    /// Adds a DUP_X1 instruction
679    pub fn dup_x1(self) -> Self {
680        self.add_instruction(JvmInstruction::DupX1)
681    }
682    /// Adds a DUP_X2 instruction
683    pub fn dup_x2(self) -> Self {
684        self.add_instruction(JvmInstruction::DupX2)
685    }
686    /// Adds a DUP2 instruction
687    pub fn dup2(self) -> Self {
688        self.add_instruction(JvmInstruction::Dup2)
689    }
690    /// Adds a DUP2_X1 instruction
691    pub fn dup2_x1(self) -> Self {
692        self.add_instruction(JvmInstruction::Dup2X1)
693    }
694    /// Adds a DUP2_X2 instruction
695    pub fn dup2_x2(self) -> Self {
696        self.add_instruction(JvmInstruction::Dup2X2)
697    }
698    /// Adds a SWAP instruction
699    pub fn swap(self) -> Self {
700        self.add_instruction(JvmInstruction::Swap)
701    }
702    /// Adds a POP instruction
703    pub fn pop(self) -> Self {
704        self.add_instruction(JvmInstruction::Pop)
705    }
706    /// Adds a POP2 instruction
707    pub fn pop2(self) -> Self {
708        self.add_instruction(JvmInstruction::Pop2)
709    }
710
711    /// Adds a RETURN instruction
712    pub fn return_void(self) -> Self {
713        self.add_instruction(JvmInstruction::Return)
714    }
715    /// Adds an IRETURN instruction
716    pub fn ireturn(self) -> Self {
717        self.add_instruction(JvmInstruction::Ireturn)
718    }
719    /// Adds an LRETURN instruction
720    pub fn lreturn(self) -> Self {
721        self.add_instruction(JvmInstruction::Lreturn)
722    }
723    /// Adds an FRETURN instruction
724    pub fn freturn(self) -> Self {
725        self.add_instruction(JvmInstruction::Freturn)
726    }
727    /// Adds a DRETURN instruction
728    pub fn dreturn(self) -> Self {
729        self.add_instruction(JvmInstruction::Dreturn)
730    }
731    /// Adds an ARETURN instruction
732    pub fn areturn(self) -> Self {
733        self.add_instruction(JvmInstruction::Areturn)
734    }
735
736    /// Adds a label instruction
737    pub fn label(self, name: impl Into<String>) -> Self {
738        self.add_instruction(JvmInstruction::Label { name: name.into() })
739    }
740
741    /// Adds a GOTO instruction
742    pub fn goto(self, target: impl Into<String>) -> Self {
743        self.add_instruction(JvmInstruction::Goto { target: target.into() })
744    }
745
746    /// Adds an IFEQ instruction
747    pub fn ifeq(self, target: impl Into<String>) -> Self {
748        self.add_instruction(JvmInstruction::Ifeq { target: target.into() })
749    }
750
751    /// Adds an IFNE instruction
752    pub fn ifne(self, target: impl Into<String>) -> Self {
753        self.add_instruction(JvmInstruction::Ifne { target: target.into() })
754    }
755
756    /// Builds and returns the JVM method
757    pub fn build(self) -> JvmMethod {
758        self.method
759    }
760}