如何使用AspectJ过滤方法的返回值?

发布于 2024-12-02 10:46:33 字数 560 浏览 0 评论 0原文

我想过滤具有 @Filter 注释的方法的返回值并返回 CollectionArrayMap< /code> 通过某个谓词。

我尝试过类似的操作:

    @Pointcut("execution(@example.annotations.Filter * *(..)) "
            + "&& @annotation(filter) ")
    public void filterOperation(final Filter filter) {
        /* ... */
    }

但是如果我将 execution((java.util.Collection+ || java.util.Map+) * * (..)) 添加到上面的 PointCut,则会出现语法错误。

如果某个方法用 @Filter 注释但不会返回集合,那么解决方案会是什么样子,最好是我可能会出错的解决方案?

I would like to filter return values of methods which have a @Filter annotation and return a Collection, an Array or a Map by a certain predicate.

I tried something like:

    @Pointcut("execution(@example.annotations.Filter * *(..)) "
            + "&& @annotation(filter) ")
    public void filterOperation(final Filter filter) {
        /* ... */
    }

But I get syntax errors if I add execution((java.util.Collection+ || java.util.Map+) * * (..)) to the PointCut above.

How would a solution look like, preferable one where I could error out if some method was annotated with @Filter but would not return a collection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云不回头 2024-12-09 10:46:33

您是否尝试过使用execution((java.util.Collection+ || java.util.Map+) *.* (..))

或者您可以使用:

@Pointcut("execution(@example.annotations.Filter java.util.Collection+ || java.util.Map+ *(..)) "
        + "&& @annotation(filter) ")
public void filterOperation(final Filter filter) {
    /* ... */
}

您可以按以下方式处理返回值:

@AfterReturning(value="filterOperation(filter)", returning="collection")
public void handlingReturnValue(final Filter filter, Object collection) {
    System.out.println(collection);
}

但我认为对 Map 和 Collection 使用单独的建议会更好:

@Pointcut("execution(@Filter java.util.Collection+ *(..)) "
        + "&& @annotation(filter) ")
public void filterCollection(final Filter filter) {
    /* ... */
}

@Pointcut("execution(@Filter java.util.Map+ *(..)) "
        + "&& @annotation(filter) ")
public void filterMap(final Filter filter) {
    /* ... */
}

@AfterReturning(value="filterCollection(filter)", returning="collection")
public void handlingReturnCollection(final Filter filter, Collection collection) {
    System.out.println(collection);
}

@AfterReturning(value="filterMap(filter)", returning="map")
public void handlingReturnMap(final Filter filter, Map map) {
    System.out.println(map);
}

Did you tried to use execution((java.util.Collection+ || java.util.Map+) *.* (..))?

Or you can use:

@Pointcut("execution(@example.annotations.Filter java.util.Collection+ || java.util.Map+ *(..)) "
        + "&& @annotation(filter) ")
public void filterOperation(final Filter filter) {
    /* ... */
}

You can handle return value the following way:

@AfterReturning(value="filterOperation(filter)", returning="collection")
public void handlingReturnValue(final Filter filter, Object collection) {
    System.out.println(collection);
}

But I suppose it will be better to use separate advices for Maps and Collections:

@Pointcut("execution(@Filter java.util.Collection+ *(..)) "
        + "&& @annotation(filter) ")
public void filterCollection(final Filter filter) {
    /* ... */
}

@Pointcut("execution(@Filter java.util.Map+ *(..)) "
        + "&& @annotation(filter) ")
public void filterMap(final Filter filter) {
    /* ... */
}

@AfterReturning(value="filterCollection(filter)", returning="collection")
public void handlingReturnCollection(final Filter filter, Collection collection) {
    System.out.println(collection);
}

@AfterReturning(value="filterMap(filter)", returning="map")
public void handlingReturnMap(final Filter filter, Map map) {
    System.out.println(map);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文