001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2021 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.meta; 021 022import java.io.File; 023import java.io.IOException; 024import java.nio.charset.StandardCharsets; 025import java.nio.file.Files; 026import java.nio.file.Path; 027import java.nio.file.Paths; 028import java.util.ArrayList; 029import java.util.Arrays; 030import java.util.List; 031import java.util.stream.Collectors; 032import java.util.stream.Stream; 033 034import com.puppycrawl.tools.checkstyle.Checker; 035import com.puppycrawl.tools.checkstyle.DefaultConfiguration; 036import com.puppycrawl.tools.checkstyle.TreeWalker; 037import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 038 039/** Class which handles all the metadata generation and writing calls. */ 040public final class MetadataGeneratorUtil { 041 042 /** Stop instances being created. **/ 043 private MetadataGeneratorUtil() { 044 } 045 046 /** 047 * Generate metadata from the module source files available in the input argument path. 048 * 049 * @param args arguments 050 * @throws IOException ioException 051 * @throws CheckstyleException checkstyleException 052 */ 053 public static void generate(String... args) throws IOException, CheckstyleException { 054 JavadocMetadataScraper.resetModuleDetailsStore(); 055 056 final Checker checker = new Checker(); 057 checker.setModuleClassLoader(Checker.class.getClassLoader()); 058 final DefaultConfiguration scraperCheckConfig = 059 new DefaultConfiguration(JavadocMetadataScraper.class.getName()); 060 final DefaultConfiguration defaultConfiguration = new DefaultConfiguration("configuration"); 061 final DefaultConfiguration treeWalkerConfig = 062 new DefaultConfiguration(TreeWalker.class.getName()); 063 defaultConfiguration.addProperty("charset", StandardCharsets.UTF_8.name()); 064 defaultConfiguration.addChild(treeWalkerConfig); 065 treeWalkerConfig.addChild(scraperCheckConfig); 066 checker.configure(defaultConfiguration); 067 dumpMetadata(checker, args[0]); 068 } 069 070 /** 071 * Process files using the checker passed and write to corresponding XML files. 072 * 073 * @param checker checker 074 * @param path rootPath 075 * @throws CheckstyleException checkstyleException 076 * @throws IOException ioException 077 */ 078 private static void dumpMetadata(Checker checker, String path) throws CheckstyleException, 079 IOException { 080 final List<File> validFiles = new ArrayList<>(); 081 final List<String> moduleFolders = Arrays.asList("checks", "filters", "filefilters"); 082 for (String folder : moduleFolders) { 083 try (Stream<Path> files = Files.walk(Paths.get(path 084 + "/" + folder))) { 085 validFiles.addAll( 086 files.map(Path::toFile) 087 .filter(file -> { 088 return file.getName().endsWith("SuppressWarningsHolder.java") 089 || file.getName().endsWith("Check.java") 090 || file.getName().endsWith("Filter.java"); 091 }) 092 .collect(Collectors.toList())); 093 } 094 } 095 096 checker.process(validFiles); 097 } 098}