Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
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
>


Back to the top