diff --git a/Source/Size/src/ca/uqac/lif/azrael/size/ArrayPrintHandler.java b/Source/Size/src/ca/uqac/lif/azrael/size/ArrayPrintHandler.java index c30d257..0605225 100644 --- a/Source/Size/src/ca/uqac/lif/azrael/size/ArrayPrintHandler.java +++ b/Source/Size/src/ca/uqac/lif/azrael/size/ArrayPrintHandler.java @@ -18,6 +18,8 @@ */ package ca.uqac.lif.azrael.size; +import java.lang.reflect.Array; + import ca.uqac.lif.azrael.PrintException; public class ArrayPrintHandler extends ReferencePrintHandler @@ -30,16 +32,17 @@ public ArrayPrintHandler(SizePrinter printer) @Override public boolean canHandle(Object o) { - return o.getClass().isArray(); + return o != null && o.getClass().isArray(); } @Override public Number getSize(Object o) throws PrintException { - Object[] array = (Object[]) o; + int length = Array.getLength(o); int size = 16; - for (Object elem : array) + for (int i = 0; i < length; i++) { + Object elem = Array.get(o, i); size += (Integer) m_printer.print(elem); if (!SizePrinter.isPrimitive(elem)) { diff --git a/Source/Size/src/ca/uqac/lif/azrael/size/SizeReflectionHandler.java b/Source/Size/src/ca/uqac/lif/azrael/size/SizeReflectionHandler.java index c6f9828..f306756 100644 --- a/Source/Size/src/ca/uqac/lif/azrael/size/SizeReflectionHandler.java +++ b/Source/Size/src/ca/uqac/lif/azrael/size/SizeReflectionHandler.java @@ -18,6 +18,8 @@ */ package ca.uqac.lif.azrael.size; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.IdentityHashMap; import java.util.Map; @@ -44,28 +46,48 @@ public Number handle(Object o) throws PrintException return 0; } m_seenObjects.put(o, 1); - return super.handle(o); - } - - @Override - public Number encapsulateFields(Object o, Map contents) throws PrintException - { int size = 24; // Basic overhead of a Java object - for (Object f_v : contents.values()) + for (Field field : getAllFields(o.getClass())) { - if (SizePrinter.isPrimitive(f_v)) + // Is this field declared as transient? + if (m_ignoreTransient && Modifier.isTransient(field.getModifiers())) { - size += (Integer) m_printer.print(f_v); + // Yes: don't serialize this field + continue; } - else + field.setAccessible(true); + + try + { + Object f_v = field.get(o); + if (SizePrinter.isPrimitive(f_v)) + { + size += (Integer) m_printer.print(f_v); + } + else + { + size += SizePrinter.OBJREF_SIZE; + size += (Integer) m_printer.print(f_v); + } + } + catch (IllegalArgumentException e) { - size += SizePrinter.OBJREF_SIZE; - size += (Integer) m_printer.print(f_v); + throw new PrintException(e); + } + catch (IllegalAccessException e) + { + throw new PrintException(e); } } return size; } + @Override + public Number encapsulateFields(Object o, Map contents) throws PrintException + { + return 0; + } + @Override public void reset() { diff --git a/Source/Size/src/ca/uqac/lif/azrael/size/SizeTest.java b/Source/Size/src/ca/uqac/lif/azrael/size/SizeTest.java index 02218b0..4b48e30 100644 --- a/Source/Size/src/ca/uqac/lif/azrael/size/SizeTest.java +++ b/Source/Size/src/ca/uqac/lif/azrael/size/SizeTest.java @@ -63,6 +63,14 @@ public void testList() throws PrintException assertTrue(size3 - size2 < so_size); } + @Test + public void testArray1() throws PrintException + { + int[] arr = new int[] {1, 2, 3}; + int so_size = (Integer) m_printer.print(arr); + assertTrue(so_size > 0); + } + public static class SimpleObject { int m_x;