List of usage examples for org.springframework.core LocalVariableTableParameterNameDiscoverer LocalVariableTableParameterNameDiscoverer
LocalVariableTableParameterNameDiscoverer
From source file:com.github.sebhoss.contract.verifier.ParameterNamesLookupConfiguration.java
/** * @return A parameter name discoverer which uses the local variable table (debug-informations) to retrieve * parameter names.//from w ww . ja v a2 s . c o m */ @Bean @SuppressWarnings(CompilerWarnings.STATIC_METHOD) public ParameterNamesLookup localVariableTableLookup() { return new SpringBasedParameterNamesLookup(new LocalVariableTableParameterNameDiscoverer()); }
From source file:ch.nydi.spring.context.support.TestGenericXmlContextLoader.java
/** * {@inheritDoc}/*from w w w .j ava 2 s .c o m*/ */ @Override public final ConfigurableApplicationContext loadContext(final String... locations) throws Exception { final DefaultListableBeanFactory beanFactory = new PrimaryResolverListableBeanFactory(); beanFactory.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer()); beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); final GenericApplicationContext context = new GenericApplicationContext(beanFactory); createBeanDefinitionReader(context).loadBeanDefinitions(locations); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); context.registerShutdownHook(); return context; }
From source file:org.icescrum.core.security.MethodScrumEvaluationContext.java
/** * Intended for testing. Don't use in practice as it creates a new parameter resolver * for each instance. Use the constructor which takes the resolver, as an argument thus * allowing for caching./*from w ww.j a v a 2 s.c o m*/ */ public MethodScrumEvaluationContext(Authentication user, MethodInvocation mi) { this(user, mi, new LocalVariableTableParameterNameDiscoverer()); }
From source file:com.taobao.rpc.doclet.RPCAPIInfoHelper.java
public static RPCAPIInfo getAPIInfo(String realPath, Method method) { RPCAPIInfo apiInfo = new RPCAPIInfo(); ClassInfo classInfo = RPCAPIDocletUtil.getClassInfo(method.getDeclaringClass().getName()); MethodInfo methodInfo = null;/*from w w w . jav a 2s . c o m*/ if (classInfo != null) { methodInfo = classInfo.getMethodInfo(method.getName()); } if (methodInfo != null) { apiInfo.setComment(methodInfo.getComment()); } Annotation[][] parameterAnnotations = method.getParameterAnnotations(); Security securityAnnotation = method.getAnnotation(Security.class); ResourceMapping resourceMapping = method.getAnnotation(ResourceMapping.class); Object returnType = null; returnType = buildTypeStructure(method.getReturnType(), method.getGenericReturnType(), null); boolean checkCSRF = false; if (securityAnnotation != null) { checkCSRF = securityAnnotation.checkCSRF(); } apiInfo.setPattern(realPath.replaceAll("///", "/")); apiInfo.setCheckCSRF(checkCSRF); apiInfo.setResponseMimes(StringUtils.arrayToDelimitedString(resourceMapping.produces(), ",")); apiInfo.setHttpMethod(StringUtils.arrayToDelimitedString(resourceMapping.method(), ",")); List<RPCAPIInfo.Parameter> parameters = new ArrayList<RPCAPIInfo.Parameter>(); RPCAPIInfo.Parameter parameter = null; LocalVariableTableParameterNameDiscoverer nameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); String[] paramNames = nameDiscoverer.getParameterNames(method); int i = 0; Class<?>[] parameterTypes = method.getParameterTypes(); Type[] genericParameterTypes = method.getGenericParameterTypes(); Class<?> paramType = null; Type genericType; for (int k = 0; k < parameterTypes.length; k++) { paramType = parameterTypes[k]; genericType = genericParameterTypes[k]; Annotation[] pAnnotations = parameterAnnotations[i]; if (HttpServletRequest.class.isAssignableFrom(paramType) || HttpServletResponse.class.isAssignableFrom(paramType) || ErrorContext.class.isAssignableFrom(paramType)) { continue; } // end if String realParamName = paramNames[k]; if (pAnnotations.length == 0) { parameter = apiInfo.new Parameter(); parameter.setName(realParamName); parameter.setType(buildTypeStructure(paramType, genericType, null)); parameters.add(parameter); setParameterComment(parameter, realParamName, methodInfo); continue; } // end if for (Annotation annotation : pAnnotations) { parameter = apiInfo.new Parameter(); setParameterComment(parameter, realParamName, methodInfo); if (annotation instanceof RequestParam) { RequestParam requestParam = (RequestParam) annotation; parameter.setName(requestParam.name()); parameter.setType(buildTypeStructure(paramType, genericType, null)); if (!StringUtil.isBlank(requestParam.defaultValue())) { parameter.setDefaultValue(requestParam.defaultValue().trim()); } parameters.add(parameter); } else if (annotation instanceof PathParam) { PathParam pathParam = (PathParam) annotation; parameter.setName(pathParam.name()); parameter.setType(buildTypeStructure(paramType, genericType, null)); if (!StringUtil.isBlank(pathParam.defaultValue())) { parameter.setDefaultValue(pathParam.defaultValue().trim()); } parameters.add(parameter); } else if (annotation instanceof JsonParam) { JsonParam pathParam = (JsonParam) annotation; parameter.setName(pathParam.value()); parameter.setType(buildTypeStructure(paramType, genericType, null)); parameters.add(parameter); } else if (annotation instanceof RequestParams) { parameter.setName(realParamName); parameter.setType(buildTypeStructure(paramType, genericType, null)); parameters.add(parameter); } else if (annotation instanceof File) { File file = (File) annotation; parameter.setName(file.value()); parameter.setType(""); parameters.add(parameter); } // end if } // end for i++; } // end for apiInfo.setParmeters(parameters); apiInfo.setReturnType(returnType); return apiInfo; }
From source file:ch.rasc.wampspring.method.PayloadArgumentResolverTest.java
@SuppressWarnings("resource") @Before/*from w ww . ja va 2s .co m*/ 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); }
From source file:springfox.documentation.spring.web.readers.parameter.ParameterNameReader.java
private ParameterNameDiscoverer parameterNameDiscoverer() { ParameterNameDiscoverer dicoverer;//from ww w . j a va 2 s. co m try { dicoverer = (ParameterNameDiscoverer) Class.forName(SPRING4_DISCOVERER).newInstance(); } catch (Exception e) { dicoverer = new LocalVariableTableParameterNameDiscoverer(); } return dicoverer; }
From source file:com.haulmont.cuba.core.app.AbstractBeansMetadata.java
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);//from w w w . j a v a 2 s .c o m } } return params; }
From source file:org.jm.swagger.SpringMVCAPIReader.java
/** * Create {@link DocumentationParameter} from {@link MethodParameter} * //www . ja va2 s .co m * @param methodParamater * @return */ private DocumentationParameter convertMethodParameter(MethodParameter methodParamater) { DocumentationParameter documentationParameter = new DocumentationParameter(); documentationParameter.setDataType(methodParamater.getParameterType().getSimpleName()); methodParamater.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); documentationParameter.setName(methodParamater.getParameterName()); documentationParameter.setDescription("description"); documentationParameter.setNotes("notes"); documentationParameter.setParamType("path"); documentationParameter.setDefaultValue("default value"); documentationParameter.setAllowMultiple(false); PathVariable pathVariable = methodParamater.getParameterAnnotation(PathVariable.class); if (pathVariable != null) { documentationParameter.setRequired(true); } RequestParam requestParam = methodParamater.getParameterAnnotation(RequestParam.class); if (requestParam != null) { documentationParameter.setRequired(requestParam.required()); documentationParameter.setDefaultValue(requestParam.defaultValue()); } ApiParam apiParam = methodParamater.getParameterAnnotation(ApiParam.class); if (apiParam != null) { documentationParameter.setName(apiParam.name()); documentationParameter.setDescription(apiParam.value()); // documentationParameter.setAllowableValues(apiParam.allowableValues()); } return documentationParameter; }
From source file:com.fengduo.bee.commons.component.FormModelMethodArgumentResolver.java
/** * {@inheritDoc}// w ww .j ava 2 s .c om * <p> * Downcast {@link org.springframework.web.bind.WebDataBinder} to * {@link org.springframework.web.bind.ServletRequestDataBinder} before binding. * * @throws Exception * @see org.springframework.web.servlet.mvc.method.annotation.ServletRequestDataBinderFactory */ protected void bindRequestParameters(ModelAndViewContainer mavContainer, WebDataBinderFactory binderFactory, WebDataBinder binder, NativeWebRequest request, MethodParameter parameter) throws Exception { // Map<String, Boolean> hasProcessedPrefixMap = new HashMap<String, Boolean>(); // // Class<?> targetType = binder.getTarget().getClass(); // WebDataBinder simpleBinder = binderFactory.createBinder(request, null, null); Collection target = (Collection) binder.getTarget(); Class<?>[] paramTypes = parameter.getMethod().getParameterTypes(); Method method = parameter.getMethod(); Object[] args = new Object[paramTypes.length]; Map<String, Object> argMap = new HashMap<String, Object>(args.length); MapBindingResult errors = new MapBindingResult(argMap, ""); ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); for (int i = 0; i < paramTypes.length; i++) { Class<?> paramType = paramTypes[i]; MethodParameter methodParam = new MethodParameter(method, i); methodParam.initParameterNameDiscovery(parameterNameDiscoverer); String paramName = methodParam.getParameterName(); // ?? if (BeanUtils.isSimpleProperty(paramType)) { SimpleTypeConverter converter = new SimpleTypeConverter(); Object value; // ? if (paramType.isArray()) { value = request.getParameterValues(paramName); } else { value = request.getParameter(paramName); } try { args[i] = converter.convertIfNecessary(value, paramType, methodParam); } catch (TypeMismatchException e) { errors.addError(new FieldError(paramName, paramName, e.getMessage())); } } else { // ???POJO if (paramType.isArray()) { ObjectArrayDataBinder binders = new ObjectArrayDataBinder(paramType.getComponentType(), paramName); target.addAll(ArrayUtils.arrayConvert(binders.bind(request))); } } } // if (Collection.class.isAssignableFrom(targetType)) {// bind collection or array // // Type type = parameter.getGenericParameterType(); // Class<?> componentType = Object.class; // // Collection target = (Collection) binder.getTarget(); // // List targetList = new ArrayList(target); // // if (type instanceof ParameterizedType) { // componentType = (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0]; // } // // if (parameter.getParameterType().isArray()) { // componentType = parameter.getParameterType().getComponentType(); // } // // for (Object key : servletRequest.getParameterMap().keySet()) { // String prefixName = getPrefixName((String) key); // // // ?prefix ?? // if (hasProcessedPrefixMap.containsKey(prefixName)) { // continue; // } else { // hasProcessedPrefixMap.put(prefixName, Boolean.TRUE); // } // // if (isSimpleComponent(prefixName)) { // bind simple type // Map<String, Object> paramValues = WebUtils.getParametersStartingWith(servletRequest, prefixName); // Matcher matcher = INDEX_PATTERN.matcher(prefixName); // if (!matcher.matches()) { // ? array=1&array=2 // for (Object value : paramValues.values()) { // targetList.add(simpleBinder.convertIfNecessary(value, componentType)); // } // } else { // ? array[0]=1&array[1]=2 // int index = Integer.valueOf(matcher.group(1)); // // if (targetList.size() <= index) { // growCollectionIfNecessary(targetList, index); // } // targetList.set(index, simpleBinder.convertIfNecessary(paramValues.values(), componentType)); // } // } else { // ? votes[1].title=votes[1].title&votes[0].title=votes[0].title&votes[0].id=0&votes[1].id=1 // Object component = null; // // ? ????? // Matcher matcher = INDEX_PATTERN.matcher(prefixName); // if (!matcher.matches()) { // throw new IllegalArgumentException("bind collection error, need integer index, key:" + key); // } // int index = Integer.valueOf(matcher.group(1)); // if (targetList.size() <= index) { // growCollectionIfNecessary(targetList, index); // } // Iterator iterator = targetList.iterator(); // for (int i = 0; i < index; i++) { // iterator.next(); // } // component = iterator.next(); // // if (component == null) { // component = BeanUtils.instantiate(componentType); // } // // WebDataBinder componentBinder = binderFactory.createBinder(request, component, null); // component = componentBinder.getTarget(); // // if (component != null) { // ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues( // servletRequest, // prefixName, // ""); // componentBinder.bind(pvs); // validateIfApplicable(componentBinder, parameter); // if (componentBinder.getBindingResult().hasErrors()) { // if (isBindExceptionRequired(componentBinder, parameter)) { // throw new BindException(componentBinder.getBindingResult()); // } // } // targetList.set(index, component); // } // } // target.clear(); // target.addAll(targetList); // } // } else if (MapWapper.class.isAssignableFrom(targetType)) { // // Type type = parameter.getGenericParameterType(); // Class<?> keyType = Object.class; // Class<?> valueType = Object.class; // // if (type instanceof ParameterizedType) { // keyType = (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0]; // valueType = (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[1]; // } // // MapWapper mapWapper = ((MapWapper) binder.getTarget()); // Map target = mapWapper.getInnerMap(); // if (target == null) { // target = new HashMap(); // mapWapper.setInnerMap(target); // } // // for (Object key : servletRequest.getParameterMap().keySet()) { // String prefixName = getPrefixName((String) key); // // // ?prefix ?? // if (hasProcessedPrefixMap.containsKey(prefixName)) { // continue; // } else { // hasProcessedPrefixMap.put(prefixName, Boolean.TRUE); // } // // Object keyValue = simpleBinder.convertIfNecessary(getMapKey(prefixName), keyType); // // if (isSimpleComponent(prefixName)) { // bind simple type // Map<String, Object> paramValues = WebUtils.getParametersStartingWith(servletRequest, prefixName); // // for (Object value : paramValues.values()) { // target.put(keyValue, simpleBinder.convertIfNecessary(value, valueType)); // } // } else { // // Object component = target.get(keyValue); // if (component == null) { // component = BeanUtils.instantiate(valueType); // } // // WebDataBinder componentBinder = binderFactory.createBinder(request, component, null); // component = componentBinder.getTarget(); // // if (component != null) { // ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues( // servletRequest, // prefixName, // ""); // componentBinder.bind(pvs); // // validateComponent(componentBinder, parameter); // // target.put(keyValue, component); // } // } // } // } else {// bind model // ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder; // servletBinder.bind(servletRequest); // } }
From source file:de.escalon.hypermedia.spring.ActionInputParameter.java
/** * Gets parameter name of this action input parameter. * * @return name// ww w. ja va 2s . c o m */ public String getParameterName() { String ret; String parameterName = methodParameter.getParameterName(); if (parameterName == null) { methodParameter.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); ret = methodParameter.getParameterName(); } else { ret = parameterName; } return ret; }