Annotation Interface Component


@Target(TYPE) @Retention(SOURCE) public @interface Component

Annotates an interface for which a dependency-injected implementation is to be generated. The generated class will have the name of the type annotated, appended with _Impl. For example, @Component interface MyComponent {...} will produce an implementation named MyComponent_Impl.

Component methods

Every type annotated with @Component must contain at least one abstract component method. Component methods may have any name, but must have no parameters and return a bound type. A bound type is one of the following:

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static @interface 
    A builder for a component.
    static @interface 
    A factory for a component.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    If true, the generated component implementation will contain a static mockBuilder method.
    Class<?>[]
    A list of classes annotated with Modulus whose bindings are used to generate the component implementation.
    boolean
    By default, the mockBuilder (or withMocks) method is only package-private.
  • Element Details

    • modules

      Class<?>[] modules
      A list of classes annotated with Modulus whose bindings are used to generate the component implementation.
      Default:
      {}
    • mockBuilder

      boolean mockBuilder
      If true, the generated component implementation will contain a static mockBuilder method. However, if this component uses a Builder, the mockBuilder method will not be generated; see Component.Builder.
      Returns:
      true if the mockBuilder method should be generated.
      Default:
      false
    • publicMockBuilder

      boolean publicMockBuilder
      By default, the mockBuilder (or withMocks) method is only package-private. This makes it harder to accidentally invoke from production code.

      In test code, mockBuilder can always be invoked, even if it is only package-visible, by placing a forwarding delegate class in the correct package. For example, if MyComponent is defined in package com.my.component, the forwarding delegate class could live in src/test/java/com/my/component and look like this:

      
       public class MyComponentAccess {
         public static MyComponent_Impl.MockBuilder mockBuilder() {
             return MyComponent_Impl.mockBuilder();
         }
       }
       
      Returns:
      true if the mockBuilder (or withMocks) method should have the same visibility as the component.
      Default:
      false