openrewrite / openrewrite/rewrite-migrate-java

`AddMissingMethodImplementation` generates wrong method for constructors

Open
#360 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug java
Dominant language
Java
Stars
156
Forks
130
Avg merge
20h 57m
Merged PRs (30d)
21

Description

What version of OpenRewrite are you using?

I am using

  • OpenRewrite v8.9.4
  • Maven/Gradle plugin RELEASE
  • rewrite-migrate-java v2.3.0

How are you running OpenRewrite?

I'm applying rules using the maven plugin and a custom recipe artifact

What is the smallest, simplest way to reproduce the problem?

package org.openrewrite.java.migrate;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class AddMissingMethodImplementationTest implements RewriteTest {

    @ParameterizedTest
    @ValueSource(strings = {
      "*..* <constructor>(String)",
      "org.openrewrite.example.ImplementationClass <constructor>(String)",
    })
    void addMissingConstructor(String methodPattern) {
        @Language("java")
        final String abstractClassTemplate = """
          package org.openrewrite.example;

          public abstract class AbstractClass {
              private final String param;
              
              protected AbstractClass(String param) {
                  this.param = param;
              }
              
              public String getParam() {
                  return param;
              }
          }
          """;

        @Language("java")
        final String originalClass = """
          package org.openrewrite.example;

          public class ImplementationClass extends AbstractClass {
          }
          """;

        @Language("java")
        final String expected = """
          package org.openrewrite.example;
                    
          public class ImplementationClass extends AbstractClass {
                    
              public ImplementationClass(String param) {
                  super(param);
              }
          }
          """;

        rewriteRun(
          spec -> spec
            .parser(JavaParser.fromJavaVersion().dependsOn(abstractClassTemplate))
            .recipe(new AddMissingMethodImplementation(
              "org.openrewrite.example.AbstractClass",
              methodPattern,
              """
                public ImplementationClass(String param) {
                    super(param);
                }"""
            )),
          java(originalClass, expected)
        );
    }
}

What did you expect to see?

package org.openrewrite.example;
  
public class ImplementationClass extends AbstractClass {
    public ImplementationClass(String param) {
        super(param);
    }
}

What did you see instead?

 package org.openrewrite.example;
 
 public class ImplementationClass extends AbstractClass {
-    public ImplementationClass(String param) {
+    public Template ImplementationClass(String param) {
         super(param);
     }
 }

What is the full stack trace of any errors you encountered?

recipe completes successfully

Are you interested in contributing a fix to OpenRewrite?

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

Start with the supplied AddMissingMethodImplementationTest reproduction and inspect AddMissingMethodImplementation for how constructor method patterns are rendered. Run the parameterized test for both method patterns; done means the generated constructor is named ImplementationClass without the erroneous Template return type and matches the expected code.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.