001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2023 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; 040import com.puppycrawl.tools.checkstyle.api.RootModule; 041 042/** Class which handles all the metadata generation and writing calls. */ 043public final class MetadataGeneratorUtil { 044 045 /** Stop instances being created. **/ 046 private MetadataGeneratorUtil() { 047 } 048 049 /** 050 * Generate metadata from the module source files available in the input argument path. 051 * 052 * @param path arguments 053 * @param out OutputStream for error messages 054 * @param moduleFolders folders to check 055 * @throws IOException ioException 056 * @throws CheckstyleException checkstyleException 057 */ 058 public static void generate(String path, OutputStream out, String... moduleFolders) 059 throws IOException, CheckstyleException { 060 JavadocMetadataScraper.resetModuleDetailsStore(); 061 062 final Checker checker = new Checker(); 063 checker.setModuleClassLoader(Checker.class.getClassLoader()); 064 final DefaultConfiguration scraperCheckConfig = 065 new DefaultConfiguration(JavadocMetadataScraper.class.getName()); 066 final DefaultConfiguration defaultConfiguration = 067 new DefaultConfiguration("configuration"); 068 final DefaultConfiguration treeWalkerConfig = 069 new DefaultConfiguration(TreeWalker.class.getName()); 070 defaultConfiguration.addProperty("charset", StandardCharsets.UTF_8.name()); 071 defaultConfiguration.addChild(treeWalkerConfig); 072 treeWalkerConfig.addChild(scraperCheckConfig); 073 checker.configure(defaultConfiguration); 074 075 checker.addListener(new MetadataGeneratorLogger(out, 076 AutomaticBean.OutputStreamOptions.NONE)); 077 078 dumpMetadata(checker, path, moduleFolders); 079 } 080 081 /** 082 * Process files using the checker passed and write to corresponding XML files. 083 * 084 * @param moduleFolders folders to check 085 * @param root root module 086 * @param path rootPath 087 * @throws CheckstyleException checkstyleException 088 * @throws IOException ioException 089 */ 090 private static void dumpMetadata(RootModule root, String path, String... moduleFolders) 091 throws CheckstyleException, 092 IOException { 093 final List<File> validFiles = new ArrayList<>(); 094 for (String folder : moduleFolders) { 095 try (Stream<Path> files = Files.walk(Paths.get(path 096 + "/" + folder))) { 097 validFiles.addAll( 098 files.map(Path::toFile) 099 .filter(file -> { 100 final String fileName = file.getName(); 101 return fileName.endsWith("SuppressWarningsHolder.java") 102 || fileName.endsWith("Check.java") 103 || fileName.endsWith("Filter.java"); 104 }) 105 .collect(Collectors.toList())); 106 } 107 } 108 root.process(validFiles); 109 } 110}