Class LazyInitialization
- java.lang.Object
-
- org.perfectable.introspection.proxy.LazyInitialization
-
public final class LazyInitialization extends Object
Helper class to create proxy which delegate invocation to lazily-initialized instance.Proxies are created by
createProxy(java.lang.Class<T>, org.perfectable.introspection.proxy.LazyInitialization.Initializer<? extends T>)
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
LazyInitialization.Initializer<T>
Provider interface for proxy delegation target.static interface
LazyInitialization.Proxy<T>
Additional interface that all proxies created bycreateProxy(java.lang.Class<T>, org.perfectable.introspection.proxy.LazyInitialization.Initializer<? extends T>)
will implement.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
createProxy(Class<T> resultClass, LazyInitialization.Initializer<? extends T> initializer)
Creates a proxy that forwards invocations to lazily initialized instance.
-
-
-
Method Detail
-
createProxy
public static <T> T createProxy(Class<T> resultClass, LazyInitialization.Initializer<? extends T> initializer)
Creates a proxy that forwards invocations to lazily initialized instance.On any method call on resulting proxy it will either initialize instance and cache it, or use existing instance, and delegate the call to it, returning the result or rethrowing exception of the call. There are two exceptions of this rule:
Object.finalize()
is always ignored when called on proxy- Methods from
LazyInitialization.Proxy
interface are treated specially and handled with separate route. They won't necessarily initialize the proxy and won't be forwarded to the target.
Proxies will have
resultClass
as a superclass/superinterface. Additionally, proxy class will implementLazyInitialization.Proxy
interface. This wont be reflected in the compile-time type, but it can be checked by instanceof and it can be casted to it.- Type Parameters:
T
- type of proxy- Parameters:
resultClass
- class or interface that proxy has to implement. This is also a superclass/superinterface of initialized instanceinitializer
- method of initializing actual delegation target- Returns:
- proxy object
-
-