info.append("Synchronized=").append(Modifier.isSynchronized(m.getModifiers())).append("\n"); info.append("Abstract=").append(Modifier.isAbstract(m.getModifiers())).append("\n"); info.append("AccessLevel=").append(getAccessLevel(m.getModifiers())).append("\n");
public static <T extends Annotation> Optional<T> getAnnotation(Class<?> type, Class<T> annotation) { Validate.notNull(annotation, "annotation cannot be null"); Validate.notNull(type, "type cannot be null"); for (Annotation ann : type.getDeclaredAnnotations()) { if (ann.annotationType().equals(annotation)) return Optional.of((T) ann); } Class<?> superType = type.getSuperclass(); if (superType != null && !superType.equals(Object.class)) { return getAnnotation(superType, annotation); } return Optional.absent(); }
/** * 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)); }
public abstract MetaHolder create(Object proxy, Method method, Object obj, Object[] args, final ProceedingJoinPoint 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; }
static HystrixPointcutType of(Method method) { if (method.isAnnotationPresent(HystrixCommand.class)) { return COMMAND; } else if (method.isAnnotationPresent(HystrixCollapser.class)) { return COLLAPSER; } else { String methodInfo = getMethodInfo(method); throw new IllegalStateException("'https://github.com/Netflix/Hystrix/issues/1458' - no valid annotation found for: \n" + methodInfo); } } }
@Around("cacheRemoveAnnotationPointcut()") public Object methodsAnnotatedWithCacheRemove(final ProceedingJoinPoint joinPoint) throws Throwable { Method method = getMethodFromTarget(joinPoint); Object obj = joinPoint.getTarget(); Object[] args = joinPoint.getArgs(); Validate.notNull(method, "failed to get method from joinPoint: %s", joinPoint); MetaHolder metaHolder = MetaHolder.builder() .args(args).method(method).obj(obj) .executionType(ExecutionType.SYNCHRONOUS) .ajcMethod(isCompileWeaving() ? getAjcMethodAroundAdvice(obj.getClass(), method) : null) .build(); CacheInvocationContext<CacheRemove> context = CacheInvocationContextFactory .createCacheRemoveInvocationContext(metaHolder); HystrixRequestCacheManager.getInstance().clearCache(context); return joinPoint.proceed(); }
Method batchCommandMethod = getDeclaredMethod(obj.getClass(), hystrixCollapser.batchMethod(), List.class);
static HystrixPointcutType of(Method method) { if (method.isAnnotationPresent(HystrixCommand.class)) { return COMMAND; } else if (method.isAnnotationPresent(HystrixCollapser.class)) { return COLLAPSER; } else { String methodInfo = getMethodInfo(method); throw new IllegalStateException("'https://github.com/Netflix/Hystrix/issues/1458' - no valid annotation found for: \n" + methodInfo); } } }
/** * 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; }
public static <T extends Annotation> Optional<T> getAnnotation(JoinPoint joinPoint, Class<T> annotation) { return getAnnotation(joinPoint.getTarget().getClass(), annotation); }
@Around("hystrixCommandAnnotationPointcut() || hystrixCollapserAnnotationPointcut()") public Object methodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinPoint) throws Throwable { Method method = getMethodFromTarget(joinPoint); Validate.notNull(method, "failed to get method from joinPoint: %s", joinPoint); if (method.isAnnotationPresent(HystrixCommand.class) && method.isAnnotationPresent(HystrixCollapser.class)) { throw new IllegalStateException("method cannot be annotated with HystrixCommand and HystrixCollapser " + "annotations at the same time"); } MetaHolderFactory metaHolderFactory = META_HOLDER_FACTORY_MAP.get(HystrixPointcutType.of(method)); MetaHolder metaHolder = metaHolderFactory.create(joinPoint); HystrixInvokable invokable = HystrixCommandFactory.getInstance().create(metaHolder); ExecutionType executionType = metaHolder.isCollapserAnnotationPresent() ? metaHolder.getCollapserExecutionType() : metaHolder.getExecutionType(); Object result; try { if (!metaHolder.isObservable()) { result = CommandExecutor.execute(invokable, executionType, metaHolder); } else { result = executeObservable(invokable, executionType, metaHolder); } } catch (HystrixBadRequestException e) { throw e.getCause() != null ? e.getCause() : e; } catch (HystrixRuntimeException e) { throw hystrixRuntimeExceptionToThrowable(metaHolder, e); } return result; }
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; }
info.append("Synchronized=").append(Modifier.isSynchronized(m.getModifiers())).append("\n"); info.append("Abstract=").append(Modifier.isAbstract(m.getModifiers())).append("\n"); info.append("AccessLevel=").append(getAccessLevel(m.getModifiers())).append("\n");
/** * 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)); }
private static MetaHolder.Builder setDefaultProperties(MetaHolder.Builder builder, Class<?> declaringClass, final ProceedingJoinPoint joinPoint) { Optional<DefaultProperties> defaultPropertiesOpt = AopUtils.getAnnotation(joinPoint, DefaultProperties.class); builder.defaultGroupKey(declaringClass.getSimpleName()); if (defaultPropertiesOpt.isPresent()) { DefaultProperties defaultProperties = defaultPropertiesOpt.get(); builder.defaultProperties(defaultProperties); if (StringUtils.isNotBlank(defaultProperties.groupKey())) { builder.defaultGroupKey(defaultProperties.groupKey()); } if (StringUtils.isNotBlank(defaultProperties.threadPoolKey())) { builder.defaultThreadPoolKey(defaultProperties.threadPoolKey()); } } return builder; }
public abstract MetaHolder create(Object proxy, Method method, Object obj, Object[] args, final ProceedingJoinPoint 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; }
public static <T extends Annotation> Optional<T> getAnnotation(Class<?> type, Class<T> annotation) { Validate.notNull(annotation, "annotation cannot be null"); Validate.notNull(type, "type cannot be null"); for (Annotation ann : type.getDeclaredAnnotations()) { if (ann.annotationType().equals(annotation)) return Optional.of((T) ann); } Class<?> superType = type.getSuperclass(); if (superType != null && !superType.equals(Object.class)) { return getAnnotation(superType, annotation); } return Optional.absent(); }