Class SiteUtil


  • public final class SiteUtil
    extends java.lang.Object
    Utility class for site generation.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  SiteUtil.DescriptionExtractor
      Utility class for extracting description from a method's Javadoc.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String CHARSET
      The string 'charset'.
      private static java.util.Set<java.lang.String> CHECK_PROPERTIES
      Set of properties that every check has.
      private static java.lang.String CHECKS
      The string 'checks'.
      private static java.lang.String CHECKSTYLE_ORG_URL
      The url of the checkstyle website.
      private static java.util.Map<java.lang.Class<?>,​java.lang.String> CLASS_TO_PARENT_MODULE
      Class name and their corresponding parent module name.
      static java.lang.String COMMA_SPACE
      The string ', '.
      private static java.lang.String CURLY_BRACKETS
      The string '{}'.
      static java.lang.String DOT
      The string '.'.
      private static java.lang.String FILE_EXTENSIONS
      The string 'fileExtensions'.
      private static java.util.Set<java.lang.String> FILESET_PROPERTIES
      Set of properties that every FileSet check has.
      private static java.lang.String HEADER_CHECK_HEADER
      Check and property name.
      private static java.util.Set<java.lang.String> JAVADOC_CHECK_PROPERTIES
      Set of properties that every Javadoc check has.
      static java.lang.String JAVADOC_TOKENS
      The string 'javadocTokens'.
      private static java.lang.String MAIN_FOLDER_PATH
      Path to main source code folder.
      private static java.util.List<java.io.File> MODULE_SUPER_CLASS_FILES
      List of files who are superclasses and contain certain properties that checks inherit.
      private static java.lang.String NAMING
      The string 'naming'.
      static java.lang.String PATH_TO_JAVADOC_TOKEN_TYPES
      The path to the JavadocTokenTypes.html file.
      static java.lang.String PATH_TO_TOKEN_TYPES
      The path to the TokenTypes.html file.
      private static java.util.Set<java.lang.String> PROPERTIES_ALLOWED_GET_TYPES_FROM_METHOD
      Properties that can not be gathered from class instance.
      private static java.lang.String REGEXP_HEADER_CHECK_HEADER
      Check and property name.
      private static java.util.regex.Pattern SETTER_PATTERN
      Precompiled regex pattern to remove the "Setter to " prefix from strings.
      private static java.util.Map<java.lang.String,​java.lang.String> SINCE_VERSION_FOR_INHERITED_PROPERTY
      Map of properties whose since version is different from module version but are not specified in code because they are inherited from their super class(es).
      private static java.lang.String SRC
      The string 'src'.
      private static java.util.Map<java.lang.String,​DetailNode> SUPER_CLASS_PROPERTIES_JAVADOCS
      Map of all superclasses properties and their javadocs.
      static java.lang.String TOKEN_TYPES
      The string 'TokenTypes'.
      static java.lang.String TOKENS
      The string 'tokens'.
      private static java.util.Set<java.lang.String> UNDOCUMENTED_PROPERTIES
      Set of properties that are undocumented.
      private static java.lang.String V824
      Frequent version.
      private static java.lang.String VERSION_3_0
      Frequent version.
      private static java.lang.String VERSION_3_2
      Frequent version.
      private static java.lang.String VERSION_3_4
      Frequent version.
      private static java.lang.String VERSION_5_0
      Frequent version.
      private static java.lang.String VERSION_5_7
      Frequent version.
      private static java.lang.String VERSION_6_9
      Frequent version.
      private static java.lang.String VERSION_7_7
      Frequent version.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private SiteUtil()
      Private utility constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static void assertAllPropertySetterJavadocsAreFound​(java.util.Set<java.lang.String> properties, java.lang.String moduleName, java.util.Map<java.lang.String,​DetailNode> javadocs)
      Assert that each property has a corresponding setter javadoc that is not null.
      private static java.util.Set<java.lang.reflect.Field> getCheckMessageKeys​(java.lang.Class<?> module)
      Gets the check's messages keys.
      static java.lang.String getDefaultValue​(java.lang.String propertyName, java.lang.reflect.Field field, java.lang.Object classInstance, java.lang.String moduleName)
      Get the default value of the property.
      static java.util.List<java.lang.Integer> getDifference​(int[] tokens, int... subtractions)
      Get the difference between two lists of tokens.
      static java.lang.reflect.Field getField​(java.lang.Class<?> fieldClass, java.lang.String propertyName)
      Gets the field with the given name from the given class.
      private static java.lang.Class<?> getFieldClass​(java.lang.reflect.Field field, java.lang.String propertyName, java.lang.String moduleName, java.lang.Object instance)
      Gets the class of the given field.
      static java.lang.Object getFieldValue​(java.lang.reflect.Field field, java.lang.Object instance)
      Returns the value of the given field.
      private static java.lang.String getIntArrayPropertyValue​(java.lang.Object value)
      Returns the name of the bean property's default value for the int array class.
      private static java.util.stream.IntStream getIntStream​(java.lang.Object value)
      Get the int stream from the given value.
      static java.lang.String getLinkToDocument​(java.lang.String moduleName, java.lang.String document)
      Constructs string with relative link to the provided document.
      static java.util.Set<java.lang.String> getMessageKeys​(java.lang.Class<?> module)
      Get string values of the message keys from the given check class.
      static java.lang.Object getModuleInstance​(java.lang.String moduleName)
      Returns the instance of the module with the given name.
      static java.lang.String getNewlineAndIndentSpaces​(int amountOfSpaces)
      Construct a string with a leading newline character and followed by the given amount of spaces.
      private static java.util.Set<java.lang.String> getNonExplicitProperties​(java.lang.Object instance, java.lang.Class<?> clss)
      Gets properties that are not explicitly captured but should be documented if certain conditions are met.
      private static PackageObjectFactory getPackageObjectFactory()
      Returns the default PackageObjectFactory with the default package names.
      static java.lang.String getParentModule​(java.lang.Class<?> moduleClass)
      Returns the parent module name for the given module class.
      private static java.lang.String getPatternArrayPropertyValue​(java.lang.Object fieldValue)
      Gets the name of the bean property's default value for the Pattern array class.
      static java.util.Set<java.lang.String> getProperties​(java.lang.Class<?> clss)
      Get a set of properties for the given class.
      static java.util.Set<java.lang.String> getPropertiesForDocumentation​(java.lang.Class<?> clss, java.lang.Object instance)
      Get a set of properties for the given class that should be documented.
      static java.util.Map<java.lang.String,​DetailNode> getPropertiesJavadocs​(java.util.Set<java.lang.String> properties, java.lang.String moduleName, java.io.File moduleFile)
      Get the javadocs of the properties of the module.
      static java.lang.Class<?> getPropertyClass​(java.lang.String propertyName, java.lang.Object instance)
      Gets the class of the given java property.
      static java.lang.String getPropertyDescription​(java.lang.String propertyName, DetailNode javadoc, java.lang.String moduleName)
      Get the description of the property.
      private static DetailNode getSinceJavadocTag​(DetailNode javadoc)
      Find the since Javadoc tag node in the given Javadoc.
      static java.lang.String getSinceVersion​(java.lang.String moduleName, DetailNode moduleJavadoc, java.lang.String propertyName, DetailNode propertyJavadoc)
      Get the since version of the property.
      private static java.lang.String getSinceVersionFromJavadoc​(DetailNode javadoc)
      Extract the since version from the Javadoc.
      private static java.lang.String getStringArrayPropertyValue​(java.lang.String propertyName, java.lang.Object value)
      Gets the name of the bean property's default value for the string array class.
      static java.nio.file.Path getTemplatePath​(java.lang.String moduleName)
      Returns path to the template for the given module name or throws an exception if the template cannot be found.
      static java.lang.String getType​(java.lang.reflect.Field field, java.lang.String propertyName, java.lang.String moduleName, java.lang.Object instance)
      Get the type of the property.
      private static java.util.stream.Stream<?> getValuesStream​(java.lang.Object value)
      Generates a stream of values from the given value.
      static java.util.Set<java.nio.file.Path> getXdocsTemplatesFilePaths()
      Gets xdocs template file paths.
      private static boolean isGlobalProperty​(java.lang.Class<?> clss, java.lang.String propertyName)
      Checks if the property is a global property.
      private static boolean isUndocumentedProperty​(java.lang.Class<?> clss, java.lang.String propertyName)
      Checks if the property is supposed to be documented.
      private static void processModule​(java.lang.String moduleName, java.io.File moduleFile)
      Scrape the Javadocs of the class and its properties setters with ClassAndPropertiesSettersJavadocScraper.
      private static void processSuperclasses()
      Collect the properties setters javadocs of the superclasses.
      private static java.lang.String removeSquareBrackets​(java.lang.String value)
      Removes square brackets [ and ] from the given string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SiteUtil

        private SiteUtil()
        Private utility constructor.
    • Method Detail

      • getMessageKeys

        public static java.util.Set<java.lang.String> getMessageKeys​(java.lang.Class<?> module)
                                                              throws org.apache.maven.doxia.macro.MacroExecutionException
        Get string values of the message keys from the given check class.
        Parameters:
        module - class to examine.
        Returns:
        a set of checkstyle's module message keys.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if extraction of message keys fails.
      • getCheckMessageKeys

        private static java.util.Set<java.lang.reflect.Field> getCheckMessageKeys​(java.lang.Class<?> module)
                                                                           throws org.apache.maven.doxia.macro.MacroExecutionException
        Gets the check's messages keys.
        Parameters:
        module - class to examine.
        Returns:
        a set of checkstyle's module message fields.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if the attempt to read a protected class fails.
      • getFieldValue

        public static java.lang.Object getFieldValue​(java.lang.reflect.Field field,
                                                     java.lang.Object instance)
                                              throws org.apache.maven.doxia.macro.MacroExecutionException
        Returns the value of the given field.
        Parameters:
        field - the field.
        instance - the instance of the module.
        Returns:
        the value of the field.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if the value could not be retrieved.
      • getModuleInstance

        public static java.lang.Object getModuleInstance​(java.lang.String moduleName)
                                                  throws org.apache.maven.doxia.macro.MacroExecutionException
        Returns the instance of the module with the given name.
        Parameters:
        moduleName - the name of the module.
        Returns:
        the instance of the module.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if the module could not be created.
      • getPackageObjectFactory

        private static PackageObjectFactory getPackageObjectFactory()
                                                             throws org.apache.maven.doxia.macro.MacroExecutionException
        Returns the default PackageObjectFactory with the default package names.
        Returns:
        the default PackageObjectFactory.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if the PackageObjectFactory cannot be created.
      • getNewlineAndIndentSpaces

        public static java.lang.String getNewlineAndIndentSpaces​(int amountOfSpaces)
        Construct a string with a leading newline character and followed by the given amount of spaces. We use this method only to match indentation in regular xdocs and have minimal diff when parsing the templates. This method exists until 13426
        Parameters:
        amountOfSpaces - the amount of spaces to add after the newline.
        Returns:
        the constructed string.
      • getTemplatePath

        public static java.nio.file.Path getTemplatePath​(java.lang.String moduleName)
                                                  throws org.apache.maven.doxia.macro.MacroExecutionException
        Returns path to the template for the given module name or throws an exception if the template cannot be found.
        Parameters:
        moduleName - the module whose template we are looking for.
        Returns:
        path to the template.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if the template cannot be found.
      • getXdocsTemplatesFilePaths

        public static java.util.Set<java.nio.file.Path> getXdocsTemplatesFilePaths()
                                                                            throws org.apache.maven.doxia.macro.MacroExecutionException
        Gets xdocs template file paths. These are files ending with .xml.template. This method will be changed to gather .xml once #13426 is resolved.
        Returns:
        a set of xdocs template file paths.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if an I/O error occurs.
      • getParentModule

        public static java.lang.String getParentModule​(java.lang.Class<?> moduleClass)
                                                throws org.apache.maven.doxia.macro.MacroExecutionException
        Returns the parent module name for the given module class. Returns either "TreeWalker" or "Checker". Returns null if the module class is null.
        Parameters:
        moduleClass - the module class.
        Returns:
        the parent module name as a string.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if the parent module cannot be found.
      • getPropertiesForDocumentation

        public static java.util.Set<java.lang.String> getPropertiesForDocumentation​(java.lang.Class<?> clss,
                                                                                    java.lang.Object instance)
        Get a set of properties for the given class that should be documented.
        Parameters:
        clss - the class to get the properties for.
        instance - the instance of the module.
        Returns:
        a set of properties for the given class.
      • getPropertiesJavadocs

        public static java.util.Map<java.lang.String,​DetailNodegetPropertiesJavadocs​(java.util.Set<java.lang.String> properties,
                                                                                             java.lang.String moduleName,
                                                                                             java.io.File moduleFile)
                                                                                      throws org.apache.maven.doxia.macro.MacroExecutionException
        Get the javadocs of the properties of the module. If the property is not present in the module, then the javadoc of the property from the superclass(es) is used.
        Parameters:
        properties - the properties of the module.
        moduleName - the name of the module.
        moduleFile - the module file.
        Returns:
        the javadocs of the properties of the module.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during processing.
      • assertAllPropertySetterJavadocsAreFound

        private static void assertAllPropertySetterJavadocsAreFound​(java.util.Set<java.lang.String> properties,
                                                                    java.lang.String moduleName,
                                                                    java.util.Map<java.lang.String,​DetailNode> javadocs)
                                                             throws org.apache.maven.doxia.macro.MacroExecutionException
        Assert that each property has a corresponding setter javadoc that is not null. 'tokens' and 'javadocTokens' are excluded from this check, because their description is different from the description of the setter.
        Parameters:
        properties - the properties of the module.
        moduleName - the name of the module.
        javadocs - the javadocs of the properties of the module.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during processing.
      • processSuperclasses

        private static void processSuperclasses()
                                         throws org.apache.maven.doxia.macro.MacroExecutionException
        Collect the properties setters javadocs of the superclasses.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during processing.
      • processModule

        private static void processModule​(java.lang.String moduleName,
                                          java.io.File moduleFile)
                                   throws org.apache.maven.doxia.macro.MacroExecutionException
        Scrape the Javadocs of the class and its properties setters with ClassAndPropertiesSettersJavadocScraper.
        Parameters:
        moduleName - the name of the module.
        moduleFile - the module file.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during processing.
      • getProperties

        public static java.util.Set<java.lang.String> getProperties​(java.lang.Class<?> clss)
        Get a set of properties for the given class.
        Parameters:
        clss - the class to get the properties for.
        Returns:
        a set of properties for the given class.
      • isGlobalProperty

        private static boolean isGlobalProperty​(java.lang.Class<?> clss,
                                                java.lang.String propertyName)
        Checks if the property is a global property. Global properties come from the base classes and are common to all checks. For example id, severity, tabWidth, etc.
        Parameters:
        clss - the class of the module.
        propertyName - the name of the property.
        Returns:
        true if the property is a global property.
      • isUndocumentedProperty

        private static boolean isUndocumentedProperty​(java.lang.Class<?> clss,
                                                      java.lang.String propertyName)
        Checks if the property is supposed to be documented.
        Parameters:
        clss - the class of the module.
        propertyName - the name of the property.
        Returns:
        true if the property is supposed to be documented.
      • getNonExplicitProperties

        private static java.util.Set<java.lang.String> getNonExplicitProperties​(java.lang.Object instance,
                                                                                java.lang.Class<?> clss)
        Gets properties that are not explicitly captured but should be documented if certain conditions are met.
        Parameters:
        instance - the instance of the module.
        clss - the class of the module.
        Returns:
        the non explicit properties.
      • getPropertyDescription

        public static java.lang.String getPropertyDescription​(java.lang.String propertyName,
                                                              DetailNode javadoc,
                                                              java.lang.String moduleName)
                                                       throws org.apache.maven.doxia.macro.MacroExecutionException
        Get the description of the property.
        Parameters:
        propertyName - the name of the property.
        javadoc - the Javadoc of the property setter method.
        moduleName - the name of the module.
        Returns:
        the description of the property.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if the description could not be extracted.
      • getSinceVersion

        public static java.lang.String getSinceVersion​(java.lang.String moduleName,
                                                       DetailNode moduleJavadoc,
                                                       java.lang.String propertyName,
                                                       DetailNode propertyJavadoc)
                                                throws org.apache.maven.doxia.macro.MacroExecutionException
        Get the since version of the property.
        Parameters:
        moduleName - the name of the module.
        moduleJavadoc - the Javadoc of the module.
        propertyName - the name of the property.
        propertyJavadoc - the Javadoc of the property setter method.
        Returns:
        the since version of the property.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if the since version could not be extracted.
      • getSinceVersionFromJavadoc

        @Nullable
        private static java.lang.String getSinceVersionFromJavadoc​(DetailNode javadoc)
        Extract the since version from the Javadoc.
        Parameters:
        javadoc - the Javadoc to extract the since version from.
        Returns:
        the since version of the setter.
      • getSinceJavadocTag

        private static DetailNode getSinceJavadocTag​(DetailNode javadoc)
        Find the since Javadoc tag node in the given Javadoc.
        Parameters:
        javadoc - the Javadoc to search.
        Returns:
        the since Javadoc tag node or null if not found.
      • getType

        public static java.lang.String getType​(java.lang.reflect.Field field,
                                               java.lang.String propertyName,
                                               java.lang.String moduleName,
                                               java.lang.Object instance)
                                        throws org.apache.maven.doxia.macro.MacroExecutionException
        Get the type of the property.
        Parameters:
        field - the field to get the type of.
        propertyName - the name of the property.
        moduleName - the name of the module.
        instance - the instance of the module.
        Returns:
        the type of the property.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during getting the type.
      • getDefaultValue

        public static java.lang.String getDefaultValue​(java.lang.String propertyName,
                                                       java.lang.reflect.Field field,
                                                       java.lang.Object classInstance,
                                                       java.lang.String moduleName)
                                                throws org.apache.maven.doxia.macro.MacroExecutionException
        Get the default value of the property.
        Parameters:
        propertyName - the name of the property.
        field - the field to get the default value of.
        classInstance - the instance of the class to get the default value of.
        moduleName - the name of the module.
        Returns:
        the default value of the property.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during getting the default value.
      • getPatternArrayPropertyValue

        private static java.lang.String getPatternArrayPropertyValue​(java.lang.Object fieldValue)
        Gets the name of the bean property's default value for the Pattern array class.
        Parameters:
        fieldValue - The bean property's value
        Returns:
        String form of property's default value
      • removeSquareBrackets

        private static java.lang.String removeSquareBrackets​(java.lang.String value)
        Removes square brackets [ and ] from the given string.
        Parameters:
        value - the string to remove square brackets from.
        Returns:
        the string without square brackets.
      • getStringArrayPropertyValue

        private static java.lang.String getStringArrayPropertyValue​(java.lang.String propertyName,
                                                                    java.lang.Object value)
        Gets the name of the bean property's default value for the string array class.
        Parameters:
        propertyName - The bean property's name
        value - The bean property's value
        Returns:
        String form of property's default value
      • getValuesStream

        private static java.util.stream.Stream<?> getValuesStream​(java.lang.Object value)
        Generates a stream of values from the given value.
        Parameters:
        value - the value to generate the stream from.
        Returns:
        the stream of values.
      • getIntArrayPropertyValue

        private static java.lang.String getIntArrayPropertyValue​(java.lang.Object value)
        Returns the name of the bean property's default value for the int array class.
        Parameters:
        value - The bean property's value.
        Returns:
        String form of property's default value.
      • getIntStream

        private static java.util.stream.IntStream getIntStream​(java.lang.Object value)
        Get the int stream from the given value.
        Parameters:
        value - the value to get the int stream from.
        Returns:
        the int stream.
      • getFieldClass

        private static java.lang.Class<?> getFieldClass​(java.lang.reflect.Field field,
                                                        java.lang.String propertyName,
                                                        java.lang.String moduleName,
                                                        java.lang.Object instance)
                                                 throws org.apache.maven.doxia.macro.MacroExecutionException
        Gets the class of the given field.
        Parameters:
        field - the field to get the class of.
        propertyName - the name of the property.
        moduleName - the name of the module.
        instance - the instance of the module.
        Returns:
        the class of the field.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during getting the class.
      • getPropertyClass

        public static java.lang.Class<?> getPropertyClass​(java.lang.String propertyName,
                                                          java.lang.Object instance)
                                                   throws org.apache.maven.doxia.macro.MacroExecutionException
        Gets the class of the given java property.
        Parameters:
        propertyName - the name of the property.
        instance - the instance of the module.
        Returns:
        the class of the java property.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during getting the class.
      • getDifference

        public static java.util.List<java.lang.Integer> getDifference​(int[] tokens,
                                                                      int... subtractions)
        Get the difference between two lists of tokens.
        Parameters:
        tokens - the list of tokens to remove from.
        subtractions - the tokens to remove.
        Returns:
        the difference between the two lists.
      • getField

        public static java.lang.reflect.Field getField​(java.lang.Class<?> fieldClass,
                                                       java.lang.String propertyName)
        Gets the field with the given name from the given class.
        Parameters:
        fieldClass - the class to get the field from.
        propertyName - the name of the field.
        Returns:
        the field we are looking for.
      • getLinkToDocument

        public static java.lang.String getLinkToDocument​(java.lang.String moduleName,
                                                         java.lang.String document)
                                                  throws org.apache.maven.doxia.macro.MacroExecutionException
        Constructs string with relative link to the provided document.
        Parameters:
        moduleName - the name of the module.
        document - the path of the document.
        Returns:
        relative link to the document.
        Throws:
        org.apache.maven.doxia.macro.MacroExecutionException - if link to the document cannot be constructed.