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