Classdesc 3.44
javaClass_serialisation.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_SERIALISATION_H
13#define CLASSDESC_JAVACLASS_SERIALISATION_H
14
15#include <vector>
16#include <string>
17#include <pack_base.h>
18#include <dump_base.h>
19#include "javaClass.h"
20
21
22namespace classdesc
23{
24 template <> void dump(dump_t& buf, const string& d, const u8& a)
25 {dump(buf,d,a.v);}
26 template <> void dump(dump_t& buf, const string& d, const u4& a)
27 {dump(buf,d,a.v);}
28 template <> void dump(dump_t& buf, const string& d, const u2& a)
29 {dump(buf,d,a.v);}
30 template <> void dump(dump_t& buf, const string& d, const u1& a)
31 {buf << std::hex << int(a);}
32
33}
34
35
36namespace classdesc_access
37{
38/*
39 Strings and vectors are have specialised serialisation for javaClass
40*/
41
42 template <>
43 struct access_pack<std::string>
44 {
45 void operator()(classdesc::pack_t& buf, const classdesc::string& d, const std::string& a)
46 {
47 classdesc::u2 length=a.length();
48 pack(buf,d,length);
49 buf.packraw(a.c_str(),length);
50 }
51 };
52
53 template <>
54 struct access_unpack<std::string>
55 {
56 template <class U>
57 void operator()(classdesc::unpack_t& buf, const classdesc::string& d, U& a)
58 {
59 classdesc::u2 length;
60 unpack(buf,d,length);
61 char *s=new char[length];
62 buf.unpackraw(s,length);
63 a=std::string(s,length);
64 delete [] s;
65 }
66 };
67
68 template <>
70 {
71 template <class U>
72 void operator()(classdesc::unpack_t& buf, const classdesc::string& d, U& a)
73 {
74 a.v=0;
75 for (int i=0; i<8; i++)
76 {
77 classdesc::u1 b;
78 unpack(buf,d,b);
79 a.v=(a.v<<8) | (0xFF&b);
80 }
81 }
82 };
83
84 template <>
86 {
87 template <class U>
88 void operator()(classdesc::pack_t& buf, const classdesc::string& d, U& a)
89 {
90 for (int i=7; i>=0; i--)
91 {
92 classdesc::u1 b = a.v >> 8*i;
93 pack(buf,d,b);
94 }
95 }
96 };
97
98 template <>
100 {
101 template <class U>
102 void operator()(classdesc::unpack_t& buf, const classdesc::string& d, U& a)
103 {
104 a.v=0;
105 for (int i=0; i<4; i++)
106 {
107 classdesc::u1 b;
108 unpack(buf,d,b);
109 a.v=(a.v<<8) | (0xFF&b);
110 }
111 }
112 };
113
114 template <>
116 {
117 void operator()(classdesc::pack_t& buf, const classdesc::string& d, const classdesc::u4& a)
118 {
119 for (int i=3; i>=0; i--)
120 {
121 classdesc::u1 b = a.v >> 8*i;
122 pack(buf,d,b);
123 }
124 }
125 };
126
127 template <>
129 {
130 void operator()(classdesc::unpack_t& buf, const classdesc::string& d, classdesc::u2& a)
131 {
132 classdesc::u1 b1, b2;
133 unpack(buf,d,b1);
134 unpack(buf,d,b2);
135 a=(b1<<8)| (0xFF & b2);
136 }
137 };
138
139 template <>
141 {
142 void operator()(classdesc::pack_t& buf, const classdesc::string& d, const classdesc::u2& a)
143 {
144 classdesc::u1 b1=a.v>>8, b2=a.v;
145 pack(buf,d,b1);
146 pack(buf,d,b2);
147 }
148 };
149
150 template <>
151 struct access_pack<classdesc::cp_info>
152 {
153 void operator()(classdesc::pack_t& buf, const classdesc::string& d, const classdesc::cp_info& a)
154 {
155 using namespace classdesc;
156 pack(buf,d,a.tag());
157 switch (a.tag())
158 {
159 case JVM_CONSTANT_Class:
160 case JVM_CONSTANT_String:
161 pack(buf,d,a.get<u2>()); break;
162 case JVM_CONSTANT_Fieldref:
163 case JVM_CONSTANT_Methodref:
164 case JVM_CONSTANT_InterfaceMethodref:
165 pack(buf,d,a.get<Ref>()); break;
166 case JVM_CONSTANT_Integer:
167 case JVM_CONSTANT_Float:
168 pack(buf,d,a.get<u4>()); break;
169 case JVM_CONSTANT_Long:
170 case JVM_CONSTANT_Double:
171 pack(buf,d,a.get<u8>()); break;
172 case JVM_CONSTANT_NameAndType:
173 pack(buf,d,a.get<NameAndTypeInfo>());
174 break;
175 case JVM_CONSTANT_Utf8:
176 pack(buf,d,a.get<std::string>()); break;
177 }
178 }
179 };
180
181 template <>
183 {
184 void operator()(classdesc::unpack_t& buf, const classdesc::string& d, classdesc::cp_info& a)
185 {
186 using namespace classdesc;
187 u1 tag;
188 unpack(buf,d,tag);
189 switch (tag)
190 {
191 case JVM_CONSTANT_Class:
192 case JVM_CONSTANT_String:
193 a.unpack<u2>(buf,tag); break;
194 case JVM_CONSTANT_Fieldref:
195 case JVM_CONSTANT_Methodref:
196 case JVM_CONSTANT_InterfaceMethodref:
197 a.unpack<Ref>(buf,tag); break;
198 case JVM_CONSTANT_Integer:
199 case JVM_CONSTANT_Float:
200 a.unpack<u4>(buf,tag); break;
201 case JVM_CONSTANT_Long:
202 case JVM_CONSTANT_Double:
203 a.unpack<u8>(buf,tag); break;
204 case JVM_CONSTANT_NameAndType:
205 a.unpack<NameAndTypeInfo>(buf,tag);
206 break;
207 case JVM_CONSTANT_Utf8:
208 a.unpack<std::string>(buf,tag); break;
209 }
210 }
211 };
212
213 // The zeroth element is not serialised in the constant_pool
214 template <>
216 {
217 template <class U>
218 void operator()(classdesc::pack_t& buf, const classdesc::string& d, U& a)
219 {
220 classdesc::u2 size=a.size();
221 pack(buf,d,size);
222 for (int i=1; i<size; i++)
223 pack(buf,d,a[i]);
224 }
225 };
226
227 // The zeroth element is not serialised in the constant_pool
228 template <>
230 {
231 void operator()(classdesc::pack_t& buf, const classdesc::string& d, std::vector<classdesc::cp_info>& a)
232 {
233 classdesc::u2 size; unpack(buf,d,size);
234 a.resize(size);
235 for (int i=1; i<size; i++)
236 unpack(buf,d,a[i]);
237 }
238 };
239
240 template <>
242 {
243 template<class U>
244 void operator()(classdesc::pack_t& buf, const classdesc::string& d, U& a)
245 {
246 pack(buf,d,a.attribute_name_index);
247 classdesc::u4 length=a.info.size();
248 pack(buf,d,length);
249 // need to code this explicitly as attribute length is u4 not u2
250 for (size_t i=0; i<a.info.size(); i++)
251 pack(buf,d,a.info[i]);
252 }
253 };
254
255 template <>
257 {
258 void operator()(classdesc::pack_t& buf, const classdesc::string& d, classdesc::attribute_info& a)
259 {
260 unpack(buf,d,a.attribute_name_index);
261 classdesc::u4 length;
262 unpack(buf,d,length);
263 // need to code this explicitly as attribute length is u4 not u2
264 a.info.resize(length);
265 for (size_t i=0; i<a.info.size(); i++)
266 unpack(buf,d,a.info[i]);
267 }
268 };
269
270}
271
272/* define this here to take advantage of cp_info's serialisation operators */
273inline bool classdesc::cp_info::operator==(const classdesc::cp_info& x) const
274{
275 pack_t b1, b2;
276 pack(b1,string(),*this);
277 pack(b2,string(),x);
278 return b1.size()==b2.size() && memcmp(b1.data(),b2.data(),b1.size())==0;
279}
280
281
282namespace classdesc
283{
284
285 void dumpp(dump_t& buf, const string& d, cp_info& a)
286 {
287 switch (a.tag())
288 {
289 case JVM_CONSTANT_Class:
290 case JVM_CONSTANT_String:
291 dump(buf,d,a.get<u2>()); break;
292 case JVM_CONSTANT_Fieldref:
293 case JVM_CONSTANT_Methodref:
294 case JVM_CONSTANT_InterfaceMethodref:
295 dump(buf,d,a.get<Ref>()); break;
296 case JVM_CONSTANT_Integer:
297 dump(buf,d,a.get<int>()); break;
298 case JVM_CONSTANT_Float:
299 dump(buf,d,a.get<float>()); break;
300 case JVM_CONSTANT_Long:
301 dump(buf,d,a.get<long long>()); break;
302 case JVM_CONSTANT_Double:
303 dump(buf,d,a.get<double>()); break;
304 case JVM_CONSTANT_NameAndType:
305 dump(buf,d,a.get<NameAndTypeInfo>());
306 break;
307 case JVM_CONSTANT_Utf8:
308 dump(buf,d,a.get<std::string>()); break;
309 }
310 }
311}
312
313
314namespace classdesc
315{
316 inline void dumpp(dump_t& targ, const string& desc,struct attribute_info& arg)
317 {
318 dump(targ,desc+".attribute_name_index",arg.attribute_name_index);
319 int tab=format(targ, desc+".info");
320 targ << std::setw(tab) << "";
321 for (u1 *c=&arg.info[0]; c!=&arg.info[0]+arg.info.size(); c++)
322 targ << " "<<std::setw(2)<<std::setfill('0')<<std::hex << int(*c);
323 targ<<std::setfill(' ')<<std::endl;
324
325 }
326}
327
328template <class T> void classdesc::cp_info::unpack(pack_t& t, u1 tag) {
329 T tmp;
330 ::unpack(t,"",tmp);
331 set(tag,tmp);
332}
333
334
335#endif
Definition javaClass.h:618
Definition dump_base.h:30
Definition pack_base.h:138
size_t size() const
size of buffer
Definition pack_base.h:170
const char * data() const
actual buffer
Definition pack_base.h:168
textual representation descriptor
Java classfile representation.
Contains access_* structs, and nothing else. These structs are used to gain access to private members...
Definition classdesc_access.h:20
Contains definitions related to classdesc functionality.
void dump(dump_t &o, const string &d, const T &a)
forward declare generic dump operation
Definition dump_epilogue.h:55
void pack(pack_t &targ, const string &desc, is_treenode dum, const T *const &arg)
serialise a tree (or DAG)
Definition pack_graph.h:28
void unpack(unpack_t &targ, const string &desc, is_treenode dum, T *&arg)
unserialise a tree.
Definition pack_graph.h:44
STL namespace.
serialisation descriptor
Definition javaClass.h:659
Definition javaClass.h:606
Definition javaClass.h:594
Definition javaClass.h:666
Definition javaClass.h:49
Definition javaClass.h:62
Definition javaClass.h:72
class to allow access to private members
Definition classdesc_access.h:21
class to allow access to private members
Definition classdesc_access.h:22