如何使用 Aspectj 捕获和抑制从 Java Class 抛出的异常
How to catch and suppress an Exception Thrown from a Java Class using Aspectj
我想使用 Aspectj 处理从 Circle.getArea()
方法抛出的异常。
Shape.java
package Shapes;
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getPerimeter(){
return 2 * Math.PI * this.radius;
}
public double getArea(){
return Math.PI * this.radius * this.radius;
}
}
Rectangle.java
package Shapes;
public class Rectangle {
private double width, height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getPerimeter() {
return 2 * (this.width + this.height);
}
public double getArea() {
return this.width * this.height;
}
}
Circle.java
package Shapes;
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getPerimeter() {
return 2 * Math.PI * this.radius;
}
public double getArea() {
throw new RuntimeException("Oops, I don't know how to calculate this :(");
}
}
Main.java
package Shapes;
public class Main {
public static void main(String[] args) {
try {
Shape s;
s = (Shape) new Rectangle(2, 10);
System.out.println("The area of " + s + " is " + s.getArea());
s = (Shape) new Rectangle(-2, 10);
System.out.println("The perimeter of " + s +" is " + s.getPerimeter());
s = (Shape) new Circle(-2);
System.out.println("The perimeter of " + s +" is " + s.getPerimeter());
s = (Shape) new Circle(2);
System.out.println("The area of " + s + " is " + s.getArea());
}
catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Resolve.aj
package Shapes;
privileged public aspect Resolve {
declare parents: Rectangle implements Shape;
declare parents: Circle implements Shape;
public String Rectangle.getName(){
return "Rectangle";
}
public String Circle.getName(){
return "Circle";
}
public String Rectangle.toString(){
return this.getName()+"("+this.width+","+this.height+")";
}
public String Circle.toString(){
return this.getName()+"("+this.radius+")";
}
after() throwing(RuntimeException e) : execution(* Circle.*()){
handleException();
}
protected void handleException()
{
System.out.println("Error detected");
}
}
当前输出为:
The area of Rectangle(2.0,10.0) is 20.0
The perimeter of Rectangle(-2.0,10.0) is 16.0
The perimeter of Circle(-2.0) is -12.566370614359172
Error detected
Error: Oops, I don't know how to calculate this :(
我想避免打印“错误:糟糕,我不知道如何计算这个:(”,最后我需要得到圆形对象的真实面积。
但是,我无法更改任何 .java 文件。所有更改都应使用 Resolve.aj 文件。
您需要使用 around
建议 而不是 after:
Object around () : execution(* Circle.*()){
try {
return proceed();
}
catch(Exception e)
{
handleException();
}
return null;
}
代码输出:
The area of Rectangle(2.0,10.0) is 20.0
The perimeter of Rectangle(-2.0,10.0) is 16.0
The perimeter of Circle(-2.0) is -12.566370614359172
Error detected
The area of Circle(2.0) is 0.0
为什么我们使用 around 建议而不是 after?
非常非正式地,around
建议拦截给定的连接点,并且可以注入 新行为 before、after 和 instead of that joinpoint。 proceed
是一个特殊的 feature 允许 around
advice 继续执行 joinpoint
.
来自 AspectJ 支持的 advice 类型(即 before
、after
和 around
), around
advice 是唯一允许 return 一个值 and/or 使用 proceed
。这使得 around
advice 可以多次执行相同的 joinpoint,或者根本不执行。此外,您甚至可以使用不同的上下文执行拦截的 joinpoint(例如,更改方法参数的值)。
可以在 .
上找到有关建议和程序如何工作的更多信息
我们的around
建议会拦截class[=]中方法的所有执行joinpoint 24=],并将相应地处理这些方法抛出的异常。
我想使用 Aspectj 处理从 Circle.getArea()
方法抛出的异常。
Shape.java
package Shapes;
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getPerimeter(){
return 2 * Math.PI * this.radius;
}
public double getArea(){
return Math.PI * this.radius * this.radius;
}
}
Rectangle.java
package Shapes;
public class Rectangle {
private double width, height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getPerimeter() {
return 2 * (this.width + this.height);
}
public double getArea() {
return this.width * this.height;
}
}
Circle.java
package Shapes;
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getPerimeter() {
return 2 * Math.PI * this.radius;
}
public double getArea() {
throw new RuntimeException("Oops, I don't know how to calculate this :(");
}
}
Main.java
package Shapes;
public class Main {
public static void main(String[] args) {
try {
Shape s;
s = (Shape) new Rectangle(2, 10);
System.out.println("The area of " + s + " is " + s.getArea());
s = (Shape) new Rectangle(-2, 10);
System.out.println("The perimeter of " + s +" is " + s.getPerimeter());
s = (Shape) new Circle(-2);
System.out.println("The perimeter of " + s +" is " + s.getPerimeter());
s = (Shape) new Circle(2);
System.out.println("The area of " + s + " is " + s.getArea());
}
catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Resolve.aj
package Shapes;
privileged public aspect Resolve {
declare parents: Rectangle implements Shape;
declare parents: Circle implements Shape;
public String Rectangle.getName(){
return "Rectangle";
}
public String Circle.getName(){
return "Circle";
}
public String Rectangle.toString(){
return this.getName()+"("+this.width+","+this.height+")";
}
public String Circle.toString(){
return this.getName()+"("+this.radius+")";
}
after() throwing(RuntimeException e) : execution(* Circle.*()){
handleException();
}
protected void handleException()
{
System.out.println("Error detected");
}
}
当前输出为:
The area of Rectangle(2.0,10.0) is 20.0
The perimeter of Rectangle(-2.0,10.0) is 16.0
The perimeter of Circle(-2.0) is -12.566370614359172
Error detected
Error: Oops, I don't know how to calculate this :(
我想避免打印“错误:糟糕,我不知道如何计算这个:(”,最后我需要得到圆形对象的真实面积。 但是,我无法更改任何 .java 文件。所有更改都应使用 Resolve.aj 文件。
您需要使用 around
建议 而不是 after:
Object around () : execution(* Circle.*()){
try {
return proceed();
}
catch(Exception e)
{
handleException();
}
return null;
}
代码输出:
The area of Rectangle(2.0,10.0) is 20.0
The perimeter of Rectangle(-2.0,10.0) is 16.0
The perimeter of Circle(-2.0) is -12.566370614359172
Error detected
The area of Circle(2.0) is 0.0
为什么我们使用 around 建议而不是 after?
非常非正式地,around
建议拦截给定的连接点,并且可以注入 新行为 before、after 和 instead of that joinpoint。 proceed
是一个特殊的 feature 允许 around
advice 继续执行 joinpoint
.
来自 AspectJ 支持的 advice 类型(即 before
、after
和 around
), around
advice 是唯一允许 return 一个值 and/or 使用 proceed
。这使得 around
advice 可以多次执行相同的 joinpoint,或者根本不执行。此外,您甚至可以使用不同的上下文执行拦截的 joinpoint(例如,更改方法参数的值)。
可以在
我们的around
建议会拦截class[=]中方法的所有执行joinpoint 24=],并将相应地处理这些方法抛出的异常。