File: hworld.java

package info (click to toggle)
jasmin-sable 2.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,232 kB
  • ctags: 1,458
  • sloc: java: 9,167; xml: 109; makefile: 19; csh: 16; sh: 1
file content (77 lines) | stat: -rw-r--r-- 2,690 bytes parent folder | download | duplicates (7)
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
import jas.*;
import java.io.*;

//
// This is program that makes calls into the jas package
// to generate a class that prints a string afew times.
//

class hworld implements RuntimeConstants
{
  public static void main(String argv[])
    throws jasError, IOException
  {

                                // class hierarchy
    ClassEnv nclass = new ClassEnv();
    nclass.setClass(new ClassCP("out"));
    nclass.setSuperClass(new ClassCP("java/lang/Object"));
    nclass.setClassAccess((short)ACC_PUBLIC);

                                // Initialization code

    CodeAttr init = new CodeAttr();
    init.addInsn(new Insn(opc_aload_0));
    init.addInsn(new Insn(opc_invokenonvirtual,
                          new MethodCP("java/lang/Object", "<init>", "()V")));
    init.addInsn(new Insn(opc_return));


                                // Actual code to print string
    CodeAttr doit = new CodeAttr();

                                // store refs in local variables
    doit.addInsn(new Insn(opc_getstatic,
                          new FieldCP("java/lang/System",
                                      "out",
                                      "Ljava/io/PrintStream;")));
    doit.addInsn(new Insn(opc_astore_1));
    doit.addInsn(new Insn(opc_ldc,
                          new StringCP("Hello World")));
    doit.addInsn(new Insn(opc_astore_2));

                                // Loop index in var reg 3
    doit.addInsn(new Insn(opc_bipush, 5));
    doit.addInsn(new Insn(opc_istore_3));

                                // Start the loop
    Label loop = new Label("loop");
    doit.addInsn(loop);
    doit.addInsn(new Insn(opc_aload_1));
    doit.addInsn(new Insn(opc_aload_2));
    doit.addInsn(new Insn(opc_invokevirtual,
                          new MethodCP("java/io/PrintStream",
                                       "println",
                                       "(Ljava/lang/String;)V")));
    doit.addInsn(new IincInsn(3, -1));
    doit.addInsn(new Insn(opc_iload_3));
    doit.addInsn(new Insn(opc_ifne, loop));
    doit.addInsn(new Insn(opc_return));

                                // set the right sizes for code
    doit.setStackSize((short)3); doit.setVarSize((short)4);

                                // Add the init code to the class.
    nclass.addMethod((short)ACC_PUBLIC, "<init>", "()V", init, null);

                                // Add the printing code
    nclass.addMethod((short)(ACC_PUBLIC|ACC_STATIC), "main",
                     "([Ljava/lang/String;)V", doit, null);

                                // write it all out
    nclass.write(new DataOutputStream
                 (new FileOutputStream("out.class")));
  }
}