Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Aspectj捕获和抑制Java类引发的异常_Java_Aop_Aspectj_Aspect - Fatal编程技术网

如何使用Aspectj捕获和抑制Java类引发的异常

如何使用Aspectj捕获和抑制Java类引发的异常,java,aop,aspectj,aspect,Java,Aop,Aspectj,Aspect,我想使用Aspectj处理从Circle.getArea()方法引发的此异常 Shape.java package Shapes; public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public double getPerimet

我想使用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;
        }
    }


    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;
        }
    }


    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 :(");
        }
    }


    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());
                }
        }
    }

Rectangle.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;
        }
    }


    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;
        }
    }


    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 :(");
        }
    }


    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());
                }
        }
    }

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(){
            return Math.PI * this.radius * this.radius;
        }
    }


    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;
        }
    }


    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 :(");
        }
    }


    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());
                }
        }
    }

Main.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;
        }
    }


    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;
        }
    }


    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 :(");
        }
    }


    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 :(
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
我想避免打印“Error:Oops,我不知道如何计算:(”,我需要在末尾得到圆形对象的实际面积。
但是,我无法更改任何.java文件。所有更改都应使用Resolve.aj文件。

您需要使用
周围的建议,而不是后面的建议:

代码输出:

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 :(
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
通知截取一个给定的连接点,并可以在该连接点之前、之后和之后注入新的行为,而不是该连接点。
procedure
是一个特殊功能,它允许
around
通知继续执行
joinpoint

来自AspectJ支持的建议类型(即
之前
之后
,以及
周围
),只有
环绕
通知才允许返回值和/或使用
继续
。这使得
环绕
通知可以多次执行相同的连接点,或者根本不执行。此外,您甚至可以使用不同的上下文执行截取的连接点(例如,更改方法参数的值)

有关建议和继续工作方式的更多信息,请访问

我们的
around
建议将截取类
Circle
中方法的所有执行接合点,并相应地处理这些方法引发的异常