Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: AW: [aspectj-users] How to build a Dirty Flag ?

Jan,
 
To make the code work on private members you just need to use getDeclaredField (i.e., it's just a Java reflection API issue). I was thinking there might be more complexity in using reflection with privates (there usually is), but in this case it's pretty easy. And, yes, you are likely to want to use !equals instead of != for most object types..
 

before(int newVal, Object container) : set(int ModelClass.*) && args(newVal) && this(container) {

  try {

    Class inClass = thisJoinPoint.getSignature().getDeclaringType();
    String fieldName = thisJoinPoint.getSignature().getName();
    Field field = inClass.getDeclaredField(fieldName);
    int oldVal = field.getInt(container);
    //System.out.println("change "+oldVal+" to "+newVal);

    if (oldVal != newVal) {

        dirty=true;

    }

  } catch (Exception e) { e.printStackTrace(); } // handle properly
}

 

I'll go ahead and solve the other exercise too. Here is the solution for static fields (use both to handle all cases). It differs because you can't bind a value for this (there is none):

before(int newVal) : set(static int ModelClass.*) && args(newVal) {

  try {

    Class inClass = thisJoinPoint.getSignature().getDeclaringType();
    String fieldName = thisJoinPoint.getSignature().getName();
    Field field = inClass.getDeclaredField(fieldName);
    int oldVal = field.getInt(null);
    //System.out.println("change "+oldVal+" to "+newVal);

    if (oldVal != newVal) {

        dirty=true;

    }

  } catch (Exception e) { e.printStackTrace(); } // handle properly
}

 

Ron Bodkin
Chief Technology Officer
New Aspects of Security
m: (415) 509-2895
 
 
------------Original Message-------------
From: "Jan Kerssenfischer" <aspectj@xxxxxxxxxxxxxxxxx>
To: <aspectj-users@xxxxxxxxxxx>
Date: Thu, Jul-31-2003 2:53 PM
Subject: AW: [aspectj-users] How to build a Dirty Flag ?

That´s a fast answer. Thank you. To get this code work on private variables I only have to declare the aspect as privileged, do I. There are different types of variables to check. I think I can also use an Object and compare it with the .equals method? I try to find out tomorrow, thank you again.

 

Jan Kerssenfischer

 

Jan,

 

You can use before advice with reflection to achieve your goal. Here's a simple example where the field is public and is not a static member. Generalizing to handling private fields and static members is left as an exercise for the reader :-)

 

before(int newVal, Object container) : set(int ModelClass.*) && args(newVal) && this(container) {

  try {

    Class inClass = thisJoinPoint.getSignature().getDeclaringType();
    String fieldName = thisJoinPoint.getSignature().getName();
    Field field = inClass.getField(fieldName);
    int oldVal = field.getInt(container);
    //System.out.println("change "+oldVal+" to "+newVal);

    if (oldVal != newVal) {

        dirty=true;

    }

  } catch (Exception e) { e.printStackTrace(); } // handle properly
}

 

Ron Bodkin

Chief Technology Officer

New Aspects of Security

m: (415) 509-2895

 

 

Hello,

 

I have the following Problem:

If have some classes representing a data structure. I want to write an aspect, that adds a dirty flag to a class, that turns true, if one of the values changed. So far the adding of the Flag is not the problem. My Problem is to find out, if any of the classvariables is set with a new value. New means in that case that it is not the old value written again (what my Gui does). To get the new Value is no problem with a combination of set und args. My Problem is how to find out the previous value of the variable witch is written. Target returns the target instance and not the Variable. Are there any solutions to this problem?

 

Best regards,

Jan Kerssenfischer

 


Back to the top