1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 26 package jdk.tools.jaotc.collect; 27 28 import static java.nio.file.FileVisitResult.CONTINUE; 29 30 import java.io.IOException; 31 import java.nio.file.FileVisitResult; 32 import java.nio.file.FileVisitor; 33 import java.nio.file.Files; 34 import java.nio.file.Path; 35 import java.nio.file.PathMatcher; 36 import java.nio.file.SimpleFileVisitor; 37 import java.nio.file.attribute.BasicFileAttributes; 38 import java.util.ArrayList; 39 import java.util.Iterator; 40 41 /** 42 * {@link FileVisitor} implementation to find class files recursively. 43 */ 44 public final class FileSystemFinder extends SimpleFileVisitor<Path> implements Iterable<Path> { 45 private final ArrayList<Path> fileNames = new ArrayList<>(); 46 private final PathMatcher filter; 47 48 public FileSystemFinder(Path combinedPath, PathMatcher filter) { 49 this.filter = filter; 50 try { 51 Files.walkFileTree(combinedPath, this); 52 } catch (IOException e) { 53 throw new InternalError(e); 54 } 55 } 56 57 /** 58 * Compares the glob pattern against the file name. 59 */ 60 private void find(Path file) { 61 Path name = file.getFileName(); 62 if (name != null && filter.matches(name)) { 63 fileNames.add(file); 64 } 65 } 66 67 @Override 68 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { 69 find(file); 70 return CONTINUE; 71 } 72 73 @Override 74 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { 75 find(dir); 76 return CONTINUE; 77 } 78 79 @Override 80 public Iterator<Path> iterator() { 81 return fileNames.iterator(); 82 } 83 }