简体   繁体   English

在aspectj之后()

[英]After() in aspectj

I am new to aspectj and I had a function which gets the integer variable and adds 100 to it as follows我是aspectj的新手,我有一个函数可以获取整数变量并将其加100,如下所示

public static void add(int no)
    {
        no=no+100;


    }

My aspect is as follows我的方面如下

pointcut printMessage(int m) : execution(* add(..)) && args(m);
 after(int m) returning: printMessage(m) {
            System.out.println(m);
}

I am calling the function with the value 10. But when I run the code, it returns the result as 10. can any one tell me why it is not returning 110. correct me if I am wrong我正在调用值为 10 的函数。但是当我运行代码时,它返回的结果为 10。谁能告诉我为什么它不返回 110。如果我错了,请纠正我

Because in java an int is not modifiable.因为在 java 中 int 是不可修改的。 If you write如果你写

int i = 10;
add(i);
System.out.println(i);

You will still get 10. So your aspect does what you asked : you pass a variable of value 10 to a function, the fuction does what it wants with its local copy, and on return the variable is unchanged你仍然会得到 10。所以你的方面做你问的:你将一个值为 10 的变量传递给一个函数,函数用它的本地副本做它想要的,并且在返回时变量没有改变

Edit :编辑 :

If you want to get a modified value, you could pass a modifiable variable like an array, or more simply use the return value (because your current add is a no op) :如果你想得到一个修改过的值,你可以传递一个像数组一样的可修改变量,或者更简单地使用返回值(因为你当前的添加是一个无操作):

Edit 2 here is full test code :在这里编辑 2 是完整的测试代码:

public class AspectTest {
    public static int add(int i) {
        return i+100;
    }

    @Test
    public void test() throws Exception {
        int j = add(10);
        assertEquals(110, j);
        add(20);
    }
}

and :和 :

aspect A {
pointcut printMessage() : execution(* add(..));
 after() returning (int m): printMessage() {
            System.out.println(m);
}
}

Output :输出 :

Running ...AspectTest
110
120
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.214 sec

Here is a variant of Serge Ballesta's sample code which makes it even clearer what happens in Java.这是 Serge Ballesta 示例代码的一个变体,它使 Java 中发生的事情更加清晰。 Primitive types like int are immutable, but even some simple built-in object types like String are, too.int这样的原始类型是不可变的,但即使是像String这样一些简单的内置对象类型也是如此。 More complex types like Set are not, though.但是,像Set这样更复杂的类型不是。

Driver application with different add methods:具有不同add方法的驱动程序应用程序:

package de.scrum_master.app;

import java.util.HashSet;
import java.util.Set;

public class Application {
    public static int add(int i) {
        i += 100;
        return i;
    }

    public static String add(String text) {
        text = "#" + text + "#";
        return text;
    }

    public static Set<String> add(Set<String> set) {
        set.add("new element");
        return set;
    }

    public static void main(String[] args) {
        add(10);
        add("foo");
        Set<String> mySet = new HashSet<>();
        mySet.add("foo");
        mySet.add("bar");
        add(mySet);
    }
}

Aspect capturing all add methods, printing their parameters and results for comparison: Aspect 捕获所有add方法,打印它们的参数和结果以进行比较:

package de.scrum_master.app;

public aspect MyAspect {
    pointcut addMethods(Object parameter) :
        execution(* add(*)) && args(parameter);

    after(Object parameter) returning (Object returnValue): addMethods(parameter) {
        System.out.println(thisJoinPoint);
        System.out.println("  parameter    = " + parameter);
        System.out.println("  return value = " + returnValue);
    }
}

Console output:控制台输出:

execution(int de.scrum_master.app.Application.add(int))
  parameter    = 10
  return value = 110
execution(String de.scrum_master.app.Application.add(String))
  parameter    = foo
  return value = #foo#
execution(Set de.scrum_master.app.Application.add(Set))
  parameter    = [new element, foo, bar]
  return value = [new element, foo, bar]

As you can see, int and String parameters remain unchanged while the Set parameter is mutable and thus updated.如您所见, intString参数保持不变,而Set参数是可变的并因此更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM