List of usage examples for org.springframework.core KotlinDetector isKotlinType
public static boolean isKotlinType(Class<?> clazz)
From source file:org.springframework.beans.BeanUtils.java
/** * Instantiate a class using its 'primary' constructor (for Kotlin classes, * potentially having default arguments declared) or its default constructor * (for regular Java classes, expecting a standard no-arg setup). * <p>Note that this method tries to set the constructor accessible * if given a non-accessible (that is, non-public) constructor. * @param clazz the class to instantiate * @return the new instance/* w ww. java 2 s. c o m*/ * @throws BeanInstantiationException if the bean cannot be instantiated. * The cause may notably indicate a {@link NoSuchMethodException} if no * primary/default constructor was found, a {@link NoClassDefFoundError} * or other {@link LinkageError} in case of an unresolvable class definition * (e.g. due to a missing dependency at runtime), or an exception thrown * from the constructor invocation itself. * @see Constructor#newInstance */ public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException { Assert.notNull(clazz, "Class must not be null"); if (clazz.isInterface()) { throw new BeanInstantiationException(clazz, "Specified class is an interface"); } try { Constructor<T> ctor = (KotlinDetector.isKotlinType(clazz) ? KotlinDelegate.getPrimaryConstructor(clazz) : clazz.getDeclaredConstructor()); return instantiateClass(ctor); } catch (NoSuchMethodException ex) { throw new BeanInstantiationException(clazz, "No default constructor found", ex); } catch (LinkageError err) { throw new BeanInstantiationException(clazz, "Unresolvable class definition", err); } }
From source file:org.springframework.beans.BeanUtils.java
/** * Convenience method to instantiate a class using the given constructor. * <p>Note that this method tries to set the constructor accessible if given a * non-accessible (that is, non-public) constructor, and supports Kotlin classes * with optional parameters and default values. * @param ctor the constructor to instantiate * @param args the constructor arguments to apply (use {@code null} for an unspecified * parameter if needed for Kotlin classes with optional parameters and default values) * @return the new instance//from ww w. ja v a2 s. c om * @throws BeanInstantiationException if the bean cannot be instantiated * @see Constructor#newInstance */ public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException { Assert.notNull(ctor, "Constructor must not be null"); try { ReflectionUtils.makeAccessible(ctor); return (KotlinDetector.isKotlinType(ctor.getDeclaringClass()) ? KotlinDelegate.instantiateClass(ctor, args) : ctor.newInstance(args)); } catch (InstantiationException ex) { throw new BeanInstantiationException(ctor, "Is it an abstract class?", ex); } catch (IllegalAccessException ex) { throw new BeanInstantiationException(ctor, "Is the constructor accessible?", ex); } catch (IllegalArgumentException ex) { throw new BeanInstantiationException(ctor, "Illegal arguments for constructor", ex); } catch (InvocationTargetException ex) { throw new BeanInstantiationException(ctor, "Constructor threw exception", ex.getTargetException()); } }
From source file:org.springframework.beans.BeanUtils.java
/** * Return the primary constructor of the provided class. For Kotlin classes, this * returns the Java constructor corresponding to the Kotlin primary constructor * (as defined in the Kotlin specification). Otherwise, in particular for non-Kotlin * classes, this simply returns {@code null}. * @param clazz the class to check//from w w w. j av a 2 s.c om * @since 5.0 * @see <a href="http://kotlinlang.org/docs/reference/classes.html#constructors">Kotlin docs</a> */ @SuppressWarnings("unchecked") @Nullable public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) { Assert.notNull(clazz, "Class must not be null"); if (KotlinDetector.isKotlinType(clazz)) { Constructor<T> kotlinPrimaryConstructor = KotlinDelegate.findPrimaryConstructor(clazz); if (kotlinPrimaryConstructor != null) { return kotlinPrimaryConstructor; } } return null; }
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 w w .j av a 2s . com*/ 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)); }