001package io.avaje.inject;
002
003import java.lang.annotation.ElementType;
004import java.lang.annotation.Retention;
005import java.lang.annotation.RetentionPolicy;
006import java.lang.annotation.Target;
007
008/**
009 * Marks a class or factory method bean to be initialized lazily.
010 *
011 * <p>When annotating a {@link Factory} class as {@code @Lazy}, the factory itself is not lazy but
012 * all beans that it provides will have lazy initialization.
013 *
014 * <p>If the annotated class or factory method is an interface or has an additional no-args
015 * constructor, a generated proxy bean will be wired for ultimate laziness.
016 */
017@Retention(RetentionPolicy.SOURCE)
018@Target({ElementType.METHOD, ElementType.TYPE, ElementType.PACKAGE, ElementType.MODULE})
019public @interface Lazy {
020  /** Determine the kind of lazy initialization. */
021  Kind value() default Kind.AUTO_PROXY;
022
023  /**
024   * Control whether a compile-time proxy is generated to support lazy initialization.
025   *
026   * <p>When using {@link Kind#FORCE_PROXY} a compile-time error will occur if the conditions for
027   * generating a proxy are not met (for example the class is final or has no no-args constructor).
028   * When using {@link Kind#AUTO_PROXY} a warning will be issued and lazy initialization will fall
029   * back to provider based lazy initialization.
030   *
031   * <p>When using {@link Kind#PROVIDER} no proxy is generated and lazy initialization is done via a
032   * provider.
033   */
034  enum Kind {
035    /**
036     * Ensures that a compile-time proxy is generated, will fail compilation if missing conditions
037     * for generation
038     */
039    FORCE_PROXY,
040    /**
041     * Attempt compile-time proxy, will warn and fallback to provider compilation if missing
042     * conditions for generation
043     */
044    AUTO_PROXY,
045    /** No proxy, use a provider based lazy initialization */
046    PROVIDER
047  }
048}