Skip to content

Support changing from a constructor to a factory method in ChangeMethodTargetToStatic #1804

@rdifrango

Description

@rdifrango

I would like to be able to convert code that is calling new Object to a Factory method perhaps in ChangeMethodTargetToStatic.

Here's an example of one of the conversions I'd like to see:

MyCustomLogger logger = new MyCustomLogger(MyClass.class);

That I want to convert to proper SLF4J loggers so it would be:

Logger logger = LoggerFactory.getLogger(MyClass.class);

I tried the following but it didn't change anything:

---
type: specs.openrewrite.org/v1beta/recipe
name: com.difrango.ChangeSimpleTrace
displayName: Change method target to static example
recipeList:
  - org.openrewrite.java.ChangeMethodTargetToStatic:
      methodPattern: com.difrango.MyCustomLogger <constructor>(Class)
      fullyQualifiedTargetTypeName: org.slf4j.LoggerFactory
      returnType: org.slf4j.Logger

Metadata

Metadata

Assignees

Labels

discoveryInvestigate and determine what should be done

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions