/** * Gets a {@link Method} object from target object by specified method name. * * @param joinPoint the {@link JoinPoint} * @param methodName the method name * @return a {@link Method} object or null if method with specified <code>methodName</code> doesn't exist */ public static Method getMethodFromTarget(JoinPoint joinPoint, String methodName) { return getDeclaredMethod(joinPoint.getTarget().getClass(), methodName, getParameterTypes(joinPoint)); }
/** * Gets declared method from specified type by mame and parameters types. * * @param type the type * @param methodName the name of the method * @param parameterTypes the parameter array * @return a {@link Method} object or null if method doesn't exist */ public static Method getDeclaredMethod(Class<?> type, String methodName, Class<?>... parameterTypes) { Method method = null; try { method = type.getDeclaredMethod(methodName, parameterTypes); if(method.isBridge()){ method = MethodProvider.getInstance().unbride(method, type); } } catch (NoSuchMethodException e) { Class<?> superclass = type.getSuperclass(); if (superclass != null) { method = getDeclaredMethod(superclass, methodName, parameterTypes); } } catch (ClassNotFoundException e) { Throwables.propagate(e); } catch (IOException e) { Throwables.propagate(e); } return method; }
/** * Gets a {@link Method} object from target object (not proxy class). * * @param joinPoint the {@link JoinPoint} * @return a {@link Method} object or null if method doesn't exist or if the signature at a join point * isn't sub-type of {@link MethodSignature} */ public static Method getMethodFromTarget(JoinPoint joinPoint) { Method method = null; if (joinPoint.getSignature() instanceof MethodSignature) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); method = getDeclaredMethod(joinPoint.getTarget().getClass(), signature.getName(), getParameterTypes(joinPoint)); } return method; }
Method batchCommandMethod = getDeclaredMethod(obj.getClass(), hystrixCollapser.batchMethod(), List.class);
private static MethodExecutionAction createCacheKeyAction(String method, MetaHolder metaHolder) { MethodExecutionAction cacheKeyAction = null; if (StringUtils.isNotBlank(method)) { Method cacheKeyMethod = getDeclaredMethod(metaHolder.getObj().getClass(), method, metaHolder.getMethod().getParameterTypes()); if (cacheKeyMethod == null) { throw new HystrixCachingException("method with name '" + method + "' doesn't exist in class '" + metaHolder.getObj().getClass() + "'"); } if (!cacheKeyMethod.getReturnType().equals(String.class)) { throw new HystrixCachingException("return type of cacheKey method must be String. Method: '" + method + "', Class: '" + metaHolder.getObj().getClass() + "'"); } MetaHolder cMetaHolder = MetaHolder.builder().obj(metaHolder.getObj()).method(cacheKeyMethod).args(metaHolder.getArgs()).build(); cacheKeyAction = new MethodExecutionAction(cMetaHolder.getObj(), cacheKeyMethod, cMetaHolder.getArgs(), cMetaHolder); } return cacheKeyAction; }
/** * Gets a {@link Method} object from target object by specified method name. * * @param joinPoint the {@link JoinPoint} * @param methodName the method name * @return a {@link Method} object or null if method with specified <code>methodName</code> doesn't exist */ public static Method getMethodFromTarget(JoinPoint joinPoint, String methodName) { return getDeclaredMethod(joinPoint.getTarget().getClass(), methodName, getParameterTypes(joinPoint)); }
/** * Gets declared method from specified type by mame and parameters types. * * @param type the type * @param methodName the name of the method * @param parameterTypes the parameter array * @return a {@link Method} object or null if method doesn't exist */ public static Method getDeclaredMethod(Class<?> type, String methodName, Class<?>... parameterTypes) { Method method = null; try { method = type.getDeclaredMethod(methodName, parameterTypes); if(method.isBridge()){ method = MethodProvider.getInstance().unbride(method, type); } } catch (NoSuchMethodException e) { Class<?> superclass = type.getSuperclass(); if (superclass != null) { method = getDeclaredMethod(superclass, methodName, parameterTypes); } } catch (ClassNotFoundException e) { Throwables.propagate(e); } catch (IOException e) { Throwables.propagate(e); } return method; }
/** * Gets a {@link Method} object from target object (not proxy class). * * @param joinPoint the {@link JoinPoint} * @return a {@link Method} object or null if method doesn't exist or if the signature at a join point * isn't sub-type of {@link MethodSignature} */ public static Method getMethodFromTarget(JoinPoint joinPoint) { Method method = null; if (joinPoint.getSignature() instanceof MethodSignature) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); method = getDeclaredMethod(joinPoint.getTarget().getClass(), signature.getName(), getParameterTypes(joinPoint)); } return method; }
Method batchCommandMethod = getDeclaredMethod(obj.getClass(), hystrixCollapser.batchMethod(), List.class);
private static MethodExecutionAction createCacheKeyAction(String method, MetaHolder metaHolder) { MethodExecutionAction cacheKeyAction = null; if (StringUtils.isNotBlank(method)) { Method cacheKeyMethod = getDeclaredMethod(metaHolder.getObj().getClass(), method, metaHolder.getMethod().getParameterTypes()); if (cacheKeyMethod == null) { throw new HystrixCachingException("method with name '" + method + "' doesn't exist in class '" + metaHolder.getObj().getClass() + "'"); } if (!cacheKeyMethod.getReturnType().equals(String.class)) { throw new HystrixCachingException("return type of cacheKey method must be String. Method: '" + method + "', Class: '" + metaHolder.getObj().getClass() + "'"); } MetaHolder cMetaHolder = MetaHolder.builder().obj(metaHolder.getObj()).method(cacheKeyMethod).args(metaHolder.getArgs()).build(); cacheKeyAction = new MethodExecutionAction(cMetaHolder.getObj(), cacheKeyMethod, cMetaHolder.getArgs(), cMetaHolder); } return cacheKeyAction; }