imgn inject API GitHub

Processor Plugins

The problem

Some injected values are derived from the injection site itself rather than resolved from the graph — the canonical case being a logger named after the class that holds it. A graph binding cannot express "a different value per declaring class", and writing Logger.getLogger(...) in every class is the boilerplate injection exists to remove.

How it solves it

The annotation processor is extensible. A plugin pairs a custom annotation, meta-annotated with @InjectingAnnotation, with an InjectPlugin implementation discovered through ServiceLoader on the processor path, or on the processor module path (see module path). For each injection site carrying the annotation, the plugin emits the initializer code that the generated factory will contain — the value is computed by generated code at build time, not looked up in the registry.

The bundled @Log plugin:

@InjectingAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
public @interface Log {}

public final class LogPlugin implements InjectPlugin {
    @Override
    public Optional<CodeBlock> initializer(InjectionSite site) {
        if (site.element().getAnnotation(Log.class) == null) {
            return Optional.empty();
        }
        // emits: Logger.getLogger(<declaring class>.class.getName())
        return Optional.of(CodeBlock.of("$T.getLogger($T.class.getName())",
                LOGGER, ClassName.of(site.beanType())));
    }
}

Usage — note the field needs only the plugin's annotation, and the site's declaring class drives the value:

public final class GreetingService {
    @Log
    Logger logger; // named "…GreetingService"
}

@Singleton
public final class Registry {
    @Inject
    public Registry(@Log Logger logger) { // named "…Registry"
        ...
    }
}

Because the plugin runs inside the processor, its output is ordinary generated source: visible in the build output, debuggable, and free at runtime.

Packaging

A plugin ships as two artifacts, mirroring inject / inject-processor:

<dependency>
    <groupId>be.imgn.inject</groupId>
    <artifactId>inject-log</artifactId>
    <version>${inject.version}</version>
</dependency>

<!-- maven-compiler-plugin -->
<annotationProcessorPaths>
    <path>
        <groupId>be.imgn.inject</groupId>
        <artifactId>inject-processor</artifactId>
        <version>${inject.version}</version>
    </path>
    <path>
        <groupId>be.imgn.inject</groupId>
        <artifactId>inject-log-processor</artifactId>
        <version>${inject.version}</version>
    </path>
</annotationProcessorPaths>

Limitations