1. 程式人生 > 其它 >獲取引數,把方法上的引數繫結到註解的變數中

獲取引數,把方法上的引數繫結到註解的變數中

package com.geekmore.modules.device.aop; 

import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;


/**
 * 該類的作用可以把方法上的引數繫結到註解的變數中,註解的語法#{變數名}
 * 能解析類似#{task}或者#{task.taskName}或者{task.project.projectName}
 */
public class AnnotationResolver {

   private static AnnotationResolver resolver ;


   public static AnnotationResolver newInstance(){

      if (resolver == null) {
         return resolver = new AnnotationResolver();
      }else{
         return resolver;
      }

   }

   /**
    * 解析註解上的值
    * @param joinPoint
    * @param str 需要解析的字串
    * @return
    */
   public Object resolver(JoinPoint joinPoint, String str) {

      if (str == null) return null ;

      Object value = null;
      if (str.matches("#\\{\\D*\\}")) {// 如果name匹配上了#{},則把內容當作變數
         String newStr = str.replaceAll("#\\{", "").replaceAll("\\}", "");
         if (newStr.contains(".")) { // 複雜型別
            try {
               value = complexResolver(joinPoint, newStr);
            } catch (Exception e) {
               e.printStackTrace();
            }
         } else { 
            value = simpleResolver(joinPoint, newStr);
         }
      } else { //非變數
         value = str;
      }
      return value;
   }


   private Object complexResolver(JoinPoint joinPoint, String str) throws Exception {

      MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();

      String[] names = methodSignature.getParameterNames();
      Object[] args = joinPoint.getArgs();
      String[] strs = str.split("\\.");

      for (int i = 0; i < names.length; i++) {
         if (strs[0].equals(names[i])) {
            Object obj = args[i];
            Method dmethod = obj.getClass().getDeclaredMethod(getMethodName(strs[1]), null);
            Object value = dmethod.invoke(args[i]);
            return getValue(value, 1, strs);
         }
      }

      return null;

   }

   private Object getValue(Object obj, int index, String[] strs) {

      try {
         if (obj != null && index < strs.length - 1) {
            Method method = obj.getClass().getDeclaredMethod(getMethodName(strs[index + 1]), null);
            obj = method.invoke(obj);
            getValue(obj, index + 1, strs);
         }

         return obj;

      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
   }

   private String getMethodName(String name) {
      return "get" + name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
   }


   private Object simpleResolver(JoinPoint joinPoint, String str) {
      MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
      String[] names = methodSignature.getParameterNames();
      Object[] args = joinPoint.getArgs();

      for (int i = 0; i < names.length; i++) {
         if (str.equals(names[i])) {
            return args[i];
         }
      }
      return null;
   }

}