Skip to content

Commit

Permalink
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
Browse files Browse the repository at this point in the history
  - Refactor class 'org.hibernate.tool.orm.jbt.wrp.PropertyWrapperFactory'
    * Create inner class 'DelegatingPropertyWrapperImpl' that extends 'Property' and implements 'Wrapper'
    * Method 'PropertyWrapperFactory#createPropertyWrapper(Property)' now uses the above inner class
    * The inner interface 'PropertyWrapper' has no default implementations anymore (these are now implemented in 'DelegatingPropertyWrapperImpl')
    * Remove inner class 'PropertyWrapperInvocationHandler' and method 'lookupMethodInPropertyWrapperClass(Method)'
  - Refactor class 'org.hibernate.tool.orm.jbt.wrp.WrapperFactory#createSpecialRootClassWrapper(Object)'
  - Adapt the setup of test class 'org.hibernate.tool.orm.jbt.wrp.PropertyWrapperFactoryTest'
  - Adapt the test case 'org.hibernate.tool.orm.jbt.wrp.WrapperFactoryTest#testCreateSpecialRootClassWrapper()'

Signed-off-by: Koen Aers <[email protected]>
  • Loading branch information
koentsje committed Jul 2, 2023
1 parent ec115ce commit a7f70dc
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package org.hibernate.tool.orm.jbt.wrp;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Value;
Expand All @@ -12,138 +8,153 @@

public class PropertyWrapperFactory {

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

static interface PropertyWrapper extends Wrapper{
static interface PropertyWrapper extends Wrapper{
@Override Property getWrappedObject();
Value getValue();
Type getType();
void setName(String name);
void setPersistentClass(PersistentClass pc);
PersistentClass getPersistentClass();
boolean isComposite();
String getPropertyAccessorName();
String getName();
void setValue(Value value);
void setPropertyAccessorName(String name);
void setCascade(String c);
boolean isBackRef();
boolean isSelectable();
boolean isUpdateable();
String getCascade();
boolean isLazy();
boolean isOptional();
boolean isNaturalIdentifier();
boolean isOptimisticLocked();
boolean isInsertable();
}

static class DelegatingPropertyWrapperImpl extends Property implements PropertyWrapper {

private Property delegate = null;

@Override Property getWrappedObject();
public DelegatingPropertyWrapperImpl(Property p) {
delegate = p;
}

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

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

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

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

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

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

default boolean isComposite() {
@Override
public boolean isComposite() {
return getWrappedObject().isComposite();
}

default String getPropertyAccessorName() {
@Override
public String getPropertyAccessorName() {
return getWrappedObject().getPropertyAccessorName();
}

default String getName() {
@Override
public 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(Value value) {
@Override
public void setValue(Value value) {
getWrappedObject().setValue(value);
}

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

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

default boolean isBackRef() {
@Override
public boolean isBackRef() {
return getWrappedObject().isBackRef();
}

default boolean isSelectable() {
@Override
public boolean isSelectable() {
return getWrappedObject().isSelectable();
}

default boolean isUpdateable() {
@Override
public boolean isUpdateable() {
return getWrappedObject().isUpdateable();
}

default String getCascade() {
@Override
public String getCascade() {
return getWrappedObject().getCascade();
}

default boolean isLazy() {
@Override
public boolean isLazy() {
return getWrappedObject().isLazy();
}

default boolean isOptional() {
@Override
public boolean isOptional() {
return getWrappedObject().isOptional();
}

default boolean isNaturalIdentifier() {
@Override
public boolean isNaturalIdentifier() {
return getWrappedObject().isNaturalIdentifier();
}

default boolean isOptimisticLocked() {
@Override
public boolean isOptimisticLocked() {
return getWrappedObject().isOptimisticLocked();
}

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

}

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;
public boolean isInsertable() {
return getWrappedObject().isInsertable();
}



}

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 @@ -35,7 +35,6 @@
import org.hibernate.tool.orm.jbt.util.MetadataHelper;
import org.hibernate.tool.orm.jbt.util.NativeConfiguration;
import org.hibernate.tool.orm.jbt.util.RevengConfiguration;
import org.hibernate.tool.orm.jbt.wrp.PropertyWrapperFactory.PropertyWrapper;

public class WrapperFactory {

Expand Down Expand Up @@ -99,7 +98,7 @@ public static Object createJoinedTableSubClassWrapper(Object persistentClassWrap

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

public static Object createPropertyWrapper() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@
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.PropertyWrapperFactory.PropertyWrapper;
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;
private PropertyWrapper propertyWrapper = null;

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

@Test
Expand Down Expand Up @@ -69,7 +70,7 @@ public void testGetPersistentClass() {
PersistentClassWrapper persistentClass = PersistentClassWrapperFactory.createRootClassWrapper();
assertNull(propertyWrapper.getPersistentClass());
wrappedProperty.setPersistentClass(persistentClass.getWrappedObject());
PersistentClassWrapper persistentClassWrapper = propertyWrapper.getPersistentClass();
PersistentClassWrapper persistentClassWrapper = (PersistentClassWrapper)propertyWrapper.getPersistentClass();
assertSame(persistentClass.getWrappedObject(), persistentClassWrapper.getWrappedObject());
}

Expand Down Expand Up @@ -135,7 +136,7 @@ public void testSetCascade() {
@Test
public void testIsBackRef() throws Exception {
assertFalse(propertyWrapper.isBackRef());
propertyWrapper = PropertyWrapperFactory.createPropertyWrapper(new Backref());
propertyWrapper = (PropertyWrapper)PropertyWrapperFactory.createPropertyWrapper(new Backref());
assertTrue(propertyWrapper.isBackRef());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void testCreateSpecialRootClassWrapper() {
assertTrue(persistentClass instanceof SpecialRootClass);
assertSame(
((SpecialRootClass)persistentClass).getProperty(),
propertyWrapper.getWrappedObject());
propertyWrapper);
}

@Test
Expand Down

0 comments on commit a7f70dc

Please sign in to comment.