Class SiteUtil

java.lang.Object
com.puppycrawl.tools.checkstyle.site.SiteUtil

public final class SiteUtil extends Object
Utility class for site generation.
  • Field Details

  • Constructor Details

    • SiteUtil

      private SiteUtil()
      Private utility constructor.
  • Method Details

    • getMessageKeys

      public static Set<String> getMessageKeys(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.
    • getCheckMessageKeysFields

      private static Set<Field> getCheckMessageKeysFields(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 Object getFieldValue(Field field, 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 Object getModuleInstance(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 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 Path getTemplatePath(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 Set<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 String getParentModule(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 Set<String> getPropertiesForDocumentation(Class<?> clss, 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.
    • getModuleSinceVersion

      public static String getModuleSinceVersion(String moduleClassName, Path modulePath) throws org.apache.maven.doxia.macro.MacroExecutionException
      Gets the since version of the module.
      Parameters:
      moduleClassName - name of module class.
      modulePath - module's path.
      Returns:
      since version of module.
      Throws:
      org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during processing.
    • buildPropertyDetails

      public static Map<String,PropertyDetails> buildPropertyDetails(Set<String> properties, String moduleName, Path modulePath, Object instance) throws org.apache.maven.doxia.macro.MacroExecutionException
      Get the property details of the module. If the property is not present in the module, then the property details from the superclass(es) is used.

      Superclass property data is built fresh on every call and never cached statically, to prevent stale data from a previous Maven execution in the same JVM from corrupting results.

      Parameters:
      properties - the properties of the module.
      moduleName - the name of the module.
      modulePath - the module file path.
      instance - the instance of the module.
      Returns:
      the property details of the module.
      Throws:
      org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during processing.
    • processInheritedProperty

      private static void processInheritedProperty(Map<String,PropertyDetails> detailsMap, String property, Object instance, String moduleName, Map<String,PropertyDetails> superClassPropertyData) throws org.apache.maven.doxia.macro.MacroExecutionException
      Processes an inherited property and adds its details to the provided map.
      Parameters:
      detailsMap - the map to add the property details to.
      property - the name of the property.
      instance - the module instance.
      moduleName - the module name.
      superClassPropertyData - the superclass property data built for this invocation.
      Throws:
      org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs.
    • assertAllPropertiesAreFound

      private static void assertAllPropertiesAreFound(Set<String> properties, String moduleName, Map<String,PropertyDetails> details) throws org.apache.maven.doxia.macro.MacroExecutionException
      Assert that each property has a corresponding detail object.
      Parameters:
      properties - the properties of the module.
      moduleName - the name of the module.
      details - the details of the properties of the module.
      Throws:
      org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during processing.
    • buildSuperClassPropertyData

      private static Map<String,PropertyDetails> buildSuperClassPropertyData() throws org.apache.maven.doxia.macro.MacroExecutionException
      Builds a fresh map of superclass property data by scraping each superclass file. This method is called once per buildPropertyDetails(java.util.Set<java.lang.String>, java.lang.String, java.nio.file.Path, java.lang.Object) invocation and returns a new local map — it never populates any static field.
      Returns:
      map of property name to PropertyDetails for all known superclasses.
      Throws:
      org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during processing.
    • processModule

      private static void processModule(String moduleName, Path modulePath, Object instance, Set<String> properties) 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.
      modulePath - the module Path.
      instance - the instance of the module.
      properties - the properties of the module.
      Throws:
      org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during processing.
    • processModule

      public static void processModule(String moduleName, Path modulePath) throws org.apache.maven.doxia.macro.MacroExecutionException
      Scrape the Javadocs of the class and its properties setters.
      Parameters:
      moduleName - the name of the module.
      modulePath - the module Path.
      Throws:
      org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs during processing.
    • constructPropertyDetails

      public static PropertyDetails constructPropertyDetails(PropertyDetails.Builder builder, Object instance, Field field, String propertyName, String moduleName) throws org.apache.maven.doxia.macro.MacroExecutionException
      Constructs a PropertyDetails object for the given property.
      Parameters:
      builder - the builder already containing name, description, and since version.
      instance - the instance of the module.
      field - the field of the property.
      propertyName - the name of the property.
      moduleName - the name of the module.
      Returns:
      the PropertyDetails object.
      Throws:
      org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs.
    • configureTokensDetails

      private static void configureTokensDetails(PropertyDetails.Builder builder, AbstractCheck check)
      Configures the tokens details for a property.
      Parameters:
      builder - the property details builder.
      check - the check instance.
    • configureJavadocTokensDetails

      Configures the javadoc tokens details for a property.
      Parameters:
      builder - the property details builder.
      check - the javadoc check instance.
    • configureOtherPropertyDetails

      private static void configureOtherPropertyDetails(PropertyDetails.Builder builder, Object instance, Field field, String propertyName, String moduleName) throws org.apache.maven.doxia.macro.MacroExecutionException
      Configures the details for properties other than tokens and javadoc tokens.
      Parameters:
      builder - the property details builder.
      instance - the module instance.
      field - the field of the property.
      propertyName - the name of the property.
      moduleName - the name of the module.
      Throws:
      org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs.
    • getProperties

      public static Set<String> getProperties(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(Class<?> clss, 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(Class<?> clss, 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 Set<String> getNonExplicitProperties(Object instance, 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.
    • getPropertyDescriptionForXdoc

      public static String getPropertyDescriptionForXdoc(String propertyName, DetailNode javadoc, 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.
    • getPropertySinceVersion

      public static String getPropertySinceVersion(String moduleSince, DetailNode propertyJavadoc)
      Get the since version of the property.

      Note: the moduleName parameter has been removed because it was unused. All call sites have been updated accordingly.

      Parameters:
      moduleSince - the since version of the module.
      propertyJavadoc - the Javadoc of the property setter method.
      Returns:
      the since version of the property.
    • getPropertyVersionFromItsJavadoc

      private static Optional<String> getPropertyVersionFromItsJavadoc(DetailNode propertyJavadoc)
      Extract the property since version from its Javadoc.
      Parameters:
      propertyJavadoc - the property Javadoc to extract the since version from.
      Returns:
      the Optional of property version specified in its javadoc.
    • getPropertySinceJavadocTag

      Find the propertySince Javadoc tag node in the given property Javadoc.
      Parameters:
      javadoc - the Javadoc to search.
      Returns:
      the Optional of propertySince Javadoc tag node or null if not found.
    • getNodesOfSpecificType

      public static List<DetailNode> getNodesOfSpecificType(DetailNode[] allNodes, int neededType)
      Gets all javadoc nodes of selected type.
      Parameters:
      allNodes - Nodes to choose from.
      neededType - the Javadoc token type to select.
      Returns:
      the List of DetailNodes of selected type.
    • getSinceVersionFromJavadoc

      private static 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, or null if not found.
    • 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.
    • isVersionAtLeast

      private static boolean isVersionAtLeast(String actualVersion, String requiredVersion)
      Returns true if actualVersion >= requiredVersion. Both versions have any trailing "-SNAPSHOT" stripped before comparison.
      Parameters:
      actualVersion - e.g. "8.3" or "8.3-SNAPSHOT"
      requiredVersion - e.g. "8.3"
      Returns:
      true if actualVersion exists, and, numerically, is at least requiredVersion
    • getType

      public static String getType(Field field, String propertyName, String moduleName, 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 String getDefaultValue(String propertyName, Field field, Object classInstance, 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.
    • getFieldDefaultValue

      private static String getFieldDefaultValue(Field field, Class<?> fieldClass, Object value)
      Gets the string representation of a field's default value based on its type. Returns null if the field type is not recognized or the value is null.
      Parameters:
      field - the field to get the default value of.
      fieldClass - the class of the field.
      value - the current value of the field.
      Returns:
      string form of the default value, or null if unrecognized.
    • getScalarFieldDefaultValue

      private static String getScalarFieldDefaultValue(Class<?> fieldClass, Object value)
      Gets the default value string for scalar (non-array) field types. Returns null if the field class is not a handled scalar type.
      Parameters:
      fieldClass - the class of the field.
      value - the current value of the field.
      Returns:
      string form of the default value, or null if not a scalar type.
    • getArrayFieldDefaultValue

      private static String getArrayFieldDefaultValue(Field field, Class<?> fieldClass, Object value)
      Gets the default value string for array field types. Returns null if the field class is not a handled array type.
      Parameters:
      field - the field (used for annotation checks).
      fieldClass - the class of the field.
      value - the current value of the field.
      Returns:
      string form of the default value, or null if not an array type.
    • getPatternDefaultValue

      private static String getPatternDefaultValue(Object value)
      Gets the string representation of a Pattern field's default value.
      Parameters:
      value - the current value of the field.
      Returns:
      string form of the Pattern default value, or null if value is null.
    • getAccessModifierDefaultValue

      Gets the string representation of an AccessModifierOption array field's default value.
      Parameters:
      value - the current value of the field.
      Returns:
      string form of the default value.
    • hasPreserveOrderAnnotation

      private static boolean hasPreserveOrderAnnotation(Field field)
      Checks if a field has the PreserveOrder annotation.
      Parameters:
      field - the field to check
      Returns:
      true if the field has PreserveOrder annotation, false otherwise
    • getPatternArrayPropertyValue

      private static String getPatternArrayPropertyValue(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 String removeSquareBrackets(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 String getStringArrayPropertyValue(Object value, boolean preserveOrder)
      Gets the name of the bean property's default value for the string array class.
      Parameters:
      value - The bean property's value
      preserveOrder - whether to preserve the original order
      Returns:
      String form of property's default value
    • getValuesStream

      private static Stream<?> getValuesStream(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 String getIntArrayPropertyValue(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 IntStream getIntStream(Object value)
      Get the int stream from the given value.
      Parameters:
      value - the value to get the int stream from.
      Returns:
      the int stream.
      Throws:
      IllegalArgumentException - if parameter is null.
    • getFieldClass

      public static Class<?> getFieldClass(Field field, String propertyName, String moduleName, 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.
    • getParameterizedTypeClass

      private static Class<?> getParameterizedTypeClass(Field field) throws org.apache.maven.doxia.macro.MacroExecutionException
      Gets the class of the parameterized type for the given field.
      Parameters:
      field - the field to get the parameterized type class of.
      Returns:
      the class of the parameterized type.
      Throws:
      org.apache.maven.doxia.macro.MacroExecutionException - if an error occurs.
    • getPropertyClass

      public static Class<?> getPropertyClass(String propertyName, 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 List<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 Field getField(Class<?> fieldClass, 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 String getLinkToDocument(String moduleName, 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.
    • getTemplatesThatContainPropertiesMacro

      public static List<Path> getTemplatesThatContainPropertiesMacro() throws CheckstyleException, org.apache.maven.doxia.macro.MacroExecutionException
      Get all templates whose content contains properties macro.
      Returns:
      templates whose content contains properties macro.
      Throws:
      CheckstyleException - if file could not be read.
      org.apache.maven.doxia.macro.MacroExecutionException - if template file is not found.
    • getFileContents

      private static String getFileContents(Path pathToFile) throws CheckstyleException
      Get file contents as string.
      Parameters:
      pathToFile - path to file.
      Returns:
      file contents as string.
      Throws:
      CheckstyleException - if file could not be read.
    • getModuleName

      public static String getModuleName(File file)
      Get the module name from the file. The module name is the file name without the extension.
      Parameters:
      file - file to extract the module name from.
      Returns:
      module name.
    • getDescriptionFromJavadocForXdoc

      private static String getDescriptionFromJavadocForXdoc(DetailNode javadoc, String moduleName) throws org.apache.maven.doxia.macro.MacroExecutionException
      Extracts the description from the javadoc detail node. Performs a DFS traversal on the detail node and extracts the text nodes. This description is additionally processed to fit Xdoc format.
      Parameters:
      javadoc - the Javadoc to extract the description from.
      moduleName - the name of the module.
      Returns:
      the description of the setter.
      Throws:
      org.apache.maven.doxia.macro.MacroExecutionException - if the description could not be extracted.
    • processDescriptionNode

      private static void processDescriptionNode(DetailNode node, StringBuilder description, SiteUtil.DescriptionTraversalState state, String moduleName) throws org.apache.maven.doxia.macro.MacroExecutionException
      Processes a single node during description extraction and updates the state. Delegates href-attribute handling and non-href node handling to separate helpers to keep cyclomatic complexity within limits.
      Parameters:
      node - the current node being visited.
      description - the description buffer to append to.
      state - the mutable traversal state.
      moduleName - the name of the module (used for internal link resolution).
      Throws:
      org.apache.maven.doxia.macro.MacroExecutionException - if an internal link cannot be resolved.
    • processHrefAttributeValue

      private static void processHrefAttributeValue(DetailNode node, StringBuilder description, SiteUtil.DescriptionTraversalState state, String moduleName) throws org.apache.maven.doxia.macro.MacroExecutionException
      Handles an ATTRIBUTE_VALUE node that belongs to an href attribute.
      Parameters:
      node - the ATTRIBUTE_VALUE node.
      description - the description buffer to append to.
      state - the mutable traversal state.
      moduleName - the name of the module (used for internal link resolution).
      Throws:
      org.apache.maven.doxia.macro.MacroExecutionException - if an internal link cannot be resolved.
    • processNonHrefNode

      private static void processNonHrefNode(DetailNode node, StringBuilder description, SiteUtil.DescriptionTraversalState state)
      Handles all nodes that are not an href ATTRIBUTE_VALUE, updating HTML-element tracking, text content, and inline-tag (code/literal) tracking.
      Parameters:
      node - the current node.
      description - the description buffer to append to.
      state - the mutable traversal state.
    • processHtmlElementTracking

      Updates HTML-element open/close tracking and appends closing tag text.
      Parameters:
      node - the current node.
      description - the description buffer to append to.
      state - the mutable traversal state.
    • processTextContent

      private static void processTextContent(DetailNode node, StringBuilder description, SiteUtil.DescriptionTraversalState state)
      Appends text content from the node, escaping special characters when inside a @code or @literal inline tag.
      Parameters:
      node - the current node.
      description - the description buffer to append to.
      state - the mutable traversal state.
    • processInlineTagTracking

      private static void processInlineTagTracking(DetailNode node, StringBuilder description, SiteUtil.DescriptionTraversalState state)
      Updates @code and @literal inline-tag tracking and appends the opening/closing <code> HTML tags as needed.
      Parameters:
      node - the current node.
      description - the description buffer to append to.
      state - the mutable traversal state.
    • isTextContent

      private static boolean isTextContent(DetailNode node, boolean isInHtmlElement)
      Checks whether the node contains text content that should be written to the description.
      Parameters:
      node - the node to check.
      isInHtmlElement - whether we are inside an HTML element.
      Returns:
      true if the node contains text content to write.
    • getFirstParagraphFromJavadoc

      Get 1st paragraph from the Javadoc with no additional processing.
      Parameters:
      javadoc - the Javadoc to extract first paragraph from.
      Returns:
      first paragraph of javadoc.
    • getFirstJavadocParagraphNodes

      Extracts first paragraph nodes from javadoc.
      Parameters:
      javadoc - the Javadoc to extract the description from.
      Returns:
      the first paragraph nodes of the setter.
    • isEndOfFirstJavadocParagraph

      public static boolean isEndOfFirstJavadocParagraph(DetailNode child)
      Determines if the given child index is the end of the first Javadoc paragraph. The end of the description is defined as 4 consecutive nodes of type NEWLINE, LEADING_ASTERISK, NEWLINE, LEADING_ASTERISK. This is an asterisk that is alone on a line. Just like the one below this line.
      Parameters:
      child - the child to check.
      Returns:
      true if the given child index is the end of the first javadoc paragraph.
    • simplifyTypeName

      public static String simplifyTypeName(String fullTypeName)
      Simplifies type name just to the name of the class, rather than entire package.
      Parameters:
      fullTypeName - full type name.
      Returns:
      simplified type name, that is, name of the class.