1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
40 public final class MetadataGeneratorUtil {
41
42
43 private MetadataGeneratorUtil() {
44 }
45
46
47
48
49
50
51
52
53
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
81
82
83
84
85
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 }