Java注解动态解析获取方法参数值

时间:2025-03-19 10:21:18
/** * @author wangdi * @date 21-9-2 */ @Component @Slf4j public class ResolverKit { private static final String PREFIX = "#{"; private static final String SUBFIX = "}"; /** * @param contextEL 带表达式的内容 * 举例:"用户名:+#{}+,用户密码:+#{}+,用户年龄:+#{}+,手机号:+#{}" * 注意:支持map对象获取key(),但是不支持在对象中嵌套map() * @param proceedingJoinPoint * @return */ public StringBuffer resolverContent(String contextEL, ProceedingJoinPoint proceedingJoinPoint) { try { StringBuffer context = new StringBuffer(); /** * 文字+#{}+,+#{}+#{}+. * 拆分 * * 文字 * #{} * , * #{} * #{} * . */ if (contextEL.contains(PREFIX) && contextEL.contains(SUBFIX) && contextEL.contains("+")) { List<String> strings = Arrays.asList(contextEL.split("\\+")); for (String s : strings) { if (s.contains(PREFIX) && s.contains(SUBFIX)) { // 解析表达式 String spel = s.substring(s.indexOf(PREFIX) + PREFIX.length(), s.lastIndexOf(SUBFIX)); context.append(resolverExpression(spel, proceedingJoinPoint)); } else { // 不需要解析 context.append(s); } } } else { context.append(contextEL); } return context; } catch (Exception e) { log.error("表达式解析错误, 请检查@Log注解"); return null; } } /** * 解析表达式(可嵌套两层) * * @param expression 举例: #{} #{} #{} * @param proceedingJoinPoint * @return */ private String resolverExpression(String expression, ProceedingJoinPoint proceedingJoinPoint) throws NoSuchFieldException, IllegalAccessException { String first = null; String second = null; String third = null; if (expression.contains(".")) { List<String> list = Arrays.asList(expression.split("\\.")); if (list.size() > 3) throw new RuntimeException("不支持三个变量以上的表达式: " + expression); for (int i = 0; i < list.size(); i++) { if (0 == i) first = list.get(i); if (1 == i) second = list.get(i); if (2 == i) third = list.get(i); } } else { first = expression; } //获取参数值 Object[] args = proceedingJoinPoint.getArgs(); //获取运行时参数的名称 Method method = ((MethodSignature) proceedingJoinPoint.getSignature()).getMethod(); //获取参数值类型 Class<?>[] parameterTypes = method.getParameterTypes(); //获取参数值的数据 String[] parameterNames = new DefaultParameterNameDiscoverer().getParameterNames(method); for (int i = 0; i < parameterNames.length; i++) { Class<?> parameterType = parameterTypes[i]; String parameterName = parameterNames[i]; Object parameterValue = args[i]; if (first.equals(parameterName)) { if (null == second) { return (String) parameterValue; } if (null != second) { // if (parameterValue instanceof Map<?, ?>) { return (String) ((Map) parameterValue).get(second); } else { return (String) getFieldValue(parameterValue, second + (null != third ? "." + third : "")); } } } } return null; } /** * 通过反射取对象指定字段(属性)的值 * * @param target 目标对象 * @param fieldName 字段的名字 * @return 字段的值 */ private Object getFieldValue(Object target, String fieldName) throws NoSuchFieldException, IllegalAccessException { Class<?> clazz = target.getClass(); String[] fs = fieldName.split("\\."); try { for (int i = 0; i < fs.length - 1; i++) { Field f = clazz.getDeclaredField(fs[i]); f.setAccessible(true); target = f.get(target); if (null == target) { // 嵌套内容中为null的返回null, 防止报错 return null; } clazz = target.getClass(); } Field f = clazz.getDeclaredField(fs[fs.length - 1]); f.setAccessible(true); return f.get(target); } catch (Exception e) { throw e; } } }