imgn inject API GitHub

Zero-Reflection Injection

The problem

Wiring an object graph by hand means every constructor call site repeats the same assembly code, and every new dependency ripples through all of them. Automating that wiring usually costs something at runtime: scanning classes, inspecting members, bypassing visibility. Those costs grow with the graph and resist ahead-of-time analysis.

How it solves it

All wiring code is generated at compile time. An annotation processor emits one Factory subclass per injectable class — plain Java source, compiled along with the rest of the build. At runtime the framework only calls those generated factories; it never inspects a class.

A class opts in by annotating its injection points with @Inject:

public final class GreetingService {
    private final Registry registry;

    @Inject
    Clock clock; // field injection

    @Inject
    public GreetingService(Registry registry) { // constructor injection
        this.registry = registry;
    }

    @Inject
    void connect(Notifier notifier) { // method injection
        // called after construction with a resolved Notifier
    }
}

The generated factory is emitted into the bean's own package, so it can legally call package-private constructors, methods, and fields. Visibility is enforced by the Java compiler at build time, not worked around at runtime.

Generated factories are discovered through ServiceLoader. Creating an injector loads them and builds the binding registry once; the registry is immutable afterwards and safe for concurrent use:

Injector injector = Injector.create();
GreetingService service = injector.getInstance(GreetingService.class);

Because wiring is ordinary generated code, mistakes the processor can see — a missing @Inject constructor, a marker misused — are reported as compile errors, with no cost deferred to runtime.

Limitations