javaClass.h
Go to the documentation of this file.
1 /*
2  @copyright Russell Standish 2000-2013
3  @author Russell Standish
4  This file is part of Classdesc
5 
6  Open source licensed under the MIT license. See LICENSE for details.
7 */
8 
12 #ifndef JAVACLASS_H
13 #define JAVACLASS_H
14 
15 #ifdef _CLASSDESC
16 #pragma omit pack classdesc::u2
17 #pragma omit pack classdesc::u4
18 #pragma omit pack classdesc::u8
19 #pragma omit pack classdesc::cp_info
20 #pragma omit pack classdesc::attribute_info
21 #pragma omit unpack classdesc::u2
22 #pragma omit unpack classdesc::u4
23 #pragma omit unpack classdesc::u8
24 #pragma omit unpack classdesc::cp_info
25 #pragma omit unpack classdesc::attribute_info
26 #pragma omit dump classdesc::u2
27 #pragma omit dump classdesc::u4
28 #pragma omit dump classdesc::u8
29 #pragma omit dump classdesc::attribute_info
30 #pragma omit dump classdesc::cp_info
31 #pragma omit javaClass classdesc::cp_info
32 #endif
33 
34 #include "pack_base.h"
35 #include <vector>
36 
37 namespace classdesc
38 {
39 
40  typedef unsigned char u1;
41 
42  // 2, 4 and 8 byte data types. Specialised serialisation methods provided
43  // to handle JVM's byte ordering.
44 
45  struct u2
46  {
47  unsigned short v;
48  operator short() const {return v;}
49  u2(): v(0) {}
50  u2(short v): v(v) {}
51  short operator=(short x) {return v=x;}
52  short operator|=(short x) {return v|=x;}
53  short operator&=(short x) {return v&=x;}
54  bool operator==(const u2& x) const {return v==x.v;}
55  };
56 
57  struct u4
58  {
59  unsigned int v;
60  operator unsigned() const {return v;}
61  u4(): v(0) {}
62  u4(unsigned v): v(v) {}
63  unsigned operator=(unsigned x) {return v=x;}
64  bool operator==(const u4& x) const {return v==x.v;}
65  };
66 
67  struct u8
68  {
69  unsigned long long v;
70  operator long long() const {return v;}
71  u8(): v(0) {}
72  u8(unsigned v): v(v) {}
73  unsigned operator=(unsigned x) {return v=x;}
74  bool operator==(const u8& x) const {return v==x.v;}
75  };
76 
77  // constants imported from jdk: classfile_constants
78 
79 enum {
80  JVM_ACC_PUBLIC = 0x0001,
81  JVM_ACC_PRIVATE = 0x0002,
82  JVM_ACC_PROTECTED = 0x0004,
83  JVM_ACC_STATIC = 0x0008,
84  JVM_ACC_FINAL = 0x0010,
85  JVM_ACC_SYNCHRONIZED = 0x0020,
86  JVM_ACC_SUPER = 0x0020,
87  JVM_ACC_VOLATILE = 0x0040,
88  JVM_ACC_BRIDGE = 0x0040,
89  JVM_ACC_TRANSIENT = 0x0080,
90  JVM_ACC_VARARGS = 0x0080,
91  JVM_ACC_NATIVE = 0x0100,
92  JVM_ACC_INTERFACE = 0x0200,
93  JVM_ACC_ABSTRACT = 0x0400,
94  JVM_ACC_STRICT = 0x0800,
95  JVM_ACC_SYNTHETIC = 0x1000,
96  JVM_ACC_ANNOTATION = 0x2000,
97  JVM_ACC_ENUM = 0x4000
98 };
99 
100 /* Used in newarray instruction. */
101 
102 enum {
103  JVM_T_BOOLEAN = 4,
104  JVM_T_CHAR = 5,
105  JVM_T_FLOAT = 6,
106  JVM_T_DOUBLE = 7,
107  JVM_T_BYTE = 8,
108  JVM_T_SHORT = 9,
109  JVM_T_INT = 10,
110  JVM_T_LONG = 11
111 };
112 
113 /* Constant Pool Entries */
114 
115 enum {
116  JVM_CONSTANT_Utf8 = 1,
117  JVM_CONSTANT_Unicode = 2, /* unused */
118  JVM_CONSTANT_Integer = 3,
119  JVM_CONSTANT_Float = 4,
120  JVM_CONSTANT_Long = 5,
121  JVM_CONSTANT_Double = 6,
122  JVM_CONSTANT_Class = 7,
123  JVM_CONSTANT_String = 8,
124  JVM_CONSTANT_Fieldref = 9,
125  JVM_CONSTANT_Methodref = 10,
126  JVM_CONSTANT_InterfaceMethodref = 11,
127  JVM_CONSTANT_NameAndType = 12
128 };
129 
130 /* StackMapTable type item numbers */
131 
132 enum {
133  JVM_ITEM_Top = 0,
134  JVM_ITEM_Integer = 1,
135  JVM_ITEM_Float = 2,
136  JVM_ITEM_Double = 3,
137  JVM_ITEM_Long = 4,
138  JVM_ITEM_Null = 5,
139  JVM_ITEM_UninitializedThis = 6,
140  JVM_ITEM_Object = 7,
141  JVM_ITEM_Uninitialized = 8
142 };
143 
144 /* Type signatures */
145 
146 enum {
147  JVM_SIGNATURE_ARRAY = '[',
148  JVM_SIGNATURE_BYTE = 'B',
149  JVM_SIGNATURE_CHAR = 'C',
150  JVM_SIGNATURE_CLASS = 'L',
151  JVM_SIGNATURE_ENDCLASS = ';',
152  JVM_SIGNATURE_ENUM = 'E',
153  JVM_SIGNATURE_FLOAT = 'F',
154  JVM_SIGNATURE_DOUBLE = 'D',
155  JVM_SIGNATURE_FUNC = '(',
156  JVM_SIGNATURE_ENDFUNC = ')',
157  JVM_SIGNATURE_INT = 'I',
158  JVM_SIGNATURE_LONG = 'J',
159  JVM_SIGNATURE_SHORT = 'S',
160  JVM_SIGNATURE_VOID = 'V',
161  JVM_SIGNATURE_BOOLEAN = 'Z'
162 };
163 
164 /* Opcodes */
165 
166 enum {
167  JVM_OPC_nop = 0,
168  JVM_OPC_aconst_null = 1,
169  JVM_OPC_iconst_m1 = 2,
170  JVM_OPC_iconst_0 = 3,
171  JVM_OPC_iconst_1 = 4,
172  JVM_OPC_iconst_2 = 5,
173  JVM_OPC_iconst_3 = 6,
174  JVM_OPC_iconst_4 = 7,
175  JVM_OPC_iconst_5 = 8,
176  JVM_OPC_lconst_0 = 9,
177  JVM_OPC_lconst_1 = 10,
178  JVM_OPC_fconst_0 = 11,
179  JVM_OPC_fconst_1 = 12,
180  JVM_OPC_fconst_2 = 13,
181  JVM_OPC_dconst_0 = 14,
182  JVM_OPC_dconst_1 = 15,
183  JVM_OPC_bipush = 16,
184  JVM_OPC_sipush = 17,
185  JVM_OPC_ldc = 18,
186  JVM_OPC_ldc_w = 19,
187  JVM_OPC_ldc2_w = 20,
188  JVM_OPC_iload = 21,
189  JVM_OPC_lload = 22,
190  JVM_OPC_fload = 23,
191  JVM_OPC_dload = 24,
192  JVM_OPC_aload = 25,
193  JVM_OPC_iload_0 = 26,
194  JVM_OPC_iload_1 = 27,
195  JVM_OPC_iload_2 = 28,
196  JVM_OPC_iload_3 = 29,
197  JVM_OPC_lload_0 = 30,
198  JVM_OPC_lload_1 = 31,
199  JVM_OPC_lload_2 = 32,
200  JVM_OPC_lload_3 = 33,
201  JVM_OPC_fload_0 = 34,
202  JVM_OPC_fload_1 = 35,
203  JVM_OPC_fload_2 = 36,
204  JVM_OPC_fload_3 = 37,
205  JVM_OPC_dload_0 = 38,
206  JVM_OPC_dload_1 = 39,
207  JVM_OPC_dload_2 = 40,
208  JVM_OPC_dload_3 = 41,
209  JVM_OPC_aload_0 = 42,
210  JVM_OPC_aload_1 = 43,
211  JVM_OPC_aload_2 = 44,
212  JVM_OPC_aload_3 = 45,
213  JVM_OPC_iaload = 46,
214  JVM_OPC_laload = 47,
215  JVM_OPC_faload = 48,
216  JVM_OPC_daload = 49,
217  JVM_OPC_aaload = 50,
218  JVM_OPC_baload = 51,
219  JVM_OPC_caload = 52,
220  JVM_OPC_saload = 53,
221  JVM_OPC_istore = 54,
222  JVM_OPC_lstore = 55,
223  JVM_OPC_fstore = 56,
224  JVM_OPC_dstore = 57,
225  JVM_OPC_astore = 58,
226  JVM_OPC_istore_0 = 59,
227  JVM_OPC_istore_1 = 60,
228  JVM_OPC_istore_2 = 61,
229  JVM_OPC_istore_3 = 62,
230  JVM_OPC_lstore_0 = 63,
231  JVM_OPC_lstore_1 = 64,
232  JVM_OPC_lstore_2 = 65,
233  JVM_OPC_lstore_3 = 66,
234  JVM_OPC_fstore_0 = 67,
235  JVM_OPC_fstore_1 = 68,
236  JVM_OPC_fstore_2 = 69,
237  JVM_OPC_fstore_3 = 70,
238  JVM_OPC_dstore_0 = 71,
239  JVM_OPC_dstore_1 = 72,
240  JVM_OPC_dstore_2 = 73,
241  JVM_OPC_dstore_3 = 74,
242  JVM_OPC_astore_0 = 75,
243  JVM_OPC_astore_1 = 76,
244  JVM_OPC_astore_2 = 77,
245  JVM_OPC_astore_3 = 78,
246  JVM_OPC_iastore = 79,
247  JVM_OPC_lastore = 80,
248  JVM_OPC_fastore = 81,
249  JVM_OPC_dastore = 82,
250  JVM_OPC_aastore = 83,
251  JVM_OPC_bastore = 84,
252  JVM_OPC_castore = 85,
253  JVM_OPC_sastore = 86,
254  JVM_OPC_pop = 87,
255  JVM_OPC_pop2 = 88,
256  JVM_OPC_dup = 89,
257  JVM_OPC_dup_x1 = 90,
258  JVM_OPC_dup_x2 = 91,
259  JVM_OPC_dup2 = 92,
260  JVM_OPC_dup2_x1 = 93,
261  JVM_OPC_dup2_x2 = 94,
262  JVM_OPC_swap = 95,
263  JVM_OPC_iadd = 96,
264  JVM_OPC_ladd = 97,
265  JVM_OPC_fadd = 98,
266  JVM_OPC_dadd = 99,
267  JVM_OPC_isub = 100,
268  JVM_OPC_lsub = 101,
269  JVM_OPC_fsub = 102,
270  JVM_OPC_dsub = 103,
271  JVM_OPC_imul = 104,
272  JVM_OPC_lmul = 105,
273  JVM_OPC_fmul = 106,
274  JVM_OPC_dmul = 107,
275  JVM_OPC_idiv = 108,
276  JVM_OPC_ldiv = 109,
277  JVM_OPC_fdiv = 110,
278  JVM_OPC_ddiv = 111,
279  JVM_OPC_irem = 112,
280  JVM_OPC_lrem = 113,
281  JVM_OPC_frem = 114,
282  JVM_OPC_drem = 115,
283  JVM_OPC_ineg = 116,
284  JVM_OPC_lneg = 117,
285  JVM_OPC_fneg = 118,
286  JVM_OPC_dneg = 119,
287  JVM_OPC_ishl = 120,
288  JVM_OPC_lshl = 121,
289  JVM_OPC_ishr = 122,
290  JVM_OPC_lshr = 123,
291  JVM_OPC_iushr = 124,
292  JVM_OPC_lushr = 125,
293  JVM_OPC_iand = 126,
294  JVM_OPC_land = 127,
295  JVM_OPC_ior = 128,
296  JVM_OPC_lor = 129,
297  JVM_OPC_ixor = 130,
298  JVM_OPC_lxor = 131,
299  JVM_OPC_iinc = 132,
300  JVM_OPC_i2l = 133,
301  JVM_OPC_i2f = 134,
302  JVM_OPC_i2d = 135,
303  JVM_OPC_l2i = 136,
304  JVM_OPC_l2f = 137,
305  JVM_OPC_l2d = 138,
306  JVM_OPC_f2i = 139,
307  JVM_OPC_f2l = 140,
308  JVM_OPC_f2d = 141,
309  JVM_OPC_d2i = 142,
310  JVM_OPC_d2l = 143,
311  JVM_OPC_d2f = 144,
312  JVM_OPC_i2b = 145,
313  JVM_OPC_i2c = 146,
314  JVM_OPC_i2s = 147,
315  JVM_OPC_lcmp = 148,
316  JVM_OPC_fcmpl = 149,
317  JVM_OPC_fcmpg = 150,
318  JVM_OPC_dcmpl = 151,
319  JVM_OPC_dcmpg = 152,
320  JVM_OPC_ifeq = 153,
321  JVM_OPC_ifne = 154,
322  JVM_OPC_iflt = 155,
323  JVM_OPC_ifge = 156,
324  JVM_OPC_ifgt = 157,
325  JVM_OPC_ifle = 158,
326  JVM_OPC_if_icmpeq = 159,
327  JVM_OPC_if_icmpne = 160,
328  JVM_OPC_if_icmplt = 161,
329  JVM_OPC_if_icmpge = 162,
330  JVM_OPC_if_icmpgt = 163,
331  JVM_OPC_if_icmple = 164,
332  JVM_OPC_if_acmpeq = 165,
333  JVM_OPC_if_acmpne = 166,
334  JVM_OPC_goto = 167,
335  JVM_OPC_jsr = 168,
336  JVM_OPC_ret = 169,
337  JVM_OPC_tableswitch = 170,
338  JVM_OPC_lookupswitch = 171,
339  JVM_OPC_ireturn = 172,
340  JVM_OPC_lreturn = 173,
341  JVM_OPC_freturn = 174,
342  JVM_OPC_dreturn = 175,
343  JVM_OPC_areturn = 176,
344  JVM_OPC_return = 177,
345  JVM_OPC_getstatic = 178,
346  JVM_OPC_putstatic = 179,
347  JVM_OPC_getfield = 180,
348  JVM_OPC_putfield = 181,
349  JVM_OPC_invokevirtual = 182,
350  JVM_OPC_invokespecial = 183,
351  JVM_OPC_invokestatic = 184,
352  JVM_OPC_invokeinterface = 185,
353  JVM_OPC_xxxunusedxxx = 186,
354  JVM_OPC_new = 187,
355  JVM_OPC_newarray = 188,
356  JVM_OPC_anewarray = 189,
357  JVM_OPC_arraylength = 190,
358  JVM_OPC_athrow = 191,
359  JVM_OPC_checkcast = 192,
360  JVM_OPC_instanceof = 193,
361  JVM_OPC_monitorenter = 194,
362  JVM_OPC_monitorexit = 195,
363  JVM_OPC_wide = 196,
364  JVM_OPC_multianewarray = 197,
365  JVM_OPC_ifnull = 198,
366  JVM_OPC_ifnonnull = 199,
367  JVM_OPC_goto_w = 200,
368  JVM_OPC_jsr_w = 201,
369  JVM_OPC_MAX = 201
370 };
371 
372 /* Opcode length initializer, use with something like:
373  * unsigned char opcode_length[JVM_OPC_MAX+1] = JVM_OPCODE_LENGTH_INITIALIZER;
374  */
375 #define JVM_OPCODE_LENGTH_INITIALIZER { \
376  1, /* nop */ \
377  1, /* aconst_null */ \
378  1, /* iconst_m1 */ \
379  1, /* iconst_0 */ \
380  1, /* iconst_1 */ \
381  1, /* iconst_2 */ \
382  1, /* iconst_3 */ \
383  1, /* iconst_4 */ \
384  1, /* iconst_5 */ \
385  1, /* lconst_0 */ \
386  1, /* lconst_1 */ \
387  1, /* fconst_0 */ \
388  1, /* fconst_1 */ \
389  1, /* fconst_2 */ \
390  1, /* dconst_0 */ \
391  1, /* dconst_1 */ \
392  2, /* bipush */ \
393  3, /* sipush */ \
394  2, /* ldc */ \
395  3, /* ldc_w */ \
396  3, /* ldc2_w */ \
397  2, /* iload */ \
398  2, /* lload */ \
399  2, /* fload */ \
400  2, /* dload */ \
401  2, /* aload */ \
402  1, /* iload_0 */ \
403  1, /* iload_1 */ \
404  1, /* iload_2 */ \
405  1, /* iload_3 */ \
406  1, /* lload_0 */ \
407  1, /* lload_1 */ \
408  1, /* lload_2 */ \
409  1, /* lload_3 */ \
410  1, /* fload_0 */ \
411  1, /* fload_1 */ \
412  1, /* fload_2 */ \
413  1, /* fload_3 */ \
414  1, /* dload_0 */ \
415  1, /* dload_1 */ \
416  1, /* dload_2 */ \
417  1, /* dload_3 */ \
418  1, /* aload_0 */ \
419  1, /* aload_1 */ \
420  1, /* aload_2 */ \
421  1, /* aload_3 */ \
422  1, /* iaload */ \
423  1, /* laload */ \
424  1, /* faload */ \
425  1, /* daload */ \
426  1, /* aaload */ \
427  1, /* baload */ \
428  1, /* caload */ \
429  1, /* saload */ \
430  2, /* istore */ \
431  2, /* lstore */ \
432  2, /* fstore */ \
433  2, /* dstore */ \
434  2, /* astore */ \
435  1, /* istore_0 */ \
436  1, /* istore_1 */ \
437  1, /* istore_2 */ \
438  1, /* istore_3 */ \
439  1, /* lstore_0 */ \
440  1, /* lstore_1 */ \
441  1, /* lstore_2 */ \
442  1, /* lstore_3 */ \
443  1, /* fstore_0 */ \
444  1, /* fstore_1 */ \
445  1, /* fstore_2 */ \
446  1, /* fstore_3 */ \
447  1, /* dstore_0 */ \
448  1, /* dstore_1 */ \
449  1, /* dstore_2 */ \
450  1, /* dstore_3 */ \
451  1, /* astore_0 */ \
452  1, /* astore_1 */ \
453  1, /* astore_2 */ \
454  1, /* astore_3 */ \
455  1, /* iastore */ \
456  1, /* lastore */ \
457  1, /* fastore */ \
458  1, /* dastore */ \
459  1, /* aastore */ \
460  1, /* bastore */ \
461  1, /* castore */ \
462  1, /* sastore */ \
463  1, /* pop */ \
464  1, /* pop2 */ \
465  1, /* dup */ \
466  1, /* dup_x1 */ \
467  1, /* dup_x2 */ \
468  1, /* dup2 */ \
469  1, /* dup2_x1 */ \
470  1, /* dup2_x2 */ \
471  1, /* swap */ \
472  1, /* iadd */ \
473  1, /* ladd */ \
474  1, /* fadd */ \
475  1, /* dadd */ \
476  1, /* isub */ \
477  1, /* lsub */ \
478  1, /* fsub */ \
479  1, /* dsub */ \
480  1, /* imul */ \
481  1, /* lmul */ \
482  1, /* fmul */ \
483  1, /* dmul */ \
484  1, /* idiv */ \
485  1, /* ldiv */ \
486  1, /* fdiv */ \
487  1, /* ddiv */ \
488  1, /* irem */ \
489  1, /* lrem */ \
490  1, /* frem */ \
491  1, /* drem */ \
492  1, /* ineg */ \
493  1, /* lneg */ \
494  1, /* fneg */ \
495  1, /* dneg */ \
496  1, /* ishl */ \
497  1, /* lshl */ \
498  1, /* ishr */ \
499  1, /* lshr */ \
500  1, /* iushr */ \
501  1, /* lushr */ \
502  1, /* iand */ \
503  1, /* land */ \
504  1, /* ior */ \
505  1, /* lor */ \
506  1, /* ixor */ \
507  1, /* lxor */ \
508  3, /* iinc */ \
509  1, /* i2l */ \
510  1, /* i2f */ \
511  1, /* i2d */ \
512  1, /* l2i */ \
513  1, /* l2f */ \
514  1, /* l2d */ \
515  1, /* f2i */ \
516  1, /* f2l */ \
517  1, /* f2d */ \
518  1, /* d2i */ \
519  1, /* d2l */ \
520  1, /* d2f */ \
521  1, /* i2b */ \
522  1, /* i2c */ \
523  1, /* i2s */ \
524  1, /* lcmp */ \
525  1, /* fcmpl */ \
526  1, /* fcmpg */ \
527  1, /* dcmpl */ \
528  1, /* dcmpg */ \
529  3, /* ifeq */ \
530  3, /* ifne */ \
531  3, /* iflt */ \
532  3, /* ifge */ \
533  3, /* ifgt */ \
534  3, /* ifle */ \
535  3, /* if_icmpeq */ \
536  3, /* if_icmpne */ \
537  3, /* if_icmplt */ \
538  3, /* if_icmpge */ \
539  3, /* if_icmpgt */ \
540  3, /* if_icmple */ \
541  3, /* if_acmpeq */ \
542  3, /* if_acmpne */ \
543  3, /* goto */ \
544  3, /* jsr */ \
545  2, /* ret */ \
546  99, /* tableswitch */ \
547  99, /* lookupswitch */ \
548  1, /* ireturn */ \
549  1, /* lreturn */ \
550  1, /* freturn */ \
551  1, /* dreturn */ \
552  1, /* areturn */ \
553  1, /* return */ \
554  3, /* getstatic */ \
555  3, /* putstatic */ \
556  3, /* getfield */ \
557  3, /* putfield */ \
558  3, /* invokevirtual */ \
559  3, /* invokespecial */ \
560  3, /* invokestatic */ \
561  5, /* invokeinterface */ \
562  0, /* xxxunusedxxx */ \
563  3, /* new */ \
564  2, /* newarray */ \
565  3, /* anewarray */ \
566  1, /* arraylength */ \
567  1, /* athrow */ \
568  3, /* checkcast */ \
569  3, /* instanceof */ \
570  1, /* monitorenter */ \
571  1, /* monitorexit */ \
572  0, /* wide */ \
573  4, /* multianewarray */ \
574  3, /* ifnull */ \
575  3, /* ifnonnull */ \
576  5, /* goto_w */ \
577  5 /* jsr_w */ \
578 }
579  struct Ref
580  {
581  u2 class_index;
582  u2 name_and_type_index;
583  Ref() {}
584  Ref(u2 class_index, u2 name_and_type_index):
585  class_index(class_index), name_and_type_index(name_and_type_index) {}
586  bool operator==(const Ref& x) const {
587  return class_index==x.class_index &&
588  name_and_type_index==x.name_and_type_index;}
589  };
590 
592  {
593  u2 name_index;
594  u2 descriptor_index;
595  NameAndTypeInfo() {}
596  NameAndTypeInfo(u2 name_index, u2 descriptor_index):
597  name_index(name_index), descriptor_index(descriptor_index) {}
598  bool operator==(const NameAndTypeInfo& x) const {
599  return name_index==x.name_index &&
600  descriptor_index==x.descriptor_index;}
601  };
602 
603  class cp_info
604  {
605  u1 _tag;
606  shared_ptr<void> info;
607 
608  public:
609  cp_info(): _tag(0) {}
610  template <class T>
611  cp_info(u1 t, const T& v) {set(t,v);}
612 
613  template <class T>
614  void set(u1 t, const T& v) {
615  _tag=t;
616  info.reset(new T(v));
617  }
618  template <class T> void unpack(pack_t& t, u1 tag);
619 
620  //Basic C types need to be unpacked in Java byte order
621  template <class T>
622  void unpack_basic(pack_t& t, u1 tag)
623  {
624  switch (sizeof(T))
625  {
626  case 1: unpack<u1>(t,tag); break;
627  case 2: unpack<u2>(t,tag); break;
628  case 4: unpack<u4>(t,tag); break;
629  case 8: unpack<u8>(t,tag); break;
630  }
631  }
632 
633  template <class T>
634  static u1 Tag();
635 
636  template <class T>
637  const T& get() const {return *static_cast<const T*>(info.get());}
638 
639  u1 tag() const {return _tag;}
640  bool operator==(const cp_info& x) const; //definition in javaClass_serialisation.h
641  };
642 
644  {
645  u2 attribute_name_index;
646  std::vector<u1> info; //[attribute_length];
647  bool operator==(const attribute_info& x) const {
648  return attribute_name_index==x.attribute_name_index && info == x.info;
649  }
650  };
651 
652  struct method_info
653  {
654  u2 access_flags;
655  u2 name_index;
656  u2 descriptor_index;
657  std::vector<attribute_info> attributes; //[attributes_count];
658  bool operator==(const method_info& x) const {
659  return access_flags==x.access_flags && name_index==x.name_index &&
660  descriptor_index == x.descriptor_index && attributes==x.attributes;
661  }
662 };
663 
664  struct field_info
665  {
666  u2 access_flags;
667  u2 name_index;
668  u2 descriptor_index;
669  std::vector<attribute_info> attributes; //[attributes_count];
670  bool operator==(const field_info& x) const {
671  return access_flags==x.access_flags && name_index==x.name_index &&
672  descriptor_index==x.descriptor_index && attributes==x.attributes;
673  }
674  };
675 
676  struct ClassFile
677  {
678  u4 magic;
679  u2 minor_version;
680  u2 major_version;
681  std::vector<cp_info> constant_pool; //[constant_pool_count-1];
682  u2 access_flags;
683  u2 this_class;
684  u2 super_class;
685  std::vector<u2> interfaces; //[interfaces_count];
686  std::vector<field_info> fields; //[fields_count];
687  std::vector<method_info> methods; //[methods_count];
688  std::vector<attribute_info> attributes; //[attributes_count];
689  ClassFile(): constant_pool(1) {} //add a zeroth (ignorable) element
690  bool operator==(const ClassFile& x) const {
691  return magic==x.magic && minor_version==x.minor_version &&
692  major_version==x.major_version && constant_pool==x.constant_pool &&
693  access_flags==x.access_flags && this_class == x.this_class &&
694  super_class == x.super_class && interfaces == x.interfaces &&
695  fields == x.fields && methods == x.methods && attributes==x.attributes;
696  }
697 
699  void addMethod(const std::string& method_name, const std::string& descriptor)
700  {
701  method_info mi;
702  mi.name_index=constant_pool.size();
703  constant_pool.push_back(cp_info(JVM_CONSTANT_Utf8, method_name));
704  mi.descriptor_index=constant_pool.size();
705  constant_pool.push_back(cp_info(JVM_CONSTANT_Utf8, descriptor));
706  //constant_pool.push_back(cp_info(JVM_CONSTANT_Utf8, std::string("([Ljava/lang/Object;)Ljava/lang/Object;")));
707  methods.push_back(mi);
708  }
710  void makeNative()
711  {
712  for (std::vector<method_info>::iterator i=methods.begin(); i!=methods.end(); ++i)
713  i->access_flags = JVM_ACC_NATIVE|JVM_ACC_PUBLIC;
714  access_flags = JVM_ACC_PUBLIC;
715  }
718  {
719  for (std::vector<method_info>::iterator i=methods.begin(); i!=methods.end(); ++i)
720  i->access_flags = JVM_ACC_ABSTRACT|JVM_ACC_PUBLIC;
721  access_flags = JVM_ACC_INTERFACE|JVM_ACC_ABSTRACT|JVM_ACC_PUBLIC;
722  }
723  };
724 
725 
726 }
727 
728 #endif
Definition: javaClass.h:67
Definition: javaClass.h:652
void makeInterface()
make this an interface with abstract methods
Definition: javaClass.h:717
Definition: javaClass.h:664
serialisation descriptor
Definition: javaClass.h:643
Definition: javaClass.h:579
Definition: javaClass.h:603
Definition: javaClass.h:676
Definition: javaClass.h:45
void addMethod(const std::string &method_name, const std::string &descriptor)
add a method
Definition: javaClass.h:699
Contains definitions related to classdesc functionality.
Definition: arrays.h:2514
Definition: pack_base.h:124
void makeNative()
make this a concrete class with native methods
Definition: javaClass.h:710
Definition: javaClass.h:57
Definition: javaClass.h:591
void unpack(unpack_t &targ, const string &desc, is_treenode dum, T *&arg)
unserialise a tree.
Definition: pack_graph.h:44