Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Re: Accessing fields

Hi Manuel,
You can access the field by capturing the object that holds it.
Taka look to the following example, which I made while experimenting with the pointcut protocol:

README.txt:
---------------------------
This experiment ilustrates how both caller and callee objects can
be captured from a set() pointcut, as well as both old and new
values from the assignement.
The example includes (in class Callee) both a primitive field and
an object field.
Run Caller
------------------------------------------------------------------------
Caller.java:
---------------------------
/*
* Created on 19/Ago/2003
*/
package capture_set;
/**
* @author Miguel Pessoa Monteiro
**/
public class Caller {
	private Callee myCallee;
	public int myField = 19;
	public Caller() {
		myCallee = new Callee("Bilbo", 5);
	}
	public void setName(String newstr) {
		myCallee.name = newstr;
	}
	public void setValue(int newval) {
		myCallee.value = newval;
	}
	public static void main(String[] args) {
		Caller caller = new Caller();
		caller.setValue(11);
		caller.setName("Frodo");
	}
}
------------------------------------------------------------------------
Callee.java:
---------------------------
/*
* Created on 19/Ago/2003
*/
package capture_set;
/**
* @author Miguel Pessoa Monteiro
**/
public class Callee {
	protected String name;
	protected int value;
	public Callee(String s, int i) {
		name = s;
		value = i;
	}
}
------------------------------------------------------------------------
---------------------------
/*
* Created on 19/Ago/2003
*/
package capture_set;
/**
* @author Miguel Pessoa Monteiro
**/
public aspect GetContext1 {
	pointcut setStr(String str, Callee obj):
		set(String Callee.name)
		&& withincode(public void Caller.setName(..))
		&& target(obj)
		&& args(str);
	before(String str, Callee obj): setStr(str, obj) {
		System.out.print("Old value = \"" + obj.name +"\", ");
		System.out.println("new value = \"" + str +"\"");
}
	pointcut setInt(int val, Callee obj, Caller caller):
		set(int Callee.value)
		&& withincode(public void Caller.setValue(..))
		&& target(obj)
		&& this(caller)
		&& args(val);
	before(int val, Callee obj, Caller caller):
			setInt(val, obj, caller) {
		System.out.print("Old value = " + obj.value +", ");
		System.out.print("new value = " + val);
		System.out.println(" and caller has " + caller.myField);
	}
/*
	//Reflective implementation:
	pointcut reflectiveGetValue(): set(public int field2);
	before(): reflectiveGetValue() {
		Object[] arguments = thisJoinPoint.getArgs();
		System.out.println("thisJoinPoint =\""+thisJoinPoint+"\"");
		System.out.println("arguments.length = " + arguments.length);
		Integer valObj = (Integer)arguments[0];
		int value = valObj.intValue();
		System.out.println("New value = " + value);
	}
*/
}
------------------------------------------------------------------------

----Original message:-----
Date: Fri, 23 Jan 2004 19:19:52 +0000
From: Manuel Menezes de Sequeira <Manuel.Sequeira@xxxxxxxx>
Organization: ISCTE
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] Accessing fields
Reply-To: aspectj-users@xxxxxxxxxxx
I'm using an around advice for set and get pointcuts.  Can I have access
to field being set or got?

Miguel J. T. Pessoa Monteiro
Ph.D. student Minho University,Portugal
eMail: mmonteiro@xxxxxxxxxxxx
URL:http://gec.di.uminho.pt/mpm/


Back to the top