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.utils;
21
22 import java.io.IOException;
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.Modifier;
25 import java.util.Collection;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28
29 import com.google.common.reflect.ClassPath;
30 import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean;
31 import com.puppycrawl.tools.checkstyle.TreeWalkerFilter;
32 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
33 import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
34 import com.puppycrawl.tools.checkstyle.api.AuditListener;
35 import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter;
36 import com.puppycrawl.tools.checkstyle.api.Filter;
37 import com.puppycrawl.tools.checkstyle.api.RootModule;
38
39 /**
40 * Contains utility methods for module reflection.
41 */
42 public final class ModuleReflectionUtil {
43
44 /** Prevent instantiation. */
45 private ModuleReflectionUtil() {
46 }
47
48 /**
49 * Gets checkstyle's modules classes (directly, not recursively) in the given packages.
50 *
51 * @param packages the collection of package names to use
52 * @param loader the class loader used to load Checkstyle package names
53 * @return the set of checkstyle's module classes
54 * @throws IOException if the attempt to read class path resources failed
55 * @see #isCheckstyleModule(Class)
56 */
57 public static Set<Class<?>> getCheckstyleModules(
58 Collection<String> packages, ClassLoader loader) throws IOException {
59 final ClassPath classPath = ClassPath.from(loader);
60 return packages.stream()
61 .flatMap(pkg -> classPath.getTopLevelClasses(pkg).stream())
62 .map(ClassPath.ClassInfo::load)
63 .filter(ModuleReflectionUtil::isCheckstyleModule)
64 .collect(Collectors.toUnmodifiableSet());
65 }
66
67 /**
68 * Checks whether a class may be considered as a checkstyle module.
69 * Checkstyle's modules are classes which extend 'AutomaticBean', is
70 * non-abstract, and has a default constructor.
71 *
72 * @param clazz class to check.
73 * @return true if a class may be considered a valid production class.
74 */
75 public static boolean isCheckstyleModule(Class<?> clazz) {
76 return AbstractAutomaticBean.class.isAssignableFrom(clazz)
77 && !Modifier.isAbstract(clazz.getModifiers())
78 && hasDefaultConstructor(clazz)
79 && isNotXpathFileGenerator(clazz);
80 }
81
82 /**
83 * Checks if the class has a default constructor.
84 *
85 * @param clazz class to check
86 * @return true if the class has a default constructor.
87 */
88 private static boolean hasDefaultConstructor(Class<?> clazz) {
89 boolean result = false;
90 for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
91 if (constructor.getParameterCount() == 0) {
92 result = true;
93 break;
94 }
95 }
96 return result;
97 }
98
99 /**
100 * Checks whether a class may be considered as the checkstyle check
101 * which has TreeWalker as a parent.
102 * Checkstyle's checks are classes which implement 'AbstractCheck' interface.
103 *
104 * @param clazz class to check.
105 * @return true if a class may be considered as the checkstyle check.
106 */
107 public static boolean isCheckstyleTreeWalkerCheck(Class<?> clazz) {
108 return AbstractCheck.class.isAssignableFrom(clazz);
109 }
110
111 /**
112 * Checks whether a class may be considered as the checkstyle file set.
113 * Checkstyle's file sets are classes which implement 'AbstractFileSetCheck' interface.
114 *
115 * @param clazz class to check.
116 * @return true if a class may be considered as the checkstyle file set.
117 */
118 public static boolean isFileSetModule(Class<?> clazz) {
119 return AbstractFileSetCheck.class.isAssignableFrom(clazz);
120 }
121
122 /**
123 * Checks whether a class may be considered as the checkstyle filter.
124 * Checkstyle's filters are classes which implement 'Filter' interface.
125 *
126 * @param clazz class to check.
127 * @return true if a class may be considered as the checkstyle filter.
128 */
129 public static boolean isFilterModule(Class<?> clazz) {
130 return Filter.class.isAssignableFrom(clazz);
131 }
132
133 /**
134 * Checks whether a class may be considered as the checkstyle file filter.
135 * Checkstyle's file filters are classes which implement 'BeforeExecutionFileFilter' interface.
136 *
137 * @param clazz class to check.
138 * @return true if a class may be considered as the checkstyle file filter.
139 */
140 public static boolean isFileFilterModule(Class<?> clazz) {
141 return BeforeExecutionFileFilter.class.isAssignableFrom(clazz);
142 }
143
144 /**
145 * Checks whether a class may be considered as the checkstyle audit listener module.
146 * Checkstyle's audit listener modules are classes which implement 'AuditListener' interface.
147 *
148 * @param clazz class to check.
149 * @return true if a class may be considered as the checkstyle audit listener module.
150 */
151 public static boolean isAuditListener(Class<?> clazz) {
152 return AuditListener.class.isAssignableFrom(clazz);
153 }
154
155 /**
156 * Checks whether a class may be considered as the checkstyle root module.
157 * Checkstyle's root modules are classes which implement 'RootModule' interface.
158 *
159 * @param clazz class to check.
160 * @return true if a class may be considered as the checkstyle root module.
161 */
162 public static boolean isRootModule(Class<?> clazz) {
163 return RootModule.class.isAssignableFrom(clazz);
164 }
165
166 /**
167 * Checks whether a class may be considered as the checkstyle {@code TreeWalker} filter.
168 * Checkstyle's {@code TreeWalker} filters are classes which implement 'TreeWalkerFilter'
169 * interface.
170 *
171 * @param clazz class to check.
172 * @return true if a class may be considered as the checkstyle {@code TreeWalker} filter.
173 */
174 public static boolean isTreeWalkerFilterModule(Class<?> clazz) {
175 return TreeWalkerFilter.class.isAssignableFrom(clazz);
176 }
177
178 /**
179 * Checks whether a class is {@code XpathFileGeneratorAstFilter} or
180 * {@code XpathFileGeneratorAuditListener}.
181 * See issue <a href="https://github.com/checkstyle/checkstyle/issues/102">#102</a>
182 *
183 * @param clazz class to check.
184 * @return true if a class name starts with `XpathFileGenerator`.
185 */
186 private static boolean isNotXpathFileGenerator(Class<?> clazz) {
187 return !clazz.getSimpleName().startsWith("XpathFileGenerator");
188 }
189 }