PreciseBeanType generates an invalid return type for anonymous beans

Open Beginner friendly
#1,286 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

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

Research direction

Start by reading the PreciseBeanType recipe named in the issue and find how it derives a return type from a bean method's implementation. Use the anonymous TenantAware bean shown in the reproducer to check the generated type and whether the source still compiles. Done means the method retains the usable TenantAware return type for this case.

Written by the indexing model from the issue text.

Description

Which recipe were you running?

org.openrewrite.java.spring.boot3.PreciseBeanType

Which versions are you using?
  • rewrite-spring 6.37.1
  • rewrite-maven-plugin 6.46.1
  • Maven 3.9.9, Java 25.0.4, Spring Framework 7.0.10

The same input also produces the invalid return type when running the PreciseBeanType source from tag v6.53.0 with rewrite-core and rewrite-java 8.90.4. The 6.53.0 release artifact was not available from the Maven repositories accessible in this environment, so that binary was not tested.

How are you running OpenRewrite?

Maven plugin. Only the affected recipe is activated:

mvn org.openrewrite.maven:rewrite-maven-plugin:6.46.1:runNoFork \
  -Drewrite.activeRecipes=org.openrewrite.java.spring.boot3.PreciseBeanType \
  -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-spring:6.37.1
Which language or file type is affected?

Java.

What is the smallest input that reproduces the problem?
package example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

interface TenantAware {
    String currentTenant();
}

@Configuration
class TestContext {
    @Bean
    TenantAware tenantAware() {
        return new TenantAware() {
            @Override
            public String currentTenant() {
                return "example";
            }
        };
    }
}
What did you expect to see?

The return type should remain TenantAware: the anonymous implementation does not have a name that can be used as a Java return type. The source should continue to compile.

What did you see instead?

The recipe changes the return type to TestContext.1:

-    TenantAware tenantAware() {
+    TestContext.1 tenantAware() {

The resulting source does not compile. javac reports <identifier> expected and invalid method declaration; return type required at the method.

Dominant language
Java
Stars
408
Forks
164
Avg merge
7h 52m
Merged PRs (30d)
81

Getting set up

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.

More from openrewrite/rewrite-spring

All issues in openrewrite/rewrite-spring

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.