001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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) throws IOException {
059        final ClassPath classPath = ClassPath.from(loader);
060        return packages.stream()
061                .flatMap(pkg -> classPath.getTopLevelClasses(pkg).stream())
062                .map(ClassPath.ClassInfo::load)
063                .filter(ModuleReflectionUtil::isCheckstyleModule)
064                .collect(Collectors.toUnmodifiableSet());
065    }
066
067    /**
068     * Checks whether a class may be considered as a checkstyle module.
069     * Checkstyle's modules are classes which extend 'AutomaticBean', is
070     * non-abstract, and has a default constructor.
071     *
072     * @param clazz class to check.
073     * @return true if a class may be considered a valid production class.
074     */
075    public static boolean isCheckstyleModule(Class<?> clazz) {
076        return AbstractAutomaticBean.class.isAssignableFrom(clazz)
077                && !Modifier.isAbstract(clazz.getModifiers())
078                && hasDefaultConstructor(clazz)
079                && isNotXpathFileGenerator(clazz);
080    }
081
082    /**
083     * Checks if the class has a default constructor.
084     *
085     * @param clazz class to check
086     * @return true if the class has a default constructor.
087     */
088    private static boolean hasDefaultConstructor(Class<?> clazz) {
089        boolean result = false;
090        for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
091            if (constructor.getParameterCount() == 0) {
092                result = true;
093                break;
094            }
095        }
096        return result;
097    }
098
099    /**
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}