@SuppressWarnings("unchecked") public static <P> P maybeUnwrapProxy(final P instance) { if (instance instanceof Proxy) { return (P) ((Proxy<P>) instance).unwrap(); } else { return instance; } }
@Override protected void afterDestroyInstance(final Object instance) { if (instance instanceof Proxy) { final Proxy<?> proxy = (Proxy<?>) instance; final Object rawInstance = proxy.unwrap(); instances.values().remove(rawInstance); proxy.clearInstance(); } else { throw new IllegalArgumentException("Cannot destroy ApplicationScoped bean without reference to proxy."); } } }
@Override public T getInstance() { final T instance = contextManager.getInstance(handle.getFactoryName()); if (instance instanceof Proxy) { @SuppressWarnings("unchecked") final Proxy<T> proxy = (Proxy<T>) instance; // Forces bean to be loaded. proxy.unwrap(); } return instance; }
@SuppressWarnings("unchecked") @Override public <T> T getEagerInstance(final String factoryName) { final T instance = getContext(factoryName).<T>getInstance(factoryName); if ((instance instanceof Proxy)) { ((Proxy<T>) instance).unwrap(); } return instance; }