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 #4276

Merged
merged 1 commit into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
@@ -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