org.springframework.core.convert.TypeDescriptor Java Examples
The following examples show how to use
org.springframework.core.convert.TypeDescriptor.
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: GenericConversionServiceTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void multipleCollectionTypesFromSameSourceType() throws Exception { conversionService.addConverter(new MyStringToRawCollectionConverter()); conversionService.addConverter(new MyStringToGenericCollectionConverter()); conversionService.addConverter(new MyStringToStringCollectionConverter()); conversionService.addConverter(new MyStringToIntegerCollectionConverter()); assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection")))); assertEquals(Collections.singleton(4), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection")))); assertEquals(Collections.singleton(4), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection")))); assertEquals(Collections.singleton(4), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection")))); assertEquals(Collections.singleton(4), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection")))); assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection")))); }
Example #2
Source File: MapToMapConverterTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void collectionMap() throws Exception { Map<String, List<String>> map = new HashMap<>(); map.put("1", Arrays.asList("9", "12")); map.put("2", Arrays.asList("37", "23")); TypeDescriptor sourceType = TypeDescriptor.forObject(map); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget")); assertTrue(conversionService.canConvert(sourceType, targetType)); try { conversionService.convert(map, sourceType, targetType); } catch (ConversionFailedException ex) { assertTrue(ex.getCause() instanceof ConverterNotFoundException); } conversionService.addConverter(new CollectionToCollectionConverter(conversionService)); conversionService.addConverterFactory(new StringToNumberConverterFactory()); assertTrue(conversionService.canConvert(sourceType, targetType)); @SuppressWarnings("unchecked") Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType); assertFalse(map.equals(result)); assertEquals(Arrays.asList(9, 12), result.get(1)); assertEquals(Arrays.asList(37, 23), result.get(2)); }
Example #3
Source File: CollectionToCollectionConverterTests.java From spring-analysis-note with MIT License | 6 votes |
@Test @SuppressWarnings("unchecked") public void stringToCollection() throws Exception { List<List<String>> list = new ArrayList<>(); list.add(Arrays.asList("9,12")); list.add(Arrays.asList("37,23")); conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverter(new StringToCollectionConverter(conversionService)); conversionService.addConverter(new ObjectToCollectionConverter(conversionService)); conversionService.addConverter(new CollectionToObjectConverter(conversionService)); TypeDescriptor sourceType = TypeDescriptor.forObject(list); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("objectToCollection")); assertTrue(conversionService.canConvert(sourceType, targetType)); List<List<List<Integer>>> result = (List<List<List<Integer>>>) conversionService.convert(list, sourceType, targetType); assertEquals((Integer) 9, result.get(0).get(0).get(0)); assertEquals((Integer) 12, result.get(0).get(0).get(1)); assertEquals((Integer) 37, result.get(1).get(0).get(0)); assertEquals((Integer) 23, result.get(1).get(0).get(1)); }
Example #4
Source File: FormModelMethodArgumentResolver.java From es with Apache License 2.0 | 6 votes |
/** * 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 org.springframework.core.convert.converter.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} * @throws Exception */ 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; }
Example #5
Source File: EnumToStringConverter.java From blade-tool with GNU Lesser General Public License v3.0 | 6 votes |
@Override public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } Class<?> sourceClazz = sourceType.getType(); AccessibleObject accessibleObject = ENUM_CACHE_MAP.computeIfAbsent(sourceClazz, EnumToStringConverter::getAnnotation); Class<?> targetClazz = targetType.getType(); // 如果为null,走默认的转换 if (accessibleObject == null) { if (String.class == targetClazz) { return ((Enum) source).name(); } int ordinal = ((Enum) source).ordinal(); return ConvertUtil.convert(ordinal, targetClazz); } try { return EnumToStringConverter.invoke(sourceClazz, accessibleObject, source, targetClazz); } catch (Exception e) { log.error(e.getMessage(), e); } return null; }
Example #6
Source File: ReflectivePropertyAccessor.java From java-technology-stack with MIT License | 6 votes |
@Nullable private TypeDescriptor getTypeDescriptor(EvaluationContext context, Object target, String name) { Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass()); if (type.isArray() && name.equals("length")) { return TypeDescriptor.valueOf(Integer.TYPE); } PropertyCacheKey cacheKey = new PropertyCacheKey(type, name, target instanceof Class); TypeDescriptor typeDescriptor = this.typeDescriptorCache.get(cacheKey); if (typeDescriptor == null) { // Attempt to populate the cache entry try { if (canRead(context, target, name) || canWrite(context, target, name)) { typeDescriptor = this.typeDescriptorCache.get(cacheKey); } } catch (AccessException ex) { // Continue with null type descriptor } } return typeDescriptor; }
Example #7
Source File: ScenariosForSpringSecurity.java From java-technology-stack with MIT License | 6 votes |
@Override public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException { try { Method m = HasRoleExecutor.class.getMethod("hasRole", String[].class); Object[] args = arguments; if (args != null) { ReflectionHelper.convertAllArguments(tc, args, m); } if (m.isVarArgs()) { args = ReflectionHelper.setupArgumentsForVarargsInvocation(m.getParameterTypes(), args); } return new TypedValue(m.invoke(null, args), new TypeDescriptor(new MethodParameter(m,-1))); } catch (Exception ex) { throw new AccessException("Problem invoking hasRole", ex); } }
Example #8
Source File: MapToMapConverterTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void collectionMapSourceTarget() throws Exception { Map<String, List<String>> map = new HashMap<>(); map.put("1", Arrays.asList("9", "12")); map.put("2", Arrays.asList("37", "23")); TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("sourceCollectionMapTarget")); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget")); assertFalse(conversionService.canConvert(sourceType, targetType)); try { conversionService.convert(map, sourceType, targetType); fail("Should have failed"); } catch (ConverterNotFoundException ex) { // expected } conversionService.addConverter(new CollectionToCollectionConverter(conversionService)); conversionService.addConverterFactory(new StringToNumberConverterFactory()); assertTrue(conversionService.canConvert(sourceType, targetType)); @SuppressWarnings("unchecked") Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType); assertFalse(map.equals(result)); assertEquals(Arrays.asList(9, 12), result.get(1)); assertEquals(Arrays.asList(37, 23), result.get(2)); }
Example #9
Source File: FormattingConversionServiceFactoryBeanTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testCustomFormatter() throws Exception { FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); Set<Object> formatters = new HashSet<>(); formatters.add(new TestBeanFormatter()); formatters.add(new SpecialIntAnnotationFormatterFactory()); factory.setFormatters(formatters); factory.afterPropertiesSet(); FormattingConversionService fcs = factory.getObject(); TestBean testBean = fcs.convert("5", TestBean.class); assertEquals(5, testBean.getSpecialInt()); assertEquals("5", fcs.convert(testBean, String.class)); TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("specialInt")); Object value = fcs.convert(":5", TypeDescriptor.valueOf(String.class), descriptor); assertEquals(5, value); value = fcs.convert(5, descriptor, TypeDescriptor.valueOf(String.class)); assertEquals(":5", value); }
Example #10
Source File: ObjectToOptionalConverter.java From spring-analysis-note with MIT License | 6 votes |
@Override public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return Optional.empty(); } else if (source instanceof Optional) { return source; } else if (targetType.getResolvableType().hasGenerics()) { Object target = this.conversionService.convert(source, sourceType, new GenericTypeDescriptor(targetType)); if (target == null || (target.getClass().isArray() && Array.getLength(target) == 0) || (target instanceof Collection && ((Collection<?>) target).isEmpty())) { return Optional.empty(); } return Optional.of(target); } else { return Optional.of(source); } }
Example #11
Source File: ReflectiveMethodExecutor.java From lams with GNU General Public License v2.0 | 6 votes |
@Override public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException { try { if (arguments != null) { this.argumentConversionOccurred = ReflectionHelper.convertArguments( context.getTypeConverter(), arguments, this.method, this.varargsPosition); if (this.method.isVarArgs()) { arguments = ReflectionHelper.setupArgumentsForVarargsInvocation( this.method.getParameterTypes(), arguments); } } ReflectionUtils.makeAccessible(this.method); Object value = this.method.invoke(target, arguments); return new TypedValue(value, new TypeDescriptor(new MethodParameter(this.method, -1)).narrow(value)); } catch (Exception ex) { throw new AccessException("Problem invoking method: " + this.method, ex); } }
Example #12
Source File: DelimitedStringToCollectionConverter.java From spring-cloud-gray with Apache License 2.0 | 6 votes |
private Object convert(String source, TypeDescriptor sourceType, TypeDescriptor targetType) { Delimiter delimiter = targetType.getAnnotation(Delimiter.class); String[] elements = getElements(source, (delimiter != null) ? delimiter.value() : ","); TypeDescriptor elementDescriptor = targetType.getElementTypeDescriptor(); Collection<Object> target = createCollection(targetType, elementDescriptor, elements.length); Stream<Object> stream = Arrays.stream(elements).map(String::trim); if (elementDescriptor != null) { stream = stream.map((element) -> this.conversionService.convert(element, sourceType, elementDescriptor)); } stream.forEach(target::add); return target; }
Example #13
Source File: GenericConversionService.java From spring-analysis-note with MIT License | 6 votes |
/** * Hook method to lookup the converter for a given sourceType/targetType pair. * First queries this ConversionService's converter cache. * On a cache miss, then performs an exhaustive search for a matching converter. * If no converter matches, returns the default converter. * @param sourceType the source type to convert from * @param targetType the target type to convert to * @return the generic converter that will perform the conversion, * or {@code null} if no suitable converter was found * @see #getDefaultConverter(TypeDescriptor, TypeDescriptor) */ @Nullable protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) { ConverterCacheKey key = new ConverterCacheKey(sourceType, targetType); GenericConverter converter = this.converterCache.get(key); if (converter != null) { return (converter != NO_MATCH ? converter : null); } converter = this.converters.find(sourceType, targetType); if (converter == null) { converter = getDefaultConverter(sourceType, targetType); } if (converter != null) { this.converterCache.put(key, converter); return converter; } this.converterCache.put(key, NO_MATCH); return null; }
Example #14
Source File: GenericConversionService.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) { Assert.notNull(targetType, "targetType to convert to cannot be null"); if (sourceType == null) { return true; } GenericConverter converter = getConverter(sourceType, targetType); return (converter != null); }
Example #15
Source File: CollectionToArrayConverter.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } Collection<?> sourceCollection = (Collection<?>) source; Object array = Array.newInstance(targetType.getElementTypeDescriptor().getType(), sourceCollection.size()); int i = 0; for (Object sourceElement : sourceCollection) { Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetType.getElementTypeDescriptor()); Array.set(array, i++, targetElement); } return array; }
Example #16
Source File: CollectionToCollectionConverterTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void emptyListToListDifferentTargetType() throws Exception { conversionService.addConverter(new CollectionToCollectionConverter(conversionService)); conversionService.addConverterFactory(new StringToNumberConverterFactory()); List<String> list = new ArrayList<>(); TypeDescriptor sourceType = TypeDescriptor.forObject(list); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyListDifferentTarget")); assertTrue(conversionService.canConvert(sourceType, targetType)); @SuppressWarnings("unchecked") LinkedList<Integer> result = (LinkedList<Integer>) conversionService.convert(list, sourceType, targetType); assertEquals(LinkedList.class, result.getClass()); assertTrue(result.isEmpty()); }
Example #17
Source File: PathVariableMethodArgumentResolver.java From lams with GNU General Public License v2.0 | 5 votes |
protected String formatUriValue(ConversionService cs, TypeDescriptor sourceType, Object value) { if (value == null) { return null; } else if (value instanceof String) { return (String) value; } else if (cs != null) { return (String) cs.convert(value, sourceType, STRING_TYPE_DESCRIPTOR); } else { return value.toString(); } }
Example #18
Source File: RequestParamMethodArgumentResolver.java From lams with GNU General Public License v2.0 | 5 votes |
protected String formatUriValue(ConversionService cs, TypeDescriptor sourceType, Object value) { if (value == null) { return null; } else if (value instanceof String) { return (String) value; } else if (cs != null) { return (String) cs.convert(value, sourceType, STRING_TYPE_DESCRIPTOR); } else { return value.toString(); } }
Example #19
Source File: SpelExpression.java From java-technology-stack with MIT License | 5 votes |
@Override @Nullable public Class<?> getValueType(EvaluationContext context) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); ExpressionState expressionState = new ExpressionState(context, this.configuration); TypeDescriptor typeDescriptor = this.ast.getValueInternal(expressionState).getTypeDescriptor(); return (typeDescriptor != null ? typeDescriptor.getType() : null); }
Example #20
Source File: MapToMapConverterTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void emptyMapDifferentTargetImplType() throws Exception { Map<String, String> map = new HashMap<>(); TypeDescriptor sourceType = TypeDescriptor.forObject(map); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapDifferentTarget")); assertTrue(conversionService.canConvert(sourceType, targetType)); @SuppressWarnings("unchecked") LinkedHashMap<String, String> result = (LinkedHashMap<String, String>) conversionService.convert(map, sourceType, targetType); assertEquals(map, result); assertEquals(LinkedHashMap.class, result.getClass()); }
Example #21
Source File: ConstructorReference.java From spring4-understanding with Apache License 2.0 | 5 votes |
private void populateReferenceTypeArray(ExpressionState state, Object newArray, TypeConverter typeConverter, InlineList initializer, Class<?> componentType) { TypeDescriptor toTypeDescriptor = TypeDescriptor.valueOf(componentType); Object[] newObjectArray = (Object[]) newArray; for (int i = 0; i < newObjectArray.length; i++) { SpelNode elementNode = initializer.getChild(i); Object arrayEntry = elementNode.getValue(state); newObjectArray[i] = typeConverter.convertValue(arrayEntry, TypeDescriptor.forObject(arrayEntry), toTypeDescriptor); } }
Example #22
Source File: DelimitedStringToArrayConverter.java From spring-cloud-gray with Apache License 2.0 | 5 votes |
@Override public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } return convert((String) source, sourceType, targetType); }
Example #23
Source File: CollectionToCollectionConverterTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test(expected = ConversionFailedException.class) public void nothingInCommon() throws Exception { List<Object> resources = new ArrayList<Object>(); resources.add(new ClassPathResource("test")); resources.add(3); TypeDescriptor sourceType = TypeDescriptor.forObject(resources); assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources")))); }
Example #24
Source File: ArtifactToStringSpringConverter.java From artifact-listener with Apache License 2.0 | 5 votes |
@Override public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } if (!(source instanceof Artifact)) { throw new IllegalStateException("Source must be an Artifact"); } return ((Artifact) source).getArtifactKey().getKey(); }
Example #25
Source File: SpringMvcContract.java From spring-cloud-openfeign with Apache License 2.0 | 5 votes |
private static TypeDescriptor getElementTypeDescriptor( TypeDescriptor typeDescriptor) { TypeDescriptor elementTypeDescriptor = typeDescriptor.getElementTypeDescriptor(); // that means it's not a collection but it is iterable, gh-135 if (elementTypeDescriptor == null && Iterable.class.isAssignableFrom(typeDescriptor.getType())) { ResolvableType type = typeDescriptor.getResolvableType().as(Iterable.class) .getGeneric(0); if (type.resolve() == null) { return null; } return new TypeDescriptor(type, null, typeDescriptor.getAnnotations()); } return elementTypeDescriptor; }
Example #26
Source File: CollectionToCollectionConverterTests.java From java-technology-stack with MIT License | 5 votes |
@Test(expected = ConversionFailedException.class) public void nothingInCommon() throws Exception { List<Object> resources = new ArrayList<>(); resources.add(new ClassPathResource("test")); resources.add(3); TypeDescriptor sourceType = TypeDescriptor.forObject(resources); assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources")))); }
Example #27
Source File: JodaTimeFormattingTests.java From spring-analysis-note with MIT License | 5 votes |
@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); }
Example #28
Source File: RelaxedConversionService.java From canal with Apache License 2.0 | 5 votes |
@Override public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (this.conversionService != null) { try { return this.conversionService.convert(source, sourceType, targetType); } catch (ConversionFailedException ex) { // Ignore and try the additional converters } } return this.additionalConverters.convert(source, sourceType, targetType); }
Example #29
Source File: StreamConverter.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { if (sourceType.isAssignableTo(STREAM_TYPE)) { return matchesFromStream(sourceType.getElementTypeDescriptor(), targetType); } if (targetType.isAssignableTo(STREAM_TYPE)) { return matchesToStream(targetType.getElementTypeDescriptor(), sourceType); } return false; }
Example #30
Source File: ConstructorReference.java From lams with GNU General Public License v2.0 | 5 votes |
private void populateReferenceTypeArray(ExpressionState state, Object newArray, TypeConverter typeConverter, InlineList initializer, Class<?> componentType) { TypeDescriptor toTypeDescriptor = TypeDescriptor.valueOf(componentType); Object[] newObjectArray = (Object[]) newArray; for (int i = 0; i < newObjectArray.length; i++) { SpelNode elementNode = initializer.getChild(i); Object arrayEntry = elementNode.getValue(state); newObjectArray[i] = typeConverter.convertValue(arrayEntry, TypeDescriptor.forObject(arrayEntry), toTypeDescriptor); } }