From 5850d3f078e0c7f262c3fa0117d443744e63b1df Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Thu, 29 Jun 2023 09:44:39 +0200 Subject: [PATCH] HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools - Add new test case 'org.hibernate.tool.orm.jbt.wrp.EntityPersisterWrapperFactoryTest#testGetPropertyTypes()' - Handle the 'getPropertyType()' case separately in 'rg.hibernate.tool.orm.jbt.wrp.EntityPersisterWrapperFactory.EntityPersisterInvocationHandler#invoke(...)' to wrap the result array Signed-off-by: Koen Aers --- .../jbt/wrp/EntityPersisterWrapperFactory.java | 15 +++++++++++++++ .../wrp/EntityPersisterWrapperFactoryTest.java | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/jbt/src/main/java/org/hibernate/tool/orm/jbt/wrp/EntityPersisterWrapperFactory.java b/jbt/src/main/java/org/hibernate/tool/orm/jbt/wrp/EntityPersisterWrapperFactory.java index f2ee06ca41..4f73baeae7 100644 --- a/jbt/src/main/java/org/hibernate/tool/orm/jbt/wrp/EntityPersisterWrapperFactory.java +++ b/jbt/src/main/java/org/hibernate/tool/orm/jbt/wrp/EntityPersisterWrapperFactory.java @@ -5,6 +5,8 @@ import java.lang.reflect.Proxy; import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.tool.orm.jbt.wrp.TypeWrapperFactory.TypeWrapper; +import org.hibernate.type.Type; public class EntityPersisterWrapperFactory { @@ -40,6 +42,8 @@ private EntityPersisterInvocationHandler(EntityPersister delegate) { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (isEntityPersisterExtensionMethod(method)) { return method.invoke(this, args); + } else if ("getPropertyTypes".equals(method.getName())) { + return getPropertyTypes(); } else { return method.invoke(delegate, args); } @@ -50,6 +54,17 @@ public EntityPersister getWrappedObject() { return delegate; } + private TypeWrapper[] getPropertyTypes() { + Type[] types = getWrappedObject().getPropertyTypes(); + if (types != null) { + TypeWrapper[] result = new TypeWrapper[types.length]; + for (int i = 0; i < types.length; i++) { + result[i] = TypeWrapperFactory.createTypeWrapper(types[i]); + } + return result; + } + return null; + } } private static boolean isEntityPersisterExtensionMethod(Method m) { diff --git a/jbt/src/test/java/org/hibernate/tool/orm/jbt/wrp/EntityPersisterWrapperFactoryTest.java b/jbt/src/test/java/org/hibernate/tool/orm/jbt/wrp/EntityPersisterWrapperFactoryTest.java index 55d46e1a97..b09ade8256 100644 --- a/jbt/src/test/java/org/hibernate/tool/orm/jbt/wrp/EntityPersisterWrapperFactoryTest.java +++ b/jbt/src/test/java/org/hibernate/tool/orm/jbt/wrp/EntityPersisterWrapperFactoryTest.java @@ -22,7 +22,10 @@ import org.hibernate.tool.orm.jbt.util.MockConnectionProvider; import org.hibernate.tool.orm.jbt.util.MockDialect; import org.hibernate.tool.orm.jbt.wrp.EntityPersisterWrapperFactory.EntityPersisterExtension; +import org.hibernate.tool.orm.jbt.wrp.TypeWrapperFactory.TypeExtension; import org.hibernate.tuple.entity.EntityMetamodel; +import org.hibernate.type.CollectionType; +import org.hibernate.type.Type; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -103,8 +106,23 @@ public void testGetPropertyNames() { assertEquals("bars", propertyNames[0]); } + @Test + public void testGetPropertyTypes() { + Type[] propertyTypeWrappers = entityPersisterWrapper.getPropertyTypes(); + assertEquals(1, propertyTypeWrappers.length); + Type propertyTypeWrapper = propertyTypeWrappers[0]; + assertTrue(propertyTypeWrapper instanceof Wrapper); + assertTrue(propertyTypeWrapper instanceof TypeExtension); + Object wrappedPropertyType = ((Wrapper)propertyTypeWrapper).getWrappedObject(); + assertTrue(wrappedPropertyType instanceof CollectionType); + assertTrue(propertyTypeWrapper.isCollectionType()); + assertEquals( + "org.hibernate.tool.orm.jbt.wrp.EntityPersisterWrapperFactoryTest$Foo.bars", + ((TypeExtension)propertyTypeWrapper).getRole()); + } +