Skip to content

Commit

Permalink
Size printer handles arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainhalle committed Apr 26, 2019
1 parent 2a4f96b commit 97aaafe
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
{
Expand Down
46 changes: 34 additions & 12 deletions Source/Size/src/ca/uqac/lif/azrael/size/SizeReflectionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String,Object> 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<String,Object> contents) throws PrintException
{
return 0;
}

@Override
public void reset()
{
Expand Down
8 changes: 8 additions & 0 deletions Source/Size/src/ca/uqac/lif/azrael/size/SizeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 97aaafe

Please sign in to comment.