List of usage examples for org.springframework.core.convert TypeDescriptor getObjectType
public Class<?> getObjectType()
From source file:guru.qas.martini.tag.TagResolver.java
protected boolean isMatch(TypeDescriptor descriptor) { Class<?> objectType = descriptor.getObjectType(); return String.class.equals(objectType); }
From source file:io.fabric8.spring.boot.converters.ServiceConverter.java
@Override public Object convert(Object o, TypeDescriptor sourceType, TypeDescriptor targetType) { Service source = (Service) o; String serviceProtocol = getProtocolOfService(source); String servicePort = getPortOfService(source); String str = getServiceURL(kubernetesClient, source, serviceProtocol, servicePort); try {//from w ww .j a v a 2 s .c o m if (String.class.equals(targetType.getObjectType())) { return str; } else if (URL.class.equals(targetType.getObjectType())) { return new URL(str); } } catch (Throwable t) { throw new RuntimeException( "Failed to convert from: " + sourceType.getObjectType() + " to: " + targetType.getObjectType()); } throw new IllegalStateException("Invalid target type: " + targetType.getObjectType()); }
From source file:org.gvnix.web.json.ConversionServiceBeanSerializerModifier.java
@Override public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) { // We need the BeanPropertyDefinition to get the related Field List<BeanPropertyDefinition> properties = beanDesc.findProperties(); Map<String, BeanPropertyDefinition> propertyDefMap = new HashMap<String, BeanPropertyDefinition>(); for (BeanPropertyDefinition property : properties) { propertyDefMap.put(property.getName(), property); }/*from w w w . j a v a 2s .com*/ // iterate over bean's properties to configure serializers for (int i = 0; i < beanProperties.size(); i++) { BeanPropertyWriter beanPropertyWriter = beanProperties.get(i); Class<?> propertyType = beanPropertyWriter.getPropertyType(); if (beanPropertyWriter.hasSerializer()) { continue; } // For conversion between collection, array, and map types, // ConversionService.canConvert() method will return 'true' // but better to delegate in default Jackson property writer for // right start and ends markers serialization and iteration if (propertyType.isArray() || Collection.class.isAssignableFrom(propertyType) || Map.class.isAssignableFrom(propertyType)) { // Don't set ConversionService serializer, let Jackson // use default Collection serializer continue; } else if (BindingResult.class.isAssignableFrom(propertyType)) { // Use BindingResultSerializer beanPropertyWriter.assignSerializer(bindingResultSerializer); } else { // ConversionService uses value Class plus related Field // annotations to be able to select the right converter, // so we must get/ the Field annotations for success // formatting BeanPropertyDefinition propertyDef = propertyDefMap.get(beanPropertyWriter.getName()); AnnotatedField annotatedField = propertyDef.getField(); if (annotatedField == null) { continue; } AnnotatedElement annotatedEl = annotatedField.getAnnotated(); // Field contains info about Annotations, info that // ConversionService uses for success formatting, use it if // available. Otherwise use the class of given value. TypeDescriptor sourceType = annotatedEl != null ? new TypeDescriptor((Field) annotatedEl) : TypeDescriptor.valueOf(propertyType); TypeDescriptor targetType = TypeDescriptor.valueOf(String.class); if (beanPropertyWriter.getSerializationType() != null) { targetType = TypeDescriptor.valueOf(beanPropertyWriter.getSerializationType().getRawClass()); } if (ObjectUtils.equals(sourceType, targetType)) { // No conversion needed continue; } else if (sourceType.getObjectType() == Object.class && targetType.getObjectType() == String.class && beanPropertyWriter.getSerializationType() == null) { // Can't determine source type and no target type has been // configure. Delegate on jackson. continue; } // All other converters must be set in ConversionService if (this.conversionService.canConvert(sourceType, targetType)) { // We must create BeanPropertyWriter own Serializer that // has knowledge about the Field related to that // BeanPropertyWriter in order to have access to // Field Annotations for success serialization JsonSerializer<Object> jsonSerializer = new ConversionServicePropertySerializer( this.conversionService, sourceType, targetType); beanPropertyWriter.assignSerializer(jsonSerializer); } // If no converter set, use default Jackson property writer else { continue; } } } return beanProperties; }
From source file:org.springframework.core.convert.support.GenericConversionService.java
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { assertNotNull(sourceType, targetType); if (logger.isDebugEnabled()) { logger.debug(/*from w ww. j a v a 2 s .c om*/ "Converting value " + StylerUtils.style(source) + " of " + sourceType + " to " + targetType); } if (sourceType == TypeDescriptor.NULL) { Assert.isTrue(source == null, "The value must be null if sourceType == TypeDescriptor.NULL"); Object result = convertNullSource(sourceType, targetType); if (logger.isDebugEnabled()) { logger.debug("Converted to " + StylerUtils.style(result)); } return result; } if (targetType == TypeDescriptor.NULL) { logger.debug("Converted to null"); return null; } Assert.isTrue(source == null || sourceType.getObjectType().isInstance(source)); GenericConverter converter = getConverter(sourceType, targetType); if (converter == null) { if (source == null || sourceType.isAssignableTo(targetType)) { logger.debug("No converter found - returning assignable source object as-is"); return source; } else { throw new ConverterNotFoundException(sourceType, targetType); } } Object result = ConversionUtils.invokeConverter(converter, source, sourceType, targetType); if (logger.isDebugEnabled()) { logger.debug("Converted to " + StylerUtils.style(result)); } return result; }
From source file:org.springframework.core.convert.support.GenericConversionService.java
private GenericConverter findConverterForClassPair(TypeDescriptor sourceType, TypeDescriptor targetType) { Class<?> sourceObjectType = sourceType.getObjectType(); if (sourceObjectType.isInterface()) { LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>(); classQueue.addFirst(sourceObjectType); while (!classQueue.isEmpty()) { Class<?> currentClass = classQueue.removeLast(); if (logger.isTraceEnabled()) { logger.trace("Searching for converters indexed by sourceType [" + currentClass.getName() + "]"); }//from ww w .j a v a 2s . c o m Map<Class<?>, MatchableConverters> converters = getTargetConvertersForSource(currentClass); GenericConverter converter = getMatchingConverterForTarget(sourceType, targetType, converters); if (converter != null) { return converter; } Class<?>[] interfaces = currentClass.getInterfaces(); for (Class<?> ifc : interfaces) { classQueue.addFirst(ifc); } } Map<Class<?>, MatchableConverters> objectConverters = getTargetConvertersForSource(Object.class); return getMatchingConverterForTarget(sourceType, targetType, objectConverters); } else { LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>(); classQueue.addFirst(sourceObjectType); while (!classQueue.isEmpty()) { Class<?> currentClass = classQueue.removeLast(); if (logger.isTraceEnabled()) { logger.trace("Searching for converters indexed by sourceType [" + currentClass.getName() + "]"); } Map<Class<?>, MatchableConverters> converters = getTargetConvertersForSource(currentClass); GenericConverter converter = getMatchingConverterForTarget(sourceType, targetType, converters); if (converter != null) { return converter; } if (currentClass.isArray()) { Class<?> componentType = ClassUtils .resolvePrimitiveIfNecessary(currentClass.getComponentType()); if (componentType.getSuperclass() != null) { classQueue.addFirst(Array.newInstance(componentType.getSuperclass(), 0).getClass()); } else if (componentType.isInterface()) { classQueue.addFirst(Object[].class); } } else { Class<?>[] interfaces = currentClass.getInterfaces(); for (Class<?> ifc : interfaces) { addInterfaceHierarchy(ifc, classQueue); } if (currentClass.getSuperclass() != null) { classQueue.addFirst(currentClass.getSuperclass()); } } } return null; } }
From source file:org.springframework.core.convert.support.GenericConversionService.java
private GenericConverter getMatchingConverterForTarget(TypeDescriptor sourceType, TypeDescriptor targetType, Map<Class<?>, MatchableConverters> converters) { Class<?> targetObjectType = targetType.getObjectType(); if (targetObjectType.isInterface()) { LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>(); classQueue.addFirst(targetObjectType); while (!classQueue.isEmpty()) { Class<?> currentClass = classQueue.removeLast(); if (logger.isTraceEnabled()) { logger.trace("and indexed by targetType [" + currentClass.getName() + "]"); }//from w w w . j a va2 s . c om MatchableConverters matchable = converters.get(currentClass); GenericConverter converter = matchConverter(matchable, sourceType, targetType); if (converter != null) { return converter; } Class<?>[] interfaces = currentClass.getInterfaces(); for (Class<?> ifc : interfaces) { classQueue.addFirst(ifc); } } if (logger.isTraceEnabled()) { logger.trace("and indexed by [java.lang.Object]"); } return matchConverter(converters.get(Object.class), sourceType, targetType); } else { LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>(); classQueue.addFirst(targetObjectType); while (!classQueue.isEmpty()) { Class<?> currentClass = classQueue.removeLast(); if (logger.isTraceEnabled()) { logger.trace("and indexed by targetType [" + currentClass.getName() + "]"); } MatchableConverters matchable = converters.get(currentClass); GenericConverter converter = matchConverter(matchable, sourceType, targetType); if (converter != null) { return converter; } if (currentClass.isArray()) { Class<?> componentType = ClassUtils .resolvePrimitiveIfNecessary(currentClass.getComponentType()); if (componentType.getSuperclass() != null) { classQueue.addFirst(Array.newInstance(componentType.getSuperclass(), 0).getClass()); } else if (componentType.isInterface()) { classQueue.addFirst(Object[].class); } } else { Class<?>[] interfaces = currentClass.getInterfaces(); for (Class<?> ifc : interfaces) { addInterfaceHierarchy(ifc, classQueue); } if (currentClass.getSuperclass() != null) { classQueue.addFirst(currentClass.getSuperclass()); } } } return null; } }
From source file:org.talend.dataprep.conversions.BeanConversionService.java
@Override public Object convert(Object o, TypeDescriptor typeDescriptor, TypeDescriptor typeDescriptor1) { return convert(o, typeDescriptor1.getObjectType()); }