001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2022 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.meta; 021 022import java.io.File; 023import java.io.IOException; 024import java.io.OutputStream; 025import java.nio.charset.StandardCharsets; 026import java.nio.file.Files; 027import java.nio.file.Path; 028import java.nio.file.Paths; 029import java.util.ArrayList; 030import java.util.List; 031import java.util.stream.Collectors; 032import java.util.stream.Stream; 033 034import com.puppycrawl.tools.checkstyle.Checker; 035import com.puppycrawl.tools.checkstyle.DefaultConfiguration; 036import com.puppycrawl.tools.checkstyle.MetadataGeneratorLogger; 037import com.puppycrawl.tools.checkstyle.TreeWalker; 038import com.puppycrawl.tools.checkstyle.api.AutomaticBean; 039import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 040 041/** Class which handles all the metadata generation and writing calls. */ 042public final class MetadataGeneratorUtil { 043 044 /** Stop instances being created. **/ 045 private MetadataGeneratorUtil() { 046 } 047 048 /** 049 * Generate metadata from the module source files available in the input argument path. 050 * 051 * @param path arguments 052 * @param out OutputStream for error messages 053 * @param moduleFolders folders to check 054 * @throws IOException ioException 055 * @throws CheckstyleException checkstyleException 056 */ 057 public static void generate(String path, OutputStream out, String... moduleFolders) 058 throws IOException, CheckstyleException { 059 JavadocMetadataScraper.resetModuleDetailsStore(); 060 061 final Checker checker = new Checker(); 062 checker.setModuleClassLoader(Checker.class.getClassLoader()); 063 final DefaultConfiguration scraperCheckConfig = 064 new DefaultConfiguration(JavadocMetadataScraper.class.getName()); 065 final DefaultConfiguration defaultConfiguration = 066 new DefaultConfiguration("configuration"); 067 final DefaultConfiguration treeWalkerConfig = 068 new DefaultConfiguration(TreeWalker.class.getName()); 069 defaultConfiguration.addProperty("charset", StandardCharsets.UTF_8.name()); 070 defaultConfiguration.addChild(treeWalkerConfig); 071 treeWalkerConfig.addChild(scraperCheckConfig); 072 checker.configure(defaultConfiguration); 073 074 checker.addListener(new MetadataGeneratorLogger(out, 075 AutomaticBean.OutputStreamOptions.NONE)); 076 077 dumpMetadata(checker, path, moduleFolders); 078 } 079 080 /** 081 * Process files using the checker passed and write to corresponding XML files. 082 * 083 * @param moduleFolders folders to check 084 * @param checker checker 085 * @param path rootPath 086 * @throws CheckstyleException checkstyleException 087 * @throws IOException ioException 088 */ 089 private static void dumpMetadata(Checker checker, String path, String... moduleFolders) 090 throws CheckstyleException, 091 IOException { 092 final List<File> validFiles = new ArrayList<>(); 093 for (String folder : moduleFolders) { 094 try (Stream<Path> files = Files.walk(Paths.get(path 095 + "/" + folder))) { 096 validFiles.addAll( 097 files.map(Path::toFile) 098 .filter(file -> { 099 final String fileName = file.getName(); 100 return fileName.endsWith("SuppressWarningsHolder.java") 101 || fileName.endsWith("Check.java") 102 || fileName.endsWith("Filter.java"); 103 }) 104 .collect(Collectors.toList())); 105 } 106 } 107 checker.process(validFiles); 108 } 109}