Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] capturing implicit toString() function calls

Hello,

Depending on what you are trying to do you may prefer to use 'execution' instead of 'call'. For your example the pointcut would be:

execution (String HelloWorld.toString())

and would result in "jpt hit" being printed out twice.

Thanks for the tip, unfortunately this does not work for me as I want to capture the toString function calls for the built in Number classes (i.e. Integer, Float,..) to pipe them through a NumberFormatter. But it seems that these classes are somewhat special because they are not captured this way.

Here is the example rewritten using execution join points. The output is
---
Goodbye world
Goodbye world
25
---

public class HelloWorld
{
	public static void main(String[] args)
	{
		HelloWorld hw = new HelloWorld();
		System.out.println(hw.toString() + "\n" + hw);
		Integer i = new Integer(25);
		System.out.println(""+i);
	}
	
	public String toString()
	{	
		return "Hello World ";
	}
}


public aspect TestAspect
{
	String around() : execution(String HelloWorld.toString())
	{
		return "Goodbye world";
	}
	
	String around() : execution(String Integer.toString() )
	{
		System.out.println("Returning 1");
		return "1";
	}
}

Greetings Ben


Back to the top