diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/FileUtil.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/FileUtil.java index ee245956c5..bb233570dd 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/FileUtil.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/FileUtil.java @@ -7,51 +7,21 @@ package net.sourceforge.pmd.util; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; -import java.nio.file.FileSystem; -import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.sql.SQLException; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.Locale; -import java.util.Set; -import java.util.function.Predicate; -import java.util.logging.Level; -import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; -import java.util.stream.Stream; import org.apache.commons.lang3.StringUtils; import org.checkerframework.checker.nullness.qual.NonNull; -import org.checkerframework.checker.nullness.qual.Nullable; -import net.sourceforge.pmd.PMD; -import net.sourceforge.pmd.PMDConfiguration; import net.sourceforge.pmd.annotation.InternalApi; -import net.sourceforge.pmd.benchmark.TimeTracker; -import net.sourceforge.pmd.benchmark.TimedOperation; -import net.sourceforge.pmd.benchmark.TimedOperationCategory; -import net.sourceforge.pmd.internal.util.PredicateUtil; -import net.sourceforge.pmd.internal.util.ShortFilenameUtil; -import net.sourceforge.pmd.lang.Language; -import net.sourceforge.pmd.lang.LanguageFilenameFilter; -import net.sourceforge.pmd.lang.LanguageVersion; -import net.sourceforge.pmd.util.database.DBMSMetadata; -import net.sourceforge.pmd.util.database.DBURI; -import net.sourceforge.pmd.util.database.SourceObject; -import net.sourceforge.pmd.util.datasource.DataSource; -import net.sourceforge.pmd.lang.document.ReferenceCountedCloseable; -import net.sourceforge.pmd.lang.document.TextFile; -import net.sourceforge.pmd.lang.document.TextFileBuilder; /** * This is a utility class for working with Files. @@ -62,8 +32,6 @@ import net.sourceforge.pmd.lang.document.TextFileBuilder; @InternalApi public final class FileUtil { - private static final Logger LOG = Logger.getLogger(PMD.class.getName()); - private FileUtil() { } @@ -101,39 +69,6 @@ public final class FileUtil { return fileName; } - @SuppressWarnings("PMD.CloseResource") - // the zip file can't be closed here, it's closed with the FileSystemCloseable during analysis - private static void collect(List result, - String root, - PMDConfiguration configuration, - Predicate filter) throws IOException { - Path rootPath = toExistingPath(root); - - Stream subfiles; - @Nullable ReferenceCountedCloseable fsCloseable; - if (Files.isDirectory(rootPath)) { - fsCloseable = null; - subfiles = Files.walk(rootPath); - } else if (root.endsWith(".zip") || root.endsWith(".jar")) { - URI uri = URI.create(root); - FileSystem zipfs = FileSystems.newFileSystem(uri, Collections.emptyMap()); - fsCloseable = new ReferenceCountedCloseable(zipfs); - subfiles = Files.walk(zipfs.getPath("/")); - } else { - if (filter.test(rootPath)) { - result.add(createNioTextFile(configuration, rootPath, null)); - } - return; - } - - try (Stream walk = subfiles) { - walk.filter(filter) - .map(path -> createNioTextFile(configuration, path, fsCloseable)) - .forEach(result::add); - } - - } - public static @NonNull Path toExistingPath(String root) throws FileNotFoundException { Path file = Paths.get(root); if (!Files.exists(file)) { @@ -190,137 +125,5 @@ public final class FileUtil { .collect(Collectors.toList()); } - /** - * Determines all the files, that should be analyzed by PMD. - * - * @param configuration contains either the file path or the DB URI, from where to - * load the files - * @param languages used to filter by file extension - * - * @return List of {@link DataSource} of files, not sorted - * - * @throws IOException If an IOException occurs - */ - public static List getApplicableFiles(PMDConfiguration configuration, - Set languages) throws IOException { - List result = new ArrayList<>(); - try (TimedOperation to = TimeTracker.startOperation(TimedOperationCategory.COLLECT_FILES)) { - - internalGetApplicableFiles(result, configuration, languages); - - } catch (IOException ioe) { - // then, close everything that's done for now, and rethrow - Exception exception = IOUtil.closeAll(result); - if (exception != null) { - ioe.addSuppressed(exception); - } - throw ioe; - } - - return result; - } - - - private static void internalGetApplicableFiles(List files, PMDConfiguration configuration, Set languages) throws IOException { - List ignoredFiles = getIgnoredFiles(configuration); - LanguageVersion forcedVersion = configuration.getForceLanguageVersion(); - Predicate fileFilter = - forcedVersion != null ? Files::isRegularFile // accept everything except dirs - : PredicateUtil.toFileFilter(new LanguageFilenameFilter(languages)); - fileFilter = fileFilter.and(path -> !ignoredFiles.contains(path.toString())); - - for (String root : configuration.getAllInputPaths()) { - collect(files, root, configuration, fileFilter); - } - - if (null != configuration.getInputUri()) { - getURIDataSources(files, configuration.getInputUri(), configuration); - } - - if (null != configuration.getInputFilePath()) { - @NonNull Path fileList = toExistingPath(configuration.getInputFilePath()); - - try { - for (String root : readFilelistEntries(fileList)) { - collect(files, root, configuration, fileFilter); - } - } catch (IOException ex) { - throw new IOException("Problem with filelist: " + configuration.getInputFilePath(), ex); - } - } - } - - private static List getIgnoredFiles(PMDConfiguration configuration) throws IOException { - if (null != configuration.getIgnoreFilePath()) { - Path ignoreFile = toExistingPath(configuration.getIgnoreFilePath()); - try { - // todo, if the file list contains relative paths, they - // should be taken relative to the filelist location, - // not the working directory, right? - return readFilelistEntries(ignoreFile); - } catch (IOException ex) { - throw new IOException("Problem with exclusion filelist: " + ignoreFile, ex); - } - } else { - return Collections.emptyList(); - } - } - - - private static void getURIDataSources(List collector, String uriString, PMDConfiguration config) throws IOException { - - try { - DBURI dbUri = new DBURI(uriString); - DBMSMetadata dbmsMetadata = new DBMSMetadata(dbUri); - LOG.log(Level.FINE, "DBMSMetadata retrieved"); - List sourceObjectList = dbmsMetadata.getSourceObjectList(); - LOG.log(Level.FINE, "Located {0} database source objects", sourceObjectList.size()); - for (SourceObject sourceObject : sourceObjectList) { - String falseFilePath = sourceObject.getPseudoFileName(); - LOG.log(Level.FINEST, "Adding database source object {0}", falseFilePath); - - try { - LanguageVersion lv = config.getLanguageVersionOfFile(falseFilePath); - collector.add(TextFile.forReader(dbmsMetadata.getSourceCode(sourceObject), falseFilePath, lv).build()); - } catch (SQLException ex) { - if (LOG.isLoggable(Level.WARNING)) { - LOG.log(Level.WARNING, "Cannot get SourceCode for " + falseFilePath + " - skipping ...", ex); - } - } - } - } catch (URISyntaxException e) { - throw new IOException("Cannot get DataSources from DBURI - \"" + uriString + "\"", e); - } catch (SQLException e) { - throw new IOException( - "Cannot get DataSources from DBURI, couldn't access the database - \"" + uriString + "\"", e); - } catch (ClassNotFoundException e) { - throw new IOException( - "Cannot get DataSources from DBURI, probably missing database jdbc driver - \"" + uriString + "\"", e); - } catch (Exception e) { - throw new IOException("Encountered unexpected problem with URI \"" + uriString + "\"", e); - } - } - - private static @Nullable String displayName(PMDConfiguration config, Path file) { - if (config.isReportShortNames()) { - return ShortFilenameUtil.determineFileName(config.getAllInputPaths(), file.toString()); - } - return null; - } - - public static TextFile createNioTextFile(PMDConfiguration config, Path file, @Nullable ReferenceCountedCloseable fsCloseable) { - return buildNioTextFile(config, file).belongingTo(fsCloseable).build(); - } - - /** - * Returns a builder that uses the configuration's encoding, and pre-fills the display name - * using the input paths ({@link PMDConfiguration#getAllInputPaths()}) if {@link PMDConfiguration#isReportShortNames()}). - */ - public static TextFileBuilder buildNioTextFile(PMDConfiguration config, Path file) { - LanguageVersion langVersion = config.getLanguageVersionOfFile(file.toString()); - - return TextFile.builderForPath(file, config.getSourceEncoding(), langVersion) - .withDisplayName(displayName(config, file)); - } }