Java Code Examples for org.springframework.core.LocalVariableTableParameterNameDiscoverer#getParameterNames()

The following examples show how to use org.springframework.core.LocalVariableTableParameterNameDiscoverer#getParameterNames() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogServiceImpl.java    From FEBS-Cloud with Apache License 2.0 6 votes vote down vote up
@Override
public void saveLog(ProceedingJoinPoint point, Method method, String ip, String operation, String username, long start) {
    Log log = new Log();
    log.setIp(ip);

    log.setUsername(username);
    log.setTime(System.currentTimeMillis() - start);
    log.setOperation(operation);

    String className = point.getTarget().getClass().getName();
    String methodName = method.getName();
    log.setMethod(className + "." + methodName + "()");

    Object[] args = point.getArgs();
    LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
    String[] paramNames = u.getParameterNames(method);
    if (args != null && paramNames != null) {
        StringBuilder params = new StringBuilder();
        params = handleParams(params, args, Arrays.asList(paramNames));
        log.setParams(params.toString());
    }
    log.setCreateTime(new Date());
    log.setLocation(AddressUtil.getCityInfo(ip));
    // 保存系统日志
    save(log);
}
 
Example 2
Source File: AbstractBeansMetadata.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected List<MethodParameterInfo> getMethodParameters(Method method) {
    ArrayList<MethodParameterInfo> params = new ArrayList<>();

    Class<?>[] parameterTypes = method.getParameterTypes();

    LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
    String[] parameterNames = parameterNameDiscoverer.getParameterNames(method);

    if (parameterTypes != null) {
        for (int i = 0; i < parameterTypes.length; i++) {
            String parameterName = parameterNames != null ? parameterNames[i] : "arg" + i;
            MethodParameterInfo parameterInfo = new MethodParameterInfo(parameterTypes[i].getName(), parameterName, null);
            params.add(parameterInfo);
        }
    }
    return params;
}
 
Example 3
Source File: AbstractRequestLockInterceptor.java    From jim-framework with Apache License 2.0 6 votes vote down vote up
private String getLockKey(Method method,String targetName, String methodName, String[] keys, Object[] arguments) {

        StringBuilder sb = new StringBuilder();
        sb.append("lock.").append(targetName).append(".").append(methodName);
        if(keys != null) {
            String keyStr = Joiner.on(".").skipNulls().join(keys);
            if(!StringUtils.isBlank(keyStr)) {
                LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
                String[] parameters =discoverer.getParameterNames(method);
                ExpressionParser parser = new SpelExpressionParser();
                Expression expression = parser.parseExpression(keyStr);
                EvaluationContext context = new StandardEvaluationContext();
                int length = parameters.length;
                if (length > 0) {
                    for (int i = 0; i < length; i++) {
                        context.setVariable(parameters[i], arguments[i]);
                    }
                }
                String keysValue = expression.getValue(context, String.class);
                sb.append("#").append(keysValue);
            }
        }
        return sb.toString();
    }
 
Example 4
Source File: LimitAop.java    From distributed-limit with Apache License 2.0 6 votes vote down vote up
/**
 * 该方法只适用cglib
 *
 * @param key
 * @param method
 * @param args
 * @return
 */
private String parseKey(String key, Method method, Object[] args) {

    //如果不含# 则判断不为spel表达式,这有一定的误差,但是够用
    // fixme
    //TODO
    if (!key.contains( "#" ) || StringUtils.isEmpty( key )) {
        return key;
    }
    //获取被拦截方法参数名列表(使用Spring支持类库)
    LocalVariableTableParameterNameDiscoverer u =
            new LocalVariableTableParameterNameDiscoverer();
    String[] paraNameArr = u.getParameterNames( method );

    //使用SPEL进行key的解析
    ExpressionParser parser = new SpelExpressionParser();
    //SPEL上下文
    StandardEvaluationContext context = new StandardEvaluationContext();
    //把方法参数放入SPEL上下文中
    for (int i = 0; i < paraNameArr.length; i++) {
        context.setVariable( paraNameArr[i], args[i] );
    }
    return parser.parseExpression( key ).getValue( context, String.class );
}
 
Example 5
Source File: AutoLogAspect.java    From jeecg-boot with Apache License 2.0 6 votes vote down vote up
/**
* @Description: 获取请求参数
* @author: scott
* @date: 2020/4/16 0:10
* @param request:  request
* @param joinPoint:  joinPoint
* @Return: java.lang.String
*/
private String getReqestParams(HttpServletRequest request, JoinPoint joinPoint) {
	String httpMethod = request.getMethod();
	String params = "";
	if ("POST".equals(httpMethod) || "PUT".equals(httpMethod) || "PATCH".equals(httpMethod)) {
		Object[] paramsArray = joinPoint.getArgs();
		params = JSONObject.toJSONString(paramsArray);
	} else {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		// 请求的方法参数值
		Object[] args = joinPoint.getArgs();
		// 请求的方法参数名称
		LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
		String[] paramNames = u.getParameterNames(method);
		if (args != null && paramNames != null) {
			for (int i = 0; i < args.length; i++) {
				params += "  " + paramNames[i] + ": " + args[i];
			}
		}
	}
	return params;
}
 
Example 6
Source File: AutoLogAspect.java    From jeecg-cloud with Apache License 2.0 6 votes vote down vote up
/**
* @Description: 获取请求参数
* @author: scott
* @date: 2020/4/16 0:10
* @param request:  request
* @param joinPoint:  joinPoint
* @Return: java.lang.String
*/
private String getReqestParams(HttpServletRequest request, JoinPoint joinPoint) {
	String httpMethod = request.getMethod();
	String params = "";
	if ("POST".equals(httpMethod) || "PUT".equals(httpMethod) || "PATCH".equals(httpMethod)) {
		Object[] paramsArray = joinPoint.getArgs();
		params = JSONObject.toJSONString(paramsArray);
	} else {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		// 请求的方法参数值
		Object[] args = joinPoint.getArgs();
		// 请求的方法参数名称
		LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
		String[] paramNames = u.getParameterNames(method);
		if (args != null && paramNames != null) {
			for (int i = 0; i < args.length; i++) {
				params += "  " + paramNames[i] + ": " + args[i];
			}
		}
	}
	return params;
}
 
Example 7
Source File: SpelUtil.java    From mall4j with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 支持 #p0 参数索引的表达式解析
 * @param rootObject 根对象,method 所在的对象
 * @param spel 表达式
 * @param method ,目标方法
 * @param args 方法入参
 * @return 解析后的字符串
 */
public static String parse(Object rootObject,String spel, Method method, Object[] args) {
    if (StrUtil.isBlank(spel)) {
        return StrUtil.EMPTY;
    }
    //获取被拦截方法参数名列表(使用Spring支持类库)
    LocalVariableTableParameterNameDiscoverer u =
            new LocalVariableTableParameterNameDiscoverer();
    String[] paraNameArr = u.getParameterNames(method);
    if (ArrayUtil.isEmpty(paraNameArr)) {
        return spel;
    }
    //使用SPEL进行key的解析
    ExpressionParser parser = new SpelExpressionParser();
    //SPEL上下文
    StandardEvaluationContext context = new MethodBasedEvaluationContext(rootObject,method,args,u);
    //把方法参数放入SPEL上下文中
    for (int i = 0; i < paraNameArr.length; i++) {
        context.setVariable(paraNameArr[i], args[i]);
    }
    return parser.parseExpression(spel).getValue(context, String.class);
}
 
Example 8
Source File: DistributedLockAspect.java    From gpmall with Apache License 2.0 6 votes vote down vote up
/**
 * 获取缓存的key
 * key 定义在注解上,支持SPEL表达式
 */
private String parseKey(String key, Method method, Object[] args) {
    //获取被拦截方法参数名列表(使用Spring支持类库)
    LocalVariableTableParameterNameDiscoverer u =
            new LocalVariableTableParameterNameDiscoverer();
    String[] paraNameArr = u.getParameterNames(method);

    //使用SPEL进行key的解析
    ExpressionParser parser = new SpelExpressionParser();
    //SPEL上下文
    StandardEvaluationContext context = new StandardEvaluationContext();
    //把方法参数放入SPEL上下文中
    for (int i = 0; i < paraNameArr.length; i++) {
        context.setVariable(paraNameArr[i], args[i]);
    }
    return parser.parseExpression(key).getValue(context, String.class);
}
 
Example 9
Source File: VisitLogAspect.java    From DimpleBlog with Apache License 2.0 5 votes vote down vote up
/**
 * 获取PageId
 *
 * @param joinPoint 切入点
 * @return PageId
 */
private Long getPageId(VLog vLog, JoinPoint joinPoint) throws NoSuchMethodException {
    String pageIdStr = vLog.pageId();
    if (StringUtils.isEmpty(pageIdStr)) {
        return null;
    }
    //get target method
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    Method method = joinPoint.getTarget().getClass().getMethod(methodSignature.getMethod().getName(), methodSignature.getMethod().getParameterTypes());
    //express SpEL
    ExpressionParser expressionParser = new SpelExpressionParser();
    LocalVariableTableParameterNameDiscoverer localVariableTableParameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
    String[] params = localVariableTableParameterNameDiscoverer.getParameterNames(method);

    Object[] args = joinPoint.getArgs();
    EvaluationContext context = new StandardEvaluationContext();
    for (int i = 0; i < params.length; i++) {
        context.setVariable(params[i], args[i]);
    }

    Expression expression = expressionParser.parseExpression(pageIdStr);
    Object value = expression.getValue(context);

    if (value == null) {
        return null;
    }
    try {
        return (Long) value;
    } catch (Exception e) {
        log.error("get pageId error for parameters {}", value);
        return null;
    }
}
 
Example 10
Source File: OperationLogAspect.java    From Shiro-Action with MIT License 5 votes vote down vote up
private void saveLog(ProceedingJoinPoint joinPoint, long time) {
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();

    SysLog sysLog = new SysLog();

    // 获取注解上的操作描述
    OperationLog operationLogAnnotation = method.getAnnotation(OperationLog.class);
    if (operationLogAnnotation != null) {
        sysLog.setOperation(operationLogAnnotation.value());
    }

    // 请求的方法名
    String className = joinPoint.getTarget().getClass().getName();
    String methodName = signature.getName();
    sysLog.setMethod(className + "." + methodName + "()");

    // 请求的方法参数
    Object[] args = joinPoint.getArgs();
    LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
    String[] paramNames = parameterNameDiscoverer.getParameterNames(method);
    if (args != null && paramNames != null) {
        StringBuilder params = new StringBuilder();
        for (int i = 0; i < args.length; i++) {
            params.append("  ").append(paramNames[i]).append(": ").append(args[i]);
        }
        sysLog.setParams(params.toString());
    }

    sysLog.setIp(IPUtils.getIpAddr());

    // 获取当前登录用户名
    if (SecurityUtils.getSubject().isAuthenticated()) {
        User user = ShiroUtil.getCurrentUser();
        sysLog.setUsername(user.getUsername());
    }

    sysLog.setTime((int) time);
    sysLogMapper.insert(sysLog);
}
 
Example 11
Source File: LogAspect.java    From FEBS-Security with Apache License 2.0 5 votes vote down vote up
@Async
public void saveLog(ProceedingJoinPoint joinPoint, long time, String ip) throws IOException {
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    String principalJson = objectMapper.writeValueAsString(principal);
    JsonNode node = objectMapper.readTree(principalJson);
    String usrename = node.get("username").asText();
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();
    SysLog log = new SysLog();
    Log logAnnotation = method.getAnnotation(Log.class);
    if (logAnnotation != null) {
        // 注解上的描述
        log.setOperation(logAnnotation.value());
    }
    // 请求的类名
    String className = joinPoint.getTarget().getClass().getName();
    // 请求的方法名
    String methodName = signature.getName();
    log.setMethod(className + "." + methodName + "()");
    // 请求的方法参数值
    Object[] args = joinPoint.getArgs();
    // 请求的方法参数名称
    LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
    String[] paramNames = u.getParameterNames(method);
    if (args != null && paramNames != null) {
        StringBuilder params = new StringBuilder();
        params = handleParams(params, args, Arrays.asList(paramNames));
        log.setParams(params.toString());
    }

    // 设置IP地址
    log.setIp(ip);
    log.setUsername(usrename);
    log.setTime(time);
    log.setCreateTime(new Date());
    log.setLocation(AddressUtils.getCityInfo(log.getIp()));
    // 保存系统日志
    this.logService.save(log);
}
 
Example 12
Source File: LogAspect.java    From SpringAll with MIT License 5 votes vote down vote up
private void saveLog(ProceedingJoinPoint joinPoint, long time) {
	MethodSignature signature = (MethodSignature) joinPoint.getSignature();
	Method method = signature.getMethod();
	SysLog sysLog = new SysLog();
	Log logAnnotation = method.getAnnotation(Log.class);
	if (logAnnotation != null) {
		// 注解上的描述
		sysLog.setOperation(logAnnotation.value());
	}
	// 请求的方法名
	String className = joinPoint.getTarget().getClass().getName();
	String methodName = signature.getName();
	sysLog.setMethod(className + "." + methodName + "()");
	// 请求的方法参数值
	Object[] args = joinPoint.getArgs();
	// 请求的方法参数名称
	LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
	String[] paramNames = u.getParameterNames(method);
	if (args != null && paramNames != null) {
		String params = "";
		for (int i = 0; i < args.length; i++) {
			params += "  " + paramNames[i] + ": " + args[i];
		}
		sysLog.setParams(params);
	}
	// 获取request
	HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
	// 设置IP地址
	sysLog.setIp(IPUtils.getIpAddr(request));
	// 模拟一个用户名
	sysLog.setUsername("mrbird");
	sysLog.setTime((int) time);
	Date date = new Date();
	sysLog.setCreateTime(date);
	// 保存系统日志
	sysLogDao.saveSysLog(sysLog);
}
 
Example 13
Source File: AopUtil.java    From phone with Apache License 2.0 5 votes vote down vote up
/**
 * 获取参数名列表
 * 
 * @param method
 * @return
 */
public static String[] getMethodParamNames(Method method) {
	// 获取被拦截方法参数名列表(使用Spring支持类库)
	LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
	String[] paraNameArr = u.getParameterNames(method);
	return paraNameArr;
}
 
Example 14
Source File: AopUtil.java    From phone with Apache License 2.0 5 votes vote down vote up
/**
 * 获取参数名列表
 * 
 * @param method
 * @return
 */
public static String[] getMethodParamNames(Method method) {
	// 获取被拦截方法参数名列表(使用Spring支持类库)
	LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
	String[] paraNameArr = u.getParameterNames(method);
	return paraNameArr;
}
 
Example 15
Source File: MethodLockInterceptor.java    From common-project with Apache License 2.0 4 votes vote down vote up
public String[] getParamterNames(Method method){
    LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
    return  u.getParameterNames(method);
}
 
Example 16
Source File: LogAspect.java    From erp-framework with MIT License 4 votes vote down vote up
@Async
    private void handleLog(final JoinPoint joinPoint, final Exception e, Object rvt) throws Exception{
        // 获得注解
        Method method = getMethod(joinPoint);
        Log log = getAnnotationLog(method);
        if (log == null) {
            return;
        }
        Date now = new Date();
        // 操作数据库日志表
        ErpLog erpLog = new ErpLog();
        erpLog.setErrorCode(0);
        erpLog.setIsDeleted(0);
        // 请求信息
        HttpServletRequest request = ToolUtil.getRequest();
        erpLog.setType(ToolUtil.isAjaxRequest(request) ? "Ajax请求" : "普通请求");
        erpLog.setTitle(log.value());
        erpLog.setHost(ToolUtil.getClientIp(request));
        erpLog.setUri(request.getRequestURI().toString());
//        erpLog.setHeader(request.getHeader(HttpHeaders.USER_AGENT));
        erpLog.setHttpMethod(request.getMethod());
        erpLog.setClassMethod(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        // 请求的方法参数值
        Object[] args = joinPoint.getArgs();
        // 请求的方法参数名称
        LocalVariableTableParameterNameDiscoverer u
                = new LocalVariableTableParameterNameDiscoverer();
        String[] paramNames = u.getParameterNames(method);
        if (args != null && paramNames != null) {
            StringBuilder params = new StringBuilder();
            params = handleParams(params, args, Arrays.asList(paramNames));
            erpLog.setParams(params.toString());
        }
        String retString = JsonUtil.bean2Json(rvt);
        erpLog.setResponseValue(retString.length() > 5000 ? JsonUtil.bean2Json("请求参数数据过长不与显示") : retString);
        if (e != null) {
            erpLog.setErrorCode(1);
            erpLog.setErrorMessage(e.getMessage());
        }
        Date stime = startTime.get();
        erpLog.setStartTime(stime);
        erpLog.setEndTime(now);
        erpLog.setExecuteTime(now.getTime() - stime.getTime());
        erpLog.setUsername(MySysUser.loginName());
        HashMap<String, String> browserMap = ToolUtil.getOsAndBrowserInfo(request);
        erpLog.setOperatingSystem(browserMap.get("os"));
        erpLog.setBrower(browserMap.get("browser"));
        erpLog.setId(IdUtil.simpleUUID());
        logService.insertSelective(erpLog);
    }
 
Example 17
Source File: SpringUtils.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public static String[] getConstructorNames(Constructor<?> targetConstructor){
	LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
	return discoverer.getParameterNames(targetConstructor);
}