/** * Create parameter info for the given method. * <p>The default implementation returns an empty array of {@code MBeanParameterInfo}. * @param method the {@code Method} to get the parameter information for * @param beanKey the key associated with the MBean in the beans map * of the {@code MBeanExporter} * @return the {@code MBeanParameterInfo} array */ protected MBeanParameterInfo[] getOperationParameters(Method method, String beanKey) { ParameterNameDiscoverer paramNameDiscoverer = getParameterNameDiscoverer(); String[] paramNames = (paramNameDiscoverer != null ? paramNameDiscoverer.getParameterNames(method) : null); if (paramNames == null) { return new MBeanParameterInfo[0]; } MBeanParameterInfo[] info = new MBeanParameterInfo[paramNames.length]; Class<?>[] typeParameters = method.getParameterTypes(); for(int i = 0; i < info.length; i++) { info[i] = new MBeanParameterInfo(paramNames[i], typeParameters[i].getName(), paramNames[i]); } return info; }
/** * Create parameter info for the given method. * <p>The default implementation returns an empty array of {@code MBeanParameterInfo}. * @param method the {@code Method} to get the parameter information for * @param beanKey the key associated with the MBean in the beans map * of the {@code MBeanExporter} * @return the {@code MBeanParameterInfo} array */ protected MBeanParameterInfo[] getOperationParameters(Method method, String beanKey) { ParameterNameDiscoverer paramNameDiscoverer = getParameterNameDiscoverer(); String[] paramNames = (paramNameDiscoverer != null ? paramNameDiscoverer.getParameterNames(method) : null); if (paramNames == null) { return new MBeanParameterInfo[0]; } MBeanParameterInfo[] info = new MBeanParameterInfo[paramNames.length]; Class<?>[] typeParameters = method.getParameterTypes(); for (int i = 0; i < info.length; i++) { info[i] = new MBeanParameterInfo(paramNames[i], typeParameters[i].getName(), paramNames[i]); } return info; }
public HandlerMethodInvoker(HandlerMethodResolver methodResolver, WebBindingInitializer bindingInitializer, SessionAttributeStore sessionAttributeStore, ParameterNameDiscoverer parameterNameDiscoverer, WebArgumentResolver[] customArgumentResolvers, HttpMessageConverter<?>[] messageConverters) { this.methodResolver = methodResolver; this.bindingInitializer = bindingInitializer; this.sessionAttributeStore = sessionAttributeStore; this.parameterNameDiscoverer = parameterNameDiscoverer; this.customArgumentResolvers = customArgumentResolvers; this.messageConverters = messageConverters; }
LazyParamAwareEvaluationContext(Object rootObject, ParameterNameDiscoverer paramDiscoverer, Method method, Object[] args, Class<?> targetClass, Map<MethodCacheKey, Method> methodCache) { super(rootObject); this.paramDiscoverer = paramDiscoverer; this.method = method; this.args = args; this.targetClass = targetClass; this.methodCache = methodCache; }
/** * 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 MethodBasedEvaluationContext(Object rootObject, Method method, Object[] args, ParameterNameDiscoverer paramDiscoverer) { super(rootObject); this.method = method; this.args = args; this.paramDiscoverer = paramDiscoverer; }
@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()); }
@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; }
/** * 取得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; }
/** * 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; }
LazyParamAwareEvaluationContext(Object rootObject, ParameterNameDiscoverer paramDiscoverer, Method method, Object[] args, Class<?> targetClass, Map<String, Method> methodCache) { super(rootObject); this.paramDiscoverer = paramDiscoverer; this.method = method; this.args = args; this.targetClass = targetClass; this.methodCache = methodCache; }
public HandlerMethodInvoker(HandlerMethodResolver methodResolver, WebBindingInitializer bindingInitializer, SessionAttributeStore sessionAttributeStore, ParameterNameDiscoverer parameterNameDiscoverer, WebArgumentResolver[] customArgumentResolvers, HttpMessageConverter[] messageConverters) { this.methodResolver = methodResolver; this.bindingInitializer = bindingInitializer; this.sessionAttributeStore = sessionAttributeStore; this.parameterNameDiscoverer = parameterNameDiscoverer; this.customArgumentResolvers = customArgumentResolvers; this.messageConverters = messageConverters; }
@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()); }
public void setParameterNameDiscoverer( ParameterNameDiscoverer parameterNameDiscoverer) { this.parameterNameDiscoverer = parameterNameDiscoverer; }
CacheEvaluationContext(Object rootObject, Method method, Object[] args, ParameterNameDiscoverer paramDiscoverer) { super(rootObject, method, args, paramDiscoverer); this.unavailableVariables = new ArrayList<String>(); }
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; }
/** * To maximize the caching feature in name discoverer, inject as a bean instead of creating every time. */ @Bean public ParameterNameDiscoverer parameterNameDiscoverer() { return new DefaultParameterNameDiscoverer(); }
private static List<String> getParameterNames(Method method) { ParameterNameDiscoverer paramNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); String[] parameterNames = paramNameDiscoverer.getParameterNames(method); return toList(parameterNames); }
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]); }
/** * Set the ParameterNameDiscoverer to use for resolving method and constructor * parameter names if needed for message interpolation. * <p>Default is a {@link org.springframework.core.DefaultParameterNameDiscoverer}. */ public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer) { this.parameterNameDiscoverer = parameterNameDiscoverer; }
/** * Set the ParameterNameDiscoverer for resolving parameter names when needed * (e.g. default request attribute name). * <p>Default is a {@link org.springframework.core.DefaultParameterNameDiscoverer}. */ public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer) { this.parameterNameDiscoverer = parameterNameDiscoverer; }
/** * Set the ParameterNameDiscoverer to use for resolving method parameter * names if needed (e.g. for parameter names of MBean operation methods). * <p>Default is a {@link DefaultParameterNameDiscoverer}. */ public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer) { this.parameterNameDiscoverer = parameterNameDiscoverer; }
/** * Return the ParameterNameDiscoverer to use for resolving method parameter * names if needed (may be {@code null} in order to skip parameter detection). */ protected ParameterNameDiscoverer getParameterNameDiscoverer() { return this.parameterNameDiscoverer; }
/** * Set the ParameterNameDiscoverer to use for resolving method parameter * names if needed (e.g. for constructor names). * <p>Default is a {@link DefaultParameterNameDiscoverer}. */ public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer) { this.parameterNameDiscoverer = parameterNameDiscoverer; }
/** * Return the ParameterNameDiscoverer to use for resolving method parameter * names if needed. */ protected ParameterNameDiscoverer getParameterNameDiscoverer() { return this.parameterNameDiscoverer; }
/** * Initialize parameter name discovery for the underlying method parameter, if any. * <p>This method does not actually try to retrieve the parameter name at * this point; it just allows discovery to happen when the application calls * {@link #getDependencyName()} (if ever). */ public void initParameterNameDiscovery(ParameterNameDiscoverer parameterNameDiscoverer) { if (this.methodParameter != null) { this.methodParameter.initParameterNameDiscovery(parameterNameDiscoverer); } }
/** * Set the ParameterNameDiscoverer to use for resolving method parameter * names if needed (e.g. for default attribute names). * <p>Default is a {@link org.springframework.core.DefaultParameterNameDiscoverer}. */ public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer) { this.parameterNameDiscoverer = parameterNameDiscoverer; }