congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
ConversionService
Code IndexAdd Tabnine to your IDE (free)

How to use
ConversionService
in
org.springframework.core.convert

Best Java code snippets using org.springframework.core.convert.ConversionService (Showing top 20 results out of 1,260)

Refine searchRefine arrow

  • Nullable
  • TypeDescriptor
origin: spring-projects/spring-framework

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (source == null) {
    return null;
  }
  TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
  Assert.state(targetElementType != null, "No target element type");
  Object target = Array.newInstance(targetElementType.getType(), 1);
  Object targetElement = this.conversionService.convert(source, sourceType, targetElementType);
  Array.set(target, 0, targetElement);
  return target;
}
origin: spring-projects/spring-framework

@Override
@Nullable
public Object fromMessage(Message<?> message, Class<?> targetClass) {
  Object payload = message.getPayload();
  if (this.conversionService.canConvert(payload.getClass(), targetClass)) {
    try {
      return this.conversionService.convert(payload, targetClass);
    }
    catch (ConversionException ex) {
      throw new MessageConversionException(message, "Failed to convert message payload '" +
          payload + "' to '" + targetClass.getName() + "'", ex);
    }
  }
  return (ClassUtils.isAssignableValue(targetClass, payload) ? payload : null);
}
origin: spring-projects/spring-framework

  @Override
  @Nullable
  public T convert(S source) {
    return this.conversionService.convert(source, this.targetType);
  }
}
origin: spring-projects/spring-framework

@Override
@Nullable
public String getAsText() {
  if (this.canConvertToString) {
    return (String) this.conversionService.convert(getValue(), this.targetDescriptor, TypeDescriptor.valueOf(String.class));
  }
  else {
    return null;
  }
}
origin: spring-projects/spring-framework

private Object convertToStream(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor streamType) {
  TypeDescriptor targetCollection = TypeDescriptor.collection(List.class, streamType.getElementTypeDescriptor());
  List<?> target = (List<?>) this.conversionService.convert(source, sourceType, targetCollection);
  if (target == null) {
    target = Collections.emptyList();
  }
  return target.stream();
}
origin: spring-projects/spring-framework

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (source == null) {
    return null;
  }
  if (sourceType.isAssignableTo(targetType)) {
    return source;
  }
  if (Array.getLength(source) == 0) {
    return null;
  }
  Object firstElement = Array.get(source, 0);
  return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType);
}
origin: spring-projects/spring-framework

/**
 * Formats the field value based on registered PropertyEditors.
 * @see #getCustomEditor
 */
@Override
protected Object formatFieldValue(String field, @Nullable Object value) {
  String fixedField = fixedField(field);
  // Try custom editor...
  PropertyEditor customEditor = getCustomEditor(fixedField);
  if (customEditor != null) {
    customEditor.setValue(value);
    String textValue = customEditor.getAsText();
    // If the PropertyEditor returned null, there is no appropriate
    // text representation for this value: only use it if non-null.
    if (textValue != null) {
      return textValue;
    }
  }
  if (this.conversionService != null) {
    // Try custom converter...
    TypeDescriptor fieldDesc = getPropertyAccessor().getPropertyTypeDescriptor(fixedField);
    TypeDescriptor strDesc = TypeDescriptor.valueOf(String.class);
    if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, strDesc)) {
      return this.conversionService.convert(value, fieldDesc, strDesc);
    }
  }
  return value;
}
origin: spring-projects/spring-framework

@Nullable
private Object convertFromByteBuffer(ByteBuffer source, TypeDescriptor targetType) {
  byte[] bytes = new byte[source.remaining()];
  source.get(bytes);
  if (targetType.isAssignableTo(BYTE_ARRAY_TYPE)) {
    return bytes;
  }
  return this.conversionService.convert(bytes, BYTE_ARRAY_TYPE, targetType);
}
origin: spring-projects/spring-framework

@Override
public boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
  return this.conversionService.canConvert(sourceType, targetType);
}
origin: spring-projects/spring-framework

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (source == null) {
    return null;
  }
  Method finder = getFinder(targetType.getType());
  Assert.state(finder != null, "No finder method");
  Object id = this.conversionService.convert(
      source, sourceType, TypeDescriptor.valueOf(finder.getParameterTypes()[0]));
  return ReflectionUtils.invokeMethod(finder, source, id);
}
origin: spring-projects/spring-framework

/**
 * Validate that a {@link Collection} of the elements held within the stream can be
 * converted to the specified {@code targetType}.
 * @param elementType the type of the stream elements
 * @param targetType the type to convert to
 */
public boolean matchesFromStream(@Nullable TypeDescriptor elementType, TypeDescriptor targetType) {
  TypeDescriptor collectionOfElement = TypeDescriptor.collection(Collection.class, elementType);
  return this.conversionService.canConvert(collectionOfElement, targetType);
}
origin: spring-projects/spring-framework

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  String text = (String) source;
  if (!StringUtils.hasText(text)) {
    return null;
  }
  Object result;
  try {
    result = this.parser.parse(text, LocaleContextHolder.getLocale());
  }
  catch (IllegalArgumentException ex) {
    throw ex;
  }
  catch (Throwable ex) {
    throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex);
  }
  TypeDescriptor resultType = TypeDescriptor.valueOf(result.getClass());
  if (!resultType.isAssignableTo(targetType)) {
    result = this.conversionService.convert(result, resultType, targetType);
  }
  return result;
}
origin: spring-projects/spring-framework

/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered
 * {@link Converter} that can perform the conversion.
 * @param sourceValue the source value to create the model attribute from
 * @param attributeName the name of the attribute (never {@code null})
 * @param parameter the method parameter
 * @param binderFactory for creating WebDataBinder instance
 * @param request the current request
 * @return the created model attribute, or {@code null} if no suitable
 * conversion found
 */
@Nullable
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName,
    MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request)
    throws Exception {
  DataBinder binder = binderFactory.createBinder(request, null, attributeName);
  ConversionService conversionService = binder.getConversionService();
  if (conversionService != null) {
    TypeDescriptor source = TypeDescriptor.valueOf(String.class);
    TypeDescriptor target = new TypeDescriptor(parameter);
    if (conversionService.canConvert(source, target)) {
      return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
    }
  }
  return null;
}
origin: spring-projects/spring-framework

/**
 * This implementation exposes a PropertyEditor adapter for a Formatter,
 * if applicable.
 */
@Override
@Nullable
public PropertyEditor findEditor(@Nullable String field, @Nullable Class<?> valueType) {
  Class<?> valueTypeForLookup = valueType;
  if (valueTypeForLookup == null) {
    valueTypeForLookup = getFieldType(field);
  }
  PropertyEditor editor = super.findEditor(field, valueTypeForLookup);
  if (editor == null && this.conversionService != null) {
    TypeDescriptor td = null;
    if (field != null && getTarget() != null) {
      TypeDescriptor ptd = getPropertyAccessor().getPropertyTypeDescriptor(fixedField(field));
      if (ptd != null && (valueType == null || valueType.isAssignableFrom(ptd.getType()))) {
        td = ptd;
      }
    }
    if (td == null) {
      td = TypeDescriptor.valueOf(valueTypeForLookup);
    }
    if (this.conversionService.canConvert(TypeDescriptor.valueOf(String.class), td)) {
      editor = new ConvertingPropertyEditorAdapter(this.conversionService, td);
    }
  }
  return editor;
}
origin: spring-projects/spring-framework

public static boolean canConvertElements(@Nullable TypeDescriptor sourceElementType,
    @Nullable TypeDescriptor targetElementType, ConversionService conversionService) {
  if (targetElementType == null) {
    // yes
    return true;
  }
  if (sourceElementType == null) {
    // maybe
    return true;
  }
  if (conversionService.canConvert(sourceElementType, targetElementType)) {
    // yes
    return true;
  }
  if (sourceElementType.getType().isAssignableFrom(targetElementType.getType())) {
    // maybe
    return true;
  }
  // no
  return false;
}
origin: spring-projects/spring-framework

@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
  NamedValueInfo namedValueInfo = getNamedValueInfo(parameter);
  MethodParameter nestedParameter = parameter.nestedIfOptional();
  Object resolvedName = resolveStringValue(namedValueInfo.name);
  if (resolvedName == null) {
    throw new IllegalArgumentException(
        "Specified name must not resolve to null: [" + namedValueInfo.name + "]");
  }
  Object arg = resolveArgumentInternal(nestedParameter, message, resolvedName.toString());
  if (arg == null) {
    if (namedValueInfo.defaultValue != null) {
      arg = resolveStringValue(namedValueInfo.defaultValue);
    }
    else if (namedValueInfo.required && !nestedParameter.isOptional()) {
      handleMissingValue(namedValueInfo.name, nestedParameter, message);
    }
    arg = handleNullValue(namedValueInfo.name, arg, nestedParameter.getNestedParameterType());
  }
  else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
    arg = resolveStringValue(namedValueInfo.defaultValue);
  }
  if (parameter != nestedParameter || !ClassUtils.isAssignableValue(parameter.getParameterType(), arg)) {
    arg = this.conversionService.convert(arg, TypeDescriptor.forObject(arg), new TypeDescriptor(parameter));
  }
  handleResolvedValue(arg, namedValueInfo.name, parameter, message);
  return arg;
}
origin: spring-projects/spring-framework

@Nullable
private Object convertValue(Object sourceValue, TypeDescriptor sourceType, @Nullable TypeDescriptor targetType) {
  if (targetType == null) {
    return sourceValue;
  }
  return this.conversionService.convert(sourceValue, sourceType.getMapValueTypeDescriptor(sourceValue), targetType);
}
origin: spring-projects/spring-framework

@Nullable
private Object convertKey(Object sourceKey, TypeDescriptor sourceType, @Nullable TypeDescriptor targetType) {
  if (targetType == null) {
    return sourceKey;
  }
  return this.conversionService.convert(sourceKey, sourceType.getMapKeyTypeDescriptor(sourceKey), targetType);
}
origin: spring-projects/spring-framework

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (source == null) {
    return null;
  }
  Collection<?> sourceCollection = (Collection<?>) source;
  if (sourceCollection.isEmpty()) {
    return "";
  }
  StringBuilder sb = new StringBuilder();
  int i = 0;
  for (Object sourceElement : sourceCollection) {
    if (i > 0) {
      sb.append(DELIMITER);
    }
    Object targetElement = this.conversionService.convert(
        sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetType);
    sb.append(targetElement);
    i++;
  }
  return sb.toString();
}
origin: spring-projects/spring-framework

@Override
@Nullable
public Object convertValue(@Nullable Object value, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
  try {
    return this.conversionService.convert(value, sourceType, targetType);
  }
  catch (ConversionException ex) {
    throw new SpelEvaluationException(ex, SpelMessage.TYPE_CONVERSION_ERROR,
        (sourceType != null ? sourceType.toString() : (value != null ? value.getClass().getName() : "null")),
        targetType.toString());
  }
}
org.springframework.core.convertConversionService

Javadoc

A service interface for type conversion. This is the entry point into the convert system. Call #convert(Object,Class) to perform a thread-safe type conversion using this system.

Most used methods

  • convert
    Convert the given source to the specified targetType. The TypeDescriptors provide additional context
  • canConvert
    Return true if objects of sourceType can be converted to the targetType. The TypeDescriptors provide

Popular in Java

  • Updating database using SQL prepared statement
  • getResourceAsStream (ClassLoader)
  • getSharedPreferences (Context)
  • notifyDataSetChanged (ArrayAdapter)
  • Pointer (com.sun.jna)
    An abstraction for a native pointer data type. A Pointer instance represents, on the Java side, a na
  • OutputStream (java.io)
    A writable sink for bytes.Most clients will use output streams that write data to the file system (
  • BigInteger (java.math)
    An immutable arbitrary-precision signed integer.FAST CRYPTOGRAPHY This implementation is efficient f
  • Hashtable (java.util)
    A plug-in replacement for JDK1.5 java.util.Hashtable. This version is based on org.cliffc.high_scale
  • StringTokenizer (java.util)
    Breaks a string into tokens; new code should probably use String#split.> // Legacy code: StringTo
  • ReentrantLock (java.util.concurrent.locks)
    A reentrant mutual exclusion Lock with the same basic behavior and semantics as the implicit monitor
  • Top Sublime Text plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now