Java Code Examples for org.springframework.core.LocalVariableTableParameterNameDiscoverer

The following examples show how to use org.springframework.core.LocalVariableTableParameterNameDiscoverer. These examples are extracted from open source projects. 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
@Before
public void setup() throws Exception {
	this.resolver = new PayloadMethodArgumentResolver(new StringMessageConverter(), testValidator());

	Method payloadMethod = PayloadMethodArgumentResolverTests.class.getDeclaredMethod(
			"handleMessage", String.class, String.class, Locale.class,
			String.class, String.class, String.class, String.class);

	this.paramAnnotated = new SynthesizingMethodParameter(payloadMethod, 0);
	this.paramAnnotatedNotRequired = new SynthesizingMethodParameter(payloadMethod, 1);
	this.paramAnnotatedRequired = new SynthesizingMethodParameter(payloadMethod, 2);
	this.paramWithSpelExpression = new SynthesizingMethodParameter(payloadMethod, 3);
	this.paramValidated = new SynthesizingMethodParameter(payloadMethod, 4);
	this.paramValidated.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
	this.paramValidatedNotAnnotated = new SynthesizingMethodParameter(payloadMethod, 5);
	this.paramNotAnnotated = new SynthesizingMethodParameter(payloadMethod, 6);
}
 
Example 2
@Ignore
@Test
public void testAutowireCandidateWithConstructorDescriptor() throws Exception {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
	lbf.registerBeanDefinition(JUERGEN, person1);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	lbf.registerBeanDefinition(MARK, person2);
	MethodParameter param = new MethodParameter(QualifiedTestBean.class.getDeclaredConstructor(Person.class), 0);
	DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(param, false);
	param.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
	assertEquals("tpb", param.getParameterName());
	assertTrue(lbf.isAutowireCandidate(JUERGEN, null));
	assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor));
	assertFalse(lbf.isAutowireCandidate(MARK, qualifiedDescriptor));
}
 
Example 3
Source Project: FEBS-Cloud   Source File: LogServiceImpl.java    License: 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 4
Source Project: gpmall   Source File: DistributedLockAspect.java    License: 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 5
Source Project: mall4j   Source File: SpelUtil.java    License: 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 6
Source Project: jeecg-cloud   Source File: AutoLogAspect.java    License: 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
@Before
public void setup() throws Exception {

	this.resolver = new PayloadArgumentResolver(new StringMessageConverter(), testValidator());

	Method payloadMethod = PayloadArgumentResolverTests.class.getDeclaredMethod(
			"handleMessage", String.class, String.class, Locale.class,
			String.class, String.class, String.class, String.class);

	this.paramAnnotated = new SynthesizingMethodParameter(payloadMethod, 0);
	this.paramAnnotatedNotRequired = new SynthesizingMethodParameter(payloadMethod, 1);
	this.paramAnnotatedRequired = new SynthesizingMethodParameter(payloadMethod, 2);
	this.paramWithSpelExpression = new SynthesizingMethodParameter(payloadMethod, 3);
	this.paramValidated = new SynthesizingMethodParameter(payloadMethod, 4);
	this.paramValidated.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
	this.paramValidatedNotAnnotated = new SynthesizingMethodParameter(payloadMethod, 5);
	this.paramNotAnnotated = new SynthesizingMethodParameter(payloadMethod, 6);
}
 
Example 8
@Ignore
@Test
public void testAutowireCandidateWithConstructorDescriptor() throws Exception {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
	lbf.registerBeanDefinition(JUERGEN, person1);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	lbf.registerBeanDefinition(MARK, person2);
	MethodParameter param = new MethodParameter(QualifiedTestBean.class.getDeclaredConstructor(Person.class), 0);
	DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(param, false);
	param.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
	assertEquals("tpb", param.getParameterName());
	assertTrue(lbf.isAutowireCandidate(JUERGEN, null));
	assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor));
	assertFalse(lbf.isAutowireCandidate(MARK, qualifiedDescriptor));
}
 
Example 9
Source Project: jeecg-boot   Source File: AutoLogAspect.java    License: 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 10
Source Project: distributed-limit   Source File: LimitAop.java    License: 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 11
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 12
@Before
public void setUp() throws Exception {
	this.resolver = new MatrixVariableMethodArgumentResolver();

	Method method = getClass().getMethod("handle", String.class, List.class, int.class);
	this.paramString = new MethodParameter(method, 0);
	this.paramColors = new MethodParameter(method, 1);
	this.paramYear = new MethodParameter(method, 2);

	this.paramColors.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());

	this.mavContainer = new ModelAndViewContainer();
	this.request = new MockHttpServletRequest();
	this.webRequest = new ServletWebRequest(request, new MockHttpServletResponse());

	Map<String, MultiValueMap<String, String>> params = new LinkedHashMap<String, MultiValueMap<String, String>>();
	this.request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, params);
}
 
Example 13
@Ignore
@Test
public void testAutowireCandidateWithConstructorDescriptor() throws Exception {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
	lbf.registerBeanDefinition(JUERGEN, person1);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	lbf.registerBeanDefinition(MARK, person2);
	MethodParameter param = new MethodParameter(QualifiedTestBean.class.getDeclaredConstructor(Person.class), 0);
	DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(param, false);
	param.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
	assertEquals("tpb", param.getParameterName());
	assertTrue(lbf.isAutowireCandidate(JUERGEN, null));
	assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor));
	assertFalse(lbf.isAutowireCandidate(MARK, qualifiedDescriptor));
}
 
Example 14
Source Project: cuba   Source File: AbstractBeansMetadata.java    License: 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 15
private BindingContext createBindingContext(String methodName, Class<?>... parameterTypes) throws Exception {
	Object handler = new InitBinderHandler();
	Method method = handler.getClass().getMethod(methodName, parameterTypes);

	SyncInvocableHandlerMethod handlerMethod = new SyncInvocableHandlerMethod(handler, method);
	handlerMethod.setArgumentResolvers(new ArrayList<>(this.argumentResolvers));
	handlerMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());

	return new InitBinderBindingContext(this.bindingInitializer, Collections.singletonList(handlerMethod));
}
 
Example 16
Source Project: spring-analysis-note   Source File: ModelFactoryTests.java    License: MIT License 5 votes vote down vote up
private ModelFactory createModelFactory(String methodName, Class<?>... parameterTypes) throws Exception {
	HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
	resolvers.addResolver(new ModelMethodProcessor());

	InvocableHandlerMethod modelMethod = createHandlerMethod(methodName, parameterTypes);
	modelMethod.setHandlerMethodArgumentResolvers(resolvers);
	modelMethod.setDataBinderFactory(null);
	modelMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());

	return new ModelFactory(Collections.singletonList(modelMethod), null, this.attributeHandler);
}
 
Example 17
private WebDataBinderFactory createFactory(String methodName, Class<?>... parameterTypes)
		throws Exception {

	Object handler = new InitBinderHandler();
	Method method = handler.getClass().getMethod(methodName, parameterTypes);

	InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(handler, method);
	handlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
	handlerMethod.setDataBinderFactory(new DefaultDataBinderFactory(null));
	handlerMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());

	return new InitBinderDataBinderFactory(
			Collections.singletonList(handlerMethod), this.bindingInitializer);
}
 
Example 18
@Ignore
@Test
public void testAutowireCandidateWithMethodDescriptor() throws Exception {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
	lbf.registerBeanDefinition(JUERGEN, person1);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	lbf.registerBeanDefinition(MARK, person2);
	MethodParameter qualifiedParam =
			new MethodParameter(QualifiedTestBean.class.getDeclaredMethod("autowireQualified", Person.class), 0);
	MethodParameter nonqualifiedParam =
			new MethodParameter(QualifiedTestBean.class.getDeclaredMethod("autowireNonqualified", Person.class), 0);
	DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(qualifiedParam, false);
	DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor(nonqualifiedParam, false);
	qualifiedParam.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
	assertEquals("tpb", qualifiedParam.getParameterName());
	nonqualifiedParam.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
	assertEquals("tpb", nonqualifiedParam.getParameterName());
	assertTrue(lbf.isAutowireCandidate(JUERGEN, null));
	assertTrue(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor));
	assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor));
	assertTrue(lbf.isAutowireCandidate(MARK, null));
	assertTrue(lbf.isAutowireCandidate(MARK, nonqualifiedDescriptor));
	assertFalse(lbf.isAutowireCandidate(MARK, qualifiedDescriptor));
}
 
Example 19
Source Project: springdoc-openapi   Source File: AbstractRequestBuilder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Instantiates a new Abstract request builder.
 *
 * @param parameterBuilder the parameter builder
 * @param requestBodyBuilder the request body builder
 * @param operationBuilder the operation builder
 * @param parameterCustomizers the parameter customizers
 * @param localSpringDocParameterNameDiscoverer the local spring doc parameter name discoverer
 */
protected AbstractRequestBuilder(GenericParameterBuilder parameterBuilder, RequestBodyBuilder requestBodyBuilder,
		OperationBuilder operationBuilder, Optional<List<ParameterCustomizer>> parameterCustomizers,
		LocalVariableTableParameterNameDiscoverer localSpringDocParameterNameDiscoverer) {
	super();
	this.parameterBuilder = parameterBuilder;
	this.requestBodyBuilder = requestBodyBuilder;
	this.operationBuilder = operationBuilder;
	if (parameterCustomizers.isPresent())
		parameterCustomizers.get().removeIf(Objects::isNull);
	this.parameterCustomizers = parameterCustomizers;
	this.localSpringDocParameterNameDiscoverer = localSpringDocParameterNameDiscoverer;
}
 
Example 20
private BindingContext createBindingContext(String methodName, Class<?>... parameterTypes) throws Exception {
	Object handler = new InitBinderHandler();
	Method method = handler.getClass().getMethod(methodName, parameterTypes);

	SyncInvocableHandlerMethod handlerMethod = new SyncInvocableHandlerMethod(handler, method);
	handlerMethod.setArgumentResolvers(new ArrayList<>(this.argumentResolvers));
	handlerMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());

	return new InitBinderBindingContext(this.bindingInitializer, Collections.singletonList(handlerMethod));
}
 
Example 21
Source Project: java-technology-stack   Source File: ModelFactoryTests.java    License: MIT License 5 votes vote down vote up
private ModelFactory createModelFactory(String methodName, Class<?>... parameterTypes) throws Exception {
	HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
	resolvers.addResolver(new ModelMethodProcessor());

	InvocableHandlerMethod modelMethod = createHandlerMethod(methodName, parameterTypes);
	modelMethod.setHandlerMethodArgumentResolvers(resolvers);
	modelMethod.setDataBinderFactory(null);
	modelMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());

	return new ModelFactory(Collections.singletonList(modelMethod), null, this.attributeHandler);
}
 
Example 22
private WebDataBinderFactory createFactory(String methodName, Class<?>... parameterTypes)
		throws Exception {

	Object handler = new InitBinderHandler();
	Method method = handler.getClass().getMethod(methodName, parameterTypes);

	InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(handler, method);
	handlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
	handlerMethod.setDataBinderFactory(new DefaultDataBinderFactory(null));
	handlerMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());

	return new InitBinderDataBinderFactory(
			Collections.singletonList(handlerMethod), this.bindingInitializer);
}
 
Example 23
@Ignore
@Test
public void testAutowireCandidateWithMethodDescriptor() throws Exception {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
	lbf.registerBeanDefinition(JUERGEN, person1);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	lbf.registerBeanDefinition(MARK, person2);
	MethodParameter qualifiedParam =
			new MethodParameter(QualifiedTestBean.class.getDeclaredMethod("autowireQualified", Person.class), 0);
	MethodParameter nonqualifiedParam =
			new MethodParameter(QualifiedTestBean.class.getDeclaredMethod("autowireNonqualified", Person.class), 0);
	DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(qualifiedParam, false);
	DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor(nonqualifiedParam, false);
	qualifiedParam.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
	assertEquals("tpb", qualifiedParam.getParameterName());
	nonqualifiedParam.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
	assertEquals("tpb", nonqualifiedParam.getParameterName());
	assertTrue(lbf.isAutowireCandidate(JUERGEN, null));
	assertTrue(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor));
	assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor));
	assertTrue(lbf.isAutowireCandidate(MARK, null));
	assertTrue(lbf.isAutowireCandidate(MARK, nonqualifiedDescriptor));
	assertFalse(lbf.isAutowireCandidate(MARK, qualifiedDescriptor));
}
 
Example 24
Source Project: DimpleBlog   Source File: VisitLogAspect.java    License: 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 25
Source Project: Shiro-Action   Source File: OperationLogAspect.java    License: 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 26
Source Project: FEBS-Security   Source File: LogAspect.java    License: 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 27
Source Project: SpringAll   Source File: LogAspect.java    License: 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 28
Source Project: bird-java   Source File: DubboRouteScanner.java    License: MIT License 5 votes vote down vote up
/**
 * 获取方法参数名与类型的Map
 *
 * @param method method
 * @return map
 */
private Map<String, String> getParameterMap(Method method) {
    Map<String, String> map = new LinkedHashMap<>();

    ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
    String[] parameterNames = parameterNameDiscoverer.getParameterNames(method);
    Class<?>[] parameterTypes = method.getParameterTypes();
    if (parameterNames != null && ArrayUtils.isNotEmpty(parameterTypes) && parameterNames.length == parameterTypes.length) {
        for (int i = 0, len = parameterNames.length; i < len; i++) {
            map.put(parameterNames[i], parameterTypes[i].getName());
        }
    }
    return map;
}
 
Example 29
@Before
public void setup() throws Exception {
	this.resolver = new PayloadArgumentResolver(new StringMessageConverter(), testValidator());
	this.payloadMethod = PayloadArgumentResolverTests.class.getDeclaredMethod("handleMessage",
			String.class, String.class, Locale.class, String.class, String.class, String.class, String.class);

	this.paramAnnotated = new SynthesizingMethodParameter(this.payloadMethod, 0);
	this.paramAnnotatedNotRequired = new SynthesizingMethodParameter(this.payloadMethod, 1);
	this.paramAnnotatedRequired = new SynthesizingMethodParameter(payloadMethod, 2);
	this.paramWithSpelExpression = new SynthesizingMethodParameter(payloadMethod, 3);
	this.paramValidated = new SynthesizingMethodParameter(this.payloadMethod, 4);
	this.paramValidated.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
	this.paramValidatedNotAnnotated = new SynthesizingMethodParameter(this.payloadMethod, 5);
	this.paramNotAnnotated = new SynthesizingMethodParameter(this.payloadMethod, 6);
}
 
Example 30
@Before
public void setUp() throws Exception {
	resolver = new RequestParamMethodArgumentResolver(null, true);

	ParameterNameDiscoverer paramNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();

	Method method = getClass().getMethod("params", String.class, String[].class,
			Map.class, MultipartFile.class, List.class, MultipartFile[].class,
			Part.class, List.class, Part[].class, Map.class,
			String.class, MultipartFile.class, List.class, Part.class,
			MultipartFile.class, String.class, String.class, Optional.class);

	paramNamedDefaultValueString = new SynthesizingMethodParameter(method, 0);
	paramNamedStringArray = new SynthesizingMethodParameter(method, 1);
	paramNamedMap = new SynthesizingMethodParameter(method, 2);
	paramMultipartFile = new SynthesizingMethodParameter(method, 3);
	paramMultipartFileList = new SynthesizingMethodParameter(method, 4);
	paramMultipartFileArray = new SynthesizingMethodParameter(method, 5);
	paramPart = new SynthesizingMethodParameter(method, 6);
	paramPartList  = new SynthesizingMethodParameter(method, 7);
	paramPartArray  = new SynthesizingMethodParameter(method, 8);
	paramMap = new SynthesizingMethodParameter(method, 9);
	paramStringNotAnnot = new SynthesizingMethodParameter(method, 10);
	paramStringNotAnnot.initParameterNameDiscovery(paramNameDiscoverer);
	paramMultipartFileNotAnnot = new SynthesizingMethodParameter(method, 11);
	paramMultipartFileNotAnnot.initParameterNameDiscovery(paramNameDiscoverer);
	paramMultipartFileListNotAnnot = new SynthesizingMethodParameter(method, 12);
	paramMultipartFileListNotAnnot.initParameterNameDiscovery(paramNameDiscoverer);
	paramPartNotAnnot = new SynthesizingMethodParameter(method, 13);
	paramPartNotAnnot.initParameterNameDiscovery(paramNameDiscoverer);
	paramRequestPartAnnot = new SynthesizingMethodParameter(method, 14);
	paramRequired = new SynthesizingMethodParameter(method, 15);
	paramNotRequired = new SynthesizingMethodParameter(method, 16);
	paramOptional = new SynthesizingMethodParameter(method, 17);

	request = new MockHttpServletRequest();
	webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
}