@Override public TypedValue getValue() { Object arrayElement = accessArrayElement(this.array, this.index); return new TypedValue(arrayElement, this.typeDescriptor.elementTypeDescriptor(arrayElement)); }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } if (sourceType.isAssignableTo(targetType)) { return source; } Collection<?> sourceCollection = (Collection<?>) source; if (sourceCollection.isEmpty()) { return null; } Object firstElement = sourceCollection.iterator().next(); return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType); }
@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); }
@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(); }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } Collection<?> sourceCollection = (Collection<?>) source; TypeDescriptor targetElementType = targetType.getElementTypeDescriptor(); Assert.state(targetElementType != null, "No target element type"); Object array = Array.newInstance(targetElementType.getType(), sourceCollection.size()); int i = 0; for (Object sourceElement : sourceCollection) { Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetElementType); Array.set(array, i++, targetElement); } return array; }
@Override public TypedValue getValue() { growCollectionIfNecessary(); if (this.collection instanceof List) { Object o = ((List) this.collection).get(this.index); exitTypeDescriptor = CodeFlow.toDescriptor(Object.class); return new TypedValue(o, this.collectionEntryDescriptor.elementTypeDescriptor(o)); } int pos = 0; for (Object o : this.collection) { if (pos == this.index) { return new TypedValue(o, this.collectionEntryDescriptor.elementTypeDescriptor(o)); } pos++; } throw new IllegalStateException("Failed to find indexed element " + this.index + ": " + this.collection); }
@Override public TypedValue getValue() { Object arrayElement = accessArrayElement(this.array, this.index); return new TypedValue(arrayElement, this.typeDescriptor.elementTypeDescriptor(arrayElement)); }
private String convertElement(Object element, TypeDescriptor sourceType, TypeDescriptor targetType) { return String.valueOf(this.conversionService.convert(element, sourceType.elementTypeDescriptor(element), targetType)); }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } if (sourceType.isAssignableTo(targetType)) { return source; } Collection<?> sourceCollection = (Collection<?>) source; if (sourceCollection.isEmpty()) { return null; } Object firstElement = sourceCollection.iterator().next(); return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType); }
@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); }
@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(); }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } int length = Array.getLength(source); TypeDescriptor elementDesc = targetType.getElementTypeDescriptor(); Collection<Object> target = CollectionFactory.createCollection(targetType.getType(), (elementDesc != null ? elementDesc.getType() : null), length); if (elementDesc == null) { for (int i = 0; i < length; i++) { Object sourceElement = Array.get(source, i); target.add(sourceElement); } } else { for (int i = 0; i < length; i++) { Object sourceElement = Array.get(source, i); Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), elementDesc); target.add(targetElement); } } return target; }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } Collection<?> sourceCollection = (Collection<?>) source; TypeDescriptor targetElementType = targetType.getElementTypeDescriptor(); Assert.state(targetElementType != null, "No target element type"); Object array = Array.newInstance(targetElementType.getType(), sourceCollection.size()); int i = 0; for (Object sourceElement : sourceCollection) { Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), targetElementType); Array.set(array, i++, targetElement); } return array; }
for (Object sourceElement : sourceCollection) { Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), elementDesc); target.add(targetElement); if (sourceElement != targetElement) {
@Override public TypedValue getValue() { growCollectionIfNecessary(); if (this.collection instanceof List) { Object o = ((List) this.collection).get(this.index); exitTypeDescriptor = CodeFlow.toDescriptor(Object.class); return new TypedValue(o, this.collectionEntryDescriptor.elementTypeDescriptor(o)); } int pos = 0; for (Object o : this.collection) { if (pos == this.index) { return new TypedValue(o, this.collectionEntryDescriptor.elementTypeDescriptor(o)); } pos++; } throw new IllegalStateException("Failed to find indexed element " + this.index + ": " + this.collection); }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } int length = Array.getLength(source); TypeDescriptor elementDesc = targetType.getElementTypeDescriptor(); Collection<Object> target = CollectionFactory.createCollection(targetType.getType(), (elementDesc != null ? elementDesc.getType() : null), length); if (elementDesc == null) { for (int i = 0; i < length; i++) { Object sourceElement = Array.get(source, i); target.add(sourceElement); } } else { for (int i = 0; i < length; i++) { Object sourceElement = Array.get(source, i); Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), elementDesc); target.add(targetElement); } } return target; }
for (Object sourceElement : sourceCollection) { Object targetElement = this.conversionService.convert(sourceElement, sourceType.elementTypeDescriptor(sourceElement), elementDesc); target.add(targetElement); if (sourceElement != targetElement) {
@Test public void elementType() { TypeDescriptor desc = TypeDescriptor.valueOf(List.class); Integer value = Integer.valueOf(3); desc = desc.elementTypeDescriptor(value); assertEquals(Integer.class, desc.getType()); }
@Test public void elementTypePreserveContext() throws Exception { TypeDescriptor desc = new TypeDescriptor(getClass().getField("listPreserveContext")); assertEquals(Integer.class, desc.getElementTypeDescriptor().getElementTypeDescriptor().getType()); List<Integer> value = new ArrayList<>(3); desc = desc.elementTypeDescriptor(value); assertEquals(Integer.class, desc.getElementTypeDescriptor().getType()); assertNotNull(desc.getAnnotation(FieldAnnotation.class)); }
public Object convert(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); }