Example usage for java.nio.file SimpleFileVisitor visitFile

List of usage examples for java.nio.file SimpleFileVisitor visitFile

Introduction

In this page you can find the example usage for java.nio.file SimpleFileVisitor visitFile.

Prototype

@Override
public FileVisitResult visitFile(T file, BasicFileAttributes attrs) throws IOException 

Source Link

Document

Invoked for a file in a directory.

Usage

From source file:net.sourceforge.pmd.cache.AbstractAnalysisCache.java

private URL[] getClassPathEntries() {
    final String classpath = System.getProperty("java.class.path");
    final String[] classpathEntries = classpath.split(File.pathSeparator);
    final List<URL> entries = new ArrayList<>();

    final SimpleFileVisitor<Path> fileVisitor = new SimpleFileVisitor<Path>() {
        @Override//from w w  w . ja  va2 s.co  m
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            if (!attrs.isSymbolicLink()) { // Broken link that can't be followed
                entries.add(file.toUri().toURL());
            }
            return FileVisitResult.CONTINUE;
        }
    };
    final SimpleFileVisitor<Path> jarFileVisitor = new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            String extension = FilenameUtils.getExtension(file.toString());
            if ("jar".equalsIgnoreCase(extension)) {
                fileVisitor.visitFile(file, attrs);
            }
            return FileVisitResult.CONTINUE;
        }
    };

    try {
        for (final String entry : classpathEntries) {
            final File f = new File(entry);
            if (isClassPathWildcard(entry)) {
                Files.walkFileTree(new File(entry.substring(0, entry.length() - 1)).toPath(),
                        EnumSet.of(FileVisitOption.FOLLOW_LINKS), 1, jarFileVisitor);
            } else if (f.isFile()) {
                entries.add(f.toURI().toURL());
            } else {
                Files.walkFileTree(f.toPath(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                        fileVisitor);
            }
        }
    } catch (final IOException e) {
        LOG.log(Level.SEVERE, "Incremental analysis can't check execution classpath contents", e);
        throw new RuntimeException(e);
    }

    return entries.toArray(new URL[0]);
}