View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.meta;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.nio.charset.StandardCharsets;
26  import java.nio.file.Files;
27  import java.nio.file.Path;
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.stream.Stream;
31  
32  import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions;
33  import com.puppycrawl.tools.checkstyle.Checker;
34  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
35  import com.puppycrawl.tools.checkstyle.MetadataGeneratorLogger;
36  import com.puppycrawl.tools.checkstyle.TreeWalker;
37  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
38  
39  /** Class which handles all the metadata generation and writing calls. */
40  public final class MetadataGeneratorUtil {
41  
42      /** Stop instances being created. **/
43      private MetadataGeneratorUtil() {
44      }
45  
46      /**
47       * Generate metadata from the module source files available in the input argument path.
48       *
49       * @param path arguments
50       * @param out OutputStream for error messages
51       * @param moduleFolders folders to check
52       * @throws IOException ioException
53       * @throws CheckstyleException checkstyleException
54       */
55      public static void generate(String path, OutputStream out, String... moduleFolders)
56              throws IOException, CheckstyleException {
57          JavadocMetadataScraper.resetModuleDetailsStore();
58  
59          final Checker checker = new Checker();
60          checker.setModuleClassLoader(Checker.class.getClassLoader());
61          final DefaultConfiguration scraperCheckConfig =
62                          new DefaultConfiguration(JavadocMetadataScraper.class.getName());
63          final DefaultConfiguration defaultConfiguration =
64                  new DefaultConfiguration("configuration");
65          final DefaultConfiguration treeWalkerConfig =
66                  new DefaultConfiguration(TreeWalker.class.getName());
67          defaultConfiguration.addProperty("charset", StandardCharsets.UTF_8.name());
68          defaultConfiguration.addChild(treeWalkerConfig);
69          treeWalkerConfig.addChild(scraperCheckConfig);
70          checker.configure(defaultConfiguration);
71  
72          checker.addListener(new MetadataGeneratorLogger(out, OutputStreamOptions.NONE));
73  
74          final List<File> moduleFiles = getTargetFiles(path, moduleFolders);
75  
76          checker.process(moduleFiles);
77      }
78  
79      /**
80       * Get files that represent modules.
81       *
82       * @param moduleFolders folders to check
83       * @param path          rootPath
84       * @return files for scrapping javadoc and generation of metadata files
85       * @throws IOException ioException
86       */
87      private static List<File> getTargetFiles(String path, String... moduleFolders)
88              throws IOException {
89          final List<File> validFiles = new ArrayList<>();
90          for (String folder : moduleFolders) {
91              try (Stream<Path> files = Files.walk(Path.of(path + "/" + folder))) {
92                  validFiles.addAll(
93                          files.map(Path::toFile)
94                          .filter(file -> {
95                              final String fileName = file.getName();
96                              return fileName.endsWith("SuppressWarningsHolder.java")
97                                      || fileName.endsWith("Check.java")
98                                      || fileName.endsWith("Filter.java");
99                          })
100                         .toList());
101             }
102         }
103 
104         return validFiles;
105     }
106 }