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

How to use
valueOf
method
in
org.springframework.core.convert.TypeDescriptor

Best Java code snippets using org.springframework.core.convert.TypeDescriptor.valueOf (Showing top 20 results out of 720)

origin: spring-projects/spring-framework

@Override
public boolean canConvert(@Nullable Class<?> sourceType, Class<?> targetType) {
  Assert.notNull(targetType, "Target type to convert to cannot be null");
  return canConvert((sourceType != null ? TypeDescriptor.valueOf(sourceType) : null),
      TypeDescriptor.valueOf(targetType));
}
origin: spring-projects/spring-framework

/**
 * Returns the websocket message type. By default the type is resolved using
 * the generic arguments of the class.
 */
protected TypeDescriptor getMessageType() {
  return TypeDescriptor.valueOf(resolveTypeArguments()[1]);
}
origin: spring-projects/spring-framework

@Override
@Nullable
public <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType, @Nullable Field field)
    throws TypeMismatchException {
  return convertIfNecessary(value, requiredType,
      (field != null ? new TypeDescriptor(field) : TypeDescriptor.valueOf(requiredType)));
}
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

@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
  Method finder = getFinder(targetType.getType());
  return (finder != null &&
      this.conversionService.canConvert(sourceType, TypeDescriptor.valueOf(finder.getParameterTypes()[0])));
}
origin: spring-projects/spring-framework

@Test
public void dateToStringWithoutGlobalFormat() {
  Date date = new Date();
  Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.class), TypeDescriptor.valueOf(String.class));
  String expected = date.toString();
  assertEquals(expected, actual);
}
origin: spring-projects/spring-framework

@Test
public void elementTypeForCollectionSubclass() throws Exception {
  @SuppressWarnings("serial")
  class CustomSet extends HashSet<String> {
  }
  assertEquals(TypeDescriptor.valueOf(CustomSet.class).getElementTypeDescriptor(), TypeDescriptor.valueOf(String.class));
  assertEquals(TypeDescriptor.forObject(new CustomSet()).getElementTypeDescriptor(), TypeDescriptor.valueOf(String.class));
}
origin: spring-projects/spring-framework

@Test
public void convertArrayToCollectionGenericTypeConversion() throws Exception {
  @SuppressWarnings("unchecked")
  List<Integer> result = (List<Integer>) conversionService.convert(new String[] {"1", "2", "3"}, TypeDescriptor
      .valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList")));
  assertEquals(Integer.valueOf(1), result.get(0));
  assertEquals(Integer.valueOf(2), result.get(1));
  assertEquals(Integer.valueOf(3), result.get(2));
}
origin: spring-projects/spring-framework

@Test(expected = ConversionFailedException.class)
public void parseParserReturnsNull() throws ParseException {
  formattingService.addFormatterForFieldType(Integer.class, new NullReturningFormatter());
  assertNull(formattingService.convert("1", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
}
origin: spring-projects/spring-framework

@Test
public void convertCollectionToCollectionNull() throws Exception {
  @SuppressWarnings("unchecked")
  List<Integer> bar = (List<Integer>) conversionService.convert(null,
      TypeDescriptor.valueOf(LinkedHashSet.class), new TypeDescriptor(getClass().getField("genericList")));
  assertNull(bar);
}
origin: spring-projects/spring-framework

@Test
public void parseEmptyString() throws ParseException {
  formattingService.addFormatterForFieldType(Number.class, new NumberStyleFormatter());
  assertNull(formattingService.convert("", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
}
origin: spring-projects/spring-framework

@Test
public void parseBlankString() throws ParseException {
  formattingService.addFormatterForFieldType(Number.class, new NumberStyleFormatter());
  assertNull(formattingService.convert("     ", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
}
origin: spring-projects/spring-framework

@Test
public void narrow() {
  TypeDescriptor desc = TypeDescriptor.valueOf(Number.class);
  Integer value = Integer.valueOf(3);
  desc = desc.narrow(value);
  assertEquals(Integer.class, desc.getType());
}
origin: spring-projects/spring-framework

@Test
public void testCollectionsEmptyList() throws Exception {
  CollectionToCollectionConverter converter = new CollectionToCollectionConverter(new GenericConversionService());
  TypeDescriptor type = new TypeDescriptor(getClass().getField("list"));
  converter.convert(list, type, TypeDescriptor.valueOf(Class.forName("java.util.Collections$EmptyList")));
}
origin: spring-projects/spring-framework

@Test
public void dateToStringWithFormat() {
  JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
  registrar.setDateTimeFormatter(org.joda.time.format.DateTimeFormat.shortDateTime());
  setup(registrar);
  Date date = new Date();
  Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.class), TypeDescriptor.valueOf(String.class));
  String expected = JodaTimeContextHolder.getFormatter(org.joda.time.format.DateTimeFormat.shortDateTime(), Locale.US).print(new DateTime(date));
  assertEquals(expected, actual);
}
origin: spring-projects/spring-framework

@Test
@SuppressWarnings("unchecked")
public void convertObjectToOptional() {
  Method method = ClassUtils.getMethod(TestEntity.class, "handleOptionalValue", Optional.class);
  MethodParameter parameter = new MethodParameter(method, 0);
  TypeDescriptor descriptor = new TypeDescriptor(parameter);
  Object actual = conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), descriptor);
  assertEquals(Optional.class, actual.getClass());
  assertEquals(Arrays.asList(1, 2, 3), ((Optional<List<Integer>>) actual).get());
}
origin: spring-projects/spring-framework

@Test
public void dateToStringWithGlobalFormat() {
  DateFormatterRegistrar registrar = new DateFormatterRegistrar();
  registrar.setFormatter(new DateFormatter());
  setup(registrar);
  Date date = new Date();
  Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.class), TypeDescriptor.valueOf(String.class));
  String expected = new DateFormatter().print(date, Locale.US);
  assertEquals(expected, actual);
}
origin: spring-projects/spring-framework

@Test
public void stringToCollectionCanConvert() throws Exception {
  conversionService.addConverter(new StringToCollectionConverter(conversionService));
  assertTrue(conversionService.canConvert(String.class, Collection.class));
  TypeDescriptor targetType = new TypeDescriptor(getClass().getField("integerCollection"));
  assertFalse(conversionService.canConvert(TypeDescriptor.valueOf(String.class), targetType));
  conversionService.addConverterFactory(new StringToNumberConverterFactory());
  assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), targetType));
}
origin: spring-projects/spring-framework

@Test
public void emptyListToObject() {
  conversionService.addConverter(new CollectionToObjectConverter(conversionService));
  conversionService.addConverterFactory(new StringToNumberConverterFactory());
  List<String> list = new ArrayList<>();
  TypeDescriptor sourceType = TypeDescriptor.forObject(list);
  TypeDescriptor targetType = TypeDescriptor.valueOf(Integer.class);
  assertTrue(conversionService.canConvert(sourceType, targetType));
  assertNull(conversionService.convert(list, sourceType, targetType));
}
origin: spring-projects/spring-framework

@Test
public void valueOfArray() throws Exception {
  TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(int[].class);
  assertTrue(typeDescriptor.isArray());
  assertFalse(typeDescriptor.isCollection());
  assertFalse(typeDescriptor.isMap());
  assertEquals(Integer.TYPE, typeDescriptor.getElementTypeDescriptor().getType());
}
org.springframework.core.convertTypeDescriptorvalueOf

Javadoc

Create a new type descriptor from the given type.

Use this to instruct the conversion system to convert an object to a specific target type, when no type location such as a method parameter or field is available to provide additional conversion context.

Generally prefer use of #forObject(Object) for constructing type descriptors from source objects, as it handles the null object case.

Popular methods of TypeDescriptor

  • getType
    The type of the backing class, method parameter, field, or property described by this TypeDescriptor
  • <init>
    Create a new type descriptor from a Property.Use this constructor when a source or target conversion
  • forObject
    Create a new type descriptor for an object.Use this factory method to introspect a source object bef
  • getObjectType
    Variation of #getType() that accounts for a primitive type by returning its object wrapper type.This
  • getElementTypeDescriptor
    If this type is an array, returns the array's component type. If this type is a Stream, returns the
  • isAssignableTo
    Returns true if an object of this type descriptor can be assigned to the location described by the g
  • isArray
    Is this type an array type?
  • isCollection
    Is this type a Collection type?
  • nested
  • getMapValueTypeDescriptor
    If this type is a Map, creates a mapValue TypeDescriptorfrom the provided map value.Narrows the #get
  • getMapKeyTypeDescriptor
    If this type is a Map, creates a mapKey TypeDescriptorfrom the provided map key. Narrows the #getMap
  • elementTypeDescriptor
    If this type is a Collection or an array, creates a element TypeDescriptor from the provided collect
  • getMapKeyTypeDescriptor,
  • elementTypeDescriptor,
  • getAnnotations,
  • isMap,
  • equals,
  • getAnnotation,
  • isPrimitive,
  • narrow,
  • toString

Popular in Java

  • Reading from database using SQL prepared statement
  • getSupportFragmentManager (FragmentActivity)
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • startActivity (Activity)
  • SecureRandom (java.security)
    This class generates cryptographically secure pseudo-random numbers. It is best to invoke SecureRand
  • ResultSet (java.sql)
    An interface for an object which represents a database table entry, returned as the result of the qu
  • BitSet (java.util)
    The BitSet class implements abit array [http://en.wikipedia.org/wiki/Bit_array]. Each element is eit
  • NoSuchElementException (java.util)
    Thrown when trying to retrieve an element past the end of an Enumeration or Iterator.
  • SortedMap (java.util)
    A map that has its keys ordered. The sorting is according to either the natural ordering of its keys
  • Runner (org.openjdk.jmh.runner)
  • Top 12 Jupyter Notebook extensions
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