ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep", "1")).getClassLoader();
Class<?> foreignType = dep1Loader.loadClass(CustomPrintStreamFactory.class.getName());
try
{
@SuppressWarnings({ "unused" })
CustomPrintStream custom = (CustomPrintStream) foreignType.getMethod("getPrintStream")
.invoke(foreignType.newInstance());
Assert.fail("Should have received a classcast exception");
}
catch (ClassCastException e)
{
}
Object delegate = foreignType.newInstance();
CustomPrintStreamFactory enhancedFactory = (CustomPrintStreamFactory) ClassLoaderAdapterBuilder
.callingLoader(thisLoader)
.delegateLoader(dep1Loader).enhance(delegate);
Assert.assertTrue(Proxies.isForgeProxy(enhancedFactory));
PrintStream result = enhancedFactory.getPrintStream();
Assert.assertFalse(Proxies.isForgeProxy(result));
File file = File.createTempFile("furnace", "printStream");
file.deleteOnExit();
enhancedFactory.usePrintStream(new CustomPrintStream(file));
try
{
@SuppressWarnings("unused")
CustomPrintStream customResult = enhancedFactory.getCustomPrintStream();
Assert.fail("Should have received a classcast exception");
}
catch (ClassCastException e)
{