

package org.fastquery.analysis;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

import org.fastquery.core.Modifying;
import org.fastquery.core.Query;
import org.fastquery.core.QueryBuilder;
import org.fastquery.core.QueryByNamed;
import org.fastquery.util.TypeUtil;

/**
 * 在QueryRepository中 {@link Modifying} 依赖检测 <br>
 * Modifying 要么跟 Query 组合, 要么跟QueryByNamed组合 不能独存
 *
 * @author xixifeng (fastquery@126.com)
 */
class ModifyingDependencyFilter implements MethodFilter
{

    @Override
    public void doFilter(Method method)
    {
        Modifying m = method.getAnnotation(Modifying.class);
        int queryLen = method.getAnnotationsByType(Query.class).length;
        QueryByNamed queryByNamed = method.getAnnotation(QueryByNamed.class);
        Parameter[] parameters = method.getParameters();
        boolean hasQueryBuilder = TypeUtil.hasType(QueryBuilder.class, parameters);
        if (m != null && queryLen == 0 && queryByNamed == null && !hasQueryBuilder)
        {
            this.abortWith(method, "@Modifying 要么跟 @Query 组合, 要么跟@QueryByNamed组合, 要么跟QueryBuilder参数组合!");
        }
    }

}
