Example usage for org.springframework.core ResolvableType forInstance

List of usage examples for org.springframework.core ResolvableType forInstance

Introduction

In this page you can find the example usage for org.springframework.core ResolvableType forInstance.

Prototype

public static ResolvableType forInstance(Object instance) 

Source Link

Document

Return a ResolvableType for the specified instance.

Usage

From source file:org.springframework.context.event.SimpleApplicationEventMulticaster.java

private ResolvableType resolveDefaultEventType(ApplicationEvent event) {
    return ResolvableType.forInstance(event);
}

From source file:org.springframework.messaging.handler.invocation.reactive.AbstractEncoderMethodReturnValueHandler.java

@SuppressWarnings("unchecked")
private Flux<DataBuffer> encodeContent(@Nullable Object content, MethodParameter returnType,
        DataBufferFactory bufferFactory, @Nullable MimeType mimeType, Map<String, Object> hints) {

    ResolvableType returnValueType = ResolvableType.forMethodParameter(returnType);
    ReactiveAdapter adapter = getAdapterRegistry().getAdapter(returnValueType.resolve(), content);

    Publisher<?> publisher;//from w  ww.  j  a va2 s . c  o m
    ResolvableType elementType;
    if (adapter != null) {
        publisher = adapter.toPublisher(content);
        boolean isUnwrapped = KotlinDetector.isKotlinReflectPresent()
                && KotlinDetector.isKotlinType(returnType.getContainingClass())
                && KotlinDelegate.isSuspend(returnType.getMethod())
                && !COROUTINES_FLOW_CLASS_NAME.equals(returnValueType.toClass().getName());
        ResolvableType genericType = isUnwrapped ? returnValueType : returnValueType.getGeneric();
        elementType = getElementType(adapter, genericType);
    } else {
        publisher = Mono.justOrEmpty(content);
        elementType = (returnValueType.toClass() == Object.class && content != null
                ? ResolvableType.forInstance(content)
                : returnValueType);
    }

    if (elementType.resolve() == void.class || elementType.resolve() == Void.class) {
        return Flux.from(publisher).cast(DataBuffer.class);
    }

    Encoder<?> encoder = getEncoder(elementType, mimeType);
    return Flux.from((Publisher) publisher)
            .map(value -> encodeValue(value, elementType, encoder, bufferFactory, mimeType, hints));
}

From source file:org.springframework.messaging.handler.invocation.reactive.AbstractEncoderMethodReturnValueHandler.java

@SuppressWarnings("unchecked")
private <T> DataBuffer encodeValue(Object element, ResolvableType elementType, @Nullable Encoder<T> encoder,
        DataBufferFactory bufferFactory, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

    if (encoder == null) {
        encoder = getEncoder(ResolvableType.forInstance(element), mimeType);
        if (encoder == null) {
            throw new MessagingException(
                    "No encoder for " + elementType + ", current value type is " + element.getClass());
        }//from w w w  .  j  av  a 2 s  .c  om
    }
    return encoder.encodeValue((T) element, bufferFactory, elementType, mimeType, hints);
}