From d201b0b22d2a2efde5d984aed09610591cefae6c Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 16 Apr 2020 08:32:25 -0500 Subject: [PATCH] HHH-13948 EnhancedSetterImpl should define writeReplace --- .../access/spi/EnhancedSetterImpl.java | 39 ++++++++++++++++--- .../property/access/spi/SetterFieldImpl.java | 15 ++++++- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/property/access/spi/EnhancedSetterImpl.java b/hibernate-core/src/main/java/org/hibernate/property/access/spi/EnhancedSetterImpl.java index 89e90c9d03..7cdf6581dc 100644 --- a/hibernate-core/src/main/java/org/hibernate/property/access/spi/EnhancedSetterImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/property/access/spi/EnhancedSetterImpl.java @@ -6,14 +6,16 @@ */ package org.hibernate.property.access.spi; -import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor; +import java.io.Serializable; +import java.lang.reflect.Field; + +import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor; import org.hibernate.engine.spi.CompositeOwner; import org.hibernate.engine.spi.CompositeTracker; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptor; import org.hibernate.engine.spi.SessionFactoryImplementor; - -import java.lang.reflect.Field; +import org.hibernate.property.access.internal.AbstractFieldSerialForm; /** * A specialized Setter implementation for handling setting values into @@ -25,9 +27,9 @@ import java.lang.reflect.Field; * @author Luis Barreiro */ public class EnhancedSetterImpl extends SetterFieldImpl { - private final String propertyName; + @SuppressWarnings("rawtypes") public EnhancedSetterImpl(Class containerClass, String propertyName, Field field) { super( containerClass, propertyName, field ); this.propertyName = propertyName; @@ -46,9 +48,34 @@ public class EnhancedSetterImpl extends SetterFieldImpl { // This marks the attribute as initialized, so it doesn't get lazy loaded afterwards if ( target instanceof PersistentAttributeInterceptable ) { PersistentAttributeInterceptor interceptor = ( (PersistentAttributeInterceptable) target ).$$_hibernate_getInterceptor(); - if ( interceptor != null && interceptor instanceof LazyAttributeLoadingInterceptor ) { - interceptor.attributeInitialized( propertyName ); + if ( interceptor instanceof BytecodeLazyAttributeInterceptor ) { + ( (BytecodeLazyAttributeInterceptor) interceptor ).attributeInitialized( propertyName ); } } } + + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // serialization + + private Object writeReplace() { + return new SerialForm( getContainerClass(), propertyName, getField() ); + } + + @SuppressWarnings("rawtypes") + private static class SerialForm extends AbstractFieldSerialForm implements Serializable { + private final Class containerClass; + private final String propertyName; + + + private SerialForm(Class containerClass, String propertyName, Field field) { + super( field ); + this.containerClass = containerClass; + this.propertyName = propertyName; + } + + private Object readResolve() { + return new EnhancedSetterImpl( containerClass, propertyName, resolveField() ); + } + } } diff --git a/hibernate-core/src/main/java/org/hibernate/property/access/spi/SetterFieldImpl.java b/hibernate-core/src/main/java/org/hibernate/property/access/spi/SetterFieldImpl.java index 58c7d9e472..cc9c19f0f5 100644 --- a/hibernate-core/src/main/java/org/hibernate/property/access/spi/SetterFieldImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/property/access/spi/SetterFieldImpl.java @@ -6,7 +6,6 @@ */ package org.hibernate.property.access.spi; -import java.io.ObjectStreamException; import java.io.Serializable; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -35,6 +34,18 @@ public class SetterFieldImpl implements Setter { this.setterMethod = ReflectHelper.setterMethodOrNull( containerClass, propertyName, field.getType() ); } + public Class getContainerClass() { + return containerClass; + } + + public String getPropertyName() { + return propertyName; + } + + protected Field getField() { + return field; + } + @Override public void set(Object target, Object value, SessionFactoryImplementor factory) { try { @@ -83,7 +94,7 @@ public class SetterFieldImpl implements Setter { return setterMethod; } - private Object writeReplace() throws ObjectStreamException { + private Object writeReplace() { return new SerialForm( containerClass, propertyName, field ); }