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.Collectors;
31  import java.util.stream.Stream;
32  
33  import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions;
34  import com.puppycrawl.tools.checkstyle.Checker;
35  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
36  import com.puppycrawl.tools.checkstyle.MetadataGeneratorLogger;
37  import com.puppycrawl.tools.checkstyle.TreeWalker;
38  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
39  
40  /** Class which handles all the metadata generation and writing calls. */
41  public final class MetadataGeneratorUtil {
42  
43      /** Stop instances being created. **/
44      private MetadataGeneratorUtil() {
45      }
46  
47      /**
48       * Generate metadata from the module source files available in the input argument path.
49       *
50       * @param path arguments
51       * @param out OutputStream for error messages
52       * @param moduleFolders folders to check
53       * @throws IOException ioException
54       * @throws CheckstyleException checkstyleException
55       */
56      public static void generate(String path, OutputStream out, String... moduleFolders)
57              throws IOException, CheckstyleException {
58          JavadocMetadataScraper.resetModuleDetailsStore();
59  
60          final Checker checker = new Checker();
61          checker.setModuleClassLoader(Checker.class.getClassLoader());
62          final DefaultConfiguration scraperCheckConfig =
63                          new DefaultConfiguration(JavadocMetadataScraper.class.getName());
64          final DefaultConfiguration defaultConfiguration =
65                  new DefaultConfiguration("configuration");
66          final DefaultConfiguration treeWalkerConfig =
67                  new DefaultConfiguration(TreeWalker.class.getName());
68          defaultConfiguration.addProperty("charset", StandardCharsets.UTF_8.name());
69          defaultConfiguration.addChild(treeWalkerConfig);
70          treeWalkerConfig.addChild(scraperCheckConfig);
71          checker.configure(defaultConfiguration);
72  
73          checker.addListener(new MetadataGeneratorLogger(out, OutputStreamOptions.NONE));
74  
75          final List<File> moduleFiles = getTargetFiles(path, moduleFolders);
76  
77          checker.process(moduleFiles);
78      }
79  
80      /**
81       * Get files that represent modules.
82       *
83       * @param moduleFolders folders to check
84       * @param path          rootPath
85       * @return files for scrapping javadoc and generation of metadata files
86       * @throws IOException ioException
87       */
88      private static List<File> getTargetFiles(String path, String... moduleFolders)
89              throws IOException {
90          final List<File> validFiles = new ArrayList<>();
91          for (String folder : moduleFolders) {
92              try (Stream<Path> files = Files.walk(Path.of(path + "/" + folder))) {
93                  validFiles.addAll(
94                          files.map(Path::toFile)
95                          .filter(file -> {
96                              final String fileName = file.getName();
97                              return fileName.endsWith("SuppressWarningsHolder.java")
98                                      || fileName.endsWith("Check.java")
99                                      || fileName.endsWith("Filter.java");
100                         })
101                         .collect(Collectors.toUnmodifiableList()));
102             }
103         }
104 
105         return validFiles;
106     }
107 }