|
@@ -0,0 +1,88 @@
|
|
|
+package com.elab.spring.intercept;
|
|
|
+
|
|
|
+import com.elab.core.aop.annotations.ExceptionHandle;
|
|
|
+import com.elab.core.exception.CheckException;
|
|
|
+import com.elab.core.utils.StringUtils;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.aop.support.AopUtils;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
|
|
+import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.lang.reflect.Method;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @describe :代码检测
|
|
|
+ * @Author : liukx
|
|
|
+ * @time : 2019/3/6 - 11:44
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class CheckAnnotationProcess implements BeanPostProcessor, BeanFactoryPostProcessor {
|
|
|
+
|
|
|
+ private Logger logger = LoggerFactory.getLogger(com.elab.spring.process.CheckAnnotationProcess.class);
|
|
|
+ private ConfigurableListableBeanFactory beanFactory;
|
|
|
+ private boolean isException = false;
|
|
|
+
|
|
|
+ public void setException(boolean exception) {
|
|
|
+ isException = exception;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
|
|
+ this.beanFactory = beanFactory;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
|
|
+ String exclude = "com.elab.marketing.auth.dao";
|
|
|
+ String proxyName = "com.sun.proxy";
|
|
|
+ if (AopUtils.isAopProxy(bean)) {
|
|
|
+ Class<?> targetClass = AopUtils.getTargetClass(bean);
|
|
|
+ String name = targetClass.getName();
|
|
|
+ if (!name.startsWith(exclude) && !name.startsWith(proxyName)) {
|
|
|
+ Method[] declaredMethods = targetClass.getDeclaredMethods();
|
|
|
+ for (int i = 0; i < declaredMethods.length; i++) {
|
|
|
+ Method declaredMethod = declaredMethods[i];
|
|
|
+ String author = "";
|
|
|
+ Transactional annotation = declaredMethod.getAnnotation(Transactional.class);
|
|
|
+ ApiOperation apiOperation = declaredMethod.getAnnotation(ApiOperation.class);
|
|
|
+ if (apiOperation != null && StringUtils.isNotEmpty(apiOperation.nickname())) {
|
|
|
+ author = apiOperation.nickname();
|
|
|
+ }
|
|
|
+ if (annotation != null || apiOperation != null) {
|
|
|
+ ExceptionHandle exceptionHandle = declaredMethod.getAnnotation(ExceptionHandle.class);
|
|
|
+ if (exceptionHandle != null) {
|
|
|
+ if (StringUtils.isEmpty(exceptionHandle.username())) {
|
|
|
+ String message = "请在" + name + " \t " + declaredMethod.getName() + " --> " + author + " 方法头的@ExceptionHandle 加上username属性";
|
|
|
+ notifyWarning(message);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ String message = "请在" + name + " \t " + declaredMethod.getName() + " --> " + author + " 方法头加上 @ExceptionHandle(username = '')";
|
|
|
+ notifyWarning(message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void notifyWarning(String message) {
|
|
|
+ if (isException) {
|
|
|
+ throw new CheckException(message);
|
|
|
+ } else {
|
|
|
+ logger.error(message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|