-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ModuleReflectionUtil.java
235 lines (214 loc) · 9.47 KB
/
ModuleReflectionUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2022 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.utils;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import com.google.common.reflect.ClassPath;
import com.puppycrawl.tools.checkstyle.TreeWalkerFilter;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.AuditListener;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter;
import com.puppycrawl.tools.checkstyle.api.Filter;
import com.puppycrawl.tools.checkstyle.api.RootModule;
/**
* Contains utility methods for module reflection.
*/
public final class ModuleReflectionUtil {
/** Default identification predicate. */
private static final Predicate<Class<?>> DEFAULT_IDENTIFICATION_PREDICATE = clazz -> {
return isCheckstyleTreeWalkerCheck(clazz)
|| isFileSetModule(clazz)
|| isFilterModule(clazz)
|| isFileFilterModule(clazz)
|| isTreeWalkerFilterModule(clazz)
|| isAuditListener(clazz)
|| isRootModule(clazz);
};
/** Current identification predicate. */
private static Predicate<Class<?>> identificationPredicate = DEFAULT_IDENTIFICATION_PREDICATE;
/** Prevent instantiation. */
private ModuleReflectionUtil() {
}
/**
* Allows Checkstyle to understand more modules to work with it via a
* predicate. This creates a chain of predicates for identification
* purposes. If a prior predicate fails to identify a module, it will
* continue next on the chain until all say the class cannot be identified.
*
* @param newPredicate The provided predicate that will return {@code true}
* if the class given to it is identified as a type of module,
* otherwise {@code false} if it does not.
*/
public static void addNewModuleIdentification(Predicate<Class<?>> newPredicate) {
identificationPredicate = newPredicate.or(identificationPredicate);
}
/** Resets the identification process Checkstyle uses to know if a class is a module. */
public static void resetIdentification() {
identificationPredicate = DEFAULT_IDENTIFICATION_PREDICATE;
}
/**
* Gets checkstyle's modules (directly, not recursively) in the given packages.
*
* @param packages the collection of package names to use
* @param loader the class loader used to load Checkstyle package names
* @return the set of checkstyle's module classes
* @throws IOException if the attempt to read class path resources failed
* @see #isCheckstyleModule(Class)
*/
public static Set<Class<?>> getCheckstyleModules(
Collection<String> packages, ClassLoader loader) throws IOException {
final ClassPath classPath = ClassPath.from(loader);
return packages.stream()
.flatMap(pkg -> classPath.getTopLevelClasses(pkg).stream())
.map(ClassPath.ClassInfo::load)
.filter(ModuleReflectionUtil::isCheckstyleModule)
.collect(Collectors.toSet());
}
/**
* Checks whether a class may be considered as a checkstyle module. Checkstyle's modules are
* non-abstract classes, which are either checkstyle's checks, file sets, filters, file filters,
* {@code TreeWalker} filters, audit listener, or root module.
*
* @param clazz class to check.
* @return true if the class may be considered as the checkstyle module.
*/
public static boolean isCheckstyleModule(Class<?> clazz) {
return isValidCheckstyleClass(clazz)
&& identificationPredicate.test(clazz);
}
/**
* Checks whether a class extends 'AutomaticBean', is non-abstract, and has a default
* constructor.
*
* @param clazz class to check.
* @return true if a class may be considered a valid production class.
*/
public static boolean isValidCheckstyleClass(Class<?> clazz) {
return AutomaticBean.class.isAssignableFrom(clazz)
&& !Modifier.isAbstract(clazz.getModifiers())
&& hasDefaultConstructor(clazz)
&& isNotXpathFileGenerator(clazz);
}
/**
* Checks if the class has a default constructor.
*
* @param clazz class to check
* @return true if the class has a default constructor.
*/
private static boolean hasDefaultConstructor(Class<?> clazz) {
boolean result = false;
for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
if (constructor.getParameterCount() == 0) {
result = true;
break;
}
}
return result;
}
/**
* Checks whether a class may be considered as the checkstyle check
* which has TreeWalker as a parent.
* Checkstyle's checks are classes which implement 'AbstractCheck' interface.
*
* @param clazz class to check.
* @return true if a class may be considered as the checkstyle check.
*/
public static boolean isCheckstyleTreeWalkerCheck(Class<?> clazz) {
return AbstractCheck.class.isAssignableFrom(clazz);
}
/**
* Checks whether a class may be considered as the checkstyle file set.
* Checkstyle's file sets are classes which implement 'AbstractFileSetCheck' interface.
*
* @param clazz class to check.
* @return true if a class may be considered as the checkstyle file set.
*/
public static boolean isFileSetModule(Class<?> clazz) {
return AbstractFileSetCheck.class.isAssignableFrom(clazz);
}
/**
* Checks whether a class may be considered as the checkstyle filter.
* Checkstyle's filters are classes which implement 'Filter' interface.
*
* @param clazz class to check.
* @return true if a class may be considered as the checkstyle filter.
*/
public static boolean isFilterModule(Class<?> clazz) {
return Filter.class.isAssignableFrom(clazz);
}
/**
* Checks whether a class may be considered as the checkstyle file filter.
* Checkstyle's file filters are classes which implement 'BeforeExecutionFileFilter' interface.
*
* @param clazz class to check.
* @return true if a class may be considered as the checkstyle file filter.
*/
public static boolean isFileFilterModule(Class<?> clazz) {
return BeforeExecutionFileFilter.class.isAssignableFrom(clazz);
}
/**
* Checks whether a class may be considered as the checkstyle audit listener module.
* Checkstyle's audit listener modules are classes which implement 'AuditListener' interface.
*
* @param clazz class to check.
* @return true if a class may be considered as the checkstyle audit listener module.
*/
public static boolean isAuditListener(Class<?> clazz) {
return AuditListener.class.isAssignableFrom(clazz);
}
/**
* Checks whether a class may be considered as the checkstyle root module.
* Checkstyle's root modules are classes which implement 'RootModule' interface.
*
* @param clazz class to check.
* @return true if a class may be considered as the checkstyle root module.
*/
public static boolean isRootModule(Class<?> clazz) {
return RootModule.class.isAssignableFrom(clazz);
}
/**
* Checks whether a class may be considered as the checkstyle {@code TreeWalker} filter.
* Checkstyle's {@code TreeWalker} filters are classes which implement 'TreeWalkerFilter'
* interface.
*
* @param clazz class to check.
* @return true if a class may be considered as the checkstyle {@code TreeWalker} filter.
*/
public static boolean isTreeWalkerFilterModule(Class<?> clazz) {
return TreeWalkerFilter.class.isAssignableFrom(clazz);
}
/**
* Checks whether a class is {@code XpathFileGeneratorAstFilter} or
* {@code XpathFileGeneratorAuditListener}.
* See issue #102 https://github.com/checkstyle/checkstyle/issues/102
*
* @param clazz class to check.
* @return true if a class name starts with `XpathFileGenerator`.
*/
private static boolean isNotXpathFileGenerator(Class<?> clazz) {
return !clazz.getSimpleName().startsWith("XpathFileGenerator");
}
}