List of usage examples for org.springframework.core MethodParameter hasParameterAnnotation
public <A extends Annotation> boolean hasParameterAnnotation(Class<A> annotationType)
From source file:org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsStreamListenerSetupMethodOrchestrator.java
@Override @SuppressWarnings({ "unchecked" }) public Object[] adaptAndRetrieveInboundArguments(Method method, String inboundName, ApplicationContext applicationContext, StreamListenerParameterAdapter... streamListenerParameterAdapters) { Object[] arguments = new Object[method.getParameterTypes().length]; for (int parameterIndex = 0; parameterIndex < arguments.length; parameterIndex++) { MethodParameter methodParameter = MethodParameter.forExecutable(method, parameterIndex); Class<?> parameterType = methodParameter.getParameterType(); Object targetReferenceValue = null; if (methodParameter.hasParameterAnnotation(Input.class)) { targetReferenceValue = AnnotationUtils .getValue(methodParameter.getParameterAnnotation(Input.class)); Input methodAnnotation = methodParameter.getParameterAnnotation(Input.class); inboundName = methodAnnotation.value(); } else if (arguments.length == 1 && StringUtils.hasText(inboundName)) { targetReferenceValue = inboundName; }//from w w w. j a va 2 s . c o m if (targetReferenceValue != null) { Assert.isInstanceOf(String.class, targetReferenceValue, "Annotation value must be a String"); Object targetBean = applicationContext.getBean((String) targetReferenceValue); BindingProperties bindingProperties = this.bindingServiceProperties .getBindingProperties(inboundName); enableNativeDecodingForKTableAlways(parameterType, bindingProperties); //Retrieve the StreamsConfig created for this method if available. //Otherwise, create the StreamsBuilderFactory and get the underlying config. if (!this.methodStreamsBuilderFactoryBeanMap.containsKey(method)) { buildStreamsBuilderAndRetrieveConfig(method, applicationContext, inboundName); } try { StreamsBuilderFactoryBean streamsBuilderFactoryBean = this.methodStreamsBuilderFactoryBeanMap .get(method); StreamsBuilder streamsBuilder = streamsBuilderFactoryBean.getObject(); KafkaStreamsConsumerProperties extendedConsumerProperties = this.kafkaStreamsExtendedBindingProperties .getExtendedConsumerProperties(inboundName); //get state store spec KafkaStreamsStateStoreProperties spec = buildStateStoreSpec(method); Serde<?> keySerde = this.keyValueSerdeResolver.getInboundKeySerde(extendedConsumerProperties); Serde<?> valueSerde = this.keyValueSerdeResolver .getInboundValueSerde(bindingProperties.getConsumer(), extendedConsumerProperties); final KafkaConsumerProperties.StartOffset startOffset = extendedConsumerProperties .getStartOffset(); Topology.AutoOffsetReset autoOffsetReset = null; if (startOffset != null) { switch (startOffset) { case earliest: autoOffsetReset = Topology.AutoOffsetReset.EARLIEST; break; case latest: autoOffsetReset = Topology.AutoOffsetReset.LATEST; break; default: break; } } if (extendedConsumerProperties.isResetOffsets()) { LOG.warn("Detected resetOffsets configured on binding " + inboundName + ". " + "Setting resetOffsets in Kafka Streams binder does not have any effect."); } if (parameterType.isAssignableFrom(KStream.class)) { KStream<?, ?> stream = getkStream(inboundName, spec, bindingProperties, streamsBuilder, keySerde, valueSerde, autoOffsetReset); KStreamBoundElementFactory.KStreamWrapper kStreamWrapper = (KStreamBoundElementFactory.KStreamWrapper) targetBean; //wrap the proxy created during the initial target type binding with real object (KStream) kStreamWrapper.wrap((KStream<Object, Object>) stream); this.kafkaStreamsBindingInformationCatalogue .addStreamBuilderFactory(streamsBuilderFactoryBean); for (StreamListenerParameterAdapter streamListenerParameterAdapter : streamListenerParameterAdapters) { if (streamListenerParameterAdapter.supports(stream.getClass(), methodParameter)) { arguments[parameterIndex] = streamListenerParameterAdapter.adapt(kStreamWrapper, methodParameter); break; } } if (arguments[parameterIndex] == null && parameterType.isAssignableFrom(stream.getClass())) { arguments[parameterIndex] = stream; } Assert.notNull(arguments[parameterIndex], "Cannot convert argument " + parameterIndex + " of " + method + "from " + stream.getClass() + " to " + parameterType); } else if (parameterType.isAssignableFrom(KTable.class)) { String materializedAs = extendedConsumerProperties.getMaterializedAs(); String bindingDestination = this.bindingServiceProperties .getBindingDestination(inboundName); KTable<?, ?> table = getKTable(streamsBuilder, keySerde, valueSerde, materializedAs, bindingDestination, autoOffsetReset); KTableBoundElementFactory.KTableWrapper kTableWrapper = (KTableBoundElementFactory.KTableWrapper) targetBean; //wrap the proxy created during the initial target type binding with real object (KTable) kTableWrapper.wrap((KTable<Object, Object>) table); this.kafkaStreamsBindingInformationCatalogue .addStreamBuilderFactory(streamsBuilderFactoryBean); arguments[parameterIndex] = table; } else if (parameterType.isAssignableFrom(GlobalKTable.class)) { String materializedAs = extendedConsumerProperties.getMaterializedAs(); String bindingDestination = this.bindingServiceProperties .getBindingDestination(inboundName); GlobalKTable<?, ?> table = getGlobalKTable(streamsBuilder, keySerde, valueSerde, materializedAs, bindingDestination, autoOffsetReset); GlobalKTableBoundElementFactory.GlobalKTableWrapper globalKTableWrapper = (GlobalKTableBoundElementFactory.GlobalKTableWrapper) targetBean; //wrap the proxy created during the initial target type binding with real object (KTable) globalKTableWrapper.wrap((GlobalKTable<Object, Object>) table); this.kafkaStreamsBindingInformationCatalogue .addStreamBuilderFactory(streamsBuilderFactoryBean); arguments[parameterIndex] = table; } } catch (Exception ex) { throw new IllegalStateException(ex); } } else { throw new IllegalStateException(StreamListenerErrorMessages.INVALID_DECLARATIVE_METHOD_PARAMETERS); } } return arguments; }
From source file:org.springframework.cloud.stream.reactive.StreamEmitterAnnotationBeanPostProcessor.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private void invokeSetupMethodOnToTargetChannel(Method method, Object bean, String outboundName) { Object[] arguments = new Object[method.getParameterCount()]; Object targetBean = null;//from ww w. j a v a 2s . c om for (int parameterIndex = 0; parameterIndex < arguments.length; parameterIndex++) { MethodParameter methodParameter = new SynthesizingMethodParameter(method, parameterIndex); Class<?> parameterType = methodParameter.getParameterType(); Object targetReferenceValue = null; if (methodParameter.hasParameterAnnotation(Output.class)) { targetReferenceValue = AnnotationUtils .getValue(methodParameter.getParameterAnnotation(Output.class)); } else if (arguments.length == 1 && StringUtils.hasText(outboundName)) { targetReferenceValue = outboundName; } if (targetReferenceValue != null) { targetBean = this.applicationContext.getBean((String) targetReferenceValue); for (StreamListenerParameterAdapter<?, Object> streamListenerParameterAdapter : this.streamListenerParameterAdapters) { if (streamListenerParameterAdapter.supports(targetBean.getClass(), methodParameter)) { arguments[parameterIndex] = streamListenerParameterAdapter.adapt(targetBean, methodParameter); if (arguments[parameterIndex] instanceof FluxSender) { closeableFluxResources.add((FluxSender) arguments[parameterIndex]); } break; } } Assert.notNull(arguments[parameterIndex], "Cannot convert argument " + parameterIndex + " of " + method + "from " + targetBean.getClass() + " to " + parameterType); } else { throw new IllegalStateException(StreamEmitterErrorMessages.ATLEAST_ONE_OUTPUT); } } Object result; try { result = method.invoke(bean, arguments); } catch (Exception e) { throw new BeanInitializationException("Cannot setup StreamEmitter for " + method, e); } if (!Void.TYPE.equals(method.getReturnType())) { if (targetBean == null) { targetBean = this.applicationContext.getBean(outboundName); } boolean streamListenerResultAdapterFound = false; for (StreamListenerResultAdapter streamListenerResultAdapter : this.streamListenerResultAdapters) { if (streamListenerResultAdapter.supports(result.getClass(), targetBean.getClass())) { Closeable fluxDisposable = streamListenerResultAdapter.adapt(result, targetBean); closeableFluxResources.add(fluxDisposable); streamListenerResultAdapterFound = true; break; } } Assert.state(streamListenerResultAdapterFound, StreamEmitterErrorMessages.CANNOT_CONVERT_RETURN_TYPE_TO_ANY_AVAILABLE_RESULT_ADAPTERS); } }
From source file:org.springframework.cloud.stream.reactive.StreamEmitterAnnotationBeanPostProcessor.java
private static void validateStreamEmitterMethod(Method method, int outputAnnotationCount, String methodAnnotatedOutboundName) { if (StringUtils.hasText(methodAnnotatedOutboundName)) { Assert.isTrue(outputAnnotationCount == 0, StreamEmitterErrorMessages.INVALID_OUTPUT_METHOD_PARAMETERS); } else {/*from w ww. j av a 2 s .c o m*/ Assert.isTrue(outputAnnotationCount > 0, StreamEmitterErrorMessages.NO_OUTPUT_SPECIFIED); } if (!method.getReturnType().equals(Void.TYPE)) { Assert.isTrue(StringUtils.hasText(methodAnnotatedOutboundName), StreamEmitterErrorMessages.RETURN_TYPE_NO_OUTBOUND_SPECIFIED); Assert.isTrue(method.getParameterCount() == 0, StreamEmitterErrorMessages.RETURN_TYPE_METHOD_ARGUMENTS); } else { if (!StringUtils.hasText(methodAnnotatedOutboundName)) { int methodArgumentsLength = method.getParameterTypes().length; for (int parameterIndex = 0; parameterIndex < methodArgumentsLength; parameterIndex++) { MethodParameter methodParameter = new MethodParameter(method, parameterIndex); if (methodParameter.hasParameterAnnotation(Output.class)) { String outboundName = (String) AnnotationUtils .getValue(methodParameter.getParameterAnnotation(Output.class)); Assert.isTrue(StringUtils.hasText(outboundName), StreamEmitterErrorMessages.INVALID_OUTBOUND_NAME); } else { throw new IllegalArgumentException( StreamEmitterErrorMessages.OUTPUT_ANNOTATION_MISSING_ON_METHOD_PARAMETERS_VOID_RETURN_TYPE); } } } } }
From source file:org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.java
private Type determineInferredType(Method method) { if (method == null) { return null; }//from ww w . j a v a2 s . c om Type genericParameterType = null; boolean hasAck = false; for (int i = 0; i < method.getParameterTypes().length; i++) { MethodParameter methodParameter = new MethodParameter(method, i); /* * We're looking for a single non-annotated parameter, or one annotated with @Payload. * We ignore parameters with type Message because they are not involved with conversion. */ if (eligibleParameter(methodParameter) && (methodParameter.getParameterAnnotations().length == 0 || methodParameter.hasParameterAnnotation(Payload.class))) { if (genericParameterType == null) { genericParameterType = methodParameter.getGenericParameterType(); if (genericParameterType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) genericParameterType; if (parameterizedType.getRawType().equals(Message.class)) { genericParameterType = ((ParameterizedType) genericParameterType) .getActualTypeArguments()[0]; } else if (parameterizedType.getRawType().equals(List.class) && parameterizedType.getActualTypeArguments().length == 1) { Type paramType = parameterizedType.getActualTypeArguments()[0]; this.isConsumerRecordList = paramType.equals(ConsumerRecord.class) || (paramType instanceof ParameterizedType && ((ParameterizedType) paramType) .getRawType().equals(ConsumerRecord.class)); this.isMessageList = paramType.equals(Message.class) || (paramType instanceof ParameterizedType && ((ParameterizedType) paramType).getRawType().equals(Message.class)); } } } else { if (this.logger.isDebugEnabled()) { this.logger.debug("Ambiguous parameters for target payload for method " + method + "; no inferred type available"); } break; } } else if (methodParameter.getGenericParameterType().equals(Acknowledgment.class)) { hasAck = true; } } Assert.state( !this.isConsumerRecordList || method.getParameterTypes().length == 1 || (method.getGenericParameterTypes().length == 2 && hasAck), "A parameter of type 'List<ConsumerRecord>' must be the only parameter " + "(except for an optional 'Acknowledgment')"); Assert.state( !this.isMessageList || method.getParameterTypes().length == 1 || (method.getGenericParameterTypes().length == 2 && hasAck), "A parameter of type 'List<Message<?>>' must be the only parameter " + "(except for an optional 'Acknowledgment')"); return genericParameterType; }
From source file:org.springframework.messaging.handler.annotation.reactive.HeaderMethodArgumentResolver.java
@Override public boolean supportsParameter(MethodParameter parameter) { return parameter.hasParameterAnnotation(Header.class); }
From source file:org.springframework.messaging.handler.annotation.reactive.PayloadMethodArgumentResolver.java
@Override public boolean supportsParameter(MethodParameter parameter) { return parameter.hasParameterAnnotation(Payload.class) || this.useDefaultResolution; }
From source file:org.springframework.web.method.annotation.ModelAttributeMethodProcessor.java
/** * Returns {@code true} if the parameter is annotated with * {@link ModelAttribute} or, if in default resolution mode, for any * method parameter that is not a simple type. *//*from w w w. ja va 2 s.co m*/ @Override public boolean supportsParameter(MethodParameter parameter) { return (parameter.hasParameterAnnotation(ModelAttribute.class) || (this.annotationNotRequired && !BeanUtils.isSimpleProperty(parameter.getParameterType()))); }
From source file:org.springframework.web.method.annotation.ModelFactory.java
/** * Find {@code @ModelAttribute} arguments also listed as {@code @SessionAttributes}. *//*from w ww. j av a 2 s.c om*/ private List<String> findSessionAttributeArguments(HandlerMethod handlerMethod) { List<String> result = new ArrayList<>(); for (MethodParameter parameter : handlerMethod.getMethodParameters()) { if (parameter.hasParameterAnnotation(ModelAttribute.class)) { String name = getNameForParameter(parameter); Class<?> paramType = parameter.getParameterType(); if (this.sessionAttributesHandler.isHandlerSessionAttribute(name, paramType)) { result.add(name); } } } return result; }
From source file:org.springframework.web.method.annotation.support.ModelAttributeMethodProcessor.java
/** * @return true if the parameter is annotated with {@link ModelAttribute} * or in default resolution mode also if it is not a simple type. *///from w w w . j a v a 2s . c om public boolean supportsParameter(MethodParameter parameter) { if (parameter.hasParameterAnnotation(ModelAttribute.class)) { return true; } else if (this.annotationNotRequired) { return !BeanUtils.isSimpleProperty(parameter.getParameterType()); } else { return false; } }