Methods – get method parameter values using AspectJ

I use AspectJ to capture method calls Then I need to get the method name and the passed parameter value

Line2D line = new Line2D.Double(lineStart,lineEnd);
and graphics.draw(line);

I need to capture on graphics2d All calls to draw (shape) I have an entry point to do this:

pointcut captureCallParameters(Shape name) : call(* *(Shape)) && args(name);

The problem is when I try to get the value of the parameter (shape in this method) I get this parameter: Java awt. geom. Line2D$ Double@596e1fb1

Instad I want to get the shape of the point, in this case is a line

On the other hand, I also have an entry point to match the construction of the new line mentioned above. I can obtain the parameters of the line But I don't know how to associate the draw method with the line constructor I can have several constructors for lines. I don't know which line to draw with the draw (line) method

Solution

You're absolutely right!

You have indeed captured the line2d instance you are looking for However, you seem to be in system out. Print the shape variable in the println (shape) statement What do you have awt. geom. Line2D$ Double@596e1fb1 Is the identifier of the variable You can now access the contents of the variable by calling any available method, such as shape. Getbounds()

In addition, you can do the following:

Line2D line = (Line2D) shape; // cast it to Line2D
line.getX1(); // will give you X1 of your line
line.getX2(); // will give you X2 of your line

Finally, this is a better entry point definition for your use case:

pointcut captureCallParameters(Shape shape) : call(* Graphics2D.draw(..)) && args(shape);

In pointcuts, you will intercept all method calls with shape parameters In my version, you will only capture calls to the draw () method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>