public HandlerMethodService(ConversionService conversionService, List<HandlerMethodArgumentResolver> customArgumentResolvers, ObjectMapper objectMapper, ApplicationContext applicationContext) { this.conversionService = conversionService; this.parameterNameDiscoverer = new DefaultParameterNameDiscoverer(); this.argumentResolvers = new HandlerMethodArgumentResolverComposite(); ConfigurableBeanFactory beanFactory = ClassUtils.isAssignableValue( ConfigurableApplicationContext.class, applicationContext) ? ((ConfigurableApplicationContext) applicationContext) .getBeanFactory() : null; this.argumentResolvers.addResolver( new HeaderMethodArgumentResolver(this.conversionService, beanFactory)); this.argumentResolvers.addResolver(new HeadersMethodArgumentResolver()); this.argumentResolvers.addResolver(new WampMessageMethodArgumentResolver()); this.argumentResolvers.addResolver(new PrincipalMethodArgumentResolver()); this.argumentResolvers.addResolver(new WampSessionIdMethodArgumentResolver()); this.argumentResolvers.addResolvers(customArgumentResolvers); this.objectMapper = objectMapper; }
/** * DefaultParameterNameDiscoverer is supported spring 4.0 * @return */ private String[] getParameterNames(Method method) { if (notSupportParameterNameDiscoverer) { return null; } else { try { parameterNameDiscoverer = new DefaultParameterNameDiscoverer();// only support from spring4 String[] strs = parameterNameDiscoverer.getParameterNames(method); if (strs == null) { notSupportParameterNameDiscoverer = true; } return strs; } catch (NoClassDefFoundError e) { notSupportParameterNameDiscoverer = true; return null; } } }
@Before public void setup() throws Exception { @SuppressWarnings("resource") GenericApplicationContext cxt = new GenericApplicationContext(); cxt.refresh(); this.resolver = new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory()); Method method = getClass().getDeclaredMethod("handleMessage", String.class, String.class, String.class, String.class, String.class); this.paramRequired = new SynthesizingMethodParameter(method, 0); this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 1); this.paramSystemProperty = new SynthesizingMethodParameter(method, 2); this.paramNotAnnotated = new SynthesizingMethodParameter(method, 3); this.paramNativeHeader = new SynthesizingMethodParameter(method, 4); this.paramRequired.initParameterNameDiscovery(new DefaultParameterNameDiscoverer()); GenericTypeResolver.resolveParameterType(this.paramRequired, HeaderMethodArgumentResolver.class); }
@Before public void setup() throws Exception { this.invocableHandlerMethod = new InvocableHandlerMethod( new HandlerMethod(this, "setup")); HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite(); argumentResolvers.addResolver(new WampMessageMethodArgumentResolver()); argumentResolvers.addResolver(new PrincipalMethodArgumentResolver()); this.invocableHandlerMethod.setArgumentResolvers(argumentResolvers); this.invocableHandlerMethod .setConversionService(new DefaultFormattingConversionService()); this.invocableHandlerMethod.setObjectMapper(new ObjectMapper()); this.invocableHandlerMethod .setParameterNameDiscoverer(new DefaultParameterNameDiscoverer()); }
/** * Create a ParameterNameDiscoverer to be used for argument binding. * <p>The default implementation creates a {@link DefaultParameterNameDiscoverer} * and adds a specifically configured {@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. DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer(); 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; }
public void test1() { DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer(); for (Method method : ReflectionUtilTest.class.getDeclaredMethods()) { if (Objects.equals(method.getName(), "test")) { String[] parameterNames = discoverer.getParameterNames(method); for (String parameterName : parameterNames) { System.out.println(parameterName); } } } }
@Before public void setup() throws Exception { this.resolver = new DestinationVariableMethodArgumentResolver(new DefaultConversionService()); Method method = getClass().getDeclaredMethod("handleMessage", String.class, String.class, String.class); this.paramAnnotated = new MethodParameter(method, 0); this.paramAnnotatedValue = new MethodParameter(method, 1); this.paramNotAnnotated = new MethodParameter(method, 2); this.paramAnnotated.initParameterNameDiscovery(new DefaultParameterNameDiscoverer()); GenericTypeResolver.resolveParameterType(this.paramAnnotated, DestinationVariableMethodArgumentResolver.class); this.paramAnnotatedValue.initParameterNameDiscovery(new DefaultParameterNameDiscoverer()); GenericTypeResolver.resolveParameterType(this.paramAnnotatedValue, DestinationVariableMethodArgumentResolver.class); }
@Override public String[] lookupParameterNames( AccessibleObject methodOrConstructor) { String[] names = null; ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer(); if (methodOrConstructor instanceof Method) { names = parameterNameDiscoverer.getParameterNames((Method) methodOrConstructor); } else { names = parameterNameDiscoverer.getParameterNames((Constructor<?>)methodOrConstructor); } if (names==null) { Annotation[][] parameterAnnotations = ((Method) methodOrConstructor).getParameterAnnotations(); names = new String[parameterAnnotations.length]; for (int index = 0; index < parameterAnnotations.length; index++) { Annotation[] annotations = parameterAnnotations[index]; boolean found = false; for (Annotation annotation : annotations) { if (annotation instanceof Named ) { Named named = (Named) annotation; names[index]=named.value(); found=true; } } if (!found) { names[index]="arg_"+index; } } } return names; }
private void initParameters(HandlerMethod handlerMethod) { for (MethodParameter parameter : handlerMethod.getMethodParameters()) { parameter.initParameterNameDiscovery(new DefaultParameterNameDiscoverer()); } }
/** * To maximize the caching feature in name discoverer, inject as a bean instead of creating every time. */ @Bean public ParameterNameDiscoverer parameterNameDiscoverer() { return new DefaultParameterNameDiscoverer(); }
public static void main(String[] args) throws Exception { ParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer(); Method declaredMethod = HttpUtils.class.getDeclaredMethod("getClient", HttpHost.class); String[] names = discoverer.getParameterNames(declaredMethod); System.out.println(names[0]); }