AnnotationValue<Client> clientAnnotation = context.findAnnotation(Client.class).orElseThrow(() -> new IllegalStateException("Client advice called from type that is not annotated with @Client: " + context) ); Class<?> declaringType = context.getDeclaringType(); if (Closeable.class == declaringType || AutoCloseable.class == declaringType) { String clientId = clientAnnotation.getValue(String.class).orElse(null); Optional<Class<? extends Annotation>> httpMethodMapping = context.getAnnotationTypeByStereotype(HttpMethodMapping.class); if (context.hasStereotype(HttpMethodMapping.class) && httpClient != null) { AnnotationValue<HttpMethodMapping> mapping = context.getAnnotation(HttpMethodMapping.class); String uri = mapping.getRequiredValue(String.class); if (StringUtils.isEmpty(uri)) { uri = "/" + context.getMethodName(); ReturnType returnType = context.getReturnType(); Class<?> javaReturnType = returnType.getType(); Map<String, Object> paramMap = context.getParameterValueMap(); Map<String, String> queryParams = new LinkedHashMap<>(); List<String> uriVariables = uriTemplate.getVariableNames(); MutableHttpRequest<Object> request; Object body = null; Map<String, MutableArgumentValue<?>> parameters = context.getParameters(); Argument[] arguments = context.getArguments(); List<AnnotationValue<Header>> headerAnnotations = context.getAnnotationValuesByType(Header.class); for (AnnotationValue<Header> headerAnnotation : headerAnnotations) {
@Override public Object intercept(MethodInvocationContext<Object, Object> context) { return executionHandle.invoke(context.getParameterValues()); } }
@SuppressWarnings("unchecked") @Override public Object intercept(MethodInvocationContext<Object, Object> context) { Map<String, Object> parameterValueMap = context.getParameterValueMap(); int len = parameterValueMap.size(); Optional<Argument> bodyArg = Arrays.stream(context.getArguments()).filter(arg -> arg.isAnnotationPresent(Body.class)).findFirst(); if (bodyArg.isPresent()) { body = parameterValueMap.get(bodyArg.get().getName()); String functionName = context.getValue(Named.class, String.class).orElse(NameUtils.hyphenate(context.getMethodName(), true)); ReturnType<Object> returnType = context.getReturnType(); Class<Object> javaReturnType = returnType.getType(); if (Publishers.isConvertibleToPublisher(javaReturnType)) {
@Override public Object intercept(MethodInvocationContext<Object, Object> context) { Optional<AnnotationValue<Retryable>> opt = context.findAnnotation(Retryable.class); if (!opt.isPresent()) { return context.proceed(); boolean isCircuitBreaker = context.hasStereotype(CircuitBreaker.class); MutableRetryState retryState; AnnotationRetryStateBuilder retryStateBuilder = new AnnotationRetryStateBuilder( .getValue(CircuitBreaker.class, "reset", Duration.class) .map(Duration::toMillis).orElse(Duration.ofSeconds(DEFAULT_CIRCUIT_BREAKER_TIMEOUT_IN_MILLIS).toMillis()); retryState = circuitContexts.computeIfAbsent( context.getExecutableMethod(), method -> new CircuitBreakerRetry(timeout, retryStateBuilder, context, eventPublisher) ); MutableConvertibleValues<Object> attrs = context.getAttributes(); attrs.put(RetryState.class.getName(), retry); ReturnType<Object> returnType = context.getReturnType(); Class<Object> javaReturnType = returnType.getType(); if (Publishers.isConvertibleToPublisher(javaReturnType)) { ConversionService<?> conversionService = ConversionService.SHARED; Object result = context.proceed(); if (result == null) { return result; while (true) { try {
@Override public Object intercept(MethodInvocationContext<Object, Object> context) { String executorName = context.getValue(Async.class, String.class).orElse(TaskExecutors.SCHEDULED); ExecutorService executorService = beanLocator.findBean(ExecutorService.class, Qualifiers.byName(executorName)).orElseThrow(() -> new TaskExecutionException("No ExecutorService named [" + executorName + "] configured in application context") ); ReturnType<Object> rt = context.getReturnType(); Class<?> returnType = rt.getType(); if (CompletionStage.class.isAssignableFrom(returnType)) { CompletionStage<?> completionStage = (CompletionStage) context.proceed(); if (completionStage == null) { newFuture.complete(null); executorService.submit(() -> { try { context.proceed(); } catch (Throwable e) { if (LOG.isErrorEnabled()) { LOG.error("Error occurred executing @Async method [" + context.getExecutableMethod() + "]: " + e.getMessage(), e); return null; } else { throw new TaskExecutionException("Method [" + context.getExecutableMethod() + "] must return either void, or an instance of Publisher or CompletionStage");
@Override public final Object intercept(MethodInvocationContext<Object, Object> context) { if (context.hasAnnotation(Transactional.class)) { String transactionManagerName = context.getValue(Transactional.class, String.class).orElse(null); if (StringUtils.isEmpty(transactionManagerName)) { transactionManagerName = null; } PlatformTransactionManager transactionManager = resolveTransactionManager(transactionManagerName); String finalTransactionManagerName = transactionManagerName; TransactionAttribute transactionDefinition = resolveTransactionAttribute( context.getExecutableMethod(), context, finalTransactionManagerName ); TransactionTemplate template = new TransactionTemplate( transactionManager, transactionDefinition ); return template.execute(status -> context.proceed()); } else { return context.proceed(); } }
@Override public final Object intercept(MethodInvocationContext<Object, Object> context) { if (context.hasDeclaredAnnotation(Transactional.class)) { String transactionManagerName = context.getValue(Transactional.class, String.class).orElse(null); if (StringUtils.isEmpty(transactionManagerName)) { transactionManagerName = null; } PlatformTransactionManager transactionManager = resolveTransactionManager(transactionManagerName); String finalTransactionManagerName = transactionManagerName; TransactionAttribute transactionDefinition = resolveTransactionAttribute( context.getExecutableMethod(), context, finalTransactionManagerName ); TransactionTemplate template = new TransactionTemplate( transactionManager, transactionDefinition ); return template.execute(status -> context.proceed()); } else { return context.proceed(); } }
private void doProceed(MethodInvocationContext context, ValueWrapper wrapper) { Object result = context.proceed(); if (result instanceof Optional) { Optional optional = (Optional) result; wrapper.optional = true; if (optional.isPresent()) { wrapper.value = optional.get(); } } else { wrapper.value = result; } }
@Override public Object intercept(MethodInvocationContext context) { if (executableValidator == null) { return context.proceed(); } else { Method targetMethod = context.getTargetMethod(); if (targetMethod.getParameterTypes().length == 0) { return context.proceed(); } else { Set<ConstraintViolation<Object>> constraintViolations = executableValidator .validateParameters( context.getTarget(), targetMethod, context.getParameterValues() ); if (constraintViolations.isEmpty()) { return context.proceed(); } else { throw new ConstraintViolationException(constraintViolations); } } } } }
@Override public Object intercept(MethodInvocationContext<Object, Object> context) { if (context.hasStereotype(CacheConfig.class)) { ReturnType returnTypeObject = context.getReturnType(); Class returnType = returnTypeObject.getType(); if (CompletableFuture.class.isAssignableFrom(returnType)) { return interceptCompletableFuture(context, returnTypeObject, returnType); } else if (Publishers.isConvertibleToPublisher(returnType)) { return interceptPublisher(context, returnTypeObject, returnType); } else { return interceptSync(context, returnTypeObject, returnType); } } else { return context.proceed(); } }
@Override public Object intercept(MethodInvocationContext<Object, Object> context) { final AnnotationMetadata annotationMetadata = context.getAnnotationMetadata(); final boolean isSingleton = MicronautBeanFactory.isSingleton(annotationMetadata); if (isSingleton) { final ExecutableMethod<Object, Object> method = context.getExecutableMethod(); synchronized (computedSingletons) { Object o = computedSingletons.get(method); if (o == null) { o = context.proceed(); if (o == null) { throw new BeanCreationException("Bean factor method [" + method + "] returned null"); } computedSingletons.put(method, o); } return o; } } return context.proceed(); }
@Override public Object intercept( MethodInvocationContext<Object, Object> ctx ) { Object retVal; Logger log = LoggerFactory.getLogger( ctx.getDeclaringType().getClass() ); Date start = new Date(); Loggable annotation = ctx.getDeclaringType().getAnnotation( Loggable.class ); try { if ( log.isDebugEnabled() && annotation.value() ) { log.debug( printBefore( ctx ) ); } retVal = ctx.proceed(); if ( log.isDebugEnabled() && annotation.value() ) { long duration = new Date().getTime() - start.getTime(); log.debug( printAfter( ctx, retVal, duration ) ); } } catch ( Throwable throwable ) { if ( log.isErrorEnabled() && annotation.value() ) { log.error( printError( ctx, throwable ), throwable ); } throw throwable; } return retVal; }
MethodExecutionHandle<?, Object> fallbackHandle = fallbackMethod.get(); if (LOG.isDebugEnabled()) { LOG.debug("Type [{}] resolved fallback: {}", context.getTarget().getClass(), fallbackHandle); fallbackResult = fallbackHandle.invoke(context.getParameterValues()); } catch (Exception e) { return Flowable.error(throwable); .convert(recoveryFlowable, context.getReturnType().asArgument()) .orElseThrow(() -> new FallbackException("Unsupported Reactive type: " + result));
private String methodInSignature( MethodInvocationContext<Object, Object> ctx ) { Method method = ctx.getTargetMethod(); String beanName = ctx.getDeclaringType().getName(); String methodName = method.getName(); return beanName + "." + methodName + "(" + ctx.getParameters() + ")"; }
private Object[] resolveParams(MethodInvocationContext<?, ?> context, String[] parameterNames) { Object[] parameterValues; if (ArrayUtils.isEmpty(parameterNames)) { parameterValues = context.getParameterValues(); } else { List list = new ArrayList(); Map<String, MutableArgumentValue<?>> parameters = context.getParameters(); for (String name : parameterNames) { list.add(parameters.get(name).getValue()); } parameterValues = list.toArray(); } return parameterValues; }
if (LOG.isErrorEnabled()) { LOG.debug(nase.getMessage(), nase); LOG.error("Type [{}] attempting to resolve fallback for unavailable service [{}]", context.getTarget().getClass().getName(), nase.getServiceID()); LOG.error("Type [" + context.getTarget().getClass().getName() + "] executed with error: " + exception.getMessage(), exception); try { if (LOG.isDebugEnabled()) { LOG.debug("Type [{}] resolved fallback: {}", context.getTarget().getClass().getName(), fallbackMethod); return fallbackMethod.invoke(context.getParameterValues()); } catch (Exception e) { throw new FallbackException("Error invoking fallback for type [" + context.getTarget().getClass().getName() + "]: " + e.getMessage(), e);
/** * Finds a fallback method for the given context. * * @param context The context * @return The fallback method if it is present */ public Optional<? extends MethodExecutionHandle<?, Object>> findFallbackMethod(MethodInvocationContext<Object, Object> context) { Class<?> declaringType = context.getTarget().getClass(); Optional<? extends MethodExecutionHandle<?, Object>> result = beanContext .findExecutionHandle(declaringType, Qualifiers.byStereotype(Fallback.class), context.getMethodName(), context.getArgumentTypes()); if (!result.isPresent()) { Set<Class> allInterfaces = ReflectionUtils.getAllInterfaces(declaringType); for (Class i : allInterfaces) { result = beanContext .findExecutionHandle(i, Qualifiers.byStereotype(Fallback.class), context.getMethodName(), context.getArgumentTypes()); if (result.isPresent()) { return result; } } } return result; }
CacheOperation(MethodInvocationContext<?, ?> context, Class returnType) { this.context = context; this.returnType = returnType; this.defaultConfig = context.getAnnotation(CacheConfig.class); this.defaultKeyGenerator = resolveKeyGenerator(defaultConfig.get(MEMBER_KEY_GENERATOR, Class.class).orElse(null)); boolean isVoid = isVoid(); this.putOperations = isVoid ? null : putOperations(context); this.invalidateOperations = invalidateOperations(context); if (!isVoid && context.hasStereotype(Cacheable.class)) { AnnotationValue<Cacheable> cacheable = context.getAnnotation(Cacheable.class); String[] names = resolveCacheNames(defaultConfig, cacheable); if (ArrayUtils.isNotEmpty(names)) { this.cacheableCacheName = names[0]; this.cacheable = cacheable; } } }
DefaultHttpClient defaultClient = (DefaultHttpClient) client; defaultClient.setClientIdentifiers(clientId); AnnotationValue<JacksonFeatures> jacksonFeatures = context.findAnnotation(JacksonFeatures.class).orElse(null);