Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Serialization aspect problem

I'm not positive, but I suspect you need to use around advice, instead of after advice. Make sure it returns the newly constructed object.

dean

On Dec 19, 2007, at 1:01 PM, neil loughran wrote:

Thanks Bhaskar,

I think there must be a simpler way to do this where I don't have to access
the attributes and copy them one by one...  In the OO example I was messing
around with I didn't have to make new references or copy attributes.

So I think it may have something to do with the joinpoint/advice type?

Cheers
Neil


-----Original Message-----
From: aspectj-users-bounces@xxxxxxxxxxx [mailto:aspectj-users-
bounces@xxxxxxxxxxx] On Behalf Of Bhaskar Maddala
Sent: 19 December 2007 18:33
To: aspectj-users@xxxxxxxxxxx
Subject: Re: [aspectj-users] Serialization aspect problem

A modified aspect that works

public aspect SerializationAspect
{
  declare parents: Foo implements Serializable;
  String filename = "Foo.ser";

  // problematic advice? - no longer problematic
  after(Foo f): this(f) && execution(Foo.new())  {
      try  {
          Foo f1 = (Foo) SerializeObject.load(filename);
          System.out.println("[MyAspect] x = "+f1.getX()); // works fine
here!
          f.setX(f1.getX()); // it's all about the references.....
      }

      catch(Exception e)  {
          System.out.println("File not found "+ e);
      }
  }

  after(Foo f): this(f) && execution(public void Foo.setX(String)) &&
!within(SerializationAspect) {
      System.out.println("Setting");
      try  {
          SerializeObject.save(f,filename);
      }

      catch(IOException e)  {
          System.out.println("File not found "+ e);
      }
  }
}

Thanks
Bhaskar

On Dec 19, 2007 10:59 AM, neil loughran <loughran@xxxxxxxxxxxxxxxx> wrote:
Hi all

I'm trying to figure out what exactly is wrong with my pointcut/advice
for a
serialisation aspect.

The code below is fairly simple but captures the problem I have.
Basically
I create an object of type Foo and set Foo.x to Foobar. My aspect has
two
advice.. one for capturing instantiation of Foo and the other for
capturing
setX on Foo objects.

On instantiation of Foo, I attempt to load a serialised file (Foo.ser).
If
it doesn't exist then an exception is thrown but the program carries on
as
usual.  When I setX the second advice serializes the Foo object as file
Foo.ser.

Therefore when I run the program a second time I expect the Foo.ser file
to
load and the contents loaded to the Foo object.  However, for some
reason I
can't seem to show the loaded value in FooDriver only in the aspect.
Therefore, I am assuming my first advice is incorrectly written and the
loaded content is lost outside of the aspect.  I have tried a few
different
varieties of around/after returning advice to no avail.

Can anyone spot the mistake in the first advice... it's driving me nuts!

Many thanks
Neil Loughran



import java.io.*;

class FooDriver  {
   public static void main(String args[])  {
       Foo f = new Foo();
       System.out.println("[FooDriver] x = "+f.getX()); // always
null!!?

       f.setX("Foobar");
   }
}

class Foo  {
   public String x;
   public void setX(String _x)  {
       x = _x;
   }

   public String getX() {
               return x;
       }
}

aspect MyAspect  {
   declare parents: Foo implements Serializable;
   String filename = "Foo.ser";

   // problematic advice?
   after(Foo f): this(f) && execution (Foo.new())  {
       try  {
           f = (Foo) SerializeObject.load(filename);
           System.out.println("[MyAspect] x = "+f.getX()); // works
fine
here!
       }

       catch(Exception e)  {
           System.out.println("File not found "+ e);
       }
   }

   after(Foo f): this(f) && execution(public void Foo.setX(String))  {
       System.out.println("Setting");
       try  {
           SerializeObject.save(f,filename);
       }

       catch(IOException e)  {
           System.out.println("File not found "+ e);
       }
   }
}

class SerializeObject  {
   public static void save(Object obj, String filename) throws
IOException
{
         System.out.println("Saving file: "+filename);
       ObjectOutputStream objstream = new ObjectOutputStream(new
FileOutputStream(filename));
       objstream.writeObject(obj);
       objstream.close();
   }

   public static Object load(String filename) throws Exception  {
       System.out.println("Loading file: "+filename);
       ObjectInputStream objstream = new ObjectInputStream(new
FileInputStream(filename));
       Object obj = objstream.readObject();
       objstream.close();
       return obj;
   }
}

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Dean Wampler, Ph.D.
dean at objectmentor.com
See also:
http://aquarium.rubyforge.org     AOP for Ruby
http://www.contract4j.org         Design by Contract for Java5




Back to the top