0

我有一个方面(用 aspectj 语法编写public aspect ....),它操纵许多返回 a 的方法的结果,Collection以便对结果进行分页。

我不能(或者我不知道如何)在建议中使用泛型,所以无论我调用建议的方法之一,我都会收到警告

unchecked conversion when advice applied at shadow method-call(java.util.Collection ...), expected java.util.Collection<T> but advice uses java.util.Collection [Xlint:uncheckedAdviceConversion]

我怎样才能摆脱这个?

斯特凡诺

-- 添加方面代码摘录

public aspect PaginationSupportAspect extends ServiceSupportAspect {

    private static final Logger log = LoggerFactory.getLogger(PaginationSupportAspect.class);

    private pointcut paginate(Criteria criteria) : call(Collection org.jcz.persistence.Service+.*(Criteria));

    @SuppressWarnings({ "unchecked", "rawtypes" })
    Collection around(Criteria criteria, Service service) : paginate(criteria) && target(service) {
        log.trace(String.format("Executing <%s> @ <%s>", this.getClass(), thisJoinPoint.toLongString()));

        return doPaginate(proceed(criteria, service), criteria.getPagination(), service);
    }

    /**
     * 
     * @param source
     * @param criteria
     * @return
     */
    private <T extends EntitySupport> Collection<T> doPaginate(Collection<T> source, Pagination pagination, Service<T> service) {
        // IMPLEMENTATION OMITTED BECAUSE IT'S QUITE LONG

        // What it does is to create another Collection<T> that holds the pagination coordinates (page number, size, count) 
        // and only the elements of the requested page
    }

}

使用示例

Collection<MyEntity> entities = myEntityService.findAll(criteria);

这就是我收到警告的地方

4

1 回答 1

1

坏消息:我不知道如何避免警告,因为我对 Java 泛型不是那么精通,特别是与 AspectJ 无关,因为在语法上并没有您可能期望的所有内容,例如将 绑定target()到参数化类型(因为类型擦除限制)。

可能您在 AspectJ 邮件列表中遇到了与问题相同的问题。

好消息:我终于设法用最小的代码示例重现了您的问题,使用了许多虚拟类,对您如何在应用程序中使用它们进行了一些有根据的猜测:

虚拟/助手类/方面:

package org.jcz.persistence;
public class EntitySupport {}
package org.jcz.persistence;
public class MyEntity {}
package org.jcz.persistence;
public class Pagination {}
package org.jcz.persistence;

public class Criteria {
    public Pagination getPagination() {
        return new Pagination();
    }
}
package org.jcz.persistence;

import java.util.ArrayList;
import java.util.Collection;

public class Service<T> {
    public Collection<T> findAll(Criteria criteria) {
        return new ArrayList<T>();
    }
}
package org.jcz.persistence;
public abstract aspect ServiceSupportAspect {}

你的方面,略有变化:

package org.jcz.persistence;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.log4j.Logger;

public aspect PaginationSupportAspect extends ServiceSupportAspect {
    private static final Logger log = Logger.getLogger(PaginationSupportAspect.class);

    private pointcut paginate(Criteria criteria) :
        call(Collection org.jcz.persistence.Service+.*(Criteria)) && args(criteria);

    @SuppressWarnings({ "unchecked", "rawtypes" })
    Collection around(Criteria criteria, Service service) : paginate(criteria) && target(service) {
        log.trace(String.format("Executing <%s> @ <%s>", this.getClass(), thisJoinPoint.toLongString()));
        return doPaginate(proceed(criteria, service), criteria.getPagination(), service);
    }

    private <T extends EntitySupport> Collection<T> doPaginate(
            Collection<T> source, Pagination pagination, Service<T> service)
    {
        System.out.println("doPaginate");
        return new ArrayList<T>();
    }
}

现在是一个小驱动程序应用程序重现 Xlint 警告:

import java.util.Collection;

import org.jcz.persistence.Criteria;
import org.jcz.persistence.MyEntity;
import org.jcz.persistence.Service;

public class Application {
    public static void main(String[] args) {
        Service<MyEntity> myEntityService = new Service<MyEntity>();
        Criteria criteria = new Criteria();
        Collection<MyEntity> entities = myEntityService.findAll(criteria);
        System.out.println("Found entities: " + entities);
    }
}

也许其他人可以做出一些我做不到的事情。我能做的最好的就是复制它。我将尝试将 Andy Clement 指向这篇文章。祝你好运!

于 2013-03-16T11:48:58.937 回答