java – 为什么proceed()给出错误

我有以下几个方面:

aspect NullifyNoResultException {

    Object around(..) : execution(public Object com.example.*.*(..) {
        try { return proceed();    
            } catch (NoResultException e) { 
                return null;
            }                  
        }
    }
}

由于某种原因,调用proceed会在Eclipse中出错:

The method proceed() is undefined for the type NullifyNoResultException

当我在maven中构建时 – > mvn install我没有错误.
但这没有任何意义,因为我仍然缺少NoResultException的导入,所以maven应该抱怨这一点.
相反,它只是建立而不是抱怨.

如何让Eclipse停止抱怨proceed()?
以及如何构建此方面?

最佳答案 我在您的代码示例中发现了一些语法错误.当我纠正它们时,以下示例运行正常.顺便说一句,我定义了自己的NoResultException,因为我没有安装Java EE.

package javax.persistence;

public class NoResultException extends RuntimeException {
    private static final long serialVersionUID = 1L;
}
package com.example.stackoverflow;

import javax.persistence.NoResultException;

public class Application {
    public static void main(String[] args) {
        Application app = new Application();
        System.out.println(app.valueReturningMethod(1, "two"));
        System.out.println(app.exceptionThrowingMethod(1, "two"));
    }

    public Object valueReturningMethod(int i, String string) {
        return "normal result";
    }

    public Object exceptionThrowingMethod(int i, String string) {
        throw new NoResultException();
    }
}
package com.example.stackoverflow;

import javax.persistence.NoResultException;

aspect NullifyNoResultException {
    Object around() : execution(public Object com.example..*(..)) {
        try {
            return proceed();
        } catch (NoResultException e) {
            return null;
        }
    }
}

输出如预期:

normal result
null
点赞