/** * 获取方法的参数 * * @param context * @return */ public static List<Param> getVariables(ExtensionContext context) { List<Param> list = new ArrayList<Param>(); Parameter[] Paramters = context.getTestMethod().get().getParameters(); LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer(); String[] params = u.getParameterNames(context.getTestMethod().get()); for (int i = 0; i < Paramters.length; i++) { Param p = new Param(); p.setName(params[i].trim()); p.setType(Paramters[i].getType().getSimpleName()); list.add(p); } return list; }
@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); }
@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)); }
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; }
public static String[] getSignatures() { List<String> signatures = new ArrayList<String>(); Method[] methods = ModelAttributeScriptHelper.class.getMethods(); LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); for (Method method : methods) { if (method.getDeclaringClass().equals(ModelAttributeScriptHelper.class) && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) { StringBuilder sig = new StringBuilder(method.getName()); sig.append("("); String[] names = discoverer.getParameterNames(method); for (String name : names) { sig.append(name); sig.append(","); } if (names.length > 0) { sig.replace(sig.length() - 1, sig.length(), ")"); } else { sig.append(")"); } signatures.add(sig.toString()); } } Collections.sort(signatures); return signatures.toArray(new String[signatures.size()]); }
@SuppressWarnings("resource") @Before public void setup() throws Exception { DefaultListableBeanFactory dlbf = new DefaultListableBeanFactory(); dlbf.registerSingleton("mvcValidator", testValidator()); GenericApplicationContext ctx = new GenericApplicationContext(dlbf); ctx.refresh(); this.resolver = new PayloadArgumentResolver(ctx, new MethodParameterConverter( new ObjectMapper(), new GenericConversionService())); this.payloadMethod = getClass().getDeclaredMethod("handleMessage", String.class, String.class, String.class, String.class, String.class, String.class, String.class, Integer.class); this.paramAnnotated = getMethodParameter(this.payloadMethod, 0); this.paramAnnotatedNotRequired = getMethodParameter(this.payloadMethod, 1); this.paramAnnotatedRequired = getMethodParameter(this.payloadMethod, 2); this.paramWithSpelExpression = getMethodParameter(this.payloadMethod, 3); this.paramValidated = getMethodParameter(this.payloadMethod, 4); this.paramValidated.initParameterNameDiscovery( new LocalVariableTableParameterNameDiscoverer()); this.paramValidatedNotAnnotated = getMethodParameter(this.payloadMethod, 5); this.paramNotAnnotated = getMethodParameter(this.payloadMethod, 6); }
@Around(value = "", anno = Controller.class) public Object around(Invocation invocation, ProxyChain proxyChain) { Method method = invocation.getMethod(); BeanContext beanContext = BeanContextManager.get(); HttpServletRequest request = (HttpServletRequest) beanContext.getBean("HttpServletRequest"); beanContext.setBean("model", new Model(request), BeanScope.request); LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer(); Class<?>[] parameterTypes = method.getParameterTypes(); String[] paramName = u.getParameterNames(method); Object[] params = new Object[paramName.length]; for (int i = 0; i < paramName.length; i++) { if (String.class.isAssignableFrom(parameterTypes[i])) { params[i] = request.getParameter(paramName[i]); } else if (Integer.class.isAssignableFrom(parameterTypes[i]) || int.class.isAssignableFrom(parameterTypes[i])) { params[i] = Integer.parseInt(request.getParameter(paramName[i])); } else if (Double.class.isAssignableFrom(parameterTypes[i]) || double.class.isAssignableFrom(parameterTypes[i])) { params[i] = Double.parseDouble(request.getParameter(paramName[i])); } else if (Boolean.class.isAssignableFrom(parameterTypes[i]) || boolean.class.isAssignableFrom(parameterTypes[i])) { params[i] = Boolean.parseBoolean(request.getParameter(paramName[i])); } } for (int i = 0; i < parameterTypes.length; i++) { System.out.println(parameterTypes[i]); if (params[i] == null) { params[i] = beanContext.getBean(parameterTypes[i]); } } invocation.setArgs(params); return proxyChain.doProxyChain(invocation); }
/** * <p> * 获取缓存的key key 定义在注解上,支持SPEL表达式 * </p> * * @param args * @return key 调用方设置的key * @author fulei.yang * @date 2016/11/03 * @version 0.0.1 */ private String parseKey(String key, Method method, Object[] args) { // 获取被拦截方法参数名列表(使用Spring支持类库) LocalVariableTableParameterNameDiscoverer localVariableTableParameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); String[] paraNameArr = localVariableTableParameterNameDiscoverer.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); }
@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); }
@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()); }
private ModelFactory createModelFactory(String methodName, Class<?>... parameterTypes) throws Exception{ Method method = TestController.class.getMethod(methodName, parameterTypes); HandlerMethodArgumentResolverComposite argResolvers = new HandlerMethodArgumentResolverComposite(); argResolvers.addResolver(new ModelMethodProcessor()); InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(this.controller, method); handlerMethod.setHandlerMethodArgumentResolvers(argResolvers); handlerMethod.setDataBinderFactory(null); handlerMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer()); return new ModelFactory(Arrays.asList(handlerMethod), null, this.sessionAttrsHandler); }
private WebDataBinderFactory createBinderFactory(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(argumentResolvers); handlerMethod.setDataBinderFactory(new DefaultDataBinderFactory(null)); handlerMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer()); return new InitBinderDataBinderFactory(Arrays.asList(handlerMethod), bindingInitializer); }
@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)); }
/** * 获取参数名列表 * * @param method * @return */ public static String[] getMethodParamNames(Method method) { // 获取被拦截方法参数名列表(使用Spring支持类库) LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer(); String[] paraNameArr = u.getParameterNames(method); return paraNameArr; }
public static String[] getConstructorNames(Constructor<?> targetConstructor){ LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); return discoverer.getParameterNames(targetConstructor); }
/** * 通过ASM方式获得方法参数信息 * @param apiMethod * @return */ private static ApiParamMeta[] getApiParamMetaByASM(Method apiMethod) { LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); String[] names = parameterNameDiscoverer.getParameterNames(apiMethod); if (names != null) { ApiParamMeta[] apiParamMetas = new ApiParamMeta[names.length]; for (int i = 0; i < names.length; i++) { apiParamMetas[i] = new ApiParamMeta(names[i]); } return apiParamMetas; } return null; }
/** * 取得cache 键值 * * @param pjp * @param method * @return 返回string */ private String getCacheKey(ProceedingJoinPoint pjp, Method method) { if (method.isAnnotationPresent(GetCacheVaule.class)) { // 如果有该注解 String key = method.getAnnotation(GetCacheVaule.class).key();// 得到要缓存的键值 Object[] values = pjp.getArgs();// 得到顺序的参数值 ParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); String[] names = discoverer.getParameterNames(method); Map<String, Integer> map = new ConcurrentHashMap<String, Integer>(); for (int i = 0; i < names.length; i++) { map.put(names[i], i);// 将名字和对应的序号放入hashmap } // 得到真正的key 、value值 try { Integer int_value = map.get(key);// hash中没有对应的值,表示getcachekey和名字不符合 if (int_value == null) { log.warn("your cachekey is not equals" + key + "please check this then change them"); } else { String cache_key_real = (String) values[int_value];// 要缓存键值的真正cahe值 return cache_key_real; } } catch (Exception e) { log.warn("your filed " + key + " must be String.class"); } } return null; }
/** * Gets parameter name of this action input parameter. * * @return name */ public String getParameterName() { String ret; String parameterName = methodParameter.getParameterName(); if (parameterName == null) { methodParameter.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); ret = methodParameter.getParameterName(); } else { ret = parameterName; } return ret; }
/** * Create a ParameterNameDiscoverer to be used for argument binding. * <p>The default implementation creates a {@link PrioritizedParameterNameDiscoverer} * containing a {@link LocalVariableTableParameterNameDiscoverer} and an * {@link AspectJAdviceParameterNameDiscoverer}. */ protected ParameterNameDiscoverer createParameterNameDiscoverer() { // We need to discover them, or if that fails, guess, // and if we can't guess with 100% accuracy, fail. PrioritizedParameterNameDiscoverer discoverer = new PrioritizedParameterNameDiscoverer(); discoverer.addDiscoverer(new LocalVariableTableParameterNameDiscoverer()); AspectJAdviceParameterNameDiscoverer adviceParameterNameDiscoverer = new AspectJAdviceParameterNameDiscoverer(this.pointcut.getExpression()); adviceParameterNameDiscoverer.setReturningName(this.returningName); adviceParameterNameDiscoverer.setThrowingName(this.throwingName); // Last in chain, so if we're called and we fail, that's bad... adviceParameterNameDiscoverer.setRaiseExceptions(true); discoverer.addDiscoverer(adviceParameterNameDiscoverer); return discoverer; }
@SuppressWarnings("unchecked") @Before public void setUp() throws Exception { Method method = getClass().getMethod("handle", SimpleBean.class, SimpleBean.class, SimpleBean.class, MultipartFile.class, List.class, Integer.TYPE, MultipartFile.class, Part.class, MultipartFile.class); paramRequestPart = new MethodParameter(method, 0); paramRequestPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); paramNamedRequestPart = new MethodParameter(method, 1); paramValidRequestPart = new MethodParameter(method, 2); paramMultipartFile = new MethodParameter(method, 3); paramMultipartFileList = new MethodParameter(method, 4); paramInt = new MethodParameter(method, 5); paramMultipartFileNotAnnot = new MethodParameter(method, 6); paramMultipartFileNotAnnot.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); paramServlet30Part = new MethodParameter(method, 7); paramServlet30Part.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); paramRequestParamAnnot = new MethodParameter(method, 8); messageConverter = mock(HttpMessageConverter.class); given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN)); resolver = new RequestPartMethodArgumentResolver(Collections.<HttpMessageConverter<?>>singletonList(messageConverter)); reset(messageConverter); multipartFile1 = new MockMultipartFile("requestPart", "", "text/plain", (byte[]) null); multipartFile2 = new MockMultipartFile("requestPart", "", "text/plain", (byte[]) null); multipartRequest = new MockMultipartHttpServletRequest(); multipartRequest.addFile(multipartFile1); multipartRequest.addFile(multipartFile2); servletResponse = new MockHttpServletResponse(); webRequest = new ServletWebRequest(multipartRequest, servletResponse); }
@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, Map.class, String.class, MultipartFile.class, List.class, Part.class, MultipartFile.class, String.class, String.class); paramNamedDefaultValueString = new MethodParameter(method, 0); paramNamedStringArray = new MethodParameter(method, 1); paramNamedMap = new MethodParameter(method, 2); paramMultiPartFile = new MethodParameter(method, 3); paramMap = new MethodParameter(method, 4); paramStringNotAnnot = new MethodParameter(method, 5); paramStringNotAnnot.initParameterNameDiscovery(paramNameDiscoverer); paramMultipartFileNotAnnot = new MethodParameter(method, 6); paramMultipartFileNotAnnot.initParameterNameDiscovery(paramNameDiscoverer); paramMultipartFileList = new MethodParameter(method, 7); paramMultipartFileList.initParameterNameDiscovery(paramNameDiscoverer); paramServlet30Part = new MethodParameter(method, 8); paramServlet30Part.initParameterNameDiscovery(paramNameDiscoverer); paramRequestPartAnnot = new MethodParameter(method, 9); paramRequired = new MethodParameter(method, 10); paramNotRequired = new MethodParameter(method, 11); request = new MockHttpServletRequest(); webRequest = new ServletWebRequest(request, new MockHttpServletResponse()); }
private ModelFactory createModelFactory(String methodName, Class<?>... parameterTypes) throws Exception{ Method method = ModelHandler.class.getMethod(methodName, parameterTypes); HandlerMethodArgumentResolverComposite argResolvers = new HandlerMethodArgumentResolverComposite(); argResolvers.addResolver(new ModelMethodProcessor()); InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(handler, method); handlerMethod.setHandlerMethodArgumentResolvers(argResolvers); handlerMethod.setDataBinderFactory(null); handlerMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer()); return new ModelFactory(Arrays.asList(handlerMethod), null, sessionAttrsHandler); }
@SuppressWarnings("unchecked") @Before public void setUp() throws Exception { Method method = getClass().getMethod("handle", SimpleBean.class, SimpleBean.class, SimpleBean.class, MultipartFile.class, List.class, MultipartFile[].class, Integer.TYPE, MultipartFile.class, Part.class, List.class, Part[].class, MultipartFile.class, Optional.class, Optional.class, Optional.class); paramRequestPart = new SynthesizingMethodParameter(method, 0); paramRequestPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); paramNamedRequestPart = new SynthesizingMethodParameter(method, 1); paramValidRequestPart = new SynthesizingMethodParameter(method, 2); paramMultipartFile = new SynthesizingMethodParameter(method, 3); paramMultipartFileList = new SynthesizingMethodParameter(method, 4); paramMultipartFileArray = new SynthesizingMethodParameter(method, 5); paramInt = new SynthesizingMethodParameter(method, 6); paramMultipartFileNotAnnot = new SynthesizingMethodParameter(method, 7); paramMultipartFileNotAnnot.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); paramPart = new SynthesizingMethodParameter(method, 8); paramPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); paramPartList = new SynthesizingMethodParameter(method, 9); paramPartArray = new SynthesizingMethodParameter(method, 10); paramRequestParamAnnot = new SynthesizingMethodParameter(method, 11); optionalMultipartFile = new SynthesizingMethodParameter(method, 12); optionalMultipartFile.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); optionalPart = new SynthesizingMethodParameter(method, 13); optionalPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); optionalRequestPart = new SynthesizingMethodParameter(method, 14); messageConverter = mock(HttpMessageConverter.class); given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN)); resolver = new RequestPartMethodArgumentResolver(Collections.<HttpMessageConverter<?>>singletonList(messageConverter)); reset(messageConverter); byte[] content = "doesn't matter as long as not empty".getBytes(Charset.forName("UTF-8")); multipartFile1 = new MockMultipartFile("requestPart", "", "text/plain", content); multipartFile2 = new MockMultipartFile("requestPart", "", "text/plain", content); multipartRequest = new MockMultipartHttpServletRequest(); multipartRequest.addFile(multipartFile1); multipartRequest.addFile(multipartFile2); webRequest = new ServletWebRequest(multipartRequest, new MockHttpServletResponse()); }
public static String[] getParameterNames(MethodParameter parameter) { Method method = parameter.getMethod(); // ParameterNameDiscoverer parameterNameDiscoverer = null; ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); return parameterNameDiscoverer.getParameterNames(method); }
@Override public void afterPropertiesSet() throws Exception { ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); for (Entry<String, Object> entry : namespacedServices.entrySet()) { Class<?> clazz = entry.getValue().getClass(); try { clazz = ((Advised) entry.getValue()).getTargetSource().getTarget().getClass(); } catch (Exception ignored) { } Namespace namespace = clazz.getAnnotation(Namespace.class); logger.debug("clazz={}, namespace={}", clazz.getName(), namespace); if (namespace == null || "".equals(namespace.value())) { logger.debug("namespace is null or empty. clazz={}", clazz.getName()); continue; } String prefix = namespace.value(); boolean exposeAll = namespace.exposeAll(); for (Method method : clazz.getDeclaredMethods()) { logger.debug("clazz={}, method={}", clazz.getName(), method.getName()); if (!Modifier.isPublic(method.getModifiers())) { logger.debug("method is not public. clazz={}, method={}", clazz.getName(), method.getName()); continue; } Expose expose = method.getAnnotation(Expose.class); if (!exposeAll && expose == null) { logger.debug("exposeAll is false and @Expose is not found. clazz={}, method={}", clazz.getName(), method.getName()); continue; } String path = Context.PATH_DELIMITER + prefix + Context.PATH_DELIMITER + (expose == null || StringUtils.isBlank(expose.value()) ? method.getName() : expose.value()); Class<?>[] parameterTypes = method.getParameterTypes(); Type[] genericParameterTypes = method.getGenericParameterTypes(); Annotation[][] parameterAnnotations = method.getParameterAnnotations(); String[] parameterNames = parameterNameDiscoverer.getParameterNames(method); Map<String, Parameter> parameters = Maps.newLinkedHashMap(); for (int i = 0; i < parameterTypes.length; i++) { parameters.put(parameterNames[i], new Parameter(genericParameterTypes[i], parameterTypes[i], parameterNames[i], parameterNames[i], true, parameterAnnotations[i], null, null)); } MethodAction methodAction = new MethodAction(entry.getValue(), method, HttpMethod.POST, path, parameters); interceptorManager.addInterceptors(methodAction); logger.debug("[remote] {} => {}", new Object[]{path, methodAction.bean().getClass().getSimpleName() + "#" + methodAction.method().getName()}); List<Action> actions = routes.get(path); if (actions == null) { actions = Lists.newArrayList(); } actions.add(methodAction); routes.put(path, actions); } } }
/** * @param invocation * @return * @throws NoSuchMethodException */ public Object buildKey(String key, ProceedingJoinPoint invocation) throws NoSuchMethodException { if (key.indexOf("#") == -1) {// 如果不是表达式,直接返回字符串 return key; } String keySpEL = ""; String pre = ""; String str[] = key.split("\\#"); if (str.length > 1) { pre = str[0]; for (int i = 1; i < str.length; i++) { keySpEL = keySpEL + "#" + str[i]; } } else keySpEL = key; MethodSignature signature = (MethodSignature) invocation.getSignature(); Method method = signature.getMethod(); Class<?>[] parameterTypes = method.getParameterTypes(); ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); String[] parameterNames = parameterNameDiscoverer.getParameterNames(invocation.getTarget().getClass().getDeclaredMethod(method.getName(), parameterTypes)); StandardEvaluationContext context = new StandardEvaluationContext(); for (int i = 0; i < parameterNames.length; i++) { context.setVariable(parameterNames[i], invocation.getArgs()[i]); } Expression expression = expCache.get(keySpEL); if (null == expression) { expression = parser.parseExpression(keySpEL); expCache.put(keySpEL, expression); } Object value = expression.getValue(context, Object.class); if (!StringUtils.isEmpty(pre)) return pre + value; else return value; }
public String[] getParameterNames() { if (this.parameterNames == null) { this.parameterNames = (new LocalVariableTableParameterNameDiscoverer()).getParameterNames(getMethod()); } return this.parameterNames; }
public RestdlJerseyAdapter() { parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); effectiveClassResolver = new JacksonTargetClassResolver(); }
public RestdlSpringMvcAdapter() { parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); effectiveClassResolver = new JacksonTargetClassResolver(); }
private static List<String> getParameterNames(Method method) { ParameterNameDiscoverer paramNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); String[] parameterNames = paramNameDiscoverer.getParameterNames(method); return toList(parameterNames); }
/** * 获取方法的参数名 * @param method 方法 * @return 方法名称数组 */ public static String[] getParamNames(Method method) { LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer(); return u.getParameterNames(method); }