Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools #4273

Merged
merged 20 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
7669f92
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
aa09494
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
9dd6964
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
ee9e03f
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
8d50b3b
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
b1611f1
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
e0bf60a
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
0531937
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
a28b13f
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
2867786
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
eaa780e
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
2dc9888
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
e9e0049
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
682c867
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
162be67
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
945e525
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
0f3534f
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
e6ce61f
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
74062e1
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
30924a3
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
koentsje Jul 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public static PersistentClassWrapper createSpecialRootClassWrapper(Property prop
new SpecialRootClassWrapperImpl(property)));
}

public static PersistentClassWrapper createPersistentClassWrapper(PersistentClassWrapper persistentClassWrapper) {
return (PersistentClassWrapper)Proxy.newProxyInstance(
PersistentClassWrapperFactory.class.getClassLoader(),
new Class[] { PersistentClassWrapper.class },
new PersistentClassWrapperInvocationHandler(
persistentClassWrapper));
}

static class PersistentClassWrapperInvocationHandler implements InvocationHandler {

private PersistentClassWrapper wrapper = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package org.hibernate.tool.orm.jbt.wrp;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.function.BooleanSupplier;

import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Value;
import org.hibernate.tool.orm.jbt.wrp.TypeWrapperFactory.TypeWrapper;
import org.hibernate.type.Type;

public class PropertyWrapperFactory {

public static PropertyWrapper createPropertyWrapper(Property wrappedProperty) {
return (PropertyWrapper)Proxy.newProxyInstance(
ValueWrapperFactory.class.getClassLoader(),
new Class[] { PropertyWrapper.class },
new PropertyWrapperInvocationHandler(wrappedProperty));
}

static interface PropertyWrapper extends Wrapper{

@Override Property getWrappedObject();

default Value getValue() {
Value v = getWrappedObject().getValue();
return v == null ? null : ValueWrapperFactory.createValueWrapper(v);
}

default void setName(String name) {
getWrappedObject().setName(name);
}

default void setPersistentClass(PersistentClass pc) {
getWrappedObject().setPersistentClass(pc);
}

default PersistentClassWrapper getPersistentClass() {
PersistentClassWrapper pc = (PersistentClassWrapper)getWrappedObject().getPersistentClass();
return pc == null ? null : PersistentClassWrapperFactory.createPersistentClassWrapper(pc);
}

default boolean isComposite() {
return getWrappedObject().isComposite();
}

default String getPropertyAccessorName() {
return getWrappedObject().getPropertyAccessorName();
}

default String getName() {
return getWrappedObject().getName();
}

default Type getType() {
TypeWrapper result = null;
if (getWrappedObject().getValue() != null) {
Type t = getWrappedObject().getType();
result = t == null ? null : TypeWrapperFactory.createTypeWrapper(t);
}
return result;
}

default void setValue(BasicValue value) {
getWrappedObject().setValue(value);
}

default void setPropertyAccessorName(String name) {
getWrappedObject().setPropertyAccessorName(name);
}

default void setCascade(String c) {
getWrappedObject().setCascade(c);
}

default boolean isBackRef() {
return getWrappedObject().isBackRef();
}

default boolean isSelectable() {
return getWrappedObject().isSelectable();
}

default boolean isUpdateable() {
return getWrappedObject().isUpdateable();
}

default String getCascade() {
return getWrappedObject().getCascade();
}

default boolean isLazy() {
return getWrappedObject().isLazy();
}

default boolean isOptional() {
return getWrappedObject().isOptional();
}

default boolean isNaturalIdentifier() {
return getWrappedObject().isNaturalIdentifier();
}

default boolean isOptimisticLocked() {
return getWrappedObject().isOptimisticLocked();
}

}

static class PropertyWrapperInvocationHandler implements InvocationHandler, PropertyWrapper {

private Property delegate = null;

public PropertyWrapperInvocationHandler(Property property) {
delegate = property;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Method m = lookupMethodInPropertyWrapperClass(method);
if (m != null) {
return m.invoke(this, args);
} else {
return method.invoke(delegate, args);
}
}

@Override
public Property getWrappedObject() {
return delegate;
}


}

private static Method lookupMethodInPropertyWrapperClass(Method method) {
try {
return PropertyWrapper.class.getMethod(method.getName(), method.getParameterTypes());
} catch (NoSuchMethodException e) {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static Object createJoinedTableSubClassWrapper(Object persistentClassWrap

public static Object createSpecialRootClassWrapper(Object property) {
return PersistentClassWrapperFactory
.createSpecialRootClassWrapper((Property)property);
.createSpecialRootClassWrapper((PropertyWrapper)property);
}

public static Object createPropertyWrapper() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package org.hibernate.tool.orm.jbt.wrp;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.hibernate.mapping.Backref;
import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.Value;
import org.hibernate.tool.orm.jbt.util.DummyMetadataBuildingContext;
import org.hibernate.tool.orm.jbt.wrp.TypeWrapperFactory.TypeWrapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class PropertyWrapperFactoryTest {

private Property wrappedProperty = null;
private PropertyWrapperFactory.PropertyWrapper propertyWrapper = null;

@BeforeEach
public void beforeEach() {
wrappedProperty = new Property();
propertyWrapper = PropertyWrapperFactory.createPropertyWrapper(wrappedProperty);
}

@Test
public void testCreatePropertyWrapper() {
assertNotNull(wrappedProperty);
assertNotNull(propertyWrapper);
assertSame(wrappedProperty, propertyWrapper.getWrappedObject());
}

@Test
public void testGetValue() {
assertNull(propertyWrapper.getValue());
Value valueTarget = new BasicValue(DummyMetadataBuildingContext.INSTANCE);
wrappedProperty.setValue(valueTarget);
Value valueWrapper = propertyWrapper.getValue();
assertNotNull(valueWrapper);
assertSame(valueTarget, ((Wrapper)valueWrapper).getWrappedObject());
}

@Test
public void testSetName() {
assertNotEquals("foo", wrappedProperty.getName());
propertyWrapper.setName("foo");
assertEquals("foo", wrappedProperty.getName());
}

@Test
public void testSetPersistentClass() {
PersistentClassWrapper persistentClass = PersistentClassWrapperFactory.createRootClassWrapper();
assertNull(wrappedProperty.getPersistentClass());
propertyWrapper.setPersistentClass(persistentClass.getWrappedObject());
assertSame(persistentClass.getWrappedObject(), wrappedProperty.getPersistentClass());
}

@Test
public void testGetPersistentClass() {
PersistentClassWrapper persistentClass = PersistentClassWrapperFactory.createRootClassWrapper();
assertNull(propertyWrapper.getPersistentClass());
wrappedProperty.setPersistentClass(persistentClass.getWrappedObject());
PersistentClassWrapper persistentClassWrapper = propertyWrapper.getPersistentClass();
assertSame(persistentClass.getWrappedObject(), persistentClassWrapper.getWrappedObject());
}

@Test
public void testIsComposite() {
wrappedProperty.setValue(new BasicValue(DummyMetadataBuildingContext.INSTANCE));
assertFalse(propertyWrapper.isComposite());
Component component = new Component(
DummyMetadataBuildingContext.INSTANCE,
new Table(""),
new RootClass(DummyMetadataBuildingContext.INSTANCE));
wrappedProperty.setValue(component);
assertTrue(propertyWrapper.isComposite());
}

@Test
public void testGetPropetyAccessorName() {
assertNotEquals("foo", propertyWrapper.getPropertyAccessorName());
wrappedProperty.setPropertyAccessorName("foo");
assertEquals("foo", propertyWrapper.getPropertyAccessorName());
}

@Test
public void testGetName() {
assertNotEquals("foo", propertyWrapper.getName());
wrappedProperty.setName("foo");
assertEquals("foo", propertyWrapper.getName());
}

@Test
public void testGetType() {
BasicValue v = new BasicValue(DummyMetadataBuildingContext.INSTANCE);
v.setTypeName("int");
assertNull(propertyWrapper.getType());
wrappedProperty.setValue(v);
TypeWrapper typeWrapper = (TypeWrapper)propertyWrapper.getType();
assertEquals("integer", typeWrapper.getName());
assertSame(v.getType(), typeWrapper.getWrappedObject());
}

@Test
public void testSetValue() {
assertNull(wrappedProperty.getValue());
BasicValue value = new BasicValue(DummyMetadataBuildingContext.INSTANCE);
propertyWrapper.setValue(value);
assertSame(value, wrappedProperty.getValue());
}

@Test
public void testSetPropertyAccessorName() {
assertNotEquals("foo", wrappedProperty.getPropertyAccessorName());
propertyWrapper.setPropertyAccessorName("foo");
assertEquals("foo", wrappedProperty.getPropertyAccessorName());
}

@Test
public void testSetCascade() {
assertNotEquals("foo", wrappedProperty.getCascade());
propertyWrapper.setCascade("foo");
assertEquals("foo", wrappedProperty.getCascade());
}

@Test
public void testIsBackRef() throws Exception {
assertFalse(propertyWrapper.isBackRef());
propertyWrapper = PropertyWrapperFactory.createPropertyWrapper(new Backref());
assertTrue(propertyWrapper.isBackRef());
}

@Test
public void testIsSelectable() {
wrappedProperty.setSelectable(true);
assertTrue(propertyWrapper.isSelectable());
wrappedProperty.setSelectable(false);
assertFalse(propertyWrapper.isSelectable());
}

@Test
public void testIsUpdateable() {
BasicValue v = new BasicValue(DummyMetadataBuildingContext.INSTANCE);
v.setTable(new Table(""));
Column c = new Column();
v.addColumn(c);
wrappedProperty.setValue(v);
wrappedProperty.setUpdateable(false);
assertFalse(propertyWrapper.isUpdateable());
wrappedProperty.setUpdateable(true);
assertTrue(propertyWrapper.isUpdateable());
}

@Test
public void testGetCascade() {
assertNotEquals("foo", propertyWrapper.getCascade());
wrappedProperty.setCascade("foo");
assertEquals("foo", propertyWrapper.getCascade());
}

@Test
public void testIsLazy() {
wrappedProperty.setLazy(true);
assertTrue(propertyWrapper.isLazy());
wrappedProperty.setLazy(false);
assertFalse(propertyWrapper.isLazy());
}

@Test
public void testIsOptional() {
wrappedProperty.setValue(new BasicValue(DummyMetadataBuildingContext.INSTANCE));
wrappedProperty.setOptional(true);
assertTrue(propertyWrapper.isOptional());
wrappedProperty.setOptional(false);
assertFalse(propertyWrapper.isOptional());
}

@Test
public void testIsNaturalIdentifier() {
wrappedProperty.setNaturalIdentifier(true);
assertTrue(propertyWrapper.isNaturalIdentifier());
wrappedProperty.setNaturalIdentifier(false);
assertFalse(propertyWrapper.isNaturalIdentifier());
}

@Test
public void testIsOptimisticLocked() {
wrappedProperty.setOptimisticLocked(true);
assertTrue(propertyWrapper.isOptimisticLocked());
wrappedProperty.setOptimisticLocked(false);
assertFalse(propertyWrapper.isOptimisticLocked());
}

}