I'm writing a small serialization utility and I would like to calculate how many bytes I need to reserve to write an object to a byte array. Let's say I have a class with a few fields/getters like
int age;
String name;
Colour colour;
boolean active;
...
I thought I could use reflection to iterate over fields and look what size they have, but all I can do is check whether they are primitive. Which is a start, since I would use a short as an offset for Objects. Okay, I could use
if(field.getType() == int.class) {
size = Integer.BYTES;
}
or use a map. It would not be that hard, since I only need to know the size of primitives. But out of curiosity: Is there any direct way?
Aucun commentaire:
Enregistrer un commentaire