@Test public void testParameterPassthroughIfTypeIsShared() throws Exception { AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader()).getAddonRegistry(); ClassLoader thisLoader = ClassLoaderAdapterPassthroughTest.class.getClassLoader(); ClassLoader loader1 = registry.getAddon(AddonId.from("dep", "1")).getClassLoader(); ClassWithGetterAndSetter local = new ClassWithGetterAndSetter(); local.setPassthrough( (ClassWithPassthroughMethod) loader1.loadClass(ClassWithPassthroughMethod.class.getName()).newInstance()); Object delegate = loader1.loadClass(ClassWithGetterAndSetter.class.getName()).newInstance(); ClassWithGetterAndSetter enhanced = (ClassWithGetterAndSetter) ClassLoaderAdapterBuilder.callingLoader(thisLoader) .delegateLoader(loader1) .enhance(delegate); enhanced.setPassthrough(new ClassWithPassthroughMethod()); Assert.assertNotNull(enhanced); Assert.assertNotNull(enhanced.getPassthrough()); Assert.assertFalse(Proxies.isForgeProxy(enhanced.getPassthrough())); Assert.assertFalse(enhanced.assertPassthroughNotProxied()); }
@Test public void testSharedImplementationTypeIncludedInProxy() throws Exception { AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader()).getAddonRegistry(); ClassLoader thisLoader = ClassLoaderAdapterExceptionProxyTest.class.getClassLoader(); ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep", "1")).getClassLoader(); Class<?> foreignType = dep1Loader.loadClass(ExceptionFactory.class.getName()); try { ExceptionFactory factory = (ExceptionFactory) foreignType.newInstance(); Assert.fail( "Should have received a " + ClassCastException.class.getName() + " but got a real object [" + factory + "]"); } catch (ClassCastException e) { } catch (Exception e) { Assert.fail( "Should have received a " + ClassCastException.class.getName() + " but was: " + e); } Object delegate = foreignType.newInstance(); ExceptionFactory enhancedFactory = (ExceptionFactory) ClassLoaderAdapterBuilder.callingLoader(thisLoader) .delegateLoader(dep1Loader) .enhance(delegate); Assert.assertTrue(Proxies.isForgeProxy(enhancedFactory)); String message = "AbstractA message."; try { enhancedFactory.throwException(message); } catch (MockException e) { Assert.assertTrue(Proxies.isForgeProxy(e)); Assert.assertEquals(message, e.getMessage()); } catch (Exception e) { Assert.fail("Exception was not of a compatible type."); } }
public AbstractRuleProvider() { /* * In the case of a proxy, the no-args constructor will be called. This is the case even if the provider * itself would normally have a metadata param passed in. * * Once completed, the getMetadata() method will be proxied correctly, so this is ok. Just allow it to pass * in this case. */ if (Proxies.isForgeProxy(this) && !Annotations.isAnnotationPresent(getClass(), RuleMetadata.class)) return; if (!Annotations.isAnnotationPresent(getClass(), RuleMetadata.class)) { throw new IllegalStateException( getClass().getName() + " must either " + "be abstract, or specify @" + RuleMetadata.class.getName() + ", or call a super() constructor and provide " + RuleProviderMetadata.class.getName()); } this.metadata = MetadataBuilder.forProvider(getClass()); }