001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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.utils; 021 022import java.io.IOException; 023import java.lang.reflect.Constructor; 024import java.lang.reflect.Modifier; 025import java.util.Collection; 026import java.util.Set; 027import java.util.stream.Collectors; 028 029import com.google.common.reflect.ClassPath; 030import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean; 031import com.puppycrawl.tools.checkstyle.TreeWalkerFilter; 032import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 033import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; 034import com.puppycrawl.tools.checkstyle.api.AuditListener; 035import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter; 036import com.puppycrawl.tools.checkstyle.api.Filter; 037import com.puppycrawl.tools.checkstyle.api.RootModule; 038 039/** 040 * Contains utility methods for module reflection. 041 */ 042public final class ModuleReflectionUtil { 043 044 /** Prevent instantiation. */ 045 private ModuleReflectionUtil() { 046 } 047 048 /** 049 * Gets checkstyle's modules classes (directly, not recursively) in the given packages. 050 * 051 * @param packages the collection of package names to use 052 * @param loader the class loader used to load Checkstyle package names 053 * @return the set of checkstyle's module classes 054 * @throws IOException if the attempt to read class path resources failed 055 * @see #isCheckstyleModule(Class) 056 */ 057 public static Set<Class<?>> getCheckstyleModules( 058 Collection<String> packages, ClassLoader loader) 059 throws IOException { 060 final ClassPath classPath = ClassPath.from(loader); 061 return packages.stream() 062 .flatMap(pkg -> classPath.getTopLevelClasses(pkg).stream()) 063 .map(ClassPath.ClassInfo::load) 064 .filter(ModuleReflectionUtil::isCheckstyleModule) 065 .collect(Collectors.toUnmodifiableSet()); 066 } 067 068 /** 069 * Checks whether a class may be considered as a checkstyle module. 070 * Checkstyle's modules are classes which extend 'AutomaticBean', is 071 * non-abstract, and has a default constructor. 072 * 073 * @param clazz class to check. 074 * @return true if a class may be considered a valid production class. 075 */ 076 public static boolean isCheckstyleModule(Class<?> clazz) { 077 return AbstractAutomaticBean.class.isAssignableFrom(clazz) 078 && !Modifier.isAbstract(clazz.getModifiers()) 079 && hasDefaultConstructor(clazz) 080 && isNotXpathFileGenerator(clazz); 081 } 082 083 /** 084 * Checks if the class has a default constructor. 085 * 086 * @param clazz class to check 087 * @return true if the class has a default constructor. 088 */ 089 private static boolean hasDefaultConstructor(Class<?> clazz) { 090 boolean result = false; 091 for (Constructor<?> constructor : clazz.getDeclaredConstructors()) { 092 if (constructor.getParameterCount() == 0) { 093 result = true; 094 break; 095 } 096 } 097 return result; 098 } 099 100 /** 101 * Checks whether a class may be considered as the checkstyle check 102 * which has TreeWalker as a parent. 103 * Checkstyle's checks are classes which implement 'AbstractCheck' interface. 104 * 105 * @param clazz class to check. 106 * @return true if a class may be considered as the checkstyle check. 107 */ 108 public static boolean isCheckstyleTreeWalkerCheck(Class<?> clazz) { 109 return AbstractCheck.class.isAssignableFrom(clazz); 110 } 111 112 /** 113 * Checks whether a class may be considered as the checkstyle file set. 114 * Checkstyle's file sets are classes which implement 'AbstractFileSetCheck' interface. 115 * 116 * @param clazz class to check. 117 * @return true if a class may be considered as the checkstyle file set. 118 */ 119 public static boolean isFileSetModule(Class<?> clazz) { 120 return AbstractFileSetCheck.class.isAssignableFrom(clazz); 121 } 122 123 /** 124 * Checks whether a class may be considered as the checkstyle filter. 125 * Checkstyle's filters are classes which implement 'Filter' interface. 126 * 127 * @param clazz class to check. 128 * @return true if a class may be considered as the checkstyle filter. 129 */ 130 public static boolean isFilterModule(Class<?> clazz) { 131 return Filter.class.isAssignableFrom(clazz); 132 } 133 134 /** 135 * Checks whether a class may be considered as the checkstyle file filter. 136 * Checkstyle's file filters are classes which implement 'BeforeExecutionFileFilter' interface. 137 * 138 * @param clazz class to check. 139 * @return true if a class may be considered as the checkstyle file filter. 140 */ 141 public static boolean isFileFilterModule(Class<?> clazz) { 142 return BeforeExecutionFileFilter.class.isAssignableFrom(clazz); 143 } 144 145 /** 146 * Checks whether a class may be considered as the checkstyle audit listener module. 147 * Checkstyle's audit listener modules are classes which implement 'AuditListener' interface. 148 * 149 * @param clazz class to check. 150 * @return true if a class may be considered as the checkstyle audit listener module. 151 */ 152 public static boolean isAuditListener(Class<?> clazz) { 153 return AuditListener.class.isAssignableFrom(clazz); 154 } 155 156 /** 157 * Checks whether a class may be considered as the checkstyle root module. 158 * Checkstyle's root modules are classes which implement 'RootModule' interface. 159 * 160 * @param clazz class to check. 161 * @return true if a class may be considered as the checkstyle root module. 162 */ 163 public static boolean isRootModule(Class<?> clazz) { 164 return RootModule.class.isAssignableFrom(clazz); 165 } 166 167 /** 168 * Checks whether a class may be considered as the checkstyle {@code TreeWalker} filter. 169 * Checkstyle's {@code TreeWalker} filters are classes which implement 'TreeWalkerFilter' 170 * interface. 171 * 172 * @param clazz class to check. 173 * @return true if a class may be considered as the checkstyle {@code TreeWalker} filter. 174 */ 175 public static boolean isTreeWalkerFilterModule(Class<?> clazz) { 176 return TreeWalkerFilter.class.isAssignableFrom(clazz); 177 } 178 179 /** 180 * Checks whether a class is {@code XpathFileGeneratorAstFilter} or 181 * {@code XpathFileGeneratorAuditListener}. 182 * See issue <a href="https://github.com/checkstyle/checkstyle/issues/102">#102</a> 183 * 184 * @param clazz class to check. 185 * @return true if a class name starts with `XpathFileGenerator`. 186 */ 187 private static boolean isNotXpathFileGenerator(Class<?> clazz) { 188 return !clazz.getSimpleName().startsWith("XpathFileGenerator"); 189 } 190 191}