All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.doudou.common.aop.aspect.AspectAnnotationUtils Maven / Gradle / Ivy

package top.doudou.common.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.HttpRequestMethodNotSupportedException;

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

/**
 * @author  傻男人<[email protected]>
 * @Date: 2020/8/21 13:25
 * @Version: 1.0
 * @Description: 切面注解的工具类
 */
public class AspectAnnotationUtils {

    /**
     * 是否存在注解,如果存在就获取
     */
    public static  T getAnnotation(JoinPoint joinPoint, Class annotationClass) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null) {
            return method.getAnnotation(annotationClass);
        }
        return null;
    }


    @SuppressWarnings("SameParameterValue")
    public static  T getMethodAnnotation(ProceedingJoinPoint joinPoint, Class annotationClass) {
        return ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(annotationClass);
    }

    @SuppressWarnings("SameParameterValue")
    public static  T getClassAnnotation(ProceedingJoinPoint joinPoint, Class annotationClass) {
        return ((MethodSignature) joinPoint.getSignature()).getMethod().getDeclaringClass().getAnnotation(annotationClass);
    }

    /**
     * 系统的错误
     * @param ex
     * @return
     */
    public static String getErrorMsg(Exception ex) {
        if (ex instanceof RuntimeException) {
            return ex.getMessage();
        } else if (ex instanceof HttpRequestMethodNotSupportedException) {
            return "请求方法不支持";
        } else {
            return "系统错误";
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy