Sorry if this has been discussed before (or if I'm completely wrong on this):

It seems that Component#getSizeInBytes() gives wrong results in case of 
non-static nested classes (which is quite common in Wicket):

>public LoginPage() {
>  add(new Link("login") {
>    public void onClick() {
>      ...
>    }
>  });
>}

Yes, Component#getSizeInBytes() sets the parent reference to null before 
calling Objects.sizeof(this).
But the reference to the enclosing object (this$0) will trigger a serialization 
of the complete page, thus resulting in a wrong size calculation.

I've experimented a little bit and came up with the following solution that 
nullifies any reference to a configurable 'stop' object:

Objects.java:
>public static long sizeof(final Object object, final Object stopObject)
>{
>  try
>  {
>    final ByteCountingOutputStream out = new ByteCountingOutputStream();
>    new StoppingObjectObjectStream(out, stopObject).writeObject(object);
>    out.close();
>    return out.size();
>  }
>  catch (IOException e)
>  {
>    e.printStackTrace();
>    return -1;
>  }
>}

StoppingObjectObjectStream.java:
>public class StoppingObjectObjectStream extends ObjectOutputStream {
>  private Object stopObject;
>  public StoppingObjectObjectStream(OutputStream out,
>      Object stopObject) throws IOException {
>    super(out);
>               
>    this.stopObject = stopObject;
>               
>    enableReplaceObject(true);
>  }
>       
>  protected Object replaceObject(Object obj) throws IOException {
>    if (obj == stopObject) {
>       return null;
>    }
>    return super.replaceObject(obj);
>  }
>}

Now there's no need any more to nullify the parent of the component and 
resetting it afterwards in Component.java:
>public long getSizeInBytes()
>{
>  return Objects.sizeof(this, getParent());
>}

What do you think? Perhaps a little bit freaky I must admit.

Sven


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to