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