Object triggerLazyLoading(Object obj) throws IllegalAccessException { for (Field field : ReflectionUtils.getDeclaredAndInheritedFields(obj.getClass(), false)) { field.setAccessible(true); if (field.getName().equals("CGLIB$CALLBACK_0")) { try { return ((LazyLoader) field.get(obj)).loadObject(); } catch (Exception e) { throw new JcrMappingException("Could not trigger lazy loading", e); } } } return obj; }
@Test public void testAdaptProxiedType() throws Exception { ClassLoader loader = MockService.class.getClassLoader(); final MockService internal = new MockService(); MockService delegate = (MockService) Enhancer.create(MockService.class, new LazyLoader() { @Override public Object loadObject() throws Exception { return internal; } }); MockService adapter = ClassLoaderAdapterBuilder.callingLoader(loader).delegateLoader(loader) .enhance(delegate, MockService.class); Assert.assertNotEquals(internal.getResult(), adapter.getResult()); Assert.assertEquals(internal.getResult().getValue(), adapter.getResult().getValue()); }
@Test public void testAdaptProxiedResult() throws Exception { ClassLoader loader = MockService.class.getClassLoader(); final MockService internal = new MockService(); MockService delegate = (MockService) Enhancer.create(MockService.class, new LazyLoader() { @Override public Object loadObject() throws Exception { return internal; } }); MockService adapter = ClassLoaderAdapterBuilder.callingLoader(loader).delegateLoader(loader) .enhance(delegate, MockService.class); Assert.assertNotEquals(internal.getResult(), adapter.getResultEnhanced()); }
@Test public void testAdaptProxiedResultReturnTypeObject() throws Exception { ClassLoader loader = MockService.class.getClassLoader(); final MockService internal = new MockService(); MockService delegate = (MockService) Enhancer.create(MockService.class, new LazyLoader() { @Override public Object loadObject() throws Exception { return internal; } }); MockService adapter = ClassLoaderAdapterBuilder.callingLoader(loader).delegateLoader(loader) .enhance(delegate, MockService.class); Assert.assertNotEquals(internal.getResult(), adapter.getResultEnhancedReturnTypeObject()); }
@Test(expected = ClassCastException.class) public void testCannotAdaptFinalResultReturnType() throws Exception { ClassLoader loader = MockService.class.getClassLoader(); final MockService internal = new MockService(); MockService delegate = (MockService) Enhancer.create(MockService.class, new LazyLoader() { @Override public Object loadObject() throws Exception { return internal; } }); MockService adapter = ClassLoaderAdapterBuilder.callingLoader(loader).delegateLoader(loader) .enhance(delegate, MockService.class); adapter.getResultFinalReturnType(); }
@Test public void testCanAdaptFinalResultReturnTypeObject() throws Exception { ClassLoader loader = MockService.class.getClassLoader(); final MockService internal = new MockService(); MockService delegate = (MockService) Enhancer.create(MockService.class, new LazyLoader() { @Override public Object loadObject() throws Exception { return internal; } }); MockService adapter = ClassLoaderAdapterBuilder.callingLoader(loader).delegateLoader(loader) .enhance(delegate, MockService.class); Assert.assertNotEquals(internal.getResultFinalReturnTypeObject(), adapter.getResultFinalReturnTypeObject()); Assert.assertEquals(((Result) internal.getResultFinalReturnTypeObject()).getValue(), ((Result) adapter.getResultFinalReturnTypeObject()).getValue()); }
@Test public void testAdaptFinalResult() throws Exception { ClassLoader loader = MockService.class.getClassLoader(); final MockService internal = new MockService(); MockService delegate = (MockService) Enhancer.create(MockService.class, new LazyLoader() { @Override public Object loadObject() throws Exception { return internal; } }); MockService adapter = ClassLoaderAdapterBuilder.callingLoader(loader).delegateLoader(loader) .enhance(delegate, MockService.class); Assert.assertNotEquals(internal.getResultInterfaceFinalImpl(), adapter.getResultInterfaceFinalImpl()); Object unwrapped = Proxies.unwrap(adapter); Assert.assertNotSame(adapter, unwrapped); }
@Test public void testNullValuesAsMethodParameters() throws Exception { ClassLoader loader = MockService.class.getClassLoader(); final MockService internal = new MockService(); MockService delegate = (MockService) Enhancer.create(MockService.class, new LazyLoader() { @Override public Object loadObject() throws Exception { return internal; } }); MockService adapter = ClassLoaderAdapterBuilder.callingLoader(loader).delegateLoader(loader) .enhance(delegate, MockService.class); Assert.assertNull(internal.echo(null)); Assert.assertNull(adapter.echo(null)); }
@Test public void testShouldNotProxyJavaNioPath() throws Exception { ClassLoader loader = JavaIOFactory.class.getClassLoader(); final JavaIOFactory internal = new JavaIOFactory(); JavaIOFactory delegate = (JavaIOFactory) Enhancer.create(JavaIOFactory.class, new LazyLoader() { @Override public Object loadObject() throws Exception { return internal; } }); JavaIOFactory adapter = ClassLoaderAdapterBuilder.callingLoader(loader).delegateLoader(loader) .enhance(delegate, JavaIOFactory.class); Path path = adapter.getPath(); Assert.assertFalse(Proxies.isProxyType(path.getClass())); }
@Test public void testShouldNotProxyJavaUtilLogging() throws Exception { ClassLoader loader = JavaUtilLoggingFactory.class.getClassLoader(); final JavaUtilLoggingFactory internal = new JavaUtilLoggingFactory(); JavaUtilLoggingFactory delegate = (JavaUtilLoggingFactory) Enhancer.create(JavaUtilLoggingFactory.class, new LazyLoader() { @Override public Object loadObject() throws Exception { return internal; } }); JavaUtilLoggingFactory adapter = ClassLoaderAdapterBuilder.callingLoader(loader).delegateLoader(loader) .enhance(delegate, JavaUtilLoggingFactory.class); LogRecord logRecord = adapter.getLogRecord(); Assert.assertFalse(Proxies.isProxyType(logRecord.getClass())); Assert.assertFalse(Proxies.isProxyType(logRecord.getLevel().getClass())); }
Object triggerLazyLoading(Object obj) throws IllegalAccessException { for (Field field : ReflectionUtils.getDeclaredAndInheritedFields(obj.getClass(), false)) { field.setAccessible(true); if (field.getName().equals("CGLIB$CALLBACK_0")) { try { return ((LazyLoader) getObject(field, obj)).loadObject(); } catch (Exception e) { throw new JcrMappingException("Could not trigger lazy loading", e); } } } return obj; }
public MockResult getResultEnhanced() { final MockResult internal = new MockResult(); MockResult delegate = (MockResult) Enhancer.create(MockResult.class, new LazyLoader() { @Override public Object loadObject() throws Exception { return internal; } }); return delegate; }
static Object getLazyLoadingProxiedEntity(final Session session, final Class<?> entityClass, final String dn) { Object entity = null; if (isDistinguishable(entityClass)) { entity = Enhancer.create(entityClass, new Class[] { Persistent.class, Distinguishable.class }, FINALIZE_AND_DISTINGUISHABLE_INTEFERCE_FILTER, new Callback[] { new LazyLoader() { public Object loadObject() throws Exception { return session.read(entityClass, dn); } }, NoOp.INSTANCE } ); ((Distinguishable)entity).setDN(dn); } else { entity = Enhancer.create(entityClass, new Class[] { Persistent.class }, FINALIZE_FILTER, new Callback[] { new LazyLoader() { public Object loadObject() throws Exception { return session.read(entityClass, dn); } }, NoOp.INSTANCE } ); } return entity; }