KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > all


1                                 // Simply try
2
// to access as much of
3
// the API as possible.
4

5 import jas.*;
6
7 import java.io.*;
8 import sun.tools.java.RuntimeConstants;
9
10 public class all implements RuntimeConstants
11 {
12   public static void main(String JavaDoc argv[])
13     throws Exception JavaDoc
14   {
15     ClassEnv c = new ClassEnv();
16
17                                 // Adding CP Items directly
18

19     c.addCPItem(new AsciiCP("fubar"));
20     c.addCPItem(new ClassCP("java/lang/Number"));
21     c.addCPItem(new DoubleCP(2.0));
22     c.addCPItem(new FieldCP("java/lang/System",
23                             "out",
24                             "Ljava/io/PrintStream;"));
25     c.addCPItem(new FloatCP((float)(2.0)));
26     c.addCPItem(new IntegerCP(2));
27     c.addCPItem(new InterfaceCP("java/lang/Runnable",
28                                 "run",
29                                 "()V"));
30     c.addCPItem(new LongCP(2));
31     c.addCPItem(new MethodCP("java/lang/Thread",
32                              "run",
33                              "()V"));
34
35     c.addCPItem(new NameTypeCP("sdfsdf", "Ljava/lang/Object;"));
36     c.addCPItem(new StringCP("sdf"));
37
38
39                                 // Add fields, creating variables
40
c.addField(new Var((short) ACC_PUBLIC,
41                        new AsciiCP("someIntvar"),
42                        new AsciiCP("I"),
43                        null));
44     c.addField(new Var((short)(ACC_PUBLIC|ACC_STATIC|ACC_FINAL),
45                        new AsciiCP("finalInt"),
46                        new AsciiCP("I"),
47                        new ConstAttr(new IntegerCP(10))));
48
49                                 // Check if I can add interfaces
50
c.addInterface(new ClassCP("java/lang/Runnable"));
51
52     c.setClass(new ClassCP("regress"));
53     c.setSuperClass(new ClassCP("java/lang/Object"));
54     c.setClassAccess((short) ACC_PUBLIC);
55
56                                 // Creating code.
57

58     CodeAttr code = new CodeAttr();
59                                 // add instructions of various
60
// operand types.
61

62                                 // No operands
63
code.addInsn(new Insn(opc_return));
64                                 // one arg operands
65
code.addInsn(new Insn(opc_astore, 5));
66                                 // one arg arguments with wide operand
67
code.addInsn(new Insn(opc_dstore, 256));
68     code.addInsn(new Insn(opc_istore, 2576));
69
70                                 // Add a label
71
code.addInsn(new Label("First label"));
72                                 // refer back to it
73
code.addInsn(new Insn(opc_jsr,
74                           new Label("First label")));
75                                 // add another label
76
code.addInsn(new Label("second_label"));
77                                 // insn with CP argument
78
code.addInsn(new Insn(opc_ldc_w, new StringCP("sdfsdf")));
79
80                                 // the "special" instructions
81
code.addInsn(new IincInsn(2, -2));
82                                 // wider version check
83
code.addInsn(new IincInsn(1234, 2));
84     code.addInsn(new IincInsn(3, -200));
85     code.addInsn(new InvokeinterfaceInsn(new ClassCP("java/lang/Number"), 1));
86     code.addInsn(new MultiarrayInsn(new ClassCP("java/lang/Double"), 3));
87     Label woo[] = new Label[3];
88     woo[0] = new Label("First label");
89     woo[1] = new Label("second_label");
90     woo[2] = new Label("second_label");
91     code.addInsn(new TableswitchInsn(0, 2, woo[0], woo));
92
93     int m[] = new int[3];
94     m[0] = 11;
95     m[1] = 15;
96     m[2] = -1;
97     code.addInsn(new LookupswitchInsn(woo[0], m, woo));
98
99
100                                 // make a catchtable
101
Catchtable ctb = new Catchtable();
102                                 // add a couple of entries
103
ctb.addEntry(new Label("First label"),
104                  new Label("second_label"),
105                  new Label("second_label"),
106                  new ClassCP("java/lang/Exception"));
107     ctb.addEntry(new Label("First label"),
108                  new Label("second_label"),
109                  new Label("second_label"),
110                  new ClassCP("java/lang/Error"));
111     code.setCatchtable(ctb);
112     code.setStackSize((short)100);
113     code.setVarSize((short)500);
114                 // Add some line table info
115
LineTableAttr ln = new LineTableAttr();
116     ln.addEntry(woo[0], 234);
117     ln.addEntry(woo[1], 245);
118     ln.addEntry(woo[2], 22);
119     code.setLineTable(ln);
120                 // Add a generic attr to a method
121
String JavaDoc foo = "sldkfj sdlfkj";
122     byte dat[] = new byte[foo.length()];
123     foo.getBytes(0, dat.length, dat, 0);
124     code.addGenericAttr(new GenericAttr("strangeAttr", dat));
125
126                 // Also adding local varinfo
127
LocalVarTableAttr lv = new LocalVarTableAttr();
128     lv.addEntry(new LocalVarEntry(woo[0], woo[2], "fakevar", "I", 22));
129     lv.addEntry(new LocalVarEntry(woo[1], woo[1], "morefa", "()V", 10));
130     code.setLocalVarTable(lv);
131
132                                 // check out add method, also
133
// adding a throws exception for
134
// good measure
135
ExceptAttr ex = new ExceptAttr();
136     ex.addException(new ClassCP("java/io/IOException"));
137     ex.addException(new ClassCP("java/lang/Error"));
138     c.addMethod((short) ACC_PUBLIC,
139                 "fubarmethod",
140                 "()V",
141                 code,
142                 ex);
143
144
145
146
147                 // Add a source file attribute
148
c.setSource(new SourceAttr("all.java"));
149                 // Add some more generic attribute
150
c.addGenericAttr(new GenericAttr("blahAttr", dat));
151     c.write(new DataOutputStream(new FileOutputStream("regress.class")));
152   }
153 }
154
155     
156
Popular Tags