TemplateModelProxyHandler implements Serializable { public static <T> T createModelProxy(StateNode stateNode, BeanModelType<T> modelType) { assert stateNode != null; assert modelType != null; Class<T> proxyType = modelType.getProxyType(); Object proxy = proxyConstructors.get(proxyType).apply(stateNode, modelType); return proxyType.cast(proxy); } private  TemplateModelProxyHandler(); @RuntimeType @SuppressWarnings("static-method") Object intercept(@This Object target, @Origin Method method,
            @AllArguments Object[] args); static T createModelProxy(StateNode stateNode,
            BeanModelType<T> modelType); static StateNode getStateNodeForProxy(Object proxy); static BeanModelType<?> getModelTypeForProxy(Object proxy); static boolean isProxy(Object proxy);  }
@Test public void testEquals() { EmptyModelTemplate emptyModelTemplate1 = new EmptyModelTemplate(); EmptyModelTemplate emptyModelTemplate2 = new EmptyModelTemplate(); TemplateModel m1 = emptyModelTemplate1.getModel(); TemplateModel m2 = emptyModelTemplate2.getModel(); Assert.assertSame(m1.getClass(), m2.getClass()); Assert.assertFalse(m1.equals(null)); Assert.assertFalse(m1.equals("foobar")); Assert.assertFalse(m1.equals(m2)); ModelDescriptor<EmptyModel> realModelType = ModelDescriptor .get(EmptyModel.class); Assert.assertTrue(m1.equals(TemplateModelProxyHandler.createModelProxy( emptyModelTemplate1.getElement().getNode(), realModelType))); class TestTemplateModelType extends BeanModelType<TemplateModel> { public TestTemplateModelType() { super(TemplateModel.class, PropertyFilter.ACCEPT_ALL, true); } } BeanModelType<TemplateModel> wrongModelType = new TestTemplateModelType(); Assert.assertFalse(m1.equals(TemplateModelProxyHandler.createModelProxy( emptyModelTemplate1.getElement().getNode(), wrongModelType))); Assert.assertTrue(m2.equals(m2)); }
@Test public void objectMethodIsNotIntercepted() { EmptyModelTemplate template = new EmptyModelTemplate(); Model proxy = TemplateModelProxyHandler.createModelProxy( template.getElement().getNode(), new TestModelType()); Assert.assertEquals(System.identityHashCode(proxy), proxy.hashCode()); }
@Test public void notAccessorIsNotIntercepted() { EmptyModelTemplate template = new EmptyModelTemplate(); Model proxy = TemplateModelProxyHandler.createModelProxy( template.getElement().getNode(), new TestModelType()); Assert.assertEquals("foo", proxy.toString()); }
@Test(expected = IllegalArgumentException.class) public void noDefaultConstructor_throwsException() { EmptyModelTemplate template = new EmptyModelTemplate(); TemplateModelProxyHandler.createModelProxy( template.getElement().getNode(), new BeanModelType<>( BadModel.class, PropertyFilter.ACCEPT_ALL, false)); }
@Test(expected = IllegalArgumentException.class) public void nonStaticNestedClass_throwsException() { EmptyModelTemplate template = new EmptyModelTemplate(); TemplateModelProxyHandler.createModelProxy( template.getElement().getNode(), new BeanModelType<>(NotStaticModel.class, PropertyFilter.ACCEPT_ALL, false)); }