@DynamicObjectAdapterBuilder
Generate a builder for an object adapter with one withXxx() slot per method.
@DynamicObjectAdapterBuilder generates a builder and an invocation handler
pair. The builder collects an original instance and per-method overrides; the
result is a proxy that prefers the override when one is registered and
delegates to the original otherwise.
Usage
import com.svenruppert.proxybuilder.objectadapter.dynamic.DynamicObjectAdapterBuilder;
@DynamicObjectAdapterBuilder
public interface Service {
String work(String input);
}
The processor generates ServiceAdapterBuilder. Use it like this:
Service adapter = ServiceAdapterBuilder.newBuilder()
.setOriginal(new ServiceImpl())
.withWork(input -> "adapted " + input)
.build();
adapter.work("hello"); // returns "adapted hello"
Each interface method gets a withXxx(...) method on the builder. The
parameter type is the matching functional interface.
How it compares to @StaticObjectAdapter
| Aspect | @StaticObjectAdapter | @DynamicObjectAdapterBuilder |
|---|---|---|
| Override surface | Setters on the adapter class itself | Builder collected before construction |
| Mutability after construction | Yes — call withXxx(...) again on the adapter | No — builder freezes overrides at build() |
| Under-the-hood implementation | Direct method calls in generated class | JDK invocation handler |
| Best for | Test stubs, single-spot overrides | Adapter factories, configuration objects |
In short: @StaticObjectAdapter is for “I want a typed slot per method on the
adapter object.” @DynamicObjectAdapterBuilder is for “I want to build an
adapter once and hand it to the rest of the code.”
When to prefer this
- The override set is decided at construction time (configuration, feature flag, test fixture).
- You want a builder API in your own code that aggregates several overrides.
When the other one wins
- You want to modify overrides after construction.
- You want the simplest possible generated code, with no invocation handler.