Skip to content

Latest commit

 

History

History
189 lines (153 loc) · 5.11 KB

renameprivatefieldstocamelcase.md

File metadata and controls

189 lines (153 loc) · 5.11 KB

Reformat private field names to camelCase

org.openrewrite.staticanalysis.RenamePrivateFieldsToCamelCase

Reformat private field names to camelCase to comply with Java naming convention. The recipe will not rename fields with default, protected or public access modifiers.The recipe will not rename private constants.The first character is set to lower case and existing capital letters are preserved. Special characters that are allowed in java field names $ and _ are removed. If a special character is removed the next valid alphanumeric will be capitalized. The recipe will not rename a field if the result already exists in the class, conflicts with a java reserved keyword, or the result is blank.

Tags

  • RSPEC-116
  • RSPEC-3008

Source

GitHub, Issue Tracker, Maven Central

  • groupId: org.openrewrite.recipe
  • artifactId: rewrite-static-analysis
  • version: 1.0.1

Example

{% tabs %} {% tab title="Test.java" %}

Before

{% code title="Test.java" %}

class Test {
    private int DoChange = 10;
    public int DoNotChangePublicMember;
    int DoNotChangeDefaultMember;

    public int getTen() {
        return DoChange;
    }

    public int getTwenty() {
        return this.DoChange * 2;
    }

    public int getThirty() {
        return DoChange * 3;
    }
}

{% endcode %}

After

{% code title="Test.java" %}

class Test {
    private int doChange = 10;
    public int DoNotChangePublicMember;
    int DoNotChangeDefaultMember;

    public int getTen() {
        return doChange;
    }

    public int getTwenty() {
        return this.doChange * 2;
    }

    public int getThirty() {
        return doChange * 3;
    }
}

{% endcode %}

{% endtab %} {% tab title="Diff" %} {% code %}

--- Test.java
+++ Test.java
@@ -2,1 +2,1 @@
class Test {
-   private int DoChange = 10;
+   private int doChange = 10;
    public int DoNotChangePublicMember;
@@ -7,1 +7,1 @@

    public int getTen() {
-       return DoChange;
+       return doChange;
    }
@@ -11,1 +11,1 @@

    public int getTwenty() {
-       return this.DoChange * 2;
+       return this.doChange * 2;
    }
@@ -15,1 +15,1 @@

    public int getThirty() {
-       return DoChange * 3;
+       return doChange * 3;
    }

{% endcode %} {% endtab %} {% endtabs %}

Usage

This recipe has no required configuration options. It can be activated by adding a dependency on org.openrewrite.recipe:rewrite-static-analysis:1.0.1 in your build file or by running a shell command (in which case no build changes are needed): {% tabs %} {% tab title="Gradle" %} {% code title="build.gradle" %}

plugins {
    id("org.openrewrite.rewrite") version("6.1.4")
}

rewrite {
    activeRecipe("org.openrewrite.staticanalysis.RenamePrivateFieldsToCamelCase")
}

repositories {
    mavenCentral()
}

dependencies {
    rewrite("org.openrewrite.recipe:rewrite-static-analysis:1.0.1")
}

{% endcode %} {% endtab %} {% tab title="Maven POM" %} {% code title="pom.xml" %}

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>5.2.4</version>
        <configuration>
          <activeRecipes>
            <recipe>org.openrewrite.staticanalysis.RenamePrivateFieldsToCamelCase</recipe>
          </activeRecipes>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.openrewrite.recipe</groupId>
            <artifactId>rewrite-static-analysis</artifactId>
            <version>1.0.1</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

{% endcode %} {% endtab %}

{% tab title="Maven Command Line" %} {% code title="shell" %} You will need to have Maven installed on your machine before you can run the following command.

mvn -U org.openrewrite.maven:rewrite-maven-plugin:run \
  -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-static-analysis:RELEASE \
  -Drewrite.activeRecipes=org.openrewrite.staticanalysis.RenamePrivateFieldsToCamelCase

{% endcode %} {% endtab %} {% endtabs %}

Contributors

See how this recipe works across multiple open-source repositories

Moderne Link Image

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.

Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.