在Aspectj建议中读取类属性名称和值

时间:2016-09-13 12:26:13

标签: java aspectj

在将属性作为切入点时,是否可以读取Aspectj建议中的类属性名称和值。 以下是代码段。

JAVA代码:

package com.test;

public class Test {
    static List<String> list;
    static List<String> removeList;
    public static void main(String[] args) {
        list = new ArrayList<String>();
        list.add("ashish");
        removeList = new ArrayList<String>();
        removeList.add("kumar");
    }
}

AspectJ代码:

package com.test;

public aspect AspectjTest {
    pointcut callAdd(): within(com.test.*) && call(* java.util.List.add(..));

    boolean around() : callAdd() {
        //code to get the property name and property value(i.e. the value going to be added to the list)
        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

您应该通过args()将方法参数绑定到变量,如下所示:

package com.test;

import java.util.List;

public aspect AspectjTest {
    pointcut callAdd(Object element) :
        within(com.test.*) &&
        call(boolean List.add(*)) &&
        args(element);

    boolean around(Object element) : callAdd(element) {
        System.out.println(thisJoinPoint + " -> " + element);
        return proceed(element);
    }
}

控制台日志:

call(boolean java.util.List.add(Object)) -> ashish
call(boolean java.util.List.add(Object)) -> kumar

如果您不理解这一点,请随时提出后续问题。

更新:当我使用菜单选项&#34; Source&#34;时,重构的意思就像Eclipse为你(或任何其他类似的IDE)做的事情。 &#34;生成委托方法&#34;。结果是这样的:

package com.test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class NameDatabase {
    private final List<String> firstNames = new ArrayList<String>();
    private final List<String> lastNames = new ArrayList<String>();

    public boolean addFirstName(String e) {
        return firstNames.add(e);
    }

    public boolean removeFirstName(Object o) {
        return firstNames.remove(o);
    }

    public Stream<String> streamFirstNames() {
        return firstNames.stream();
    }

    public boolean addLastName(String e) {
        return lastNames.add(e);
    }

    public boolean removeLastName(Object o) {
        return lastNames.remove(o);
    }

    public Stream<String> streamLastNames() {
        return lastNames.stream();
    }

    public static void main(String[] args) {
        NameDatabase database = new NameDatabase();
        database.addFirstName("Galileo");
        database.addLastName("Galilei");
        database.addFirstName("Isaac");
        database.addLastName("Newton");
        database.addFirstName("Albert");
        database.addLastName("Einstein");
        database.addFirstName("Werner");
        database.addLastName("Heisenberg");
        database.addFirstName("Stephen");
        database.addLastName("Hawking");

        database.removeFirstName("Werner");
        database.removeLastName("Heisenberg");

        System.out.println(database.streamFirstNames().collect(Collectors.joining(", ")));
        System.out.println(database.streamLastNames().collect(Collectors.joining(", ")));
    }
}

相应方面:

package com.test;

public aspect NameDBContentAuditor {
    before(String name) :
        execution(public * NameDatabase.*stName(*)) &&
        args(name)
    {
        System.out.println(thisJoinPoint + " -> " + name);
    }
}

控制台日志:

execution(boolean com.test.NameDatabase.addFirstName(String)) -> Galileo
execution(boolean com.test.NameDatabase.addLastName(String)) -> Galilei
execution(boolean com.test.NameDatabase.addFirstName(String)) -> Isaac
execution(boolean com.test.NameDatabase.addLastName(String)) -> Newton
execution(boolean com.test.NameDatabase.addFirstName(String)) -> Albert
execution(boolean com.test.NameDatabase.addLastName(String)) -> Einstein
execution(boolean com.test.NameDatabase.addFirstName(String)) -> Werner
execution(boolean com.test.NameDatabase.addLastName(String)) -> Heisenberg
execution(boolean com.test.NameDatabase.addFirstName(String)) -> Stephen
execution(boolean com.test.NameDatabase.addLastName(String)) -> Hawking
execution(boolean com.test.NameDatabase.removeFirstName(Object)) -> Werner
execution(boolean com.test.NameDatabase.removeLastName(Object)) -> Heisenberg
Galileo, Isaac, Albert, Stephen
Galilei, Newton, Einstein, Hawking