openrewrite/rewrite-spring

repository·main·Indexed 19 days ago

https://github.com/openrewrite/rewrite-spring

A specialized OpenRewrite module providing automated recipes and visitors for Java Spring Boot applications. It enables seamless upgrades, best-practice enforcement, and codebase modernization. Features include the FindConfigurationProperties recipe for cataloging @ConfigurationProperties usage and utilities for generating Spring Boot configuration properties replacement recipes.

Tokens
1.3K
Snippets
0
Records
8
Agent score
62%

What's inside rewrite-spring

  1. What is rewrite-spring?

    main
    The rewrite-spring project provides a collection of OpenRewrite recipes and visitors designed to automatically apply best practices and perform migrations in Java Spring Boot applications. It is used to modernize codebases and automate Spring-specific upgrades.
  2. Understand the limitations of the Moderne Source Available License

    main

    The license grants a non-exclusive, royalty-free, worldwide, non-sublicensable, and non-transferable license to use, copy, distribute internally, and prepare derivative works. However, there are strict usage limitations:

    Service and Distribution Prohibitions

    You may not make the functionality of the Software (or any Modified version) available to third parties as a service. This includes:

    • Enabling third parties to interact with the functionality remotely through a computer network.
    • Offering a product or service where the value derives from the Software.
    • Offering a product or service that accomplishes the same purpose as the Software.

    Prohibited examples include (but are not limited to):

    • Sourcegraph and Sourcegraph Batch Changes
    • Amazon Q Code Transformer
    • Broadcom Application Advisor

    Notice Requirements

    • Licensing Notices: You must not alter, remove, or obscure any licensing, copyright, or other notices of the Licensor.
    • Redistribution: Anyone receiving a copy of the Software or a Modification must also receive a copy of this Agreement.
    • Modifications: If you modify the Software, you must include prominent notices in the Modified copies stating that you have modified the Software.
  3. Accept the Moderne Source Available License

    main
    By installing, downloading, copying, accessing, using, creating derivative works of, or distributing any of the Software provided under this license, you automatically agree to all terms and conditions of the Moderne Source Available License Agreement. If you are acting on behalf of an organization, you represent that you have the authority to bind that organization to this agreement.
  4. How to generate Spring Boot configuration properties replacement recipes

    main

    If you are maintaining or contributing to this project and need to generate new recipes for Spring Boot configuration property replacements, follow these steps:

    1. Run the GeneratePropertiesMigratorConfiguration.java utility located at src/test/java/org/openrewrite/java/spring/internal/GeneratePropertiesMigratorConfiguration.java.
    2. Commit and push the generated changes.
    3. Repeat this process periodically whenever new minor versions of Spring Boot are released.
  5. Use the FindConfigurationProperties recipe to discover configuration properties

    main

    The FindConfigurationProperties recipe is designed to scan Spring Boot codebases to identify and catalog all usages of the @ConfigurationProperties annotation. This is useful for documenting the configuration surface area, auditing patterns, or preparing for configuration schema migrations.

    What the recipe does:

    1. Scans for @ConfigurationProperties: Traverses the AST to find all class declarations using this annotation.
    2. Extracts Prefixes: Identifies the configuration namespace (e.g., "my.service") from the value or prefix attribute.
    3. Emits Structured Data: Produces a DataTable containing:
      • Source file path
      • Fully qualified class name
      • Configuration prefix value
    4. Marks Results: Identifies annotated classes as search results for visibility in the OpenRewrite UI.

    Supported Annotation Formats:

    The recipe handles various ways the annotation may be declared:

    • @ConfigurationProperties("prefix")
    • @ConfigurationProperties(value = "prefix")
    • @ConfigurationProperties(prefix = "prefix")
  6. Understanding bundled libraries and vulnerability scanner alerts

    main

    Artifact scanners may detect vulnerabilities in rewrite-spring JARs because the module bundles copies of old or vulnerable libraries.

    Key details for security teams:

    • Purpose: These libraries are bundled to allow OpenRewrite to exercise the Java compiler internally. This enables the engine to match code patterns from old/vulnerable libraries to identify and repair them in your codebase.
    • Execution Safety: These libraries are stored in META-INF/rewrite/classpath and are never executed. They are used strictly during the compilation phase to define patterns. Because they are not loaded via traditional reflection or class initialization, the vulnerabilities are not exploitable.
    • Non-Shaded: These are not part of a Fat Jar or shaded library; they are isolated to the rewrite classpath.
  7. Find Spring `@ConfigurationProperties` usage

    main

    The FindConfigurationProperties recipe identifies all Java classes annotated with @ConfigurationProperties and extracts their associated configuration prefixes. This is useful for discovering all externalized configuration properties within a Spring Boot application.

    When run, the recipe:

    1. Scans for the @ConfigurationProperties annotation.
    2. Extracts the prefix value (handling literals, identifiers, field access, or assignments like prefix = "value").
    3. Populates a ConfigurationPropertiesTable with the source path, class type, and the extracted prefix.
    4. Marks the discovered annotations in the source code using SearchResult.found to highlight them in IDEs or reporting tools.