diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/PassThroughFieldExtractor.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/PassThroughFieldExtractor.java index 06236d0f0..345fdc59b 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/PassThroughFieldExtractor.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/PassThroughFieldExtractor.java @@ -16,6 +16,7 @@ package org.springframework.batch.item.file.transform; import java.util.Collection; +import java.util.Map; /** * {@link FieldExtractor} that just returns the original item. If the item is an @@ -28,6 +29,20 @@ import java.util.Collection; public class PassThroughFieldExtractor implements FieldExtractor { /** + * Get an array of fields as close as possible to the input. The result + * depends on the type of the input: + * + * Note that no attempt is made to sort the values, so passing in an + * unordered collection or map is probably a bad idea. Spring often gives + * you an ordered Map (e.g. if extracting data from a generic query using + * JDBC), so check the documentation for whatever is being used to generate + * the input. + * * @param item the object to convert * @return an array of objects as close as possible to the original item */ @@ -41,6 +56,10 @@ public class PassThroughFieldExtractor implements FieldExtractor { return ((Collection) item).toArray(); } + if (item instanceof Map) { + return ((Map) item).values().toArray(); + } + if (item instanceof FieldSet) { return ((FieldSet) item).getValues(); } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/PassThroughFieldExtractorTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/PassThroughFieldExtractorTests.java index 01998a915..43bc16062 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/PassThroughFieldExtractorTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/PassThroughFieldExtractorTests.java @@ -18,7 +18,9 @@ package org.springframework.batch.item.file.transform; import static junit.framework.Assert.assertTrue; import java.util.Arrays; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import org.junit.Test; @@ -55,4 +57,17 @@ public class PassThroughFieldExtractorTests { Object[] result = extractor.extract(Arrays.asList("a", "b", null, "d")); assertTrue(Arrays.equals(new Object[] { "a", "b", null, "d" }, result)); } + + @Test + public void testExtractMap() { + PassThroughFieldExtractor> extractor = new PassThroughFieldExtractor>(); + Map map = new LinkedHashMap(); + map.put("A", "a"); + map.put("B", "b"); + map.put("C", null); + map.put("D", "d"); + Object[] result = extractor.extract(map); + assertTrue(Arrays.equals(new Object[] { "a", "b", null, "d" }, result)); + } + }