Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe to replace autoboxing with explicit valueOf #288

Open
JasperTeng opened this issue May 7, 2024 · 1 comment
Open

Recipe to replace autoboxing with explicit valueOf #288

JasperTeng opened this issue May 7, 2024 · 1 comment
Labels

Comments

@JasperTeng
Copy link

What problem are you trying to solve?

When using ParameterizedLogging, the output is not compatible in my IDE, since restriction is placed on autoboxing usage.
I wish I can apply another recipe that performs automatic wrapping of those primitives.
Note1: its fine if all applicable primitives are changed, since the entire project doesn't use autoboxing in the first place.
Note2: should be applicable to all primitives that can be autoboxed. i.e.: byte, short, int, long, float, double, char, boolean

What precondition(s) should be checked before applying this recipe?

Nil.

Describe the situation before applying the recipe

class A {
    void bar() {
        int i = 0;
        foo(i);
    }
    void foo(Integer bar) {
        // do something
    }
}

Describe the situation after applying the recipe

class A {
    void bar() {
        int i = 0;
        foo(Integer.valueOf(i));
    }
    void foo(Integer bar) {
        // do something
    }
}

Have you considered any alternatives or workarounds?

Unfortunately, I did not manage to fine any recipe, or rewrite tool, that supports this.
I was hoping IDE (Eclipse) can separate autoboxing and autounboxing criticality, but it doesn't allow me to do so.

Any additional context

Nil.

Are you interested in contributing this recipe to OpenRewrite?

Sure, if someone more experienced tells me its possible to create such a recipe.
I am afraid to dig deep into it, only to find out it is not possible with the current openrewrite concept.

@timtebeek timtebeek transferred this issue from openrewrite/rewrite May 7, 2024
@timtebeek timtebeek changed the title recipe to remove use of autoboxing Recipe to replace autoboxing with explicit valueOf May 7, 2024
@timtebeek
Copy link
Contributor

Hi @JasperTeng ; Thanks for the suggestion! The closest we have right now is this recipe to remove autoboxing where Boolean's are involved:

public class AvoidBoxedBooleanExpressions extends Recipe {
@Override
public String getDisplayName() {
return "Avoid boxed boolean expressions";
}
@Override
public String getDescription() {
return "Under certain conditions the `java.lang.Boolean` type is used as an expression, " +
"and it may throw a `NullPointerException` if the value is null.";
}

It sounds like your suggestion would be a separate recipe where we visit any method invocation, figure out if the types are boxed versions of the argument types passed in, and if so replace the arguments with explicit boxing. Would you also want to handle cases where there's a need to go from Integer to int?

I think it should be possible to create such a recipe with the type information that we have. You can look here to get started with either a fork of this repository, or a custom recipe module for your own purposes based on the moderneinc/rewrite-recipe-starter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Recipes Wanted
Development

No branches or pull requests

2 participants