Java Code Examples for org.springframework.core.ResolvableType#toClass()
The following examples show how to use
org.springframework.core.ResolvableType#toClass() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApplicationListenerMethodAdapter.java From spring-analysis-note with MIT License | 6 votes |
@Nullable private ResolvableType getResolvableType(ApplicationEvent event) { ResolvableType payloadType = null; if (event instanceof PayloadApplicationEvent) { PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event; ResolvableType eventType = payloadEvent.getResolvableType(); if (eventType != null) { payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric(); } } for (ResolvableType declaredEventType : this.declaredEventTypes) { Class<?> eventClass = declaredEventType.toClass(); if (!ApplicationEvent.class.isAssignableFrom(eventClass) && payloadType != null && declaredEventType.isAssignableFrom(payloadType)) { return declaredEventType; } if (eventClass.isInstance(event)) { return declaredEventType; } } return null; }
Example 2
Source File: ApplicationListenerMethodAdapter.java From java-technology-stack with MIT License | 6 votes |
@Nullable private ResolvableType getResolvableType(ApplicationEvent event) { ResolvableType payloadType = null; if (event instanceof PayloadApplicationEvent) { PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event; ResolvableType eventType = payloadEvent.getResolvableType(); if (eventType != null) { payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric(); } } for (ResolvableType declaredEventType : this.declaredEventTypes) { Class<?> eventClass = declaredEventType.toClass(); if (!ApplicationEvent.class.isAssignableFrom(eventClass) && payloadType != null && declaredEventType.isAssignableFrom(payloadType)) { return declaredEventType; } if (eventClass.isInstance(event)) { return declaredEventType; } } return null; }
Example 3
Source File: Jaxb2XmlDecoder.java From spring-analysis-note with MIT License | 6 votes |
@Override public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { Flux<XMLEvent> xmlEventFlux = this.xmlEventDecoder.decode( inputStream, ResolvableType.forClass(XMLEvent.class), mimeType, hints); Class<?> outputClass = elementType.toClass(); QName typeName = toQName(outputClass); Flux<List<XMLEvent>> splitEvents = split(xmlEventFlux, typeName); return splitEvents.map(events -> { Object value = unmarshal(events, outputClass); LogFormatUtils.traceDebug(logger, traceOn -> { String formatted = LogFormatUtils.formatValue(value, !traceOn); return Hints.getLogPrefix(hints) + "Decoded [" + formatted + "]"; }); return value; }); }
Example 4
Source File: Jaxb2XmlEncoder.java From spring-analysis-note with MIT License | 5 votes |
@Override public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) { if (super.canEncode(elementType, mimeType)) { Class<?> outputClass = elementType.toClass(); return (outputClass.isAnnotationPresent(XmlRootElement.class) || outputClass.isAnnotationPresent(XmlType.class)); } else { return false; } }
Example 5
Source File: Jaxb2XmlEncoder.java From java-technology-stack with MIT License | 5 votes |
@Override public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) { if (super.canEncode(elementType, mimeType)) { Class<?> outputClass = elementType.toClass(); return (outputClass.isAnnotationPresent(XmlRootElement.class) || outputClass.isAnnotationPresent(XmlType.class)); } else { return false; } }
Example 6
Source File: AbstractEncoderMethodReturnValueHandler.java From spring-analysis-note with MIT License | 5 votes |
@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; 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)); }
Example 7
Source File: ByteBufferEncoder.java From spring-analysis-note with MIT License | 4 votes |
@Override public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) { Class<?> clazz = elementType.toClass(); return super.canEncode(elementType, mimeType) && ByteBuffer.class.isAssignableFrom(clazz); }
Example 8
Source File: AbstractMessageWriterResultHandler.java From java-technology-stack with MIT License | 4 votes |
/** * Write a given body to the response with {@link HttpMessageWriter}. * @param body the object to write * @param bodyParameter the {@link MethodParameter} of the body to write * @param actualParam the actual return type of the method that returned the value; * could be different from {@code bodyParameter} when processing {@code HttpEntity} * for example * @param exchange the current exchange * @return indicates completion or error * @since 5.0.2 */ @SuppressWarnings({"unchecked", "rawtypes"}) protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParameter, @Nullable MethodParameter actualParam, ServerWebExchange exchange) { ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParameter); ResolvableType actualType = (actualParam != null ? ResolvableType.forMethodParameter(actualParam) : bodyType); ReactiveAdapter adapter = getAdapterRegistry().getAdapter(bodyType.resolve(), body); Publisher<?> publisher; ResolvableType elementType; if (adapter != null) { publisher = adapter.toPublisher(body); ResolvableType genericType = bodyType.getGeneric(); elementType = getElementType(adapter, genericType); } else { publisher = Mono.justOrEmpty(body); elementType = (bodyType.toClass() == Object.class && body != null ? ResolvableType.forInstance(body) : bodyType); } if (elementType.resolve() == void.class || elementType.resolve() == Void.class) { return Mono.from((Publisher<Void>) publisher); } ServerHttpRequest request = exchange.getRequest(); ServerHttpResponse response = exchange.getResponse(); MediaType bestMediaType = selectMediaType(exchange, () -> getMediaTypesFor(elementType)); if (bestMediaType != null) { String logPrefix = exchange.getLogPrefix(); if (logger.isDebugEnabled()) { logger.debug(logPrefix + (publisher instanceof Mono ? "0..1" : "0..N") + " [" + elementType + "]"); } for (HttpMessageWriter<?> writer : getMessageWriters()) { if (writer.canWrite(elementType, bestMediaType)) { return writer.write((Publisher) publisher, actualType, elementType, bestMediaType, request, response, Hints.from(Hints.LOG_PREFIX_HINT, logPrefix)); } } } else { if (getMediaTypesFor(elementType).isEmpty()) { return Mono.error(new IllegalStateException("No writer for : " + elementType)); } } return Mono.error(new NotAcceptableStatusException(getMediaTypesFor(elementType))); }
Example 9
Source File: ByteBufEncoder.java From alibaba-rsocket-broker with Apache License 2.0 | 4 votes |
@Override public boolean canEncode(ResolvableType elementType, MimeType mimeType) { Class<?> clazz = elementType.toClass(); return super.canEncode(elementType, mimeType) && ByteBuf.class.isAssignableFrom(clazz); }
Example 10
Source File: ByteBufEncoder.java From alibaba-rsocket-broker with Apache License 2.0 | 4 votes |
@Override public boolean canEncode(ResolvableType elementType, MimeType mimeType) { Class<?> clazz = elementType.toClass(); return super.canEncode(elementType, mimeType) && ByteBuf.class.isAssignableFrom(clazz); }
Example 11
Source File: CharSequenceEncoder.java From java-technology-stack with MIT License | 4 votes |
@Override public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) { Class<?> clazz = elementType.toClass(); return super.canEncode(elementType, mimeType) && CharSequence.class.isAssignableFrom(clazz); }
Example 12
Source File: AbstractJackson2Encoder.java From spring-analysis-note with MIT License | 4 votes |
@Override public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) { Class<?> clazz = elementType.toClass(); return supportsMimeType(mimeType) && (Object.class == clazz || (!String.class.isAssignableFrom(elementType.resolve(clazz)) && getObjectMapper().canSerialize(clazz))); }
Example 13
Source File: ResourceEncoder.java From spring-analysis-note with MIT License | 4 votes |
@Override public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) { Class<?> clazz = elementType.toClass(); return (super.canEncode(elementType, mimeType) && Resource.class.isAssignableFrom(clazz)); }
Example 14
Source File: ByteArrayEncoder.java From java-technology-stack with MIT License | 4 votes |
@Override public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) { Class<?> clazz = elementType.toClass(); return super.canEncode(elementType, mimeType) && byte[].class.isAssignableFrom(clazz); }
Example 15
Source File: AbstractJackson2Encoder.java From java-technology-stack with MIT License | 4 votes |
@Override public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) { Class<?> clazz = elementType.toClass(); return supportsMimeType(mimeType) && (Object.class == clazz || (!String.class.isAssignableFrom(elementType.resolve(clazz)) && getObjectMapper().canSerialize(clazz))); }
Example 16
Source File: GenericConversionService.java From spring-analysis-note with MIT License | 4 votes |
public ConverterAdapter(Converter<?, ?> converter, ResolvableType sourceType, ResolvableType targetType) { this.converter = (Converter<Object, Object>) converter; this.typeInfo = new ConvertiblePair(sourceType.toClass(), targetType.toClass()); this.targetType = targetType; }
Example 17
Source File: ResourceEncoder.java From java-technology-stack with MIT License | 4 votes |
@Override public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) { Class<?> clazz = elementType.toClass(); return (super.canEncode(elementType, mimeType) && Resource.class.isAssignableFrom(clazz)); }
Example 18
Source File: AbstractMessageWriterResultHandler.java From spring-analysis-note with MIT License | 4 votes |
/** * Write a given body to the response with {@link HttpMessageWriter}. * @param body the object to write * @param bodyParameter the {@link MethodParameter} of the body to write * @param actualParam the actual return type of the method that returned the value; * could be different from {@code bodyParameter} when processing {@code HttpEntity} * for example * @param exchange the current exchange * @return indicates completion or error * @since 5.0.2 */ @SuppressWarnings({"unchecked", "rawtypes", "ConstantConditions"}) protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParameter, @Nullable MethodParameter actualParam, ServerWebExchange exchange) { ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParameter); ResolvableType actualType = (actualParam != null ? ResolvableType.forMethodParameter(actualParam) : bodyType); ReactiveAdapter adapter = getAdapterRegistry().getAdapter(bodyType.resolve(), body); Publisher<?> publisher; ResolvableType elementType; ResolvableType actualElementType; if (adapter != null) { publisher = adapter.toPublisher(body); boolean isUnwrapped = KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(bodyParameter.getContainingClass()) && KotlinDelegate.isSuspend(bodyParameter.getMethod()) && !COROUTINES_FLOW_CLASS_NAME.equals(bodyType.toClass().getName()); ResolvableType genericType = isUnwrapped ? bodyType : bodyType.getGeneric(); elementType = getElementType(adapter, genericType); actualElementType = elementType; } else { publisher = Mono.justOrEmpty(body); actualElementType = body != null ? ResolvableType.forInstance(body) : bodyType; elementType = (bodyType.toClass() == Object.class && body != null ? actualElementType : bodyType); } if (elementType.resolve() == void.class || elementType.resolve() == Void.class) { return Mono.from((Publisher<Void>) publisher); } MediaType bestMediaType = selectMediaType(exchange, () -> getMediaTypesFor(elementType)); if (bestMediaType != null) { String logPrefix = exchange.getLogPrefix(); if (logger.isDebugEnabled()) { logger.debug(logPrefix + (publisher instanceof Mono ? "0..1" : "0..N") + " [" + elementType + "]"); } for (HttpMessageWriter<?> writer : getMessageWriters()) { if (writer.canWrite(actualElementType, bestMediaType)) { return writer.write((Publisher) publisher, actualType, elementType, bestMediaType, exchange.getRequest(), exchange.getResponse(), Hints.from(Hints.LOG_PREFIX_HINT, logPrefix)); } } } List<MediaType> mediaTypes = getMediaTypesFor(elementType); if (bestMediaType == null && mediaTypes.isEmpty()) { return Mono.error(new IllegalStateException("No HttpMessageWriter for " + elementType)); } return Mono.error(new NotAcceptableStatusException(mediaTypes)); }
Example 19
Source File: ByteBufferEncoder.java From java-technology-stack with MIT License | 4 votes |
@Override public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) { Class<?> clazz = elementType.toClass(); return super.canEncode(elementType, mimeType) && ByteBuffer.class.isAssignableFrom(clazz); }
Example 20
Source File: TypeDescriptor.java From java-technology-stack with MIT License | 2 votes |
/** * Create a new type descriptor from a {@link ResolvableType}. * <p>This constructor is used internally and may also be used by subclasses * that support non-Java languages with extended type systems. It is public * as of 5.1.4 whereas it was protected before. * @param resolvableType the resolvable type * @param type the backing type (or {@code null} if it should get resolved) * @param annotations the type annotations * @since 4.0 */ public TypeDescriptor(ResolvableType resolvableType, @Nullable Class<?> type, @Nullable Annotation[] annotations) { this.resolvableType = resolvableType; this.type = (type != null ? type : resolvableType.toClass()); this.annotatedElement = new AnnotatedElementAdapter(annotations); }