package com.qiaoba.common.base.util;

import cn.hutool.core.util.ClassUtil;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 注解Util
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/6/21 9:53
 */
public class AnnotationUtil extends cn.hutool.core.annotation.AnnotationUtil {

    public static boolean hasAnnotation(String referenceMethod, Class<? extends Annotation> annotation) {
        // com.qiaoba.common.base.util.AnnotationUtil.hasAnnotation
        StringBuilder sb = new StringBuilder(referenceMethod);
        int index = sb.lastIndexOf(".");
        // com.qiaoba.common.base.util.AnnotationUtil
        String clazzName = sb.substring(0, index);
        // hasAnnotation
        String methodName = sb.substring(index + 1, sb.length());
        Class<?> clazz = ClassUtil.loadClass(clazzName);
        for (Method method : clazz.getDeclaredMethods()) {
            if (methodName.equals(method.getName())) {
                return hasAnnotation(method, annotation);
            }
        }
        return false;
    }
}
