Skip to content

Latest commit

 

History

History
181 lines (154 loc) · 5.09 KB

primitivewrapperclassconstructortovalueof.md

File metadata and controls

181 lines (154 loc) · 5.09 KB

Use primitive wrapper valueOf method

org.openrewrite.staticanalysis.PrimitiveWrapperClassConstructorToValueOf

The constructor of all primitive types has been deprecated in favor of using the static factory method valueOf available for each of the primitive type wrappers.

Tags

  • RSPEC-2129

Source

GitHub, Issue Tracker, Maven Central

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

Example

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

Before

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

class A {
    Boolean bool = new Boolean(true);
    Byte b = new Byte("1");
    Character c = new Character('c');
    Double d = new Double(1.0);
    Float f = new Float(1.1f);
    Long l = new Long(1);
    Short sh = new Short("12");
    short s3 = 3;
    Short sh3 = new Short(s3);
    Integer i = new Integer(1);
}

{% endcode %}

After

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

class A {
    Boolean bool = Boolean.valueOf(true);
    Byte b = Byte.valueOf("1");
    Character c = Character.valueOf('c');
    Double d = Double.valueOf(1.0);
    Float f = Float.valueOf(1.1f);
    Long l = Long.valueOf(1);
    Short sh = Short.valueOf("12");
    short s3 = 3;
    Short sh3 = Short.valueOf(s3);
    Integer i = Integer.valueOf(1);
}

{% endcode %}

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

--- A.java
+++ A.java
@@ -2,7 +2,7 @@
class A {
-   Boolean bool = new Boolean(true);
-   Byte b = new Byte("1");
-   Character c = new Character('c');
-   Double d = new Double(1.0);
-   Float f = new Float(1.1f);
-   Long l = new Long(1);
-   Short sh = new Short("12");
+   Boolean bool = Boolean.valueOf(true);
+   Byte b = Byte.valueOf("1");
+   Character c = Character.valueOf('c');
+   Double d = Double.valueOf(1.0);
+   Float f = Float.valueOf(1.1f);
+   Long l = Long.valueOf(1);
+   Short sh = Short.valueOf("12");
    short s3 = 3;
@@ -10,2 +10,2 @@
    Short sh = new Short("12");
    short s3 = 3;
-   Short sh3 = new Short(s3);
-   Integer i = new Integer(1);
+   Short sh3 = Short.valueOf(s3);
+   Integer i = Integer.valueOf(1);
}

{% 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.PrimitiveWrapperClassConstructorToValueOf")
}

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.PrimitiveWrapperClassConstructorToValueOf</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.PrimitiveWrapperClassConstructorToValueOf

{% 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.