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.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
42 public final class MetadataGeneratorUtil {
43
44
45 private MetadataGeneratorUtil() {
46 }
47
48
49
50
51
52
53
54
55
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
83
84
85
86
87
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 }