Skip to content

Generate Compile Time Proxies for certain @Lazy beans #831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

SentryMan
Copy link
Collaborator

Now will generate proxy classes for true laziness in the following situations with @Lazy beans:

  • class with a no-arg constructor
  • factory bean method returning a class with a no-arg constructor
  • class without a no-arg constructor, but a @BeanTypes class that is an interface or has a no arg constructor
  • same as above but with factory bean methods
  • factory method, but an interface is the return type

given the following class:

@Lazy
@Singleton
public class LazyBean {

  Provider<Integer> intProvider;

  public LazyBean() {}

  @Inject
  public LazyBean(Provider<Integer> intProvider) {
    this.intProvider = intProvider;
  }

  public LazyBean() {}

  void something() {}
}

the following proxy class is generated:

@Proxy
@Generated("avaje-inject-generator")
public final class LazyBean$Lazy extends LazyBean {

  private final Provider<LazyBean> onceProvider;

  public LazyBean$Lazy(Provider<LazyBean> onceProvider) {
    this.onceProvider = onceProvider;
  }

   void something() {
    onceProvider.get().something();
  }

}

and the DI code is modified like

@Generated("io.avaje.inject.generator")
public final class LazyBean$DI  {

  public static void build(Builder builder) {
    if (builder.isBeanAbsent(LazyBean.class)) {
      builder.registerLazy(() -> {
        var bean = new LazyBean(builder.getProvider(Integer.class));
        return bean;
     }, LazyBean$Lazy::new);
    }
  }

}

resolves #830

@SentryMan SentryMan added this to the 11.6 milestone Jun 12, 2025
@SentryMan SentryMan requested a review from rbygrave June 12, 2025 23:51
@SentryMan SentryMan self-assigned this Jun 12, 2025
@SentryMan SentryMan added the enhancement New feature or request label Jun 12, 2025
@SentryMan SentryMan enabled auto-merge June 12, 2025 23:52
@SentryMan SentryMan disabled auto-merge June 14, 2025 03:05
@SentryMan SentryMan enabled auto-merge (squash) June 14, 2025 03:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate generating a proxy for @Lazy annotated beans
1 participant