0

我是 AspectJ 的新手(在 Eclipse 中)。我发现编码一个简单类的问题,我需要获取一个输入参数(在这个例子中是一个简单的值)。

public aspect TestingAspectJ 
{
    pointcut start(int value) : target(int) && execution(void start(int)) && args(value);

    after(int value) : start(value)
    {
        System.out.println("Hello World! My value is " + value);
    }
}

AspectJ 编译器在“after(int value)”中抛出错误:此方法必须返回 int 类型的结果。

  • 我尝试刷新 (F5) + 清理项目但不起作用。
  • 我尝试了另一个定义相同概念的示例,编译器抛出相同的错误。

你可以帮帮我吗?

谢谢。

帕科。

4

1 回答 1

0

方面在这里编译得很好。可能编译错误来自另一个类,您在其中执行以下操作:

int start(int number) {
    System.out.println("Number = " + number);
}

即你定义了一个int返回类型的方法,但根本不返回一个int,或者至少不总是像这个错误的例子中那样:

int max(int a, int b, int c) {
    if (a > b) {
        if (a > c) {
            return a;
        } else if (b > c) {
            return b;
        } else {
            return c;
        }
    }
}

在这个例子中 if a <= b,没有返回任何东西,这是一个错误。也许您在示例 1 或 2 中做了类似的事情。

顺便说一句,您的切入点中确实有一个错误,但不是一个使其无法编译的错误,只是一个使其与任何方法执行不匹配的错误:您需要删除该target(int)部分,因为这意味着您要匹配 class 的方法int,但首先int是在 JDK 中定义的(不是由 AspectJ 编织的),其次int根本不是一个类,而是一个原始类型。可能你想要这个:

public aspect TestingAspectJ {
    pointcut start(int value) : execution(void start(int)) && args(value);

    after(int value) : start(value) {
        System.out.println("Hello World! My value is " + value);
    }
}
于 2013-07-09T09:42:46.443 回答