slight cleanup to old code

This commit is contained in:
Gavin King 2024-11-16 10:35:35 +01:00
parent e1f62098e2
commit b0b258de11
2 changed files with 36 additions and 30 deletions

View File

@ -11,9 +11,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
import org.hibernate.collection.spi.PersistentArrayHolder; import org.hibernate.collection.spi.PersistentArrayHolder;
import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -21,6 +19,9 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.CollectionClassification; import org.hibernate.metamodel.CollectionClassification;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
import static org.hibernate.Hibernate.isInitialized;
import static org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer.UNFETCHED_PROPERTY;
/** /**
* A type for persistent arrays. * A type for persistent arrays.
* @author Gavin King * @author Gavin King
@ -48,7 +49,7 @@ public class ArrayType extends CollectionType {
@Override @Override
public PersistentCollection<?> instantiate(SharedSessionContractImplementor session, CollectionPersister persister, Object key) public PersistentCollection<?> instantiate(SharedSessionContractImplementor session, CollectionPersister persister, Object key)
throws HibernateException { throws HibernateException {
return new PersistentArrayHolder<>(session, persister); return new PersistentArrayHolder<>(session, persister);
} }
@ -75,20 +76,21 @@ public class ArrayType extends CollectionType {
if ( value == null ) { if ( value == null ) {
return "null"; return "null";
} }
int length = Array.getLength(value); else {
List<String> list = new ArrayList<>(length); final int length = Array.getLength( value );
Type elemType = getElementType(factory); final Type elemType = getElementType( factory );
for ( int i=0; i<length; i++ ) { final List<String> list = new ArrayList<>( length );
Object element = Array.get(value, i); for ( int i = 0; i < length; i++ ) {
if ( element == LazyPropertyInitializer.UNFETCHED_PROPERTY list.add( loggableString( factory, Array.get( value, i ), elemType ) );
|| !Hibernate.isInitialized( element ) ) {
list.add( "<uninitialized>" );
}
else {
list.add( elemType.toLoggableString( element, factory ) );
} }
return list.toString();
} }
return list.toString(); }
private static String loggableString(SessionFactoryImplementor factory, Object element, Type elemType) {
return element == UNFETCHED_PROPERTY || !isInitialized( element )
? "<uninitialized>"
: elemType.toLoggableString( element, factory );
} }
@Override @Override
@ -101,16 +103,16 @@ public class ArrayType extends CollectionType {
Object original, Object original,
Object target, Object target,
Object owner, Object owner,
Map copyCache, Map<Object, Object> copyCache,
SharedSessionContractImplementor session) throws HibernateException { SharedSessionContractImplementor session) throws HibernateException {
int length = Array.getLength(original); final int length = Array.getLength(original);
if ( length!=Array.getLength(target) ) { if ( length!=Array.getLength(target) ) {
//note: this affects the return value! //note: this affects the return value!
target=instantiateResult(original); target=instantiateResult(original);
} }
Type elemType = getElementType( session.getFactory() ); final Type elemType = getElementType( session.getFactory() );
for ( int i=0; i<length; i++ ) { for ( int i=0; i<length; i++ ) {
Array.set( target, i, elemType.replace( Array.get(original, i), null, session, owner, copyCache ) ); Array.set( target, i, elemType.replace( Array.get(original, i), null, session, owner, copyCache ) );
} }
@ -126,10 +128,10 @@ public class ArrayType extends CollectionType {
@Override @Override
public Object indexOf(Object array, Object element) { public Object indexOf(Object array, Object element) {
int length = Array.getLength(array); final int length = Array.getLength(array);
for ( int i=0; i<length; i++ ) { for ( int i=0; i<length; i++ ) {
//TODO: proxies! //TODO: proxies!
if ( Array.get(array, i)==element ) { if ( Array.get(array, i) == element ) {
return i; return i;
} }
} }

View File

@ -57,22 +57,25 @@ public class MapType extends CollectionType {
: new HashMap<>( anticipatedSize + (int) ( anticipatedSize * .75f ), .75f ); : new HashMap<>( anticipatedSize + (int) ( anticipatedSize * .75f ), .75f );
} }
@Override @SuppressWarnings("rawtypes") @Override @SuppressWarnings({"rawtypes", "unchecked"})
public Object replaceElements( public Object replaceElements(
final Object original, final Object original,
final Object target, final Object target,
final Object owner, final Object owner,
final Map copyCache, final Map<Object, Object> copyCache,
final SharedSessionContractImplementor session) throws HibernateException { final SharedSessionContractImplementor session) throws HibernateException {
CollectionPersister cp = session.getFactory().getRuntimeMetamodels().getMappingMetamodel().getCollectionDescriptor( getRole() ); final CollectionPersister persister =
session.getFactory().getRuntimeMetamodels().getMappingMetamodel()
.getCollectionDescriptor( getRole() );
Map result = (Map) target; final Map source = (Map) original;
final Map result = (Map) target;
result.clear(); result.clear();
for ( Object o : ( (Map) original ).entrySet() ) { for ( Object entry : source.entrySet() ) {
Map.Entry me = (Map.Entry) o; final Map.Entry me = (Map.Entry) entry;
Object key = cp.getIndexType().replace( me.getKey(), null, session, owner, copyCache ); final Object key = persister.getIndexType().replace( me.getKey(), null, session, owner, copyCache );
Object value = cp.getElementType().replace( me.getValue(), null, session, owner, copyCache ); final Object value = persister.getElementType().replace( me.getValue(), null, session, owner, copyCache );
result.put( key, value ); result.put( key, value );
} }
@ -82,8 +85,9 @@ public class MapType extends CollectionType {
@Override @SuppressWarnings("rawtypes") @Override @SuppressWarnings("rawtypes")
public Object indexOf(Object collection, Object element) { public Object indexOf(Object collection, Object element) {
for ( Object o : ( (Map) collection ).entrySet() ) { final Map map = (Map) collection;
Map.Entry me = (Map.Entry) o; for ( Object entry : map.entrySet() ) {
final Map.Entry me = (Map.Entry) entry;
//TODO: proxies! //TODO: proxies!
if ( me.getValue() == element ) { if ( me.getValue() == element ) {
return me.getKey(); return me.getKey();