openrewrite / openrewrite/rewrite
Augment TypeUtils/Primitive with a way to box primitives
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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