This closes #59 pull request at github/apache/ant repo This NPE happens whenever any of the sub mappers returns `null`, which may happen eg. with `GlobPatternMapper`.master
@@ -18,6 +18,7 @@ | |||||
package org.apache.tools.ant.util; | package org.apache.tools.ant.util; | ||||
import java.util.Objects; | |||||
import java.util.stream.Stream; | import java.util.stream.Stream; | ||||
/** | /** | ||||
@@ -33,7 +34,7 @@ public class ChainedMapper extends ContainerMapper { | |||||
public String[] mapFileName(String sourceFileName) { | public String[] mapFileName(String sourceFileName) { | ||||
String[] result = getMappers().stream() | String[] result = getMappers().stream() | ||||
.reduce(new String[] { sourceFileName }, (i, m) -> Stream.of(i) | .reduce(new String[] { sourceFileName }, (i, m) -> Stream.of(i) | ||||
.map(m::mapFileName).flatMap(Stream::of).toArray(String[]::new), | |||||
.map(m::mapFileName).filter(Objects::nonNull).flatMap(Stream::of).toArray(String[]::new), | |||||
(i, o) -> o); | (i, o) -> o); | ||||
return result == null || result.length == 0 ? null : result; | return result == null || result.length == 0 ? null : result; | ||||
} | } | ||||
@@ -35,6 +35,7 @@ import org.junit.Test; | |||||
import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
import static org.junit.Assert.assertNotNull; | import static org.junit.Assert.assertNotNull; | ||||
import static org.junit.Assert.assertNull; | |||||
import static org.junit.Assert.assertTrue; | import static org.junit.Assert.assertTrue; | ||||
import static org.junit.Assert.fail; | import static org.junit.Assert.fail; | ||||
@@ -224,6 +225,9 @@ public class MapperTest { | |||||
List list = Arrays.asList(targets); | List list = Arrays.asList(targets); | ||||
assertTrue("cannot find expected target \"def\"", list.contains("def")); | assertTrue("cannot find expected target \"def\"", list.contains("def")); | ||||
assertTrue("cannot find expected target \"ghi\"", list.contains("ghi")); | assertTrue("cannot find expected target \"ghi\"", list.contains("ghi")); | ||||
targets = fileNameMapper.mapFileName("z"); | |||||
assertNull(targets); | |||||
} | } | ||||
@Test | @Test | ||||