ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep", "1")).getClassLoader();
Class<?> foreignType = dep1Loader.loadClass(FACTORY_IMPL_TYPE);
try
{
SuperInterface local = (SuperInterface) foreignType.getMethod("getInstance")
.invoke(foreignType.newInstance());
Assert.fail("Should have received a " + ClassCastException.class.getName() + " but got a real object ["
+ local + "]");
}
catch (ClassCastException e)
{
}
catch (Exception e)
{
Assert.fail("Should have received a " + ClassCastException.class.getName() + " but was: " + e);
}
Object delegate = foreignType.newInstance();
InstanceFactory enhancedFactory = (InstanceFactory) ClassLoaderAdapterBuilder.callingLoader(thisLoader)
.delegateLoader(dep1Loader).enhance(delegate);
Assert.assertTrue(Proxies.isForgeProxy(enhancedFactory));
SuperInterface enhancedInstance = (SuperInterface) enhancedFactory.getRawInstance();
Assert.assertTrue(Proxies.isForgeProxy(enhancedInstance));
}