Uploaded image for project: 'Spring Integration'
  1. Spring Integration
  2. INT-4216

ChainFileListFilter provides Object[] instead of File[] during iteration

    XMLWordPrintable

Details

    Description

      The test-case demonstrates the problem:

      @Test
      public void patternThenModifiedFilters() throws IOException {
      	try (ChainFileListFilter<File> chain = new ChainFileListFilter<>(File.class)) {
      		chain.addFilter(new SimplePatternFileListFilter("*.txt"));
      		chain.addFilter(new LastModifiedFileListFilter());
      		List<File> result = chain.filterFiles(files);
      		assertEquals(1, result.size());
      	}
      }
      

      Since LastModifiedFileListFilter isn't generic and expects only File[] it doesn't work as a nested filter in the ChainFileListFilter because generics erasure in Java makes cast to F[] in subsequent iteration useless.

      As a workaround I suggest this:

      public class ChainFileListFilter<F> extends CompositeFileListFilter<F> {
      
      	private final Class<F> fileClass;
      
      	public ChainFileListFilter(Class<F> fileClass) {
      		this.fileClass = fileClass;
      	}
      
      	@Override
      	public List<F> filterFiles(F[] files) {
      		Assert.notNull(files, "'files' should not be null");
      		List<F> leftOver = Arrays.asList(files);
      		for (FileListFilter<F> fileFilter : this.fileFilters) {
      			@SuppressWarnings("unchecked")
      			F[] fileArray = leftOver.toArray((F[]) Array.newInstance(this.fileClass, leftOver.size()));
      			leftOver = fileFilter.filterFiles(fileArray);
      		}
      		return leftOver;
      	}
      
      }
      

      And it works even with an empty list, so we can revert the fail fast logic.

      Any other suggestions are welcome!

      Attachments

        Issue Links

          Activity

            People

              abilan Artem Bilan
              abilan Artem Bilan
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: