001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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.site;
021
022import java.beans.PropertyDescriptor;
023import java.io.File;
024import java.io.IOException;
025import java.lang.module.ModuleDescriptor.Version;
026import java.lang.reflect.Array;
027import java.lang.reflect.Field;
028import java.lang.reflect.InvocationTargetException;
029import java.lang.reflect.ParameterizedType;
030import java.net.URI;
031import java.nio.charset.StandardCharsets;
032import java.nio.file.Files;
033import java.nio.file.Path;
034import java.util.ArrayDeque;
035import java.util.ArrayList;
036import java.util.Arrays;
037import java.util.BitSet;
038import java.util.Collection;
039import java.util.Deque;
040import java.util.HashMap;
041import java.util.HashSet;
042import java.util.LinkedHashMap;
043import java.util.List;
044import java.util.Locale;
045import java.util.Map;
046import java.util.Objects;
047import java.util.Optional;
048import java.util.Set;
049import java.util.TreeSet;
050import java.util.regex.Pattern;
051import java.util.stream.Collectors;
052import java.util.stream.IntStream;
053import java.util.stream.Stream;
054
055import javax.annotation.Nullable;
056
057import org.apache.commons.beanutils.PropertyUtils;
058import org.apache.maven.doxia.macro.MacroExecutionException;
059
060import com.google.common.collect.Lists;
061import com.puppycrawl.tools.checkstyle.Checker;
062import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
063import com.puppycrawl.tools.checkstyle.ModuleFactory;
064import com.puppycrawl.tools.checkstyle.PackageNamesLoader;
065import com.puppycrawl.tools.checkstyle.PackageObjectFactory;
066import com.puppycrawl.tools.checkstyle.PropertyCacheFile;
067import com.puppycrawl.tools.checkstyle.PropertyType;
068import com.puppycrawl.tools.checkstyle.TreeWalker;
069import com.puppycrawl.tools.checkstyle.TreeWalkerFilter;
070import com.puppycrawl.tools.checkstyle.XdocsPropertyType;
071import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
072import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
073import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter;
074import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
075import com.puppycrawl.tools.checkstyle.api.DetailNode;
076import com.puppycrawl.tools.checkstyle.api.Filter;
077import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
078import com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck;
079import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption;
080import com.puppycrawl.tools.checkstyle.checks.regexp.RegexpMultilineCheck;
081import com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck;
082import com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck;
083import com.puppycrawl.tools.checkstyle.meta.JavadocMetadataScraperUtil;
084import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
085import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
086import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
087
088/**
089 * Utility class for site generation.
090 */
091public final class SiteUtil {
092
093    /** The string 'tokens'. */
094    public static final String TOKENS = "tokens";
095    /** The string 'javadocTokens'. */
096    public static final String JAVADOC_TOKENS = "javadocTokens";
097    /** The string '.'. */
098    public static final String DOT = ".";
099    /** The string ','. */
100    public static final String COMMA = ",";
101    /** The whitespace. */
102    public static final String WHITESPACE = " ";
103    /** The string ', '. */
104    public static final String COMMA_SPACE = COMMA + WHITESPACE;
105    /** The string 'TokenTypes'. */
106    public static final String TOKEN_TYPES = "TokenTypes";
107    /** The path to the TokenTypes.html file. */
108    public static final String PATH_TO_TOKEN_TYPES =
109            "apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html";
110    /** The path to the JavadocTokenTypes.html file. */
111    public static final String PATH_TO_JAVADOC_TOKEN_TYPES =
112            "apidocs/com/puppycrawl/tools/checkstyle/api/JavadocTokenTypes.html";
113    /** The string of JavaDoc module marking 'Since version'. */
114    public static final String SINCE_VERSION = "Since version";
115    /** The 'Check' pattern at the end of string. */
116    public static final Pattern FINAL_CHECK = Pattern.compile("Check$");
117    /** The string 'fileExtensions'. */
118    public static final String FILE_EXTENSIONS = "fileExtensions";
119    /** The string 'charset'. */
120    public static final String CHARSET = "charset";
121    /** The url of the checkstyle website. */
122    private static final String CHECKSTYLE_ORG_URL = "https://checkstyle.org/";
123    /** The string 'checks'. */
124    private static final String CHECKS = "checks";
125    /** The string 'naming'. */
126    private static final String NAMING = "naming";
127    /** The string 'src'. */
128    private static final String SRC = "src";
129    /** Template file extension. */
130    private static final String TEMPLATE_FILE_EXTENSION = ".xml.template";
131
132    /** Precompiled regex pattern to remove the "Setter to " prefix from strings. */
133    private static final Pattern SETTER_PATTERN = Pattern.compile("^Setter to ");
134
135    /** Class name and their corresponding parent module name. */
136    private static final Map<Class<?>, String> CLASS_TO_PARENT_MODULE = Map.ofEntries(
137        Map.entry(AbstractCheck.class, TreeWalker.class.getSimpleName()),
138        Map.entry(TreeWalkerFilter.class, TreeWalker.class.getSimpleName()),
139        Map.entry(AbstractFileSetCheck.class, Checker.class.getSimpleName()),
140        Map.entry(Filter.class, Checker.class.getSimpleName()),
141        Map.entry(BeforeExecutionFileFilter.class, Checker.class.getSimpleName())
142    );
143
144    /** Set of properties that every check has. */
145    private static final Set<String> CHECK_PROPERTIES =
146            getProperties(AbstractCheck.class);
147
148    /** Set of properties that every Javadoc check has. */
149    private static final Set<String> JAVADOC_CHECK_PROPERTIES =
150            getProperties(AbstractJavadocCheck.class);
151
152    /** Set of properties that every FileSet check has. */
153    private static final Set<String> FILESET_PROPERTIES =
154            getProperties(AbstractFileSetCheck.class);
155
156    /**
157     * Check and property name.
158     */
159    private static final String HEADER_CHECK_HEADER = "HeaderCheck.header";
160
161    /**
162     * Check and property name.
163     */
164    private static final String REGEXP_HEADER_CHECK_HEADER = "RegexpHeaderCheck.header";
165
166    /** Set of properties that are undocumented. Those are internal properties. */
167    private static final Set<String> UNDOCUMENTED_PROPERTIES = Set.of(
168        "SuppressWithNearbyCommentFilter.fileContents",
169        "SuppressionCommentFilter.fileContents"
170    );
171
172    /** Properties that can not be gathered from class instance. */
173    private static final Set<String> PROPERTIES_ALLOWED_GET_TYPES_FROM_METHOD = Set.of(
174        // static field (all upper case)
175        "SuppressWarningsHolder.aliasList",
176        // loads string into memory similar to file
177        HEADER_CHECK_HEADER,
178        REGEXP_HEADER_CHECK_HEADER,
179        // property is an int, but we cut off excess to accommodate old versions
180        "RedundantModifierCheck.jdkVersion",
181        // until https://github.com/checkstyle/checkstyle/issues/13376
182        "CustomImportOrderCheck.customImportOrderRules"
183    );
184
185    /** Map of all superclasses properties and their javadocs. */
186    private static final Map<String, DetailNode> SUPER_CLASS_PROPERTIES_JAVADOCS =
187            new HashMap<>();
188
189    /** Path to main source code folder. */
190    private static final String MAIN_FOLDER_PATH = Path.of(
191            SRC, "main", "java", "com", "puppycrawl", "tools", "checkstyle").toString();
192
193    /** List of files who are superclasses and contain certain properties that checks inherit. */
194    private static final List<Path> MODULE_SUPER_CLASS_PATHS = List.of(
195        Path.of(MAIN_FOLDER_PATH, CHECKS, NAMING, "AbstractAccessControlNameCheck.java"),
196        Path.of(MAIN_FOLDER_PATH, CHECKS, NAMING, "AbstractNameCheck.java"),
197        Path.of(MAIN_FOLDER_PATH, CHECKS, "javadoc", "AbstractJavadocCheck.java"),
198        Path.of(MAIN_FOLDER_PATH, "api", "AbstractFileSetCheck.java"),
199        Path.of(MAIN_FOLDER_PATH, CHECKS, "header", "AbstractHeaderCheck.java"),
200        Path.of(MAIN_FOLDER_PATH, CHECKS, "metrics", "AbstractClassCouplingCheck.java"),
201        Path.of(MAIN_FOLDER_PATH, CHECKS, "whitespace", "AbstractParenPadCheck.java")
202    );
203
204    /**
205     * Private utility constructor.
206     */
207    private SiteUtil() {
208    }
209
210    /**
211     * Get string values of the message keys from the given check class.
212     *
213     * @param module class to examine.
214     * @return a set of checkstyle's module message keys.
215     * @throws MacroExecutionException if extraction of message keys fails.
216     */
217    public static Set<String> getMessageKeys(Class<?> module)
218            throws MacroExecutionException {
219        final Set<Field> messageKeyFields = getCheckMessageKeysFields(module);
220        // We use a TreeSet to sort the message keys alphabetically
221        final Set<String> messageKeys = new TreeSet<>();
222        for (Field field : messageKeyFields) {
223            messageKeys.add(getFieldValue(field, module).toString());
224        }
225        return messageKeys;
226    }
227
228    /**
229     * Gets the check's messages keys.
230     *
231     * @param module class to examine.
232     * @return a set of checkstyle's module message fields.
233     * @throws MacroExecutionException if the attempt to read a protected class fails.
234     * @noinspection ChainOfInstanceofChecks
235     * @noinspectionreason ChainOfInstanceofChecks - We will deal with this at
236     *                     <a href="https://github.com/checkstyle/checkstyle/issues/13500">13500</a>
237     *
238     */
239    private static Set<Field> getCheckMessageKeysFields(Class<?> module)
240            throws MacroExecutionException {
241        try {
242            final Set<Field> checkstyleMessages = new HashSet<>();
243
244            // get all fields from current class
245            final Field[] fields = module.getDeclaredFields();
246
247            for (Field field : fields) {
248                if (field.getName().startsWith("MSG_")) {
249                    checkstyleMessages.add(field);
250                }
251            }
252
253            // deep scan class through hierarchy
254            final Class<?> superModule = module.getSuperclass();
255
256            if (superModule != null) {
257                checkstyleMessages.addAll(getCheckMessageKeysFields(superModule));
258            }
259
260            // special cases that require additional classes
261            if (module == RegexpMultilineCheck.class) {
262                checkstyleMessages.addAll(getCheckMessageKeysFields(Class
263                    .forName("com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector")));
264            }
265            else if (module == RegexpSinglelineCheck.class
266                    || module == RegexpSinglelineJavaCheck.class) {
267                checkstyleMessages.addAll(getCheckMessageKeysFields(Class
268                    .forName("com.puppycrawl.tools.checkstyle.checks.regexp.SinglelineDetector")));
269            }
270
271            return checkstyleMessages;
272        }
273        catch (ClassNotFoundException exc) {
274            final String message = String.format(Locale.ROOT, "Couldn't find class: %s",
275                    module.getName());
276            throw new MacroExecutionException(message, exc);
277        }
278    }
279
280    /**
281     * Returns the value of the given field.
282     *
283     * @param field the field.
284     * @param instance the instance of the module.
285     * @return the value of the field.
286     * @throws MacroExecutionException if the value could not be retrieved.
287     */
288    public static Object getFieldValue(Field field, Object instance)
289            throws MacroExecutionException {
290        try {
291            Object fieldValue = null;
292
293            if (field != null) {
294                // required for package/private classes
295                field.trySetAccessible();
296                fieldValue = field.get(instance);
297            }
298
299            return fieldValue;
300        }
301        catch (IllegalAccessException exc) {
302            throw new MacroExecutionException("Couldn't get field value", exc);
303        }
304    }
305
306    /**
307     * Returns the instance of the module with the given name.
308     *
309     * @param moduleName the name of the module.
310     * @return the instance of the module.
311     * @throws MacroExecutionException if the module could not be created.
312     */
313    public static Object getModuleInstance(String moduleName) throws MacroExecutionException {
314        final ModuleFactory factory = getPackageObjectFactory();
315        try {
316            return factory.createModule(moduleName);
317        }
318        catch (CheckstyleException exc) {
319            throw new MacroExecutionException("Couldn't find class: " + moduleName, exc);
320        }
321    }
322
323    /**
324     * Returns the default PackageObjectFactory with the default package names.
325     *
326     * @return the default PackageObjectFactory.
327     * @throws MacroExecutionException if the PackageObjectFactory cannot be created.
328     */
329    private static PackageObjectFactory getPackageObjectFactory() throws MacroExecutionException {
330        try {
331            final ClassLoader cl = ViolationMessagesMacro.class.getClassLoader();
332            final Set<String> packageNames = PackageNamesLoader.getPackageNames(cl);
333            return new PackageObjectFactory(packageNames, cl);
334        }
335        catch (CheckstyleException exc) {
336            throw new MacroExecutionException("Couldn't load checkstyle modules", exc);
337        }
338    }
339
340    /**
341     * Construct a string with a leading newline character and followed by
342     * the given amount of spaces. We use this method only to match indentation in
343     * regular xdocs and have minimal diff when parsing the templates.
344     * This method exists until
345     * <a href="https://github.com/checkstyle/checkstyle/issues/13426">13426</a>
346     *
347     * @param amountOfSpaces the amount of spaces to add after the newline.
348     * @return the constructed string.
349     */
350    public static String getNewlineAndIndentSpaces(int amountOfSpaces) {
351        return System.lineSeparator() + WHITESPACE.repeat(amountOfSpaces);
352    }
353
354    /**
355     * Returns path to the template for the given module name or throws an exception if the
356     * template cannot be found.
357     *
358     * @param moduleName the module whose template we are looking for.
359     * @return path to the template.
360     * @throws MacroExecutionException if the template cannot be found.
361     */
362    public static Path getTemplatePath(String moduleName) throws MacroExecutionException {
363        final String fileNamePattern = ".*[\\\\/]"
364                + moduleName.toLowerCase(Locale.ROOT) + "\\..*";
365        return getXdocsTemplatesFilePaths()
366            .stream()
367            .filter(path -> path.toString().matches(fileNamePattern))
368            .findFirst()
369            .orElse(null);
370    }
371
372    /**
373     * Gets xdocs template file paths. These are files ending with .xml.template.
374     * This method will be changed to gather .xml once
375     * <a href="https://github.com/checkstyle/checkstyle/issues/13426">#13426</a> is resolved.
376     *
377     * @return a set of xdocs template file paths.
378     * @throws MacroExecutionException if an I/O error occurs.
379     */
380    public static Set<Path> getXdocsTemplatesFilePaths() throws MacroExecutionException {
381        final Path directory = Path.of("src/site/xdoc");
382        try (Stream<Path> stream = Files.find(directory, Integer.MAX_VALUE,
383                (path, attr) -> {
384                    return attr.isRegularFile()
385                            && path.toString().endsWith(TEMPLATE_FILE_EXTENSION);
386                })) {
387            return stream.collect(Collectors.toUnmodifiableSet());
388        }
389        catch (IOException ioException) {
390            throw new MacroExecutionException("Failed to find xdocs templates", ioException);
391        }
392    }
393
394    /**
395     * Returns the parent module name for the given module class. Returns either
396     * "TreeWalker" or "Checker". Returns null if the module class is null.
397     *
398     * @param moduleClass the module class.
399     * @return the parent module name as a string.
400     * @throws MacroExecutionException if the parent module cannot be found.
401     */
402    public static String getParentModule(Class<?> moduleClass)
403                throws MacroExecutionException {
404        String parentModuleName = "";
405        Class<?> parentClass = moduleClass.getSuperclass();
406
407        while (parentClass != null) {
408            parentModuleName = CLASS_TO_PARENT_MODULE.get(parentClass);
409            if (parentModuleName != null) {
410                break;
411            }
412            parentClass = parentClass.getSuperclass();
413        }
414
415        // If parent class is not found, check interfaces
416        if (parentModuleName == null || parentModuleName.isEmpty()) {
417            final Class<?>[] interfaces = moduleClass.getInterfaces();
418            for (Class<?> interfaceClass : interfaces) {
419                parentModuleName = CLASS_TO_PARENT_MODULE.get(interfaceClass);
420                if (parentModuleName != null) {
421                    break;
422                }
423            }
424        }
425
426        if (parentModuleName == null || parentModuleName.isEmpty()) {
427            final String message = String.format(Locale.ROOT,
428                    "Failed to find parent module for %s", moduleClass.getSimpleName());
429            throw new MacroExecutionException(message);
430        }
431
432        return parentModuleName;
433    }
434
435    /**
436     * Get a set of properties for the given class that should be documented.
437     *
438     * @param clss the class to get the properties for.
439     * @param instance the instance of the module.
440     * @return a set of properties for the given class.
441     */
442    public static Set<String> getPropertiesForDocumentation(Class<?> clss, Object instance) {
443        final Set<String> properties =
444                getProperties(clss).stream()
445                    .filter(prop -> {
446                        return !isGlobalProperty(clss, prop) && !isUndocumentedProperty(clss, prop);
447                    })
448                    .collect(Collectors.toCollection(HashSet::new));
449        properties.addAll(getNonExplicitProperties(instance, clss));
450        return new TreeSet<>(properties);
451    }
452
453    /**
454     * Gets the javadoc of module class.
455     *
456     * @param moduleClassName name of module class.
457     * @param modulePath module's path.
458     * @return javadoc of module.
459     * @throws MacroExecutionException if an error occurs during processing.
460     */
461    public static DetailNode getModuleJavadoc(String moduleClassName, Path modulePath)
462            throws MacroExecutionException {
463
464        processModule(moduleClassName, modulePath);
465        return JavadocScraperResultUtil.getModuleJavadocNode();
466    }
467
468    /**
469     * Get the javadocs of the properties of the module. If the property is not present in the
470     * module, then the javadoc of the property from the superclass(es) is used.
471     *
472     * @param properties the properties of the module.
473     * @param moduleName the name of the module.
474     * @param modulePath the module file path.
475     * @return the javadocs of the properties of the module.
476     * @throws MacroExecutionException if an error occurs during processing.
477     */
478    public static Map<String, DetailNode> getPropertiesJavadocs(Set<String> properties,
479                                                                String moduleName, Path modulePath)
480            throws MacroExecutionException {
481        // lazy initialization
482        if (SUPER_CLASS_PROPERTIES_JAVADOCS.isEmpty()) {
483            processSuperclasses();
484        }
485
486        processModule(moduleName, modulePath);
487
488        final Map<String, DetailNode> unmodifiablePropertiesJavadocs =
489                JavadocScraperResultUtil.getPropertiesJavadocNode();
490        final Map<String, DetailNode> propertiesJavadocs =
491            new LinkedHashMap<>(unmodifiablePropertiesJavadocs);
492
493        properties.forEach(property -> {
494            final DetailNode superClassPropertyJavadoc =
495                    SUPER_CLASS_PROPERTIES_JAVADOCS.get(property);
496            if (superClassPropertyJavadoc != null) {
497                propertiesJavadocs.putIfAbsent(property, superClassPropertyJavadoc);
498            }
499        });
500
501        assertAllPropertySetterJavadocsAreFound(properties, moduleName, propertiesJavadocs);
502
503        return propertiesJavadocs;
504    }
505
506    /**
507     * Assert that each property has a corresponding setter javadoc that is not null.
508     * 'tokens' and 'javadocTokens' are excluded from this check, because their
509     * description is different from the description of the setter.
510     *
511     * @param properties the properties of the module.
512     * @param moduleName the name of the module.
513     * @param javadocs the javadocs of the properties of the module.
514     * @throws MacroExecutionException if an error occurs during processing.
515     */
516    private static void assertAllPropertySetterJavadocsAreFound(
517            Set<String> properties, String moduleName, Map<String, DetailNode> javadocs)
518            throws MacroExecutionException {
519        for (String property : properties) {
520            final boolean isDocumented = javadocs.containsKey(property)
521                   || SUPER_CLASS_PROPERTIES_JAVADOCS.containsKey(property)
522                   || TOKENS.equals(property) || JAVADOC_TOKENS.equals(property);
523            if (!isDocumented) {
524                throw new MacroExecutionException(String.format(Locale.ROOT,
525                   "%s: Missing documentation for property '%s'. Check superclasses.",
526                        moduleName, property));
527            }
528        }
529    }
530
531    /**
532     * Collect the properties setters javadocs of the superclasses.
533     *
534     * @throws MacroExecutionException if an error occurs during processing.
535     */
536    private static void processSuperclasses() throws MacroExecutionException {
537        for (Path superclassPath : MODULE_SUPER_CLASS_PATHS) {
538            final Path fileNamePath = superclassPath.getFileName();
539            if (fileNamePath == null) {
540                throw new MacroExecutionException("Invalid superclass path: " + superclassPath);
541            }
542            final String superclassName = CommonUtil.getFileNameWithoutExtension(
543                fileNamePath.toString());
544            processModule(superclassName, superclassPath);
545            final Map<String, DetailNode> superclassPropertiesJavadocs =
546                JavadocScraperResultUtil.getPropertiesJavadocNode();
547            SUPER_CLASS_PROPERTIES_JAVADOCS.putAll(superclassPropertiesJavadocs);
548        }
549    }
550
551    /**
552     * Scrape the Javadocs of the class and its properties setters with
553     * ClassAndPropertiesSettersJavadocScraper.
554     *
555     * @param moduleName the name of the module.
556     * @param modulePath the module Path.
557     * @throws MacroExecutionException if an error occurs during processing.
558     */
559    private static void processModule(String moduleName, Path modulePath)
560            throws MacroExecutionException {
561        if (!Files.isRegularFile(modulePath)) {
562            final String message = String.format(Locale.ROOT,
563                    "File %s is not a file. Please check the 'modulePath' property.", modulePath);
564            throw new MacroExecutionException(message);
565        }
566        ClassAndPropertiesSettersJavadocScraper.initialize(moduleName);
567        final Checker checker = new Checker();
568        checker.setModuleClassLoader(Checker.class.getClassLoader());
569        final DefaultConfiguration scraperCheckConfig =
570                        new DefaultConfiguration(
571                                ClassAndPropertiesSettersJavadocScraper.class.getName());
572        final DefaultConfiguration defaultConfiguration =
573                new DefaultConfiguration("configuration");
574        final DefaultConfiguration treeWalkerConfig =
575                new DefaultConfiguration(TreeWalker.class.getName());
576        defaultConfiguration.addProperty(CHARSET, StandardCharsets.UTF_8.name());
577        defaultConfiguration.addChild(treeWalkerConfig);
578        treeWalkerConfig.addChild(scraperCheckConfig);
579        try {
580            checker.configure(defaultConfiguration);
581            final List<File> filesToProcess = List.of(modulePath.toFile());
582            checker.process(filesToProcess);
583            checker.destroy();
584        }
585        catch (CheckstyleException checkstyleException) {
586            final String message = String.format(Locale.ROOT, "Failed processing %s", moduleName);
587            throw new MacroExecutionException(message, checkstyleException);
588        }
589    }
590
591    /**
592     * Get a set of properties for the given class.
593     *
594     * @param clss the class to get the properties for.
595     * @return a set of properties for the given class.
596     */
597    public static Set<String> getProperties(Class<?> clss) {
598        final Set<String> result = new TreeSet<>();
599        final PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(clss);
600
601        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
602            if (propertyDescriptor.getWriteMethod() != null) {
603                result.add(propertyDescriptor.getName());
604            }
605        }
606
607        return result;
608    }
609
610    /**
611     * Checks if the property is a global property. Global properties come from the base classes
612     * and are common to all checks. For example id, severity, tabWidth, etc.
613     *
614     * @param clss the class of the module.
615     * @param propertyName the name of the property.
616     * @return true if the property is a global property.
617     */
618    private static boolean isGlobalProperty(Class<?> clss, String propertyName) {
619        return AbstractCheck.class.isAssignableFrom(clss)
620                    && CHECK_PROPERTIES.contains(propertyName)
621                || AbstractJavadocCheck.class.isAssignableFrom(clss)
622                    && JAVADOC_CHECK_PROPERTIES.contains(propertyName)
623                || AbstractFileSetCheck.class.isAssignableFrom(clss)
624                    && FILESET_PROPERTIES.contains(propertyName);
625    }
626
627    /**
628     * Checks if the property is supposed to be documented.
629     *
630     * @param clss the class of the module.
631     * @param propertyName the name of the property.
632     * @return true if the property is supposed to be documented.
633     */
634    private static boolean isUndocumentedProperty(Class<?> clss, String propertyName) {
635        return UNDOCUMENTED_PROPERTIES.contains(clss.getSimpleName() + DOT + propertyName);
636    }
637
638    /**
639     * Gets properties that are not explicitly captured but should be documented if
640     * certain conditions are met.
641     *
642     * @param instance the instance of the module.
643     * @param clss the class of the module.
644     * @return the non explicit properties.
645     */
646    private static Set<String> getNonExplicitProperties(
647            Object instance, Class<?> clss) {
648        final Set<String> result = new TreeSet<>();
649        if (AbstractCheck.class.isAssignableFrom(clss)) {
650            final AbstractCheck check = (AbstractCheck) instance;
651
652            final int[] acceptableTokens = check.getAcceptableTokens();
653            Arrays.sort(acceptableTokens);
654            final int[] defaultTokens = check.getDefaultTokens();
655            Arrays.sort(defaultTokens);
656            final int[] requiredTokens = check.getRequiredTokens();
657            Arrays.sort(requiredTokens);
658
659            if (!Arrays.equals(acceptableTokens, defaultTokens)
660                    || !Arrays.equals(acceptableTokens, requiredTokens)) {
661                result.add(TOKENS);
662            }
663        }
664
665        if (AbstractJavadocCheck.class.isAssignableFrom(clss)) {
666            final AbstractJavadocCheck check = (AbstractJavadocCheck) instance;
667            result.add("violateExecutionOnNonTightHtml");
668
669            final int[] acceptableJavadocTokens = check.getAcceptableJavadocTokens();
670            Arrays.sort(acceptableJavadocTokens);
671            final int[] defaultJavadocTokens = check.getDefaultJavadocTokens();
672            Arrays.sort(defaultJavadocTokens);
673            final int[] requiredJavadocTokens = check.getRequiredJavadocTokens();
674            Arrays.sort(requiredJavadocTokens);
675
676            if (!Arrays.equals(acceptableJavadocTokens, defaultJavadocTokens)
677                    || !Arrays.equals(acceptableJavadocTokens, requiredJavadocTokens)) {
678                result.add(JAVADOC_TOKENS);
679            }
680        }
681
682        if (AbstractFileSetCheck.class.isAssignableFrom(clss)) {
683            result.add(FILE_EXTENSIONS);
684        }
685        return result;
686    }
687
688    /**
689     * Get the description of the property.
690     *
691     * @param propertyName the name of the property.
692     * @param javadoc the Javadoc of the property setter method.
693     * @param moduleName the name of the module.
694     * @return the description of the property.
695     * @throws MacroExecutionException if the description could not be extracted.
696     */
697    public static String getPropertyDescriptionForXdoc(
698            String propertyName, DetailNode javadoc, String moduleName)
699            throws MacroExecutionException {
700        final String description;
701        if (TOKENS.equals(propertyName)) {
702            description = "tokens to check";
703        }
704        else if (JAVADOC_TOKENS.equals(propertyName)) {
705            description = "javadoc tokens to check";
706        }
707        else {
708            final String descriptionString = SETTER_PATTERN.matcher(
709                    getDescriptionFromJavadocForXdoc(javadoc, moduleName))
710                    .replaceFirst("");
711
712            final String firstLetterCapitalized = descriptionString.substring(0, 1)
713                    .toUpperCase(Locale.ROOT);
714            description = firstLetterCapitalized + descriptionString.substring(1);
715        }
716        return description;
717    }
718
719    /**
720     * Get the since version of the property.
721     *
722     * @param moduleName the name of the module.
723     * @param moduleJavadoc the Javadoc of the module.
724     * @param propertyJavadoc the Javadoc of the property setter method.
725     * @return the since version of the property.
726     * @throws MacroExecutionException if the module since version could not be extracted.
727     */
728    public static String getPropertySinceVersion(String moduleName, DetailNode moduleJavadoc,
729                                                 DetailNode propertyJavadoc)
730            throws MacroExecutionException {
731        final String sinceVersion;
732
733        final Optional<String> specifiedPropertyVersionInPropertyJavadoc =
734            getPropertyVersionFromItsJavadoc(propertyJavadoc);
735
736        if (specifiedPropertyVersionInPropertyJavadoc.isPresent()) {
737            sinceVersion = specifiedPropertyVersionInPropertyJavadoc.get();
738        }
739        else {
740            final String moduleSince = getSinceVersionFromJavadoc(moduleJavadoc);
741
742            if (moduleSince == null) {
743                throw new MacroExecutionException(
744                        "Missing @since on module " + moduleName);
745            }
746
747            String propertySetterSince = null;
748            if (propertyJavadoc != null) {
749                propertySetterSince = getSinceVersionFromJavadoc(propertyJavadoc);
750            }
751
752            if (propertySetterSince != null
753                    && isVersionAtLeast(propertySetterSince, moduleSince)) {
754                sinceVersion = propertySetterSince;
755            }
756            else {
757                sinceVersion = moduleSince;
758            }
759        }
760
761        return sinceVersion;
762    }
763
764    /**
765     * Extract the property since version from its Javadoc.
766     *
767     * @param propertyJavadoc the property Javadoc to extract the since version from.
768     * @return the Optional of property version specified in its javadoc.
769     */
770    @Nullable
771    private static Optional<String> getPropertyVersionFromItsJavadoc(DetailNode propertyJavadoc) {
772        final Optional<DetailNode> propertyJavadocTag =
773            getPropertySinceJavadocTag(propertyJavadoc);
774
775        return propertyJavadocTag
776            .map(tag -> JavadocUtil.findFirstToken(tag, JavadocTokenTypes.DESCRIPTION))
777            .map(description -> JavadocUtil.findFirstToken(description, JavadocTokenTypes.TEXT))
778            .map(DetailNode::getText);
779    }
780
781    /**
782     * Find the propertySince Javadoc tag node in the given property Javadoc.
783     *
784     * @param javadoc the Javadoc to search.
785     * @return the Optional of propertySince Javadoc tag node or null if not found.
786     */
787    private static Optional<DetailNode> getPropertySinceJavadocTag(DetailNode javadoc) {
788        Optional<DetailNode> propertySinceJavadocTag = Optional.empty();
789
790        final Optional<DetailNode[]> propertyJavadocNodes = Optional.ofNullable(javadoc)
791            .map(DetailNode::getChildren);
792
793        if (propertyJavadocNodes.isPresent()) {
794            for (final DetailNode child : propertyJavadocNodes.get()) {
795                if (child.getType() == JavadocTokenTypes.JAVADOC_TAG) {
796                    final DetailNode customName = JavadocUtil.findFirstToken(
797                            child, JavadocTokenTypes.CUSTOM_NAME);
798                    if (customName != null && "@propertySince".equals(customName.getText())) {
799                        propertySinceJavadocTag = Optional.of(child);
800                        break;
801                    }
802                }
803            }
804        }
805
806        return propertySinceJavadocTag;
807    }
808
809    /**
810     * Gets the javadoc node part of the property from the javadoc of the module.
811     *
812     * @param propertyName the name of property.
813     * @param moduleJavadoc the javadoc of module.
814     * @return the Optional of javadoc node part of the property.
815     */
816    public static Optional<DetailNode> getPropertyJavadocNodeInModule(String propertyName,
817                                                             DetailNode moduleJavadoc) {
818        final List<DetailNode> htmlElementNodes = getNodesOfSpecificType(
819            moduleJavadoc.getChildren(), JavadocTokenTypes.HTML_ELEMENT);
820
821        final List<DetailNode> ulTags = htmlElementNodes.stream()
822            .map(JavadocUtil::getFirstChild)
823            .filter(child -> {
824                final boolean isHtmlTag = child.getType() == JavadocTokenTypes.HTML_TAG;
825                final DetailNode htmlTagNameNode = JavadocUtil.findFirstToken(
826                    JavadocUtil.getFirstChild(child), JavadocTokenTypes.HTML_TAG_NAME);
827
828                return isHtmlTag && "ul".equals(htmlTagNameNode.getText());
829            })
830            .toList();
831        final DetailNode[] childrenOfUlTags = ulTags.stream()
832            .flatMap(ulTag -> Arrays.stream(ulTag.getChildren()))
833            .toArray(DetailNode[]::new);
834        final List<DetailNode> innerHtmlElementsOfUlTags =
835            getNodesOfSpecificType(childrenOfUlTags, JavadocTokenTypes.HTML_ELEMENT);
836
837        final List<DetailNode> liTags = innerHtmlElementsOfUlTags.stream()
838            .map(JavadocUtil::getFirstChild)
839            .filter(tag -> tag.getType() == JavadocTokenTypes.LI)
840            .toList();
841
842        final List<DetailNode> liTagsInlineTexts = liTags.stream()
843            .map(liTag -> JavadocUtil.findFirstToken(liTag, JavadocTokenTypes.JAVADOC_INLINE_TAG))
844            .filter(Objects::nonNull)
845            .map(inlineTag -> JavadocUtil.findFirstToken(inlineTag, JavadocTokenTypes.TEXT))
846            .toList();
847
848        return liTagsInlineTexts.stream()
849            .filter(text -> text.getText().equals(propertyName))
850            .findFirst()
851            .map(textNode -> textNode.getParent().getParent());
852
853    }
854
855    /**
856     * Gets all javadoc nodes of selected type.
857     *
858     * @param allNodes Nodes to choose from.
859     * @param neededType the Javadoc token type to select.
860     * @return the List of DetailNodes of selected type.
861     */
862    public static List<DetailNode> getNodesOfSpecificType(DetailNode[] allNodes, int neededType) {
863        return Arrays.stream(allNodes)
864            .filter(child -> child.getType() == neededType)
865            .toList();
866    }
867
868    /**
869     * Extract the since version from the Javadoc.
870     *
871     * @param javadoc the Javadoc to extract the since version from.
872     * @return the since version of the setter.
873     */
874    @Nullable
875    private static String getSinceVersionFromJavadoc(DetailNode javadoc) {
876        final DetailNode sinceJavadocTag = getSinceJavadocTag(javadoc);
877        return Optional.ofNullable(sinceJavadocTag)
878            .map(tag -> JavadocUtil.findFirstToken(tag, JavadocTokenTypes.DESCRIPTION))
879            .map(description -> JavadocUtil.findFirstToken(description, JavadocTokenTypes.TEXT))
880            .map(DetailNode::getText)
881            .orElse(null);
882    }
883
884    /**
885     * Find the since Javadoc tag node in the given Javadoc.
886     *
887     * @param javadoc the Javadoc to search.
888     * @return the since Javadoc tag node or null if not found.
889     */
890    private static DetailNode getSinceJavadocTag(DetailNode javadoc) {
891        final DetailNode[] children = javadoc.getChildren();
892        DetailNode javadocTagWithSince = null;
893        for (final DetailNode child : children) {
894            if (child.getType() == JavadocTokenTypes.JAVADOC_TAG) {
895                final DetailNode sinceNode = JavadocUtil.findFirstToken(
896                        child, JavadocTokenTypes.SINCE_LITERAL);
897                if (sinceNode != null) {
898                    javadocTagWithSince = child;
899                    break;
900                }
901            }
902        }
903        return javadocTagWithSince;
904    }
905
906    /**
907     * Returns {@code true} if {@code actualVersion} ≥ {@code requiredVersion}.
908     * Both versions have any trailing "-SNAPSHOT" stripped before comparison.
909     *
910     * @param actualVersion   e.g. "8.3" or "8.3-SNAPSHOT"
911     * @param requiredVersion e.g. "8.3"
912     * @return {@code true} if actualVersion exists, and, numerically, is at least requiredVersion
913     */
914    private static boolean isVersionAtLeast(String actualVersion,
915                                            String requiredVersion) {
916        final Version actualVersionParsed = Version.parse(actualVersion);
917        final Version requiredVersionParsed = Version.parse(requiredVersion);
918
919        return actualVersionParsed.compareTo(requiredVersionParsed) >= 0;
920    }
921
922    /**
923     * Get the type of the property.
924     *
925     * @param field the field to get the type of.
926     * @param propertyName the name of the property.
927     * @param moduleName the name of the module.
928     * @param instance the instance of the module.
929     * @return the type of the property.
930     * @throws MacroExecutionException if an error occurs during getting the type.
931     */
932    public static String getType(Field field, String propertyName,
933                                 String moduleName, Object instance)
934            throws MacroExecutionException {
935        final Class<?> fieldClass = getFieldClass(field, propertyName, moduleName, instance);
936        return Optional.ofNullable(field)
937                .map(nonNullField -> nonNullField.getAnnotation(XdocsPropertyType.class))
938                .filter(propertyType -> propertyType.value() != PropertyType.TOKEN_ARRAY)
939                .map(propertyType -> propertyType.value().getDescription())
940                .orElseGet(fieldClass::getTypeName);
941    }
942
943    /**
944     * Get the default value of the property.
945     *
946     * @param propertyName the name of the property.
947     * @param field the field to get the default value of.
948     * @param classInstance the instance of the class to get the default value of.
949     * @param moduleName the name of the module.
950     * @return the default value of the property.
951     * @throws MacroExecutionException if an error occurs during getting the default value.
952     * @noinspection IfStatementWithTooManyBranches
953     * @noinspectionreason IfStatementWithTooManyBranches - complex nature of getting properties
954     *      from XML files requires giant if/else statement
955     */
956    // -@cs[CyclomaticComplexity] Splitting would not make the code more readable
957    public static String getDefaultValue(String propertyName, Field field,
958                                         Object classInstance, String moduleName)
959            throws MacroExecutionException {
960        final Object value = getFieldValue(field, classInstance);
961        final Class<?> fieldClass = getFieldClass(field, propertyName, moduleName, classInstance);
962        String result = null;
963
964        if (classInstance instanceof PropertyCacheFile) {
965            result = "null (no cache file)";
966        }
967        else if (fieldClass == boolean.class
968                || fieldClass == int.class
969                || fieldClass == URI.class
970                || fieldClass == String.class) {
971            if (value != null) {
972                result = value.toString();
973            }
974        }
975        else if (fieldClass == int[].class
976                || ModuleJavadocParsingUtil.isPropertySpecialTokenProp(field)) {
977            result = getIntArrayPropertyValue(value);
978        }
979        else if (fieldClass == double[].class) {
980            result = removeSquareBrackets(Arrays.toString((double[]) value).replace(".0", ""));
981        }
982        else if (fieldClass == String[].class) {
983            result = getStringArrayPropertyValue(value);
984        }
985        else if (fieldClass == Pattern.class) {
986            if (value != null) {
987                result = value.toString().replace("\n", "\\n").replace("\t", "\\t")
988                        .replace("\r", "\\r").replace("\f", "\\f");
989            }
990        }
991        else if (fieldClass == Pattern[].class) {
992            result = getPatternArrayPropertyValue(value);
993        }
994        else if (fieldClass.isEnum()) {
995            if (value != null) {
996                result = value.toString().toLowerCase(Locale.ENGLISH);
997            }
998        }
999        else if (fieldClass == AccessModifierOption[].class) {
1000            result = removeSquareBrackets(Arrays.toString((Object[]) value));
1001        }
1002
1003        if (result == null) {
1004            result = "null";
1005        }
1006
1007        return result;
1008    }
1009
1010    /**
1011     * Gets the name of the bean property's default value for the Pattern array class.
1012     *
1013     * @param fieldValue The bean property's value
1014     * @return String form of property's default value
1015     */
1016    private static String getPatternArrayPropertyValue(Object fieldValue) {
1017        Object value = fieldValue;
1018        if (value instanceof Collection<?> collection) {
1019            value = collection.stream()
1020                    .map(Pattern.class::cast)
1021                    .toArray(Pattern[]::new);
1022        }
1023
1024        String result = "";
1025        if (value != null && Array.getLength(value) > 0) {
1026            result = removeSquareBrackets(
1027                    Arrays.stream((Pattern[]) value)
1028                    .map(Pattern::pattern)
1029                    .collect(Collectors.joining(COMMA_SPACE)));
1030        }
1031
1032        return result;
1033    }
1034
1035    /**
1036     * Removes square brackets [ and ] from the given string.
1037     *
1038     * @param value the string to remove square brackets from.
1039     * @return the string without square brackets.
1040     */
1041    private static String removeSquareBrackets(String value) {
1042        return value
1043                .replace("[", "")
1044                .replace("]", "");
1045    }
1046
1047    /**
1048     * Gets the name of the bean property's default value for the string array class.
1049     *
1050     * @param value The bean property's value
1051     * @return String form of property's default value
1052     */
1053    private static String getStringArrayPropertyValue(Object value) {
1054        final String result;
1055        if (value == null) {
1056            result = "";
1057        }
1058        else {
1059            try (Stream<?> valuesStream = getValuesStream(value)) {
1060                result = valuesStream
1061                    .map(String.class::cast)
1062                    .sorted()
1063                    .collect(Collectors.joining(COMMA_SPACE));
1064            }
1065        }
1066
1067        return result;
1068    }
1069
1070    /**
1071     * Generates a stream of values from the given value.
1072     *
1073     * @param value the value to generate the stream from.
1074     * @return the stream of values.
1075     */
1076    private static Stream<?> getValuesStream(Object value) {
1077        final Stream<?> valuesStream;
1078        if (value instanceof Collection<?> collection) {
1079            valuesStream = collection.stream();
1080        }
1081        else {
1082            final Object[] array = (Object[]) value;
1083            valuesStream = Arrays.stream(array);
1084        }
1085        return valuesStream;
1086    }
1087
1088    /**
1089     * Returns the name of the bean property's default value for the int array class.
1090     *
1091     * @param value The bean property's value.
1092     * @return String form of property's default value.
1093     */
1094    private static String getIntArrayPropertyValue(Object value) {
1095        try (IntStream stream = getIntStream(value)) {
1096            return stream
1097                    .mapToObj(TokenUtil::getTokenName)
1098                    .sorted()
1099                    .collect(Collectors.joining(COMMA_SPACE));
1100        }
1101    }
1102
1103    /**
1104     * Get the int stream from the given value.
1105     *
1106     * @param value the value to get the int stream from.
1107     * @return the int stream.
1108     * @noinspection ChainOfInstanceofChecks
1109     * @noinspectionreason ChainOfInstanceofChecks - We will deal with this at
1110     *                     <a href="https://github.com/checkstyle/checkstyle/issues/13500">13500</a>
1111     */
1112    private static IntStream getIntStream(Object value) {
1113        final IntStream stream;
1114        if (value instanceof Collection<?> collection) {
1115            stream = collection.stream()
1116                    .mapToInt(int.class::cast);
1117        }
1118        else if (value instanceof BitSet set) {
1119            stream = set.stream();
1120        }
1121        else {
1122            stream = Arrays.stream((int[]) value);
1123        }
1124        return stream;
1125    }
1126
1127    /**
1128     * Gets the class of the given field.
1129     *
1130     * @param field the field to get the class of.
1131     * @param propertyName the name of the property.
1132     * @param moduleName the name of the module.
1133     * @param instance the instance of the module.
1134     * @return the class of the field.
1135     * @throws MacroExecutionException if an error occurs during getting the class.
1136     */
1137    // -@cs[CyclomaticComplexity] Splitting would not make the code more readable
1138    // -@cs[ForbidWildcardAsReturnType] Implied by design to return different types
1139    public static Class<?> getFieldClass(Field field, String propertyName,
1140                                          String moduleName, Object instance)
1141            throws MacroExecutionException {
1142        Class<?> result = null;
1143
1144        if (PROPERTIES_ALLOWED_GET_TYPES_FROM_METHOD
1145                .contains(moduleName + DOT + propertyName)) {
1146            result = getPropertyClass(propertyName, instance);
1147        }
1148        if (ModuleJavadocParsingUtil.isPropertySpecialTokenProp(field)) {
1149            result = String[].class;
1150        }
1151        if (field != null && result == null) {
1152            result = field.getType();
1153        }
1154
1155        if (result == null) {
1156            throw new MacroExecutionException(
1157                    "Could not find field " + propertyName + " in class " + moduleName);
1158        }
1159
1160        if (field != null && (result == List.class || result == Set.class)) {
1161            final ParameterizedType type = (ParameterizedType) field.getGenericType();
1162            final Class<?> parameterClass = (Class<?>) type.getActualTypeArguments()[0];
1163
1164            if (parameterClass == Integer.class) {
1165                result = int[].class;
1166            }
1167            else if (parameterClass == String.class) {
1168                result = String[].class;
1169            }
1170            else if (parameterClass == Pattern.class) {
1171                result = Pattern[].class;
1172            }
1173            else {
1174                final String message = "Unknown parameterized type: "
1175                        + parameterClass.getSimpleName();
1176                throw new MacroExecutionException(message);
1177            }
1178        }
1179        else if (result == BitSet.class) {
1180            result = int[].class;
1181        }
1182
1183        return result;
1184    }
1185
1186    /**
1187     * Gets the class of the given java property.
1188     *
1189     * @param propertyName the name of the property.
1190     * @param instance the instance of the module.
1191     * @return the class of the java property.
1192     * @throws MacroExecutionException if an error occurs during getting the class.
1193     */
1194    // -@cs[ForbidWildcardAsReturnType] Object is received as param, no prediction on type of field
1195    public static Class<?> getPropertyClass(String propertyName, Object instance)
1196            throws MacroExecutionException {
1197        final Class<?> result;
1198        try {
1199            final PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(instance,
1200                    propertyName);
1201            result = descriptor.getPropertyType();
1202        }
1203        catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException exc) {
1204            throw new MacroExecutionException(exc.getMessage(), exc);
1205        }
1206        return result;
1207    }
1208
1209    /**
1210     * Get the difference between two lists of tokens.
1211     *
1212     * @param tokens the list of tokens to remove from.
1213     * @param subtractions the tokens to remove.
1214     * @return the difference between the two lists.
1215     */
1216    public static List<Integer> getDifference(int[] tokens, int... subtractions) {
1217        final Set<Integer> subtractionsSet = Arrays.stream(subtractions)
1218                .boxed()
1219                .collect(Collectors.toUnmodifiableSet());
1220        return Arrays.stream(tokens)
1221                .boxed()
1222                .filter(token -> !subtractionsSet.contains(token))
1223                .toList();
1224    }
1225
1226    /**
1227     * Gets the field with the given name from the given class.
1228     *
1229     * @param fieldClass the class to get the field from.
1230     * @param propertyName the name of the field.
1231     * @return the field we are looking for.
1232     */
1233    public static Field getField(Class<?> fieldClass, String propertyName) {
1234        Field result = null;
1235        Class<?> currentClass = fieldClass;
1236
1237        while (!Object.class.equals(currentClass)) {
1238            try {
1239                result = currentClass.getDeclaredField(propertyName);
1240                result.trySetAccessible();
1241                break;
1242            }
1243            catch (NoSuchFieldException ignored) {
1244                currentClass = currentClass.getSuperclass();
1245            }
1246        }
1247
1248        return result;
1249    }
1250
1251    /**
1252     * Constructs string with relative link to the provided document.
1253     *
1254     * @param moduleName the name of the module.
1255     * @param document the path of the document.
1256     * @return relative link to the document.
1257     * @throws MacroExecutionException if link to the document cannot be constructed.
1258     */
1259    public static String getLinkToDocument(String moduleName, String document)
1260            throws MacroExecutionException {
1261        final Path templatePath = getTemplatePath(FINAL_CHECK.matcher(moduleName).replaceAll(""));
1262        if (templatePath == null) {
1263            throw new MacroExecutionException(
1264                    String.format(Locale.ROOT,
1265                            "Could not find template for %s", moduleName));
1266        }
1267        final Path templatePathParent = templatePath.getParent();
1268        if (templatePathParent == null) {
1269            throw new MacroExecutionException("Failed to get parent path for " + templatePath);
1270        }
1271        return templatePathParent
1272                .relativize(Path.of(SRC, "site/xdoc", document))
1273                .toString()
1274                .replace(".xml", ".html")
1275                .replace('\\', '/');
1276    }
1277
1278    /**
1279     * Get all templates whose content contains properties macro.
1280     *
1281     * @return templates whose content contains properties macro.
1282     * @throws CheckstyleException if file could not be read.
1283     * @throws MacroExecutionException if template file is not found.
1284     */
1285    public static List<Path> getTemplatesThatContainPropertiesMacro()
1286            throws CheckstyleException, MacroExecutionException {
1287        final List<Path> result = new ArrayList<>();
1288        final Set<Path> templatesPaths = getXdocsTemplatesFilePaths();
1289        for (Path templatePath: templatesPaths) {
1290            final String content = getFileContents(templatePath);
1291            final String propertiesMacroDefinition = "<macro name=\"properties\"";
1292            if (content.contains(propertiesMacroDefinition)) {
1293                result.add(templatePath);
1294            }
1295        }
1296        return result;
1297    }
1298
1299    /**
1300     * Get file contents as string.
1301     *
1302     * @param pathToFile path to file.
1303     * @return file contents as string.
1304     * @throws CheckstyleException if file could not be read.
1305     */
1306    private static String getFileContents(Path pathToFile) throws CheckstyleException {
1307        final String content;
1308        try {
1309            content = Files.readString(pathToFile);
1310        }
1311        catch (IOException ioException) {
1312            final String message = String.format(Locale.ROOT, "Failed to read file: %s",
1313                    pathToFile);
1314            throw new CheckstyleException(message, ioException);
1315        }
1316        return content;
1317    }
1318
1319    /**
1320     * Get the module name from the file. The module name is the file name without the extension.
1321     *
1322     * @param file file to extract the module name from.
1323     * @return module name.
1324     */
1325    public static String getModuleName(File file) {
1326        final String fullFileName = file.getName();
1327        return CommonUtil.getFileNameWithoutExtension(fullFileName);
1328    }
1329
1330    /**
1331     * Extracts the description from the javadoc detail node. Performs a DFS traversal on the
1332     * detail node and extracts the text nodes. This description is additionally processed to
1333     * fit Xdoc format.
1334     *
1335     * @param javadoc the Javadoc to extract the description from.
1336     * @param moduleName the name of the module.
1337     * @return the description of the setter.
1338     * @throws MacroExecutionException if the description could not be extracted.
1339     * @noinspection TooBroadScope
1340     * @noinspectionreason TooBroadScope - complex nature of method requires large scope
1341     */
1342    // -@cs[NPathComplexity] Splitting would not make the code more readable
1343    // -@cs[CyclomaticComplexity] Splitting would not make the code more readable.
1344    private static String getDescriptionFromJavadocForXdoc(DetailNode javadoc, String moduleName)
1345            throws MacroExecutionException {
1346        boolean isInCodeLiteral = false;
1347        boolean isInHtmlElement = false;
1348        boolean isInHrefAttribute = false;
1349        final StringBuilder description = new StringBuilder(128);
1350        final Deque<DetailNode> queue = new ArrayDeque<>();
1351        final List<DetailNode> descriptionNodes = getFirstJavadocParagraphNodes(javadoc);
1352        Lists.reverse(descriptionNodes).forEach(queue::push);
1353
1354        // Perform DFS traversal on description nodes
1355        while (!queue.isEmpty()) {
1356            final DetailNode node = queue.pop();
1357            Lists.reverse(Arrays.asList(node.getChildren())).forEach(queue::push);
1358
1359            if (node.getType() == JavadocTokenTypes.HTML_TAG_NAME
1360                    && "href".equals(node.getText())) {
1361                isInHrefAttribute = true;
1362            }
1363            if (isInHrefAttribute && node.getType() == JavadocTokenTypes.ATTR_VALUE) {
1364                final String href = node.getText();
1365                if (href.contains(CHECKSTYLE_ORG_URL)) {
1366                    DescriptionExtractor.handleInternalLink(description, moduleName, href);
1367                }
1368                else {
1369                    description.append(href);
1370                }
1371
1372                isInHrefAttribute = false;
1373                continue;
1374            }
1375            if (node.getType() == JavadocTokenTypes.HTML_ELEMENT) {
1376                isInHtmlElement = true;
1377            }
1378            if (node.getType() == JavadocTokenTypes.END
1379                    && node.getParent().getType() == JavadocTokenTypes.HTML_ELEMENT_END) {
1380                description.append(node.getText());
1381                isInHtmlElement = false;
1382            }
1383            if (node.getType() == JavadocTokenTypes.TEXT
1384                    // If a node has children, its text is not part of the description
1385                    || isInHtmlElement && node.getChildren().length == 0
1386                        // Some HTML elements span multiple lines, so we avoid the asterisk
1387                        && node.getType() != JavadocTokenTypes.LEADING_ASTERISK) {
1388                description.append(node.getText());
1389            }
1390            if (node.getType() == JavadocTokenTypes.CODE_LITERAL) {
1391                isInCodeLiteral = true;
1392                description.append("<code>");
1393            }
1394            if (isInCodeLiteral
1395                    && node.getType() == JavadocTokenTypes.JAVADOC_INLINE_TAG_END) {
1396                isInCodeLiteral = false;
1397                description.append("</code>");
1398            }
1399        }
1400        return description.toString().trim();
1401    }
1402
1403    /**
1404     * Get 1st paragraph from the Javadoc with no additional processing.
1405     *
1406     * @param javadoc the Javadoc to extract first paragraph from.
1407     * @return first paragraph of javadoc.
1408     */
1409    public static String getFirstParagraphFromJavadoc(DetailNode javadoc) {
1410        final Deque<DetailNode> stack = new ArrayDeque<>();
1411        final List<DetailNode> firstParagraphNodes = getFirstJavadocParagraphNodes(javadoc);
1412        Lists.reverse(firstParagraphNodes).forEach(stack::push);
1413        final StringBuilder result = new StringBuilder(1024);
1414        while (!stack.isEmpty()) {
1415            final DetailNode detailNode = stack.pop();
1416
1417            Lists.reverse(Arrays.asList(detailNode.getChildren())).forEach(stack::push);
1418
1419            String childText = detailNode.getText();
1420
1421            if (detailNode.getParent().getType() == JavadocTokenTypes.JAVADOC_INLINE_TAG) {
1422                childText = JavadocMetadataScraperUtil.adjustCodeInlineTagChildToHtml(detailNode);
1423            }
1424
1425            // Regular expression for detecting ANTLR tokens(for e.g. CLASS_DEF).
1426            final Pattern tokenTextPattern = Pattern.compile("([A-Z_]{2,})+");
1427            if (detailNode.getType() != JavadocTokenTypes.LEADING_ASTERISK
1428                    && !tokenTextPattern.matcher(childText).matches()) {
1429                result.append(childText);
1430            }
1431        }
1432        return result.toString().trim();
1433    }
1434
1435    /**
1436     * Extracts first paragraph nodes from javadoc.
1437     *
1438     * @param javadoc the Javadoc to extract the description from.
1439     * @return the first paragraph nodes of the setter.
1440     */
1441    public static List<DetailNode> getFirstJavadocParagraphNodes(DetailNode javadoc) {
1442        final DetailNode[] children = javadoc.getChildren();
1443        final List<DetailNode> firstParagraphNodes = new ArrayList<>();
1444        for (final DetailNode child : children) {
1445            if (isEndOfFirstJavadocParagraph(child)) {
1446                break;
1447            }
1448            firstParagraphNodes.add(child);
1449        }
1450        return firstParagraphNodes;
1451    }
1452
1453    /**
1454     * Determines if the given child index is the end of the first Javadoc paragraph. The end
1455     * of the description is defined as 4 consecutive nodes of type NEWLINE, LEADING_ASTERISK,
1456     * NEWLINE, LEADING_ASTERISK. This is an asterisk that is alone on a line. Just like the
1457     * one below this line.
1458     *
1459     * @param child the child to check.
1460     * @return true if the given child index is the end of the first javadoc paragraph.
1461     */
1462    public static boolean isEndOfFirstJavadocParagraph(DetailNode child) {
1463        final DetailNode nextSibling = JavadocUtil.getNextSibling(child);
1464        final DetailNode secondNextSibling = JavadocUtil.getNextSibling(nextSibling);
1465        final DetailNode thirdNextSibling = JavadocUtil.getNextSibling(secondNextSibling);
1466
1467        return child.getType() == JavadocTokenTypes.NEWLINE
1468                    && nextSibling.getType() == JavadocTokenTypes.LEADING_ASTERISK
1469                    && secondNextSibling.getType() == JavadocTokenTypes.NEWLINE
1470                    && thirdNextSibling.getType() == JavadocTokenTypes.LEADING_ASTERISK;
1471    }
1472
1473    /**
1474     * Simplifies type name just to the name of the class, rather than entire package.
1475     *
1476     * @param fullTypeName full type name.
1477     * @return simplified type name, that is, name of the class.
1478     */
1479    public static String simplifyTypeName(String fullTypeName) {
1480        final int simplifiedStartIndex;
1481
1482        if (fullTypeName.contains("$")) {
1483            simplifiedStartIndex = fullTypeName.lastIndexOf('$') + 1;
1484        }
1485        else {
1486            simplifiedStartIndex = fullTypeName.lastIndexOf('.') + 1;
1487        }
1488
1489        return fullTypeName.substring(simplifiedStartIndex);
1490    }
1491
1492    /** Utility class for extracting description from a method's Javadoc. */
1493    private static final class DescriptionExtractor {
1494
1495        /**
1496         * Converts the href value to a relative link to the document and appends it to the
1497         * description.
1498         *
1499         * @param description the description to append the relative link to.
1500         * @param moduleName the name of the module.
1501         * @param value the href value.
1502         * @throws MacroExecutionException if the relative link could not be created.
1503         */
1504        private static void handleInternalLink(StringBuilder description,
1505                                               String moduleName, String value)
1506                throws MacroExecutionException {
1507            String href = value;
1508            href = href.replace(CHECKSTYLE_ORG_URL, "");
1509            // Remove first and last characters, they are always double quotes
1510            href = href.substring(1, href.length() - 1);
1511
1512            final String relativeHref = getLinkToDocument(moduleName, href);
1513            final char doubleQuote = '\"';
1514            description.append(doubleQuote).append(relativeHref).append(doubleQuote);
1515        }
1516    }
1517}