openrewrite / openrewrite/rewrite

Augment TypeUtils/Primitive with a way to box primitives

Open
#6,563 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
3.7k
Forks
570
Avg merge
13h 12m
Merged PRs (30d)
261

Description

What problem are you trying to solve?

When trying to change a MethodDeclaration where the return type is a Primitive to an Optional<SameType>, I was expecting TypeUtils or Primitive to have something that does the conversion for me.

When I now have a method like public void int convertMe(), it is making public void Optional<int> convertMe() and I need an additional block to transfer Primitives. This might be an operation which might be needed more often and could potentially make it into either TypeUtils or Primitive.

Have you considered any alternatives or workarounds?

Currently, I have this block

                    if (returnType instanceof JavaType.Primitive) {
                        //Get Boxed type
                        String boxedType = ((JavaType.Primitive) returnType).getClassName();
                        //Build type tree
                        TypeTree newReturnTypeExpression = TypeTree.build(boxedType);
                        //As we do not need an import for these, we can reduce them to the Identifier only, but need to transfer type information from FieldAccess to Identifier.
                        if (newReturnTypeExpression instanceof J.FieldAccess) {
                            newReturnTypeExpression = ((J.FieldAccess) newReturnTypeExpression).getName().withType(newReturnTypeExpression.getType());
                        }
                        //Now also move Markers/Prefix from existing Primitive to boxed Identifier.
                        newReturnTypeExpression = newReturnTypeExpression
                                .withPrefix(method.getReturnTypeExpression().getPrefix())
                                .withMarkers(method.getReturnTypeExpression().getMarkers());

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read TypeUtils and JavaType.Primitive, then trace how a MethodDeclaration return type is represented and transformed into a TypeTree. Compare the requested operation with the workaround shown in the issue, including preservation of prefix and markers. Done means callers can reuse a primitive-to-boxed conversion for the Optional return-type case.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.