HHH-12760 Remove no longer needed EqualsHelper and cleanup some equality checks

This commit is contained in:
Sanne Grinovero 2018-07-02 23:28:24 +01:00
parent f54949d280
commit a67c6b9cd4
13 changed files with 20 additions and 122 deletions

View File

@ -11,11 +11,9 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import org.hibernate.EntityMode; import org.hibernate.EntityMode;
import org.hibernate.boot.MappingException;
import org.hibernate.boot.jaxb.Origin; import org.hibernate.boot.jaxb.Origin;
import org.hibernate.boot.jaxb.hbm.spi.EntityInfo; import org.hibernate.boot.jaxb.hbm.spi.EntityInfo;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmEntityBaseDefinition; import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmEntityBaseDefinition;
@ -46,7 +44,6 @@ import org.hibernate.boot.model.source.spi.SubclassEntitySource;
import org.hibernate.boot.model.source.spi.ToolingHintContext; import org.hibernate.boot.model.source.spi.ToolingHintContext;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.internal.util.compare.EqualsHelper;
/** /**
* @author Steve Ebersole * @author Steve Ebersole

View File

@ -7,12 +7,12 @@
package org.hibernate.bytecode.enhance.internal.bytebuddy; package org.hibernate.bytecode.enhance.internal.bytebuddy;
import java.util.Collection; import java.util.Collection;
import java.util.Objects;
import javax.persistence.Embedded; import javax.persistence.Embedded;
import javax.persistence.EmbeddedId; import javax.persistence.EmbeddedId;
import javax.persistence.Id; import javax.persistence.Id;
import org.hibernate.bytecode.enhance.spi.EnhancerConstants; import org.hibernate.bytecode.enhance.spi.EnhancerConstants;
import org.hibernate.internal.util.compare.EqualsHelper;
import net.bytebuddy.ClassFileVersion; import net.bytebuddy.ClassFileVersion;
import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice;
@ -125,8 +125,8 @@ class InlineDirtyCheckingHandler implements Implementation, ByteCodeAppender {
else { else {
methodVisitor.visitMethodInsn( methodVisitor.visitMethodInsn(
Opcodes.INVOKESTATIC, Opcodes.INVOKESTATIC,
Type.getInternalName( EqualsHelper.class ), Type.getInternalName( Objects.class ),
"areEqual", "deepEquals",
Type.getMethodDescriptor( Type.getType( boolean.class ), Type.getType( Object.class ), Type.getType( Object.class ) ), Type.getMethodDescriptor( Type.getType( boolean.class ), Type.getType( Object.class ), Type.getType( Object.class ) ),
false false
); );

View File

@ -15,9 +15,7 @@ import javassist.CtClass;
import javassist.CtField; import javassist.CtField;
import javassist.NotFoundException; import javassist.NotFoundException;
import org.hibernate.bytecode.enhance.spi.EnhancementContext;
import org.hibernate.bytecode.enhance.spi.EnhancerConstants; import org.hibernate.bytecode.enhance.spi.EnhancerConstants;
import org.hibernate.internal.util.compare.EqualsHelper;
/** /**
* utility class to generate interceptor methods * utility class to generate interceptor methods
@ -66,8 +64,7 @@ public abstract class AttributeTypeDescriptor {
} }
builder.append( builder.append(
String.format( String.format(
" if ( !%s.areEqual( %s, $1 ) )", " if ( !Objects.deepEquals( %s, $1 ) )",
EqualsHelper.class.getName(),
readFragment readFragment
) )
); );

View File

@ -12,6 +12,7 @@ import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import javax.persistence.PersistenceException; import javax.persistence.PersistenceException;
@ -43,7 +44,6 @@ import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper; import org.hibernate.pretty.MessageHelper;
import org.hibernate.type.descriptor.java.StringTypeDescriptor;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
@ -108,7 +108,7 @@ public class EnabledCaching implements CacheImplementor, DomainDataRegionBuildin
final DomainDataRegion region = getRegionFactory().buildDomainDataRegion( regionConfig, this ); final DomainDataRegion region = getRegionFactory().buildDomainDataRegion( regionConfig, this );
regionsByName.put( region.getName(), region ); regionsByName.put( region.getName(), region );
if ( !StringTypeDescriptor.INSTANCE.areEqual( region.getName(), regionConfig.getRegionName() ) ) { if ( ! Objects.equals( region.getName(), regionConfig.getRegionName() ) ) {
throw new HibernateException( throw new HibernateException(
String.format( String.format(
Locale.ROOT, Locale.ROOT,

View File

@ -1,86 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.internal.util.compare;
import java.util.Arrays;
/**
* Helper for equality determination
*
* @author Gavin King
* @author Steve Ebersole
*/
public final class EqualsHelper {
/**
* Like the legacy {@link #equals} method, but handles array-specific checks
*
* @param x One value to check
* @param y The other value
*
* @return {@code true} if the 2 values are equal; {@code false} otherwise.
*/
public static boolean areEqual(final Object x, final Object y) {
if ( x == y ) {
return true;
}
if ( x == null || y == null ) {
// One is null, but the other is not (otherwise the `x == y` check would have passed).
// null can never equal a non-null
return false;
}
if ( x.equals( y ) ) {
return true;
}
// Check for possibility of arrays
final Class xClass = x.getClass();
final Class yClass = y.getClass();
if ( xClass.isArray() && yClass.isArray() ) {
if ( xClass.equals( yClass ) ) {
if ( x instanceof boolean[] ) {
return Arrays.equals( (boolean[]) x, (boolean[]) y );
}
else if ( x instanceof byte[] ) {
return Arrays.equals( (byte[]) x, (byte[]) y );
}
else if ( x instanceof char[] ) {
return Arrays.equals( (char[]) x, (char[]) y );
}
else if ( x instanceof short[] ) {
return Arrays.equals( (short[]) x, (short[]) y );
}
else if ( x instanceof int[] ) {
return Arrays.equals( (int[]) x, (int[]) y );
}
else if ( x instanceof long[] ) {
return Arrays.equals( (long[]) x, (long[]) y );
}
else if ( x instanceof double[] ) {
return Arrays.equals( (double[]) x, (double[]) y );
}
else if ( x instanceof float[] ) {
return Arrays.equals( (float[]) x, (float[]) y );
}
}
return Arrays.equals( (Object[]) x, (Object[]) y );
}
return false;
}
/**
* Private ctor - disallow instantiation
*/
private EqualsHelper() {
// disallow instantiation
}
}

View File

@ -8,9 +8,9 @@ package org.hibernate.type.descriptor.java;
import java.io.Serializable; import java.io.Serializable;
import java.util.Comparator; import java.util.Comparator;
import java.util.Objects;
import org.hibernate.internal.util.compare.ComparableComparator; import org.hibernate.internal.util.compare.ComparableComparator;
import org.hibernate.internal.util.compare.EqualsHelper;
import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.WrapperOptions;
/** /**
@ -75,7 +75,7 @@ public interface JavaTypeDescriptor<T> extends Serializable {
* @return True if the two are considered equal; false otherwise. * @return True if the two are considered equal; false otherwise.
*/ */
default boolean areEqual(T one, T another) { default boolean areEqual(T one, T another) {
return EqualsHelper.areEqual( one, another ); return Objects.deepEquals( one, another );
} }
/** /**

View File

@ -11,6 +11,7 @@ import java.io.InputStream;
import java.io.Serializable; import java.io.Serializable;
import java.sql.Blob; import java.sql.Blob;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Arrays;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.annotations.Immutable; import org.hibernate.annotations.Immutable;
@ -71,7 +72,7 @@ public class SerializableTypeDescriptor<T extends Serializable> extends Abstract
return false; return false;
} }
return one.equals( another ) return one.equals( another )
|| PrimitiveByteArrayTypeDescriptor.INSTANCE.areEqual( toBytes( one ), toBytes( another ) ); || Arrays.equals( toBytes( one ), toBytes( another ) );
} }
@Override @Override

View File

@ -21,7 +21,6 @@ import org.hibernate.annotations.NaturalIdCache;
import org.hibernate.boot.MetadataSources; import org.hibernate.boot.MetadataSources;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.spi.CacheImplementor; import org.hibernate.engine.spi.CacheImplementor;
import org.hibernate.internal.util.compare.EqualsHelper;
import org.hibernate.metamodel.model.domain.NavigableRole; import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.stat.NaturalIdCacheStatistics; import org.hibernate.stat.NaturalIdCacheStatistics;
import org.hibernate.stat.SecondLevelCacheStatistics; import org.hibernate.stat.SecondLevelCacheStatistics;
@ -149,7 +148,7 @@ public class RegionNameTest extends BaseNonConfigCoreFunctionalTestCase {
boolean foundRegion = false; boolean foundRegion = false;
for ( String name : cache.getSecondLevelCacheRegionNames() ) { for ( String name : cache.getSecondLevelCacheRegionNames() ) {
if ( EqualsHelper.areEqual( name, regionName ) ) { if ( regionName.equals( name ) ) {
foundRegion = true; foundRegion = true;
break; break;
} }

View File

@ -11,6 +11,7 @@ import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.collection.spi.PersistentCollection;
@ -22,7 +23,6 @@ import org.hibernate.envers.internal.entities.PropertyData;
import org.hibernate.envers.internal.reader.AuditReaderImplementor; import org.hibernate.envers.internal.reader.AuditReaderImplementor;
import org.hibernate.envers.internal.tools.ReflectionTools; import org.hibernate.envers.internal.tools.ReflectionTools;
import org.hibernate.envers.internal.tools.StringTools; import org.hibernate.envers.internal.tools.StringTools;
import org.hibernate.internal.util.compare.EqualsHelper;
import org.hibernate.property.access.spi.Setter; import org.hibernate.property.access.spi.Setter;
import org.hibernate.property.access.spi.SetterFieldImpl; import org.hibernate.property.access.spi.SetterFieldImpl;
@ -171,6 +171,6 @@ public class SinglePropertyMapper extends AbstractPropertyMapper implements Simp
} }
// todo (6.0) - Confirm if this is still necessary as everything should use a JavaTypeDescriptor. // todo (6.0) - Confirm if this is still necessary as everything should use a JavaTypeDescriptor.
// This was maintained for legacy 5.2 behavior only. // This was maintained for legacy 5.2 behavior only.
return EqualsHelper.areEqual( newObj, oldObj ); return Objects.deepEquals( newObj, oldObj );
} }
} }

View File

@ -15,6 +15,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.collection.spi.PersistentCollection;
@ -29,7 +30,6 @@ import org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeD
import org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor.Initializor; import org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor.Initializor;
import org.hibernate.envers.internal.reader.AuditReaderImplementor; import org.hibernate.envers.internal.reader.AuditReaderImplementor;
import org.hibernate.envers.internal.tools.ReflectionTools; import org.hibernate.envers.internal.tools.ReflectionTools;
import org.hibernate.internal.util.compare.EqualsHelper;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.property.access.spi.Setter; import org.hibernate.property.access.spi.Setter;
@ -167,7 +167,7 @@ public abstract class AbstractCollectionMapper<T> extends AbstractPropertyMapper
if ( propertyData.isUsingModifiedFlag() ) { if ( propertyData.isUsingModifiedFlag() ) {
if ( isNotPersistentCollection( newObj ) || isNotPersistentCollection( oldObj ) ) { if ( isNotPersistentCollection( newObj ) || isNotPersistentCollection( oldObj ) ) {
// Compare POJOs. // Compare POJOs.
data.put( propertyData.getModifiedFlagPropertyName(), !EqualsHelper.areEqual( newObj, oldObj ) ); data.put( propertyData.getModifiedFlagPropertyName(), !Objects.deepEquals( newObj, oldObj ) );
} }
else if ( isFromNullToEmptyOrFromEmptyToNull( (PersistentCollection) newObj, (Serializable) oldObj ) ) { else if ( isFromNullToEmptyOrFromEmptyToNull( (PersistentCollection) newObj, (Serializable) oldObj ) ) {
data.put( propertyData.getModifiedFlagPropertyName(), true ); data.put( propertyData.getModifiedFlagPropertyName(), true );

View File

@ -8,6 +8,7 @@ package org.hibernate.envers.internal.entities.mapper.relation;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.envers.RevisionType; import org.hibernate.envers.RevisionType;
@ -17,7 +18,6 @@ import org.hibernate.envers.internal.entities.mapper.id.IdMapper;
import org.hibernate.envers.internal.reader.AuditReaderImplementor; import org.hibernate.envers.internal.reader.AuditReaderImplementor;
import org.hibernate.envers.internal.tools.EntityTools; import org.hibernate.envers.internal.tools.EntityTools;
import org.hibernate.envers.internal.tools.query.Parameters; import org.hibernate.envers.internal.tools.query.Parameters;
import org.hibernate.internal.util.compare.EqualsHelper;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
/** /**
@ -113,7 +113,7 @@ public class ToOneIdMapper extends AbstractToOneMapper {
resolvedOldObjectId = EntityTools.getIdentifier( session, referencedEntityName, oldObj ); resolvedOldObjectId = EntityTools.getIdentifier( session, referencedEntityName, oldObj );
} }
return !EqualsHelper.areEqual( resolvedNewObjectId, resolvedOldObjectId ); return !Objects.deepEquals( resolvedNewObjectId, resolvedOldObjectId );
} }
@Override @Override

View File

@ -6,11 +6,12 @@
*/ */
package org.hibernate.envers.internal.tools; package org.hibernate.envers.internal.tools;
import java.util.Objects;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.compare.EqualsHelper;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.proxy.HibernateProxy; import org.hibernate.proxy.HibernateProxy;
@ -22,7 +23,7 @@ public abstract class EntityTools {
final Object id1 = getIdentifier( session, entityName, obj1 ); final Object id1 = getIdentifier( session, entityName, obj1 );
final Object id2 = getIdentifier( session, entityName, obj2 ); final Object id2 = getIdentifier( session, entityName, obj2 );
return EqualsHelper.areEqual( id1, id2 ); return Objects.deepEquals( id1, id2 );
} }
public static Object getIdentifier(SessionImplementor session, String entityName, Object obj) { public static Object getIdentifier(SessionImplementor session, String entityName, Object obj) {

View File

@ -17,7 +17,6 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import org.hibernate.envers.tools.Pair; import org.hibernate.envers.tools.Pair;
import org.hibernate.internal.util.compare.EqualsHelper;
/** /**
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
@ -38,16 +37,6 @@ public abstract class Tools {
return new LinkedHashMap<>(); return new LinkedHashMap<>();
} }
/**
* @deprecated (since 5.2), use {@link EqualsHelper#areEqual(Object, Object)}.
*/
@Deprecated
public static boolean objectsEqual(Object obj1, Object obj2) {
// HHH-10734
// Delegates to core's EqualsHelper to support array and non-array types
return EqualsHelper.areEqual( obj1, obj2 );
}
public static <T> List<T> iteratorToList(Iterator<T> iter) { public static <T> List<T> iteratorToList(Iterator<T> iter) {
final List<T> ret = new ArrayList<>(); final List<T> ret = new ArrayList<>();
while ( iter.hasNext() ) { while ( iter.hasNext() ) {